An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): the listbox is named by the field label in every presentation (#9737)
Cindy Zhange70850f04b84
diff --git a/.changeset/selector-listbox-named-by-label.md b/.changeset/selector-listbox-named-by-label.mdnew file mode 100644index 0000000000..8fa7a5a839--- /dev/null+++ b/.changeset/selector-listbox-named-by-label.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A `Selector`'s open option list is now named by the field's label in every presentation. In the popover without search, the list was named by the trigger, so a screen reader announced the selected value, or the placeholder when nothing was selected (for example "Banana" instead of "Fruit"), against WCAG 2.2 SC 4.1.2. `getByLabelText(<label>)` now matches the list as well as the trigger.++@cixzhangdiff --git a/docs/specs/components/Selector/Selector.spec.md b/docs/specs/components/Selector/Selector.spec.mdindex 3e7bcdba80..d8011d8c7c 100644--- a/docs/specs/components/Selector/Selector.spec.md+++ b/docs/specs/components/Selector/Selector.spec.md@@ -18,6 +18,7 @@ verified_by:packages/core/src/Selector/__tests__/Listbox.a11y.chromium.spec.ts,packages/core/src/Selector/__tests__/Selector.combobox.a11y.test.tsx,packages/core/src/Selector/__tests__/Selector.combobox.a11y.chromium.spec.ts,+ packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts,apps/storybook/stories/Selector.stories.tsx,.github/scripts/story-play-guard.js,]@@ -207,7 +208,9 @@ These requirements describe shipped behavior on current `main`.- **AR1 — Semantics follow presentation.** The anchored presentation exposes alistbox through a Popover and the trigger uses `aria-haspopup="listbox"`. Thetouch presentation exposes a modal BottomSheet dialog containing the listbox- and the trigger uses `aria-haspopup="dialog"`.+ and the trigger uses `aria-haspopup="dialog"`. In every presentation the+ listbox's accessible name is the field's label (`label`), not the trigger's+ value.- **AR2 — Focus follows the active interaction model.** The anchored presentationkeeps the combobox relationship at the trigger/search control. The modal touchpresentation moves focus into its search control or listbox. On close, focus@@ -340,18 +343,18 @@ new theming target.## Verification map-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |-| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |-| FR1, AR2, AR3 | `Selector.test.tsx` selection, focus, and keyboard suites, including "leaves focus on the field a press moved it to" and "returns focus to the trigger when the press lands on nothing focusable" | closed/open, search/non-search, disabled option | Removing selection wiring, focus movement, or keyboard behavior fails the named interaction tests | `audit:Selector/behavior` |-| FR2 | placement and selected-item geometry tests | default, explicit, RTL, transformed entry | Using the wrong positioning model fails the expected position-area or margin | `audit:Selector/behavior` |-| FR3, AV1 | Focused `Selector.test.tsx` computed-display checks plus guarded Chromium Storybook matrix and stable visual stories | popover/bottom sheet, narrow/wide, start/end, LTR/RTL, selected/unselected, check/radio | Empty output reserves width, visible output collapses, logical placement flips incorrectly, or row content overflows | `audit:Selector/design-rendered` |-| FR4, AR1, AR2 | presentation tests plus `aria-haspopup` source review | pointer, compact coarse pointer, search/non-search | Wrong Popover/dialog roles or focus destinations fail tests; `aria-haspopup` values require source/a11y review | `audit:Selector/accessibility` |-| AR1, AV2 | `Selector.listbox.a11y.test.tsx` and `Listbox.a11y.chromium.spec.ts` against the shared Listbox contract | popover/sheet, flat/grouped, selected/unselected, disabled, search, loading, custom RTL | A new or wider loss of role, listbox/option name, state, or ownership gates; the exact unnamed no-search sheet state remains known debt | `audit:Selector/accessibility` |-| FR5, FR6, AR1, AR2 (anchored), AR4 | `Selector.combobox.a11y.test.tsx` and `Selector.combobox.a11y.chromium.spec.ts` against the shared Combobox contract | closed, open with active option, read-only, busy, bottom-sheet open, search input with active option | The trigger or search input loses its role/name/state or popup kind, its open popup or active option relationship breaks, or read-only exposes a selection surface; the bottom-sheet trigger's `aria-controls` naming the inner listbox remains exact known debt | `audit:Selector/accessibility` |-| FR5 | loading, empty-state, and live-region tests | empty options, unmatched search, loading | Empty/no-results output appears or is announced while loading | `audit:Selector/behavior` |-| FR6, AR4 | read-only interaction, form, ARIA, and theme-state tests | search/non-search, clearable, open→read-only, disabled precedence | A value changes, popup or edit affordance remains, form value disappears, or read-only semantics are absent | `audit:Selector/accessibility` |-| FR7 | `Selector.test.tsx` press model suite | finger release over another option, finger/mouse release outside, listbox marker | The press option is selected, the listbox takes focus, a finger release outside closes the list, or the stray click selects | `audit:Selector/behavior` |-| source-build contract | `Selector.source-build.test.mjs` | package source compiled by consumer Babel | Moving evaluated StyleX values outside the supported source form fails compilation | `audit:Selector/code-health` |+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |+| ---------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |+| FR1, AR2, AR3 | `Selector.test.tsx` selection, focus, and keyboard suites, including "leaves focus on the field a press moved it to" and "returns focus to the trigger when the press lands on nothing focusable" | closed/open, search/non-search, disabled option | Removing selection wiring, focus movement, or keyboard behavior fails the named interaction tests | `audit:Selector/behavior` |+| FR2 | placement and selected-item geometry tests | default, explicit, RTL, transformed entry | Using the wrong positioning model fails the expected position-area or margin | `audit:Selector/behavior` |+| FR3, AV1 | Focused `Selector.test.tsx` computed-display checks plus guarded Chromium Storybook matrix and stable visual stories | popover/bottom sheet, narrow/wide, start/end, LTR/RTL, selected/unselected, check/radio | Empty output reserves width, visible output collapses, logical placement flips incorrectly, or row content overflows | `audit:Selector/design-rendered` |+| FR4, AR1, AR2 | presentation tests plus `aria-haspopup` source review | pointer, compact coarse pointer, search/non-search | Wrong Popover/dialog roles or focus destinations fail tests; `aria-haspopup` values require source/a11y review | `audit:Selector/accessibility` |+| AR1, AV2 | `Selector.listbox.a11y.test.tsx` and `Listbox.a11y.chromium.spec.ts` against the shared Listbox contract; `SelectorListboxName.a11y.chromium.spec.ts` and `Selector.test.tsx` "names the listbox with the field label, not the trigger that shows the value" for the listbox name | popover/sheet, flat/grouped, selected/unselected, disabled, search, loading, custom RTL | A new or wider loss of role, listbox/option name, state, or ownership gates | `audit:Selector/accessibility` |+| FR5, FR6, AR1, AR2 (anchored), AR4 | `Selector.combobox.a11y.test.tsx` and `Selector.combobox.a11y.chromium.spec.ts` against the shared Combobox contract | closed, open with active option, read-only, busy, bottom-sheet open, search input with active option | The trigger or search input loses its role/name/state or popup kind, its open popup or active option relationship breaks, or read-only exposes a selection surface; the bottom-sheet trigger's `aria-controls` naming the inner listbox remains exact known debt | `audit:Selector/accessibility` |+| FR5 | loading, empty-state, and live-region tests | empty options, unmatched search, loading | Empty/no-results output appears or is announced while loading | `audit:Selector/behavior` |+| FR6, AR4 | read-only interaction, form, ARIA, and theme-state tests | search/non-search, clearable, open→read-only, disabled precedence | A value changes, popup or edit affordance remains, form value disappears, or read-only semantics are absent | `audit:Selector/accessibility` |+| FR7 | `Selector.test.tsx` press model suite | finger release over another option, finger/mouse release outside, listbox marker | The press option is selected, the listbox takes focus, a finger release outside closes the list, or the stray click selects | `audit:Selector/behavior` |+| source-build contract | `Selector.source-build.test.mjs` | package source compiled by consumer Babel | Moving evaluated StyleX values outside the supported source form fails compilation | `audit:Selector/code-health` |## Decision logdiff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 7d767f73ce..2aea79d351 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -3242,30 +3242,39 @@ describe('Selector', () => {});describe('Selector caller-supplied id', () => {- it('names the listbox with the id the caller put on the trigger', () => {- render(<Selector label="Fruit" options={OPTIONS} id="fruit-picker" />);+ it('names the listbox with the field label, not the trigger that shows the value', () => {+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ id="fruit-picker"+ value={OPTIONS[1]}+ onChange={() => {}}+ />,+ );const trigger = screen.getByRole('combobox');expect(trigger).toHaveAttribute('id', 'fruit-picker');-- const labelledBy = screen- .getByRole('listbox', h)- .getAttribute('aria-labelledby');- expect(document.getElementById(labelledBy!)).toBe(trigger);+ const listbox = screen.getByRole('listbox', h);+ expect(listbox).toHaveAccessibleName('Fruit');+ expect(listbox).not.toHaveAttribute('aria-labelledby');});- it('names the search-variant listbox with the caller id', () => {+ it('names the search-variant listbox with the field label too', () => {render(- <Selector label="Fruit" options={OPTIONS} id="fruit-picker" hasSearch />,+ <Selector+ label="Fruit"+ options={OPTIONS}+ id="fruit-picker"+ hasSearch+ value={OPTIONS[1]}+ onChange={() => {}}+ />,);- const trigger = document.getElementById('fruit-picker');- expect(trigger?.tagName).toBe('BUTTON');-- const labelledBy = screen- .getByRole('listbox', h)- .getAttribute('aria-labelledby');- expect(document.getElementById(labelledBy!)).toBe(trigger);+ const listbox = screen.getByRole('listbox', h);+ expect(listbox).toHaveAccessibleName('Fruit');+ expect(listbox).not.toHaveAttribute('aria-labelledby');});it('points the field label at the caller id', () => {@@ -3273,7 +3282,7 @@ describe('Selector caller-supplied id', () => {const trigger = screen.getByRole('combobox', {name: 'Fruit'});expect(trigger).toHaveAttribute('id', 'fruit-picker');- expect(screen.getByLabelText('Fruit')).toBe(trigger);+ expect(screen.getByLabelText('Fruit', {selector: 'button'})).toBe(trigger);});it('generates the trigger id when the caller supplies none', () => {@@ -3281,12 +3290,7 @@ describe('Selector caller-supplied id', () => {const trigger = screen.getByRole('combobox', {name: 'Fruit'});expect(trigger.id).not.toBe('');- expect(screen.getByLabelText('Fruit')).toBe(trigger);-- const labelledBy = screen- .getByRole('listbox', h)- .getAttribute('aria-labelledby');- expect(document.getElementById(labelledBy!)).toBe(trigger);+ expect(screen.getByLabelText('Fruit', {selector: 'button'})).toBe(trigger);});});diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex a287ea6eee..8990645f9a 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -1907,7 +1907,7 @@ export function Selector<T extends SelectorOptionType>(ref={attachListbox}id={listboxId}role="listbox"- aria-labelledby={triggerId}+ aria-label={label}{...listboxPress.menuProps}{...stylex.props(styles.dropdown,@@ -1927,17 +1927,10 @@ export function Selector<T extends SelectorOptionType>(id={listboxId}role="listbox"{...listboxPress.menuProps}- // The bottom sheet is a modal layer, so Chromium drops the trigger- // outside it from the accessibility tree and a reference to it yields- // no name. Name only this no-search sheet directly from the component's- // label; the searchable sheet and the popovers keep the trigger- // relationship.- aria-label={- surface.activePresentation === 'bottom-sheet' ? label : undefined- }- aria-labelledby={- surface.activePresentation === 'bottom-sheet' ? undefined : triggerId- }+ // The trigger shows the value, so it cannot name the list.+ // A modal sheet removes an outside label from the accessibility tree.+ // The label element also holds the required marker and the tooltip.+ aria-label={label}aria-activedescendant={surface.isOpen && highlightedIndex >= 0? getItemId(highlightedIndex)diff --git a/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts b/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.tsnew file mode 100644index 0000000000..9ba6735ec9--- /dev/null+++ b/packages/core/src/Selector/__tests__/SelectorListboxName.a11y.chromium.spec.ts@@ -0,0 +1,91 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+// @pr-a11y scope ^(packages/core/src/Selector/|apps/storybook/stories/Selector\.stories)++/**+ * @file SelectorListboxName.a11y.chromium.spec.ts+ * @input Storybook stories core-selector--default and --searchable with a+ * value selected+ * @output Chromium's own accessibility tree names the open listbox by the+ * field's label, never by the trigger's value, in the popover and the+ * bottom sheet.+ * @position Real-browser check: a modal sheet and the trigger reference+ * behave differently in jsdom.+ */++import {expect, test, type Page} from '@playwright/test';+import {+ DEFAULT_STORYBOOK_DIR,+ serveStorybook,+ type StaticServer,+} from '@astryxdesign/a11y-spec/storybook';++let storybook: StaticServer;++test.beforeAll(async () => {+ storybook = await serveStorybook(+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,+ );+});++test.afterAll(async () => {+ await storybook?.close();+});++async function listboxNames(+ page: Page,+ presentation: string,+ story = 'default',+) {+ await page.goto(+ `${storybook.origin}/iframe.html?id=core-selector--${story}&viewMode=story&args=presentation:${presentation};value:Banana`,+ {waitUntil: 'load'},+ );+ // The searchable trigger is a plain button until the list opens.+ const trigger = page+ .locator('[role="combobox"], button[aria-haspopup]')+ .first();+ await expect(trigger).toContainText('Banana');+ await trigger.click();+ await expect(page.getByRole('listbox')).toBeVisible();+ const cdp = await page.context().newCDPSession(page);+ const {nodes} = (await cdp.send('Accessibility.getFullAXTree')) as {+ nodes: {role?: {value?: string}; name?: {value?: string}}[];+ };+ return nodes+ .filter(node => node.role?.value === 'listbox')+ .map(node => node.name?.value ?? '');+}++test('the popover listbox is named by the field label, not the selected value', async ({+ page,+}) => {+ expect(await listboxNames(page, 'popover')).toEqual(['Fruit']);+});++test('the searchable popover listbox is named by the field label', async ({+ page,+}) => {+ expect(await listboxNames(page, 'popover', 'searchable')).toEqual(['Fruit']);+});++test.describe('bottom sheet', () => {+ test.use({+ hasTouch: true,+ isMobile: true,+ viewport: {width: 390, height: 844},+ });++ test('the bottom-sheet listbox is named by the field label too', async ({+ page,+ }) => {+ expect(await listboxNames(page, 'bottom-sheet')).toEqual(['Fruit']);+ });++ test('the searchable bottom-sheet listbox is named by the field label', async ({+ page,+ }) => {+ expect(await listboxNames(page, 'bottom-sheet', 'searchable')).toEqual([+ 'Fruit',+ ]);+ });+});diff --git a/packages/core/src/Table/plugins/pagination/useTablePagination.test.tsx b/packages/core/src/Table/plugins/pagination/useTablePagination.test.tsxindex 22b68091c0..88b2662131 100644--- a/packages/core/src/Table/plugins/pagination/useTablePagination.test.tsx+++ b/packages/core/src/Table/plugins/pagination/useTablePagination.test.tsx@@ -344,12 +344,16 @@ describe('useTablePagination', () => {pageSizeOptions={[10, 25, 50]}/>,);- expect(screen.getByLabelText('Items per page')).toBeInTheDocument();+ expect(+ screen.getByRole('combobox', {name: 'Items per page'}),+ ).toBeInTheDocument();});it('paginationProps exclude pageSizeOptions when not provided', () => {render(<PaginatedTable data={generateItems(50)} pageSize={10} />);- expect(screen.queryByLabelText('Items per page')).not.toBeInTheDocument();+ expect(+ screen.queryByRole('combobox', {name: 'Items per page'}),+ ).not.toBeInTheDocument();});});