An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector, MultiSelector): a light dismiss onto another field leaves focus there (#9018)
Cindy Zhang0bdda320c0c9
diff --git a/.changeset/selector-light-dismiss-focus.md b/.changeset/selector-light-dismiss-focus.mdnew file mode 100644index 0000000000..d37bfaa451--- /dev/null+++ b/.changeset/selector-light-dismiss-focus.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Selector, MultiSelector: pressing another field while the list is open now leaves focus in that field. Before, the list's close handler moved focus back to the Selector's trigger, so the next key went to the closed Selector (a letter could change its value through type-to-select). Escape, picking an option, or a press on nothing focusable still return focus to the trigger, and a MultiSelector with `renderTrigger` still returns focus to the caller's control on every close.++@cixzhangdiff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex bd0f11ed36..0a0c2a0a25 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -3505,6 +3505,47 @@ describe('MultiSelector trigger render prop', () => {expect(screen.queryByText('Labels')).toBeNull();});+ // spec:AST-055 AR3: a light dismiss returns focus to the caller's control,+ // even when the press that closed the list focused another field.+ it('returns focus to the caller control after a light dismiss onto another field', async () => {+ const user = userEvent.setup();+ render(+ <>+ <MultiSelector+ label="Labels"+ options={OPTIONS}+ value={[]}+ onChange={() => {}}+ renderTrigger={props => (+ <button type="button" {...props}>+ Pick labels+ </button>+ )}+ />+ <input aria-label="Notes" />+ </>,+ );+ const opener = screen.getByRole('button', {name: 'Pick labels'});+ await user.click(opener);+ expect(opener).toHaveAttribute('aria-expanded', 'true');++ act(() => {+ screen.getByRole('textbox', {name: 'Notes'}).focus();+ });+ const popover = document.querySelector('[popover]') as HTMLElement;+ act(() => {+ popover.dispatchEvent(+ Object.assign(new Event('toggle'), {+ oldState: 'open',+ newState: 'closed',+ }),+ );+ });++ expect(opener).toHaveAttribute('aria-expanded', 'false');+ expect(opener).toHaveFocus();+ });+it('opens against the caller control and names the listbox by label', async () => {const user = userEvent.setup();render(diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 604ad37fae..47114eb19c 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -1450,6 +1450,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({onHide: handleLayerHide,onShow: handleLayerShow,triggerRef,+ isFocusReturnAlways: hasExternalTrigger,popoverOptions: {hasLightDismiss: true,hasCloseButton: false,diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 779d335653..0edf56ab80 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -4288,6 +4288,71 @@ describe('Selector popup theme target', () => {});});+describe('Selector light dismiss focus (spec:Popover AR4)', () => {+ function closeByLightDismiss() {+ const popover = document.querySelector('[popover]') as HTMLElement;+ act(() => {+ popover.dispatchEvent(+ Object.assign(new Event('toggle'), {+ oldState: 'open',+ newState: 'closed',+ }),+ );+ });+ }++ it('leaves focus on the field a press moved it to', async () => {+ const user = userEvent.setup();+ render(+ <>+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Banana"+ onChange={() => {}}+ placement="below"+ />+ <input aria-label="Notes" />+ </>,+ );+ const trigger = screen.getByRole('combobox');+ await user.click(trigger);+ expect(trigger).toHaveAttribute('aria-expanded', 'true');++ // The press on another field focuses it before the browser's light+ // dismiss closes the list.+ const notes = screen.getByRole('textbox', {name: 'Notes'});+ act(() => {+ notes.focus();+ });+ closeByLightDismiss();++ expect(trigger).toHaveAttribute('aria-expanded', 'false');+ expect(notes).toHaveFocus();+ });++ it('returns focus to the trigger when the press lands on nothing focusable', async () => {+ const user = userEvent.setup();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ value="Banana"+ onChange={() => {}}+ placement="below"+ />,+ );+ const trigger = screen.getByRole('combobox');+ await user.click(trigger);+ act(() => {+ (document.activeElement as HTMLElement | null)?.blur();+ });+ closeByLightDismiss();++ expect(trigger).toHaveFocus();+ });+});+describe('Selector option-row theme target', () => {const ROW_OPTIONS = ['Apple', 'Banana', 'Orange'];diff --git a/packages/core/src/Selector/useSelectorPresentation.ts b/packages/core/src/Selector/useSelectorPresentation.tsindex ab9be1ba04..1853c224d5 100644--- a/packages/core/src/Selector/useSelectorPresentation.ts+++ b/packages/core/src/Selector/useSelectorPresentation.ts@@ -34,6 +34,13 @@ interface UseSelectorPresentationOptions {onShow: () => void;popoverOptions: Omit<UsePopoverOptions, 'onHide' | 'onShow'>;triggerRef: RefObject<HTMLElement | null>;+ /**+ * Return focus to the trigger on every close, even when a press already+ * moved focus to another element. A caller-rendered trigger needs this+ * (spec:AST-055 AR3); the built-in trigger leaves focus where the press put+ * it (spec:Popover AR4).+ */+ isFocusReturnAlways?: boolean;}interface SelectorPresentationController {@@ -55,6 +62,7 @@ export function useSelectorPresentation({onShow,popoverOptions,triggerRef,+ isFocusReturnAlways = false,}: UseSelectorPresentationOptions): SelectorPresentationController {const resolvedPresentation = useAdaptivePresentation(presentation);const activePresentationRef =@@ -68,6 +76,9 @@ export function useSelectorPresentation({const onHideRef = useRef(onHide);const onShowRef = useRef(onShow);const didShowPopoverRef = useRef(false);+ // The open list's container, read when the popover hides to tell focus that+ // is still inside the list from focus that moved to another element.+ const popoverContentRef = useRef<HTMLDivElement | null>(null);const {isFocusRingSuppressed,onFocusReturnTargetFocus,@@ -78,10 +89,26 @@ export function useSelectorPresentation({onShowRef.current = onShow;const handlePopoverHide = useCallback(() => {- prepareFocusReturn();onHideRef.current();- triggerRef.current?.focus();- }, [prepareFocusReturn, triggerRef]);+ const trigger = triggerRef.current;+ // A light dismiss caused by pressing another focusable element has already+ // moved focus there; taking it back would leave the next key on this+ // closed Selector (spec:Popover AR4 returns focus only when it would+ // otherwise be lost). Escape, a choice in the list, or a press on nothing+ // focusable still returns focus to the trigger.+ const active = document.activeElement;+ const focusLandedElsewhere =+ !isFocusReturnAlways &&+ active instanceof HTMLElement &&+ active !== document.body &&+ active !== trigger &&+ !popoverContentRef.current?.contains(active);+ if (focusLandedElsewhere) {+ return;+ }+ prepareFocusReturn();+ trigger?.focus();+ }, [isFocusReturnAlways, prepareFocusReturn, triggerRef]);const handlePopoverShow = useCallback(() => {didShowPopoverRef.current = true;resetFocusReturn();@@ -92,6 +119,7 @@ export function useSelectorPresentation({onHide: handlePopoverHide,onShow: handlePopoverShow,});+ popoverContentRef.current = popover.contentRef.current;const {hide: hidePopover, show: showPopover} = popover;const show = useCallback((): boolean => {