Skip to content

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

TypeScript

test(Selector, ComplexSelector): a focused trigger keeps focus when disabled, in a real engine (#7874)

Cindy Zhang7c96b5735ecf
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 2f8aa9bb46..af08c1d910 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1411,6 +1411,14 @@ jobs:
--project chromium \
--output multiselector-disabled-focus-output
+ # The same guard for Selector and ComplexSelector triggers.
+ - name: Run disabled trigger focus contract
+ run: |
+ pnpm exec playwright test \
+ packages/core/src/__tests__/DisabledTriggerFocus.a11y.chromium.spec.ts \
+ --project chromium \
+ --output disabled-trigger-focus-output
+
# Table merges a consumer min-width with its column floors through CSS
# max(). jsdom keeps any string, so only a real engine shows whether the
# emitted declaration parses; an invalid one drops the floors entirely.
diff --git a/apps/storybook/stories/DisabledTriggerFocusA11y.stories.tsx b/apps/storybook/stories/DisabledTriggerFocusA11y.stories.tsx
new file mode 100644
index 0000000000..33cbd20ae9
--- /dev/null
+++ b/apps/storybook/stories/DisabledTriggerFocusA11y.stories.tsx
@@ -0,0 +1,55 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file DisabledTriggerFocusA11y.stories.tsx
+ * @input Uses Selector and ComplexSelector with an `isDisabled` story arg
+ * @output Browser fixtures for a trigger that becomes disabled while focused
+ * @position Stable fixtures for DisabledTriggerFocus.a11y.chromium.spec.ts
+ *
+ * A test drives `isDisabled` through the story args while the trigger holds
+ * focus, so the trigger is disabled in place rather than by a press elsewhere
+ * that would move focus first.
+ */
+
+import {useState} from 'react';
+import type {Meta, StoryObj} from '@storybook/react';
+import {ComplexSelector} from '../../../packages/core/src/ComplexSelector/ComplexSelector';
+import {Selector} from '../../../packages/core/src/Selector/Selector';
+
+type Args = {isDisabled: boolean};
+
+const meta: Meta<Args> = {
+ title: 'a11y/Disabled trigger focus',
+ tags: ['no-visual'],
+ args: {isDisabled: false},
+};
+export default meta;
+
+export const SelectorTrigger: StoryObj<Args> = {
+ name: 'Selector trigger',
+ render: ({isDisabled}) => {
+ const [value, setValue] = useState('Apple');
+ return (
+ <Selector
+ label="Fruit"
+ options={['Apple', 'Banana']}
+ value={value}
+ onChange={setValue}
+ isDisabled={isDisabled}
+ />
+ );
+ },
+};
+
+export const ComplexSelectorTrigger: StoryObj<Args> = {
+ name: 'ComplexSelector trigger',
+ render: ({isDisabled}) => (
+ <ComplexSelector<string>
+ label="View options"
+ value="Comfortable"
+ onChange={() => {}}
+ isDisabled={isDisabled}>
+ {() => <button type="button">Apply</button>}
+ </ComplexSelector>
+ ),
+};
diff --git a/packages/core/src/__tests__/DisabledTriggerFocus.a11y.chromium.spec.ts b/packages/core/src/__tests__/DisabledTriggerFocus.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..41efe13df2
--- /dev/null
+++ b/packages/core/src/__tests__/DisabledTriggerFocus.a11y.chromium.spec.ts
@@ -0,0 +1,86 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file DisabledTriggerFocus.a11y.chromium.spec.ts
+ * @input The `a11y/Disabled trigger focus` stories in a built Storybook.
+ * @output Real-engine proof of `spec:AST-085` FR4 for Selector and
+ * ComplexSelector: a trigger disabled while it holds keyboard focus keeps
+ * focus and reports itself disabled.
+ * @position Run in `pr-a11y`; a DOM emulator does not move focus off an
+ * element that becomes disabled, so it cannot show the loss.
+ */
+
+import {expect, test} 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();
+});
+
+const CASES = [
+ {
+ storyId: 'a11y-disabled-trigger-focus--selector-trigger',
+ role: 'combobox',
+ name: 'Fruit',
+ },
+ {
+ storyId: 'a11y-disabled-trigger-focus--complex-selector-trigger',
+ role: 'button',
+ name: 'View options',
+ },
+] as const;
+
+for (const {storyId, role, name} of CASES) {
+ test(`${name}: a focused trigger keeps focus when it becomes disabled`, async ({
+ page,
+ }) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=${storyId}&viewMode=story`,
+ );
+ const trigger = page.getByRole(role, {name});
+ await trigger.focus();
+ await expect(trigger).toBeFocused();
+
+ await page.evaluate(id => {
+ const channel = (
+ window as unknown as {
+ __STORYBOOK_ADDONS_CHANNEL__?: {
+ emit: (event: string, payload: unknown) => void;
+ };
+ }
+ ).__STORYBOOK_ADDONS_CHANNEL__;
+ if (channel == null) {
+ throw new Error('Storybook channel is unavailable');
+ }
+ channel.emit('updateStoryArgs', {
+ storyId: id,
+ updatedArgs: {isDisabled: true},
+ });
+ }, storyId);
+
+ // Wait for the disabled re-render in either form, so focus is checked
+ // after it lands.
+ await expect
+ .poll(async () =>
+ trigger.evaluate(
+ el =>
+ el.hasAttribute('disabled') ||
+ el.getAttribute('aria-disabled') === 'true',
+ ),
+ )
+ .toBe(true);
+ await expect(trigger).toBeFocused();
+ });
+}