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 1 commit
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 | ||
| --- | ||
|
|
||
| [fix] `Tokenizer` hands focus to the new control when a presentation swap removes the focused one, through the controls' callback refs instead of a read of the focused element during render. A render React discards, as concurrent rendering can, no longer drops a keyboard user's focus to the page. After the sheet's Clear all confirmation closes, focus moves, and the clear is announced, in the confirm or cancel handler itself. Thanks @imdreamrunner (#7977). | ||
|
|
||
| @cixzhang @duytbp | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -823,14 +823,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 +833,34 @@ 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; | ||
| const active = node.ownerDocument.activeElement; | ||
| if (takesFocus && (active == null || active === node.ownerDocument.body)) { | ||
|
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 is |
||
| 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; | ||
| } | ||
| }; | ||
|
Comment on lines
+858
to
+862
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. Only the input or the trigger notes a pending hand-off here. With |
||
| }, []); | ||
| // 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). | ||
|
|
@@ -954,6 +942,7 @@ export function Tokenizer<T extends SearchableItem>({ | |
| triggerRef, | ||
| ref as React.Ref<HTMLButtonElement> | undefined, | ||
| disabledMessageTooltip.ref, | ||
| handOffFocusRef, | ||
| ); | ||
|
|
||
| useImperativeHandle(handleRef, () => ({ | ||
|
|
@@ -1034,7 +1023,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| layer.show(); | ||
| } | ||
| if (comingFromOutside) { | ||
| isControlFocusedRef.current = true; | ||
| if (!isHandingOffFocusRef.current) { | ||
| hasReportedFocusRef.current = true; | ||
| onFocus?.(e); | ||
|
|
@@ -1052,7 +1040,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| const handleBlurCapture = useCallback( | ||
| (e: React.FocusEvent) => { | ||
| if (!isFocusInTokenizer(e.relatedTarget)) { | ||
| isControlFocusedRef.current = false; | ||
| hasReportedFocusRef.current = false; | ||
| setIsFocusedWithin(false); | ||
| onBlur?.(e); | ||
|
|
@@ -1399,6 +1386,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 +1521,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,7 +1549,6 @@ export function Tokenizer<T extends SearchableItem>({ | |
| if (isSheetOpen) { | ||
| return; | ||
| } | ||
| isControlFocusedRef.current = false; | ||
| hasReportedFocusRef.current = false; | ||
| onBlur?.(event); | ||
| }} | ||
|
|
||
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.
The public
Tokenizeroffers no presentation option and renders only the popover, so it has no presentation swap or sheet, and on the base a discarded render only affected thehasAutoFocushydration focus. As written, the release note tells consumers a public focus bug was fixed. Suggest wording that says the change is inside the internal sheet presentation (not yet public) and that public fields are unchanged.