From d2a1b88e9592b7cb0b5cc330ab2813c59e58cb60 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Wed, 17 Jun 2026 12:11:37 -0700 Subject: [PATCH] Defer smart workspace source popover until user interaction (#5613) Prevent the source selection popover from opening automatically during dialog autofocus or other programmatic focus calls upon dialog mount. Instead, defer opening the popover until the user actively interacts with the name input (clicks or typing) or moves focus to it from another control within the workspace composer. --- .../components/NewWorkspaceComposerCard.tsx | 1 + ...rkspaceNameField-source-boundaries.test.ts | 8 +++ .../new-workspace/SmartWorkspaceNameField.tsx | 58 +++++++++++++++-- ...art-workspace-source-popover-focus.test.ts | 65 +++++++++++++++++++ .../smart-workspace-source-popover-focus.ts | 14 ++++ 5 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.test.ts create mode 100644 src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.ts diff --git a/src/renderer/src/components/NewWorkspaceComposerCard.tsx b/src/renderer/src/components/NewWorkspaceComposerCard.tsx index bc7600744..5867b65d5 100644 --- a/src/renderer/src/components/NewWorkspaceComposerCard.tsx +++ b/src/renderer/src/components/NewWorkspaceComposerCard.tsx @@ -480,6 +480,7 @@ export default function NewWorkspaceComposerCard({ return (
{ expect(FIELD_SOURCE).toContain('onActiveSourceModeChange?.(mode)') expect(FIELD_SOURCE).toContain('[mode, onActiveSourceModeChange]') }) + + it('defers the source popover until composer interaction', () => { + expect(FIELD_SOURCE).toContain('deferSourcePopoverUntilInteractionRef') + expect(FIELD_SOURCE).toContain('handleSourcePopoverOpenChange') + expect(FIELD_SOURCE).toContain('isComposerFieldToFieldFocus') + expect(FIELD_SOURCE).toContain('onPointerDown={() => {') + expect(FIELD_SOURCE).toContain('markSourcePopoverUserEngaged()') + }) }) diff --git a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx index 7e00610df..97536db3f 100644 --- a/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx +++ b/src/renderer/src/components/new-workspace/SmartWorkspaceNameField.tsx @@ -69,6 +69,7 @@ import type { LinearIssue } from '../../../../shared/types' import { resolveSmartWorkspaceCommandValue } from './smart-workspace-command-value' +import { isComposerFieldToFieldFocus } from './smart-workspace-source-popover-focus' import { translate } from '@/i18n/i18n' import { getMrStateFilters, @@ -308,6 +309,10 @@ export default function SmartWorkspaceNameField({ const repoSlugCacheRef = useRef>(new Map()) const handledCrossRepoUrlRef = useRef(null) const localInputFocusFrameRef = useRef(null) + // Why: dialog autofocus and other programmatic .focus() calls can look + // user-initiated in Electron, so gate the source popover until the user + // actually interacts with this field or tabs from another composer control. + const deferSourcePopoverUntilInteractionRef = useRef(true) const [crossRepoPrompt, setCrossRepoPrompt] = useState<{ link: NonNullable> matchingRepo: RepoOption | null @@ -406,6 +411,31 @@ export default function SmartWorkspaceNameField({ localInputFocusFrameRef.current = null }, []) + const markSourcePopoverUserEngaged = useCallback((): void => { + deferSourcePopoverUntilInteractionRef.current = false + }, []) + + const tryOpenSourcePopover = useCallback((): void => { + if (disabled || mode === 'text' || deferSourcePopoverUntilInteractionRef.current) { + return + } + setOpen(true) + }, [disabled, mode]) + + const handleSourcePopoverOpenChange = useCallback( + (next: boolean): void => { + if (disabled || selectedSource) { + setOpen(false) + return + } + if (next && deferSourcePopoverUntilInteractionRef.current) { + return + } + setOpen(next) + }, + [disabled, selectedSource] + ) + const setInputNode = useCallback( (node: HTMLInputElement | null) => { if (node === null) { @@ -1246,7 +1276,12 @@ export default function SmartWorkspaceNameField({ const nextMode = next as SmartNameMode onActiveSourceModeChange?.(nextMode) setMode(nextMode) - setOpen(!disabled && nextMode !== 'text' && selectedSource === null) + if (!disabled && nextMode !== 'text' && selectedSource === null) { + markSourcePopoverUserEngaged() + setOpen(true) + } else { + setOpen(false) + } cancelLocalInputFocusFrame() localInputFocusFrameRef.current = requestAnimationFrame(() => { localInputFocusFrameRef.current = null @@ -1297,7 +1332,7 @@ export default function SmartWorkspaceNameField({ setOpen(disabled || selectedSource ? false : next)} + onOpenChange={handleSourcePopoverOpenChange} > { - onValueChange(event.target.value) + onPointerDown={() => { if (!disabled && mode !== 'text') { + markSourcePopoverUserEngaged() setOpen(true) } }} - onFocus={() => { + onChange={(event) => { + onValueChange(event.target.value) if (!disabled && mode !== 'text') { + markSourcePopoverUserEngaged() setOpen(true) } }} + onFocus={(event) => { + // Why: only open when focus moves from another composer + // control (Tab/Shift+Tab). Dialog autofocus comes from + // outside the composer root and stays suppressed until + // click/type/tab-within-composer engagement above. + if (!isComposerFieldToFieldFocus(event)) { + return + } + markSourcePopoverUserEngaged() + tryOpenSourcePopover() + }} onKeyDown={(event) => { if (event.key === 'Tab' && event.shiftKey) { const activeTrigger = tabsListRef.current?.querySelector( diff --git a/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.test.ts b/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.test.ts new file mode 100644 index 000000000..3b9cd543d --- /dev/null +++ b/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.test.ts @@ -0,0 +1,65 @@ +// @vitest-environment happy-dom +import { describe, expect, it } from 'vitest' + +import { + isComposerFieldToFieldFocus, + WORKSPACE_COMPOSER_ROOT_SELECTOR +} from './smart-workspace-source-popover-focus' + +describe('isComposerFieldToFieldFocus', () => { + it('returns true when focus moves between fields inside the composer root', () => { + document.body.innerHTML = ` +
+ + +
+ ` + const nameInput = document.getElementById('name') as HTMLInputElement + const projectButton = document.getElementById('project') as HTMLButtonElement + + expect( + isComposerFieldToFieldFocus({ + currentTarget: nameInput, + relatedTarget: projectButton + }) + ).toBe(true) + }) + + it('returns false for dialog autofocus where focus comes from outside the composer', () => { + document.body.innerHTML = ` + +
+ +
+ ` + const nameInput = document.getElementById('name') as HTMLInputElement + const closeButton = document.getElementById('close') as HTMLButtonElement + + expect( + isComposerFieldToFieldFocus({ + currentTarget: nameInput, + relatedTarget: closeButton + }) + ).toBe(false) + }) + + it('returns false when relatedTarget is missing', () => { + document.body.innerHTML = ` +
+ +
+ ` + const nameInput = document.getElementById('name') as HTMLInputElement + + expect( + isComposerFieldToFieldFocus({ + currentTarget: nameInput, + relatedTarget: null + }) + ).toBe(false) + }) + + it('exports the composer root selector used by the card', () => { + expect(WORKSPACE_COMPOSER_ROOT_SELECTOR).toBe('[data-workspace-composer-root="true"]') + }) +}) diff --git a/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.ts b/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.ts new file mode 100644 index 000000000..147001fb6 --- /dev/null +++ b/src/renderer/src/components/new-workspace/smart-workspace-source-popover-focus.ts @@ -0,0 +1,14 @@ +import type { FocusEvent } from 'react' + +export const WORKSPACE_COMPOSER_ROOT_SELECTOR = '[data-workspace-composer-root="true"]' + +export function isComposerFieldToFieldFocus( + event: Pick, 'currentTarget' | 'relatedTarget'> +): boolean { + const related = event.relatedTarget + if (!(related instanceof HTMLElement)) { + return false + } + const composer = event.currentTarget.closest(WORKSPACE_COMPOSER_ROOT_SELECTOR) + return composer !== null && composer.contains(related) +}