Repository navigation
fix(Tokenizer): focus hand-offs live in the controls' callback refs and the confirmation's close handler #9162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7013635
ecdd14c
8573c14
d7d5496
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [chore] `Tokenizer` hands focus between its controls from callback refs and the Clear all confirmation's close handler, not from effects, as `architecture:react-component-runtime` INV2 and INV6 require. Where focus lands is unchanged. The presentation swap and the sheet are internal (not yet public). Thanks @imdreamrunner (#7977). | ||
|
|
||
| @cixzhang @duytbp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ import { | |
| import type {SearchableItem, SearchSource} from '../Typeahead/types'; | ||
| import {composeEventHandlers, mergeProps} from '../utils'; | ||
| import {joinAriaIDs} from '../utils/inputAria'; | ||
| import {isFocusDetached} from '../utils/focusReturn'; | ||
| import {themeProps} from '../utils/themeProps'; | ||
| import {useTranslator} from '../i18n'; | ||
| import type {TokenizerBottomSheetProps} from './TokenizerBottomSheet'; | ||
|
|
@@ -823,14 +824,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| }, | ||
| [onChangeQuery], | ||
| ); | ||
| // Whether the field's control — the inline input or the trigger — holds | ||
| // focus. Kept in a ref by the focus and blur handlers of both, because a | ||
| // control unmounted while focused fires no blur: when a presentation swap | ||
| // removes the focused control (the latch releasing into the popover, or | ||
| // `adaptive` changing while the sheet is closed), the new presentation's | ||
| // control takes the focus over once it has mounted, so focus never lands on | ||
| // the page body (`component:Tokenizer` FR16, `spec:AST-085` FR4). | ||
| const isControlFocusedRef = useRef(false); | ||
| // Set around a focus move the field makes itself — a presentation swap | ||
| // handing focus from one control to the other — so the receiving control | ||
| // does not report it as focus entering from outside. | ||
|
|
@@ -841,38 +834,33 @@ export function Tokenizer<T extends SearchableItem>({ | |
| // entry, and the one focus the trigger then gets — BottomSheet's return | ||
| // focus — is the entry to report. | ||
| const hasReportedFocusRef = useRef(false); | ||
| // A control can hold focus without the field having seen a focus event: | ||
| // the inline input server-rendered with `autofocus` (`hasAutoFocus`) is | ||
| // focused before hydration, and the swap to the trigger in the first | ||
| // render after it removes that input. React detaches the old control | ||
| // before any effect of the swapping commit runs, so the live focus is | ||
| // read during that render, while the control is still in the document — | ||
| // a read of `document.activeElement` only, with no update made in render. | ||
| const previousPresentationRef = useRef(isSheetPresentation); | ||
| if (previousPresentationRef.current !== isSheetPresentation) { | ||
| previousPresentationRef.current = isSheetPresentation; | ||
| if (typeof document !== 'undefined') { | ||
| const active = document.activeElement; | ||
| if ( | ||
| active != null && | ||
| (active === inputRef.current || active === triggerRef.current) | ||
| ) { | ||
| isControlFocusedRef.current = true; | ||
| } | ||
| } | ||
| } | ||
| useEffect(() => { | ||
| if (!isControlFocusedRef.current) { | ||
| // When a presentation swap removes the focused control (the latch | ||
| // releasing into the popover, or `adaptive` changing while the sheet is | ||
| // closed), the new presentation's control takes the focus over, so focus | ||
| // never lands on the page body (`component:Tokenizer` FR16, `spec:AST-085` | ||
| // FR4). Both controls' callback refs own the hand-off | ||
| // (`architecture:react-component-runtime` INV6): the detaching control | ||
| // notes that it held focus — it is still in the document then, so this also | ||
| // covers a control focused before hydration (`hasAutoFocus`), which no focus | ||
| // event reported — and the attaching control takes it. | ||
| const isFocusPendingHandOffRef = useRef(false); | ||
| const handOffFocusRef = useCallback((node: HTMLElement | null) => { | ||
| if (node == null) { | ||
| return; | ||
| } | ||
| const active = document.activeElement; | ||
| if (active != null && active !== document.body) { | ||
| return; | ||
| const takesFocus = isFocusPendingHandOffRef.current; | ||
| isFocusPendingHandOffRef.current = false; | ||
| if (takesFocus && isFocusDetached(node.ownerDocument)) { | ||
| isHandingOffFocusRef.current = true; | ||
| node.focus(); | ||
| isHandingOffFocusRef.current = false; | ||
| } | ||
| isHandingOffFocusRef.current = true; | ||
| (isSheetPresentation ? triggerRef.current : inputRef.current)?.focus(); | ||
| isHandingOffFocusRef.current = false; | ||
| }, [isSheetPresentation]); | ||
| return () => { | ||
| if (node.ownerDocument.activeElement === node) { | ||
| isFocusPendingHandOffRef.current = true; | ||
| } | ||
| }; | ||
| }, []); | ||
| // The trigger's accessible description names the selected tokens: the | ||
| // button's children are tokens whose own content assistive technology | ||
| // leaves out of a button's name (`spec:AST-020` FR1). | ||
|
|
@@ -947,13 +935,37 @@ export function Tokenizer<T extends SearchableItem>({ | |
| focusTrigger: 'always', | ||
| isEnabled: showsDisabledMessage, | ||
| }); | ||
| // The inline field's wrapper holds every control a swap removes with the | ||
| // input — a token's Remove button, the inline Clear all — so it notes focus | ||
| // anywhere inside it for the hand-off above. It also anchors the | ||
| // disabled-message tooltip, whose handlers are gated internally by | ||
| // isEnabled, so attaching unconditionally is safe. | ||
| const tooltipAnchorRef = disabledMessageTooltip.ref; | ||
| const wrapperCallbackRef = useCallback( | ||
| (el: HTMLDivElement | null) => { | ||
| wrapperRef.current = el; | ||
| tooltipAnchorRef(el); | ||
| if (el == null) { | ||
| return; | ||
| } | ||
| return () => { | ||
| if (el.contains(el.ownerDocument.activeElement)) { | ||
| isFocusPendingHandOffRef.current = true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cleanup also runs when the same wrapper re-attaches through a new |
||
| } | ||
| wrapperRef.current = null; | ||
| tooltipAnchorRef(null); | ||
| }; | ||
| }, | ||
| [tooltipAnchorRef], | ||
| ); | ||
|
|
||
| // The sheet presentation has no input; the public ref reaches the trigger, | ||
| // the control it stands for (`family:input-fields` FR10). | ||
| const triggerMergedRef = useMergedRefs<HTMLButtonElement>( | ||
| triggerRef, | ||
| ref as React.Ref<HTMLButtonElement> | undefined, | ||
| disabledMessageTooltip.ref, | ||
| handOffFocusRef, | ||
| ); | ||
|
|
||
| useImperativeHandle(handleRef, () => ({ | ||
|
|
@@ -1034,7 +1046,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| layer.show(); | ||
| } | ||
| if (comingFromOutside) { | ||
| isControlFocusedRef.current = true; | ||
| if (!isHandingOffFocusRef.current) { | ||
| hasReportedFocusRef.current = true; | ||
| onFocus?.(e); | ||
|
|
@@ -1052,8 +1063,9 @@ export function Tokenizer<T extends SearchableItem>({ | |
| const handleBlurCapture = useCallback( | ||
| (e: React.FocusEvent) => { | ||
| if (!isFocusInTokenizer(e.relatedTarget)) { | ||
| isControlFocusedRef.current = false; | ||
| hasReportedFocusRef.current = false; | ||
| // Focus left the field: no hand-off is owed any more. | ||
| isFocusPendingHandOffRef.current = false; | ||
| setIsFocusedWithin(false); | ||
| onBlur?.(e); | ||
| if (isLayerMode) { | ||
|
|
@@ -1327,13 +1339,7 @@ export function Tokenizer<T extends SearchableItem>({ | |
|
|
||
| const wrapperContent = ( | ||
| <div | ||
| ref={el => { | ||
| wrapperRef.current = el; | ||
| // Anchor + hover/focus listeners for the disabled-message tooltip. | ||
| // Handlers are gated internally by isEnabled, so attaching | ||
| // unconditionally is safe. | ||
| disabledMessageTooltip.ref(el); | ||
| }} | ||
| ref={wrapperCallbackRef} | ||
| role="group" | ||
| aria-label={label} | ||
| onClick={handleWrapperClick} | ||
|
|
@@ -1399,6 +1405,7 @@ export function Tokenizer<T extends SearchableItem>({ | |
| // With `experimental`, the ref type admits the trigger too; in | ||
| // this presentation it receives the input. | ||
| ref as React.Ref<HTMLInputElement> | undefined, | ||
| handOffFocusRef, | ||
| )} | ||
| searchSource={isAtMax ? emptySource : filteredSource} | ||
| value={null} | ||
|
|
@@ -1533,7 +1540,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| React.MouseEventHandler<HTMLButtonElement> | undefined, | ||
| )} | ||
| onFocus={event => { | ||
| isControlFocusedRef.current = true; | ||
| // BottomSheet focuses the trigger only after its exit completes; | ||
| // that focus is the signal that the retained sheet can leave. If | ||
| // the resolution moved to the popover meanwhile, the inline input | ||
|
|
@@ -1562,8 +1568,8 @@ export function Tokenizer<T extends SearchableItem>({ | |
| if (isSheetOpen) { | ||
| return; | ||
| } | ||
| isControlFocusedRef.current = false; | ||
| hasReportedFocusRef.current = false; | ||
| isFocusPendingHandOffRef.current = false; | ||
| onBlur?.(event); | ||
| }} | ||
| onKeyDown={onKeyDown} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the input or the trigger notes a pending hand-off here. With
presentation: 'adaptive', a keyboard user on a token's Remove button or the inline Clear all button loses focus to the page body when the screen turns compact-touch; the base moved it to the trigger, because its flag was set on any focus entry into the field. Noting the hand-off when focus is anywhere inside the detaching field (for example from a cleanup-returning callback ref on the inline wrapper that checksel.contains(el.ownerDocument.activeElement)) would restore that, with swap tests that start on Remove and on Clear all.