Skip to content

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

TypeScript

fix(Selector): the sheet trigger names the active option only once it is rendered (#9028)

Cindy Zhang537d4db23b01
diff --git a/.changeset/selector-sheet-active-option-mounted.md b/.changeset/selector-sheet-active-option-mounted.md
new file mode 100644
index 0000000000..1ca9262df2
--- /dev/null
+++ b/.changeset/selector-sheet-active-option-mounted.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Selector's touch sheet no longer points screen readers at an option that isn't there yet. On the first open in the bottom-sheet presentation, the trigger used to report the active option (`aria-activedescendant`) while the sheet was still loading and none of its options existed. The trigger now names the active option only once the sheet has rendered it.
+
+@cixzhang
diff --git a/packages/core/src/Selector/Selector.sheetActiveDescendant.test.tsx b/packages/core/src/Selector/Selector.sheetActiveDescendant.test.tsx
new file mode 100644
index 0000000000..a9ef252e9e
--- /dev/null
+++ b/packages/core/src/Selector/Selector.sheetActiveDescendant.test.tsx
@@ -0,0 +1,95 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Selector.sheetActiveDescendant.test.tsx
+ * @input Uses vitest, Testing Library, and Selector with its bottom-sheet
+ * module held until the test releases it
+ * @output Tests that the bottom-sheet trigger names an active option only once
+ * that option is in the document, while the sheet's surface is still loading
+ * @position Tests; Selector's touch presentation on first open, alongside
+ * Selector.test.tsx. The held module stands in for the code-split sheet a
+ * real first open waits for.
+ */
+
+import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
+import {render, screen, waitFor} from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+
+const sheetModule = vi.hoisted(() => {
+ let release: () => void = () => {};
+ const loaded = new Promise<void>(resolve => {
+ release = resolve;
+ });
+ return {loaded, release: () => release()};
+});
+
+vi.mock('../BottomSheet/BottomSheet', async importOriginal => {
+ await sheetModule.loaded;
+ return importOriginal();
+});
+
+import {Selector} from './Selector';
+
+// jsdom has no modal dialogs or media queries; stand in for them the way
+// Selector.test.tsx does.
+beforeEach(() => {
+ HTMLDialogElement.prototype.showModal = vi.fn(function (
+ this: HTMLDialogElement,
+ ) {
+ this.setAttribute('open', '');
+ });
+ HTMLDialogElement.prototype.close = vi.fn(function (this: HTMLDialogElement) {
+ this.removeAttribute('open');
+ });
+ vi.stubGlobal(
+ 'matchMedia',
+ vi.fn().mockImplementation((query: string) => ({
+ matches: false,
+ media: query,
+ onchange: null,
+ addEventListener: vi.fn(),
+ removeEventListener: vi.fn(),
+ addListener: vi.fn(),
+ removeListener: vi.fn(),
+ dispatchEvent: vi.fn(),
+ })),
+ );
+});
+
+afterEach(() => {
+ sheetModule.release();
+ vi.unstubAllGlobals();
+});
+
+describe('Selector bottom-sheet active option', () => {
+ it('names no active option on the trigger until the sheet has rendered it', async () => {
+ const user = userEvent.setup();
+ render(
+ <Selector
+ label="Fruit"
+ options={['Apple', 'Banana']}
+ presentation="bottom-sheet"
+ />,
+ );
+ const trigger = screen.getByRole('combobox');
+ trigger.focus();
+ await user.keyboard('{Enter}');
+
+ // Open, but the sheet's surface has not loaded: no option exists yet, so
+ // the trigger must not point at one.
+ expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ expect(screen.queryByRole('option', {hidden: true})).toBeNull();
+ expect(trigger).not.toHaveAttribute('aria-activedescendant');
+
+ // The first real load of the sheet module under the test transform can
+ // take a few seconds.
+ sheetModule.release();
+ await screen.findByRole('listbox', {hidden: true}, {timeout: 5_000});
+ await waitFor(() =>
+ expect(trigger).toHaveAttribute('aria-activedescendant'),
+ );
+ expect(
+ document.getElementById(trigger.getAttribute('aria-activedescendant')!),
+ ).toHaveAttribute('role', 'option');
+ });
+});
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index 77adf71c6e..ef9521e905 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -1140,6 +1140,14 @@ export function Selector<T extends SelectorOptionType>(
// Ref for listbox to measure selected item position
const listboxRef = useRef<HTMLDivElement>(null);
+ // The trigger may name the active option only once the listbox holding it
+ // is in the document. The bottom sheet loads its surface on first open, so
+ // the trigger expands a moment before its options exist.
+ const [isListboxMounted, setIsListboxMounted] = useState(false);
+ const attachListbox = useCallback((node: HTMLDivElement | null) => {
+ listboxRef.current = node;
+ setIsListboxMounted(node != null);
+ }, []);
// Typeahead is defined below (it needs the popover), but closing and clearing
// must drop its pending buffer — otherwise a stale prefix survives the reset
@@ -1816,7 +1824,7 @@ export function Selector<T extends SelectorOptionType>(
{renderSearch()}
<Divider />
<div
- ref={listboxRef}
+ ref={attachListbox}
id={listboxId}
role="listbox"
aria-labelledby={triggerId}
@@ -1835,7 +1843,7 @@ export function Selector<T extends SelectorOptionType>(
</div>
) : (
<div
- ref={listboxRef}
+ ref={attachListbox}
id={listboxId}
role="listbox"
{...listboxPress.menuProps}
@@ -2042,6 +2050,7 @@ export function Selector<T extends SelectorOptionType>(
!isEffectivelyReadOnly &&
!hasSearch &&
surface.isOpen &&
+ isListboxMounted &&
highlightedIndex >= 0
? getItemId(highlightedIndex)
: undefined