Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(lab): MobileTokenizer reports the sheet's focus hand-back as the entry when the trigger never had focus (#8768)

Xinzi Zhoud9da59f6ef73
diff --git a/packages/lab/src/MobileTokenizer/MobileTokenizer.test.tsx b/packages/lab/src/MobileTokenizer/MobileTokenizer.test.tsx
index f23125ecfb..1bdad40a41 100644
--- a/packages/lab/src/MobileTokenizer/MobileTokenizer.test.tsx
+++ b/packages/lab/src/MobileTokenizer/MobileTokenizer.test.tsx
@@ -764,6 +764,51 @@ describe('MobileTokenizer Core-sheet parity', () => {
expect(onBlur).toHaveBeenCalledTimes(1);
});
+ it("reports the sheet's focus hand-back as the entry when the trigger never had focus (a tap that opens without focusing, as Safari does), so the caller never gets a blur without its focus", async () => {
+ presentModally();
+ const events: string[] = [];
+ render(
+ <>
+ <button type="button">Elsewhere</button>
+ <MobileTokenizer
+ label="Tags"
+ onFocus={() => events.push('focus')}
+ onBlur={() => events.push('blur')}
+ searchSource={source}
+ value={[ITEMS[0]]}
+ onChange={() => {}}
+ debounceMs={0}
+ />
+ </>,
+ );
+ const trigger = screen.getByRole('button', {name: 'Tags'});
+ // A click with no focus first: the sheet opens, nothing reported yet.
+ fireEvent.click(trigger);
+ await screen.findByRole('dialog');
+ await screen.findByRole('checkbox', {name: 'Design'});
+ expect(trigger).not.toHaveFocus();
+ expect(events).toEqual([]);
+ // Done: BottomSheet hands focus to the trigger. The trigger never had
+ // focus, so this is the entry.
+ fireEvent.click(screen.getByRole('button', {name: 'Done'}));
+ act(() => trigger.focus());
+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
+ expect(events).toEqual(['focus']);
+ // Leaving the field reports the blur, after its focus.
+ act(() => screen.getByRole('button', {name: 'Elsewhere'}).focus());
+ expect(events).toEqual(['focus', 'blur']);
+ // A second round from a focused trigger: entry on focus, quiet on the
+ // hand-back, blur on leaving — one pair again.
+ act(() => trigger.focus());
+ fireEvent.click(trigger);
+ await screen.findByRole('dialog');
+ fireEvent.click(screen.getByRole('button', {name: 'Done'}));
+ act(() => trigger.focus());
+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
+ act(() => screen.getByRole('button', {name: 'Elsewhere'}).focus());
+ expect(events).toEqual(['focus', 'blur', 'focus', 'blur']);
+ });
+
it('forwards the base props — id, data-*, aria-*, handlers — to the trigger button, not the field root', () => {
const onPointerDown = vi.fn();
render(
diff --git a/packages/lab/src/MobileTokenizer/MobileTokenizer.tsx b/packages/lab/src/MobileTokenizer/MobileTokenizer.tsx
index e2be8f0279..00ecf86324 100644
--- a/packages/lab/src/MobileTokenizer/MobileTokenizer.tsx
+++ b/packages/lab/src/MobileTokenizer/MobileTokenizer.tsx
@@ -38,6 +38,7 @@ import {
useEffect,
useId,
useImperativeHandle,
+ useLayoutEffect,
useMemo,
useRef,
useState,
@@ -327,6 +328,12 @@ export function MobileTokenizer<T extends SearchableItem>({
const tokenElementId = (itemId: string) =>
`${countId}-${encodeURIComponent(itemId)}`;
const triggerRef = useRef<HTMLButtonElement>(null);
+ // Whether the caller has been told focus entered the field and not yet
+ // that it left: the sheet's focus hand-back is reported as the entry when
+ // the trigger never had focus (a tap or click that opened the sheet without
+ // focusing the trigger, as Safari does), so a caller never sees a blur
+ // without its focus.
+ const hasReportedFocusRef = useRef(false);
const [isSheetOpen, setIsSheetOpen] = useState(false);
// The sheet stays mounted after it closes until BottomSheet has run its
// exit and handed focus back to the trigger; at rest nothing of the sheet
@@ -497,12 +504,20 @@ export function MobileTokenizer<T extends SearchableItem>({
// that focus is the signal that the retained sheet can leave. The
// field's own sheet is part of the field, so focus coming back from
// it is not focus entering from outside: the caller's onFocus stays
- // quiet for it.
+ // quiet for it — unless the trigger never had focus (a tap that
+ // opened the sheet without focusing it, as Safari does), in which
+ // case the hand-back is the entry, and the caller hears it before
+ // any blur.
const isReturningFromSheet = !isSheetOpen && isSheetPresented;
if (isReturningFromSheet) {
setIsSheetPresented(false);
+ }
+ const isInsideTheField =
+ isReturningFromSheet && hasReportedFocusRef.current;
+ if (isInsideTheField) {
return;
}
+ hasReportedFocusRef.current = true;
(
callerOnFocus as unknown as
React.FocusEventHandler<HTMLButtonElement> | undefined
@@ -514,6 +529,7 @@ export function MobileTokenizer<T extends SearchableItem>({
if (isSheetOpen) {
return;
}
+ hasReportedFocusRef.current = false;
(
callerOnBlur as unknown as
React.FocusEventHandler<HTMLButtonElement> | undefined
@@ -724,12 +740,26 @@ function MobileTokenizerSheetContent<T extends SearchableItem>({
// The source is read through a ref at request time, so a caller that
// rebuilds the source object in render causes no new request on an
// unrelated render (a row toggle); the sheet asks the source only on open,
- // on a query change, and on clearing (FR20).
+ // on a query change, and on clearing (FR20). The ref is updated in a layout
+ // effect, before any request this commit makes. (React's own
+ // useLayoutEffect: this content renders only inside an open sheet, after a
+ // press, so it never runs on the server.)
const searchSourceRef = useRef(searchSource);
- useEffect(() => {
+ useLayoutEffect(() => {
searchSourceRef.current = searchSource;
}, [searchSource]);
- useEffect(() => {
+ // Synchronizes the row list with the caller's `searchSource`, an external
+ // system this component does not own: it asks the source for the bootstrap
+ // set or the query's results and copies the answer into state. The
+ // bootstrap set is requested at once — on open and when the query clears —
+ // and a typed query waits `debounceMs`. A layout effect, as Core's sheet
+ // runs it, so a source that answers synchronously settles before the
+ // browser paints the open commit and the Loading row never reaches the
+ // screen. Cleanup clears the debounce timer so a superseded query never
+ // fetches, and the sequence guard drops a response that lands after a
+ // newer request, so the list never shows stale results
+ // (`architecture:react-component-runtime` INV3).
+ useLayoutEffect(() => {
if (!isOpen) {
return;
}