Skip to content

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

TypeScript

fix(Field): a focused field shows its focus under forced colors (#9392)

Cindy Zhang789197bba564
diff --git a/.changeset/field-focus-forced-colors.md b/.changeset/field-focus-forced-colors.md
new file mode 100644
index 0000000000..abd5cd105f
--- /dev/null
+++ b/.changeset/field-focus-forced-colors.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Enabled Field inputs (TextInput, TextArea, NumberInput, Selector, MultiSelector, Typeahead, Tokenizer, and the date and time inputs) now show keyboard focus under forced colors, such as Windows High Contrast. Forced colors drop the inset focus shadow and repaint every border in one system color, so a focused field looked the same as one at rest. A focused field's border now takes the system Highlight color there, with or without a status. Disabled fields, which stay focusable, don't get it yet. Nothing changes outside forced colors.
+
+@cixzhang
diff --git a/packages/core/src/Field/__tests__/FieldForcedColorsFocus.a11y.chromium.spec.ts b/packages/core/src/Field/__tests__/FieldForcedColorsFocus.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..9484b60884
--- /dev/null
+++ b/packages/core/src/Field/__tests__/FieldForcedColorsFocus.a11y.chromium.spec.ts
@@ -0,0 +1,89 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+// @pr-a11y scope ^(packages/core/src/Field/inputStyles\.stylex\.ts|packages/core/src/Field/__tests__/FieldForcedColorsFocus|apps/storybook/stories/(TextInput|Selector)\.stories)
+
+/**
+ * @file FieldForcedColorsFocus.a11y.chromium.spec.ts
+ * @input Checked-in TextInput and Selector stories
+ * @output Real-browser evidence that a field shows keyboard focus under forced
+ * colors (Windows High Contrast), where box shadows are dropped
+ * @position Browser lane for `spec:AST-020` FR1 (WCAG 2.4.7) on the shared
+ * input wrapper styles
+ */
+
+import {expect, test, type Locator} 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 () => storybook?.close());
+
+/** The painted edge of the bordered wrapper around a field's control. */
+async function wrapperEdge(control: Locator) {
+ return control.evaluate(el => {
+ let node: HTMLElement | null = el as HTMLElement;
+ while (node != null) {
+ const style = getComputedStyle(node);
+ if (
+ style.borderTopStyle === 'solid' &&
+ parseFloat(style.borderTopWidth) > 0
+ ) {
+ return {
+ border: style.borderTopColor,
+ shadow: style.boxShadow,
+ outline: style.outlineStyle === 'none' ? 'none' : style.outlineColor,
+ };
+ }
+ node = node.parentElement;
+ }
+ throw new Error('no bordered wrapper around the control');
+ });
+}
+
+const CASES = [
+ {story: 'core-textinput--default', label: undefined},
+ {story: 'core-textinput--status-variations', label: 'Error with message'},
+ {story: 'core-selector--with-status', label: 'Error status'},
+] as const;
+
+for (const {story, label} of CASES) {
+ test(`${story}${label ? `: "${label}"` : ''} shows keyboard focus under forced colors`, async ({
+ page,
+ }) => {
+ await page.emulateMedia({forcedColors: 'active', reducedMotion: 'reduce'});
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=${story}&viewMode=story`,
+ );
+ const control =
+ label == null
+ ? page.getByRole('textbox').first()
+ : page.getByLabel(label, {exact: true}).first();
+ await expect(control).toBeVisible();
+ const atRest = await wrapperEdge(control);
+
+ for (let i = 0; i < 12; i++) {
+ await page.keyboard.press('Tab');
+ if (await control.evaluate(el => el === document.activeElement)) {
+ break;
+ }
+ }
+ await expect(control).toBeFocused();
+ const focused = await wrapperEdge(control);
+
+ // Forced colors drop every box shadow, so the edge itself must change.
+ expect(focused.shadow).toBe('none');
+ expect(
+ focused.border !== atRest.border || focused.outline !== atRest.outline,
+ `focus changes the field's edge (rest ${JSON.stringify(atRest)}, focus ${JSON.stringify(focused)})`,
+ ).toBe(true);
+ });
+}
diff --git a/packages/core/src/Field/inputStyles.stylex.ts b/packages/core/src/Field/inputStyles.stylex.ts
index 5c225e5b66..f871074aae 100644
--- a/packages/core/src/Field/inputStyles.stylex.ts
+++ b/packages/core/src/Field/inputStyles.stylex.ts
@@ -41,9 +41,16 @@ export const inputWrapperStyles = stylex.create({
paddingInline: spacingVars['--spacing-2'],
borderWidth: borderVars['--border-width'],
borderStyle: 'solid',
+ // Forced colors drop the inset shadow below and repaint author border
+ // colors in one system color, so the field-focus treatment would vanish
+ // there. A focused field's border takes the system Highlight color instead
+ // (`spec:AST-020` FR1).
borderColor: {
default: colorVars['--color-border-emphasized'],
- ':focus-within': colorVars['--color-accent'],
+ ':focus-within': {
+ default: colorVars['--color-accent'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
},
'--_field-radius': radiusVars['--radius-element'],
borderRadius: 'var(--_field-radius)',
@@ -75,17 +82,37 @@ export const inputWrapperStyles = stylex.create({
/**
* Status border colors for input wrappers.
- * Keyed by InputStatusType.
+ * Keyed by InputStatusType. Under forced colors a focused field's border is
+ * Highlight, as in the base style, for wrappers that apply no status focus
+ * style of their own (Selector, MultiSelector).
*/
export const inputStatusBorderStyles = stylex.create({
warning: {
- borderColor: colorVars['--color-border-warning'],
+ borderColor: {
+ default: colorVars['--color-border-warning'],
+ ':focus-within': {
+ default: colorVars['--color-border-warning'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
+ },
},
error: {
- borderColor: colorVars['--color-border-error'],
+ borderColor: {
+ default: colorVars['--color-border-error'],
+ ':focus-within': {
+ default: colorVars['--color-border-error'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
+ },
},
success: {
- borderColor: colorVars['--color-border-success'],
+ borderColor: {
+ default: colorVars['--color-border-success'],
+ ':focus-within': {
+ default: colorVars['--color-border-success'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
+ },
},
});
@@ -124,7 +151,8 @@ export const inputStatusHoverShadowStyles = stylex.create({
});
/**
- * Status focus border styles using :focus-within.
+ * Status focus border styles using :focus-within. Under forced colors the
+ * focused border is Highlight, as in the base style.
* Used by input wrappers that contain a child input/textarea element.
* Keyed by InputStatusType.
*/
@@ -132,19 +160,28 @@ export const inputStatusFocusWithinStyles = stylex.create({
warning: {
borderColor: {
default: colorVars['--color-border-warning'],
- ':focus-within': colorVars['--color-border-warning'],
+ ':focus-within': {
+ default: colorVars['--color-border-warning'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
},
},
error: {
borderColor: {
default: colorVars['--color-border-error'],
- ':focus-within': colorVars['--color-border-error'],
+ ':focus-within': {
+ default: colorVars['--color-border-error'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
},
},
success: {
borderColor: {
default: colorVars['--color-border-success'],
- ':focus-within': colorVars['--color-border-success'],
+ ':focus-within': {
+ default: colorVars['--color-border-success'],
+ '@media (forced-colors: active)': 'Highlight',
+ },
},
},
});