@@ -88,6 +88,7 @@ import {
8888import type { SearchableItem , SearchSource } from '../Typeahead/types' ;
8989import { composeEventHandlers , mergeProps } from '../utils' ;
9090import { joinAriaIDs } from '../utils/inputAria' ;
91+ import { isFocusDetached } from '../utils/focusReturn' ;
9192import { themeProps } from '../utils/themeProps' ;
9293import { useTranslator } from '../i18n' ;
9394import type { TokenizerBottomSheetProps } from './TokenizerBottomSheet' ;
@@ -823,14 +824,6 @@ export function Tokenizer<T extends SearchableItem>({
823824 } ,
824825 [ onChangeQuery ] ,
825826 ) ;
826- // Whether the field's control — the inline input or the trigger — holds
827- // focus. Kept in a ref by the focus and blur handlers of both, because a
828- // control unmounted while focused fires no blur: when a presentation swap
829- // removes the focused control (the latch releasing into the popover, or
830- // `adaptive` changing while the sheet is closed), the new presentation's
831- // control takes the focus over once it has mounted, so focus never lands on
832- // the page body (`component:Tokenizer` FR16, `spec:AST-085` FR4).
833- const isControlFocusedRef = useRef ( false ) ;
834827 // Set around a focus move the field makes itself — a presentation swap
835828 // handing focus from one control to the other — so the receiving control
836829 // does not report it as focus entering from outside.
@@ -841,38 +834,33 @@ export function Tokenizer<T extends SearchableItem>({
841834 // entry, and the one focus the trigger then gets — BottomSheet's return
842835 // focus — is the entry to report.
843836 const hasReportedFocusRef = useRef ( false ) ;
844- // A control can hold focus without the field having seen a focus event:
845- // the inline input server-rendered with `autofocus` (`hasAutoFocus`) is
846- // focused before hydration, and the swap to the trigger in the first
847- // render after it removes that input. React detaches the old control
848- // before any effect of the swapping commit runs, so the live focus is
849- // read during that render, while the control is still in the document —
850- // a read of `document.activeElement` only, with no update made in render.
851- const previousPresentationRef = useRef ( isSheetPresentation ) ;
852- if ( previousPresentationRef . current !== isSheetPresentation ) {
853- previousPresentationRef . current = isSheetPresentation ;
854- if ( typeof document !== 'undefined' ) {
855- const active = document . activeElement ;
856- if (
857- active != null &&
858- ( active === inputRef . current || active === triggerRef . current )
859- ) {
860- isControlFocusedRef . current = true ;
861- }
862- }
863- }
864- useEffect ( ( ) => {
865- if ( ! isControlFocusedRef . current ) {
837+ // When a presentation swap removes the focused control (the latch
838+ // releasing into the popover, or `adaptive` changing while the sheet is
839+ // closed), the new presentation's control takes the focus over, so focus
840+ // never lands on the page body (`component:Tokenizer` FR16, `spec:AST-085`
841+ // FR4). Both controls' callback refs own the hand-off
842+ // (`architecture:react-component-runtime` INV6): the detaching control
843+ // notes that it held focus — it is still in the document then, so this also
844+ // covers a control focused before hydration (`hasAutoFocus`), which no focus
845+ // event reported — and the attaching control takes it.
846+ const isFocusPendingHandOffRef = useRef ( false ) ;
847+ const handOffFocusRef = useCallback ( ( node : HTMLElement | null ) => {
848+ if ( node == null ) {
866849 return ;
867850 }
868- const active = document . activeElement ;
869- if ( active != null && active !== document . body ) {
870- return ;
851+ const takesFocus = isFocusPendingHandOffRef . current ;
852+ isFocusPendingHandOffRef . current = false ;
853+ if ( takesFocus && isFocusDetached ( node . ownerDocument ) ) {
854+ isHandingOffFocusRef . current = true ;
855+ node . focus ( ) ;
856+ isHandingOffFocusRef . current = false ;
871857 }
872- isHandingOffFocusRef . current = true ;
873- ( isSheetPresentation ? triggerRef . current : inputRef . current ) ?. focus ( ) ;
874- isHandingOffFocusRef . current = false ;
875- } , [ isSheetPresentation ] ) ;
858+ return ( ) => {
859+ if ( node . ownerDocument . activeElement === node ) {
860+ isFocusPendingHandOffRef . current = true ;
861+ }
862+ } ;
863+ } , [ ] ) ;
876864 // The trigger's accessible description names the selected tokens: the
877865 // button's children are tokens whose own content assistive technology
878866 // leaves out of a button's name (`spec:AST-020` FR1).
@@ -947,13 +935,37 @@ export function Tokenizer<T extends SearchableItem>({
947935 focusTrigger : 'always' ,
948936 isEnabled : showsDisabledMessage ,
949937 } ) ;
938+ // The inline field's wrapper holds every control a swap removes with the
939+ // input — a token's Remove button, the inline Clear all — so it notes focus
940+ // anywhere inside it for the hand-off above. It also anchors the
941+ // disabled-message tooltip, whose handlers are gated internally by
942+ // isEnabled, so attaching unconditionally is safe.
943+ const tooltipAnchorRef = disabledMessageTooltip . ref ;
944+ const wrapperCallbackRef = useCallback (
945+ ( el : HTMLDivElement | null ) => {
946+ wrapperRef . current = el ;
947+ tooltipAnchorRef ( el ) ;
948+ if ( el == null ) {
949+ return ;
950+ }
951+ return ( ) => {
952+ if ( el . contains ( el . ownerDocument . activeElement ) ) {
953+ isFocusPendingHandOffRef . current = true ;
954+ }
955+ wrapperRef . current = null ;
956+ tooltipAnchorRef ( null ) ;
957+ } ;
958+ } ,
959+ [ tooltipAnchorRef ] ,
960+ ) ;
950961
951962 // The sheet presentation has no input; the public ref reaches the trigger,
952963 // the control it stands for (`family:input-fields` FR10).
953964 const triggerMergedRef = useMergedRefs < HTMLButtonElement > (
954965 triggerRef ,
955966 ref as React . Ref < HTMLButtonElement > | undefined ,
956967 disabledMessageTooltip . ref ,
968+ handOffFocusRef ,
957969 ) ;
958970
959971 useImperativeHandle ( handleRef , ( ) => ( {
@@ -1034,7 +1046,6 @@ export function Tokenizer<T extends SearchableItem>({
10341046 layer . show ( ) ;
10351047 }
10361048 if ( comingFromOutside ) {
1037- isControlFocusedRef . current = true ;
10381049 if ( ! isHandingOffFocusRef . current ) {
10391050 hasReportedFocusRef . current = true ;
10401051 onFocus ?.( e ) ;
@@ -1052,8 +1063,9 @@ export function Tokenizer<T extends SearchableItem>({
10521063 const handleBlurCapture = useCallback (
10531064 ( e : React . FocusEvent ) => {
10541065 if ( ! isFocusInTokenizer ( e . relatedTarget ) ) {
1055- isControlFocusedRef . current = false ;
10561066 hasReportedFocusRef . current = false ;
1067+ // Focus left the field: no hand-off is owed any more.
1068+ isFocusPendingHandOffRef . current = false ;
10571069 setIsFocusedWithin ( false ) ;
10581070 onBlur ?.( e ) ;
10591071 if ( isLayerMode ) {
@@ -1327,13 +1339,7 @@ export function Tokenizer<T extends SearchableItem>({
13271339
13281340 const wrapperContent = (
13291341 < div
1330- ref = { el => {
1331- wrapperRef . current = el ;
1332- // Anchor + hover/focus listeners for the disabled-message tooltip.
1333- // Handlers are gated internally by isEnabled, so attaching
1334- // unconditionally is safe.
1335- disabledMessageTooltip . ref ( el ) ;
1336- } }
1342+ ref = { wrapperCallbackRef }
13371343 role = "group"
13381344 aria-label = { label }
13391345 onClick = { handleWrapperClick }
@@ -1399,6 +1405,7 @@ export function Tokenizer<T extends SearchableItem>({
13991405 // With `experimental`, the ref type admits the trigger too; in
14001406 // this presentation it receives the input.
14011407 ref as React . Ref < HTMLInputElement > | undefined ,
1408+ handOffFocusRef ,
14021409 ) }
14031410 searchSource = { isAtMax ? emptySource : filteredSource }
14041411 value = { null }
@@ -1533,7 +1540,6 @@ export function Tokenizer<T extends SearchableItem>({
15331540 React . MouseEventHandler < HTMLButtonElement > | undefined ,
15341541 ) }
15351542 onFocus = { event => {
1536- isControlFocusedRef . current = true ;
15371543 // BottomSheet focuses the trigger only after its exit completes;
15381544 // that focus is the signal that the retained sheet can leave. If
15391545 // the resolution moved to the popover meanwhile, the inline input
@@ -1562,8 +1568,8 @@ export function Tokenizer<T extends SearchableItem>({
15621568 if ( isSheetOpen ) {
15631569 return ;
15641570 }
1565- isControlFocusedRef . current = false ;
15661571 hasReportedFocusRef . current = false ;
1572+ isFocusPendingHandOffRef . current = false ;
15671573 onBlur ?.( event ) ;
15681574 } }
15691575 onKeyDown = { onKeyDown }
0 commit comments