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.
This commit is contained in:
@@ -480,6 +480,7 @@ export default function NewWorkspaceComposerCard({
|
||||
return (
|
||||
<div
|
||||
ref={setComposerNode}
|
||||
data-workspace-composer-root="true"
|
||||
// Why: preload classifies native OS file drops by the nearest
|
||||
// `data-native-file-drop-target` marker in the composedPath. Tagging
|
||||
// the composer root makes drops anywhere on the card route to the
|
||||
|
||||
+8
@@ -87,4 +87,12 @@ describe('SmartWorkspaceNameField repo-backed source boundaries', () => {
|
||||
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()')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<Map<string, RepoSlug | null>>(new Map())
|
||||
const handledCrossRepoUrlRef = useRef<string | null>(null)
|
||||
const localInputFocusFrameRef = useRef<number | null>(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<ReturnType<typeof parseGitHubIssueOrPRLink>>
|
||||
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({
|
||||
|
||||
<Popover
|
||||
open={!disabled && open && mode !== 'text' && selectedSource === null}
|
||||
onOpenChange={(next) => setOpen(disabled || selectedSource ? false : next)}
|
||||
onOpenChange={handleSourcePopoverOpenChange}
|
||||
>
|
||||
<Command
|
||||
value={resolvedCommandValue}
|
||||
@@ -1397,17 +1432,30 @@ export default function SmartWorkspaceNameField({
|
||||
ref={setInputNode}
|
||||
data-workspace-name-input="true"
|
||||
value={value}
|
||||
onChange={(event) => {
|
||||
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<HTMLElement>(
|
||||
|
||||
+65
@@ -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 = `
|
||||
<div data-workspace-composer-root="true">
|
||||
<button id="project" type="button">Project</button>
|
||||
<input id="name" type="text" />
|
||||
</div>
|
||||
`
|
||||
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 = `
|
||||
<button id="close" type="button">Close</button>
|
||||
<div data-workspace-composer-root="true">
|
||||
<input id="name" type="text" />
|
||||
</div>
|
||||
`
|
||||
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 = `
|
||||
<div data-workspace-composer-root="true">
|
||||
<input id="name" type="text" />
|
||||
</div>
|
||||
`
|
||||
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"]')
|
||||
})
|
||||
})
|
||||
@@ -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<FocusEvent<HTMLElement>, '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)
|
||||
}
|
||||
Reference in New Issue
Block a user