An open source design system that's fully customizable and agent ready
TypeScript
fix(core): expand InputClearButton hit area to 24x24 on touch (WCAG 2.5.8 AA) (#4956)
rubyycheung82a416ae7e83
diff --git a/.changeset/input-clear-button-touch-hit-area.md b/.changeset/input-clear-button-touch-hit-area.mdnew file mode 100644index 0000000000..b2c52d1b57--- /dev/null+++ b/.changeset/input-clear-button-touch-hit-area.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] InputClearButton: the clear (✕) affordance now meets the WCAG 2.5.8 AA 24×24 minimum on touch. The shared button rendered a 20px glyph with a 20px tap target, so every input that clears through it — Typeahead, Tokenizer, FileInput and the rest of the family — was under the floor on a phone. An `::after` overlay now expands the tappable region to 24×24, gated behind `@media (pointer: coarse)`: on a fine pointer the overlay is not generated at all, because a mouse is precise enough, an unconditional overlay could overlap neighboring controls in dense desktop layouts, and an overlay covering the button would take hover away from the `astryx-input-clear-icon` theme target. The visual glyph is unchanged at every breakpoint, and the overlay stops at 24px so it stays clear of the 8px adornment gap and the input's own caret area.++@rubyycheungdiff --git a/packages/core/src/Field/Field.doc.mjs b/packages/core/src/Field/Field.doc.mjsindex b46f488fba..9bce008401 100644--- a/packages/core/src/Field/Field.doc.mjs+++ b/packages/core/src/Field/Field.doc.mjs@@ -39,6 +39,8 @@ export const docs = {],vars: [{name: '--_field-radius', description: 'Border radius of input fields', default: 'var(--radius-element)', private: true},+ {name: '--_input-clear-hit-inset', description: 'Outset of the clear (\u2715) button\u2019s invisible hit area, applied to a ::after overlay. 0 on a fine pointer; negative on a coarse one, which grows the 20px button to the 24px touch target without changing what is drawn.', default: '0px', private: true},+ {name: '--_input-clear-hit-content', description: 'Whether the clear (\u2715) button\u2019s invisible hit overlay exists. `none` on a fine pointer, so no ::after is generated and hover still reaches the glyph; `""` on a coarse one, where the overlay provides the 24px touch target.', default: 'none', private: true},],derived: [{property: 'borderRadius', vars: ['--_field-radius']},diff --git a/packages/core/src/Field/InputClearButton.test.tsx b/packages/core/src/Field/InputClearButton.test.tsxindex a9d0705dd7..2f71a8d899 100644--- a/packages/core/src/Field/InputClearButton.test.tsx+++ b/packages/core/src/Field/InputClearButton.test.tsx@@ -2,7 +2,8 @@/*** @file InputClearButton.test.tsx- * @input Uses vitest, @testing-library/react, InputClearButton, Icon, theme+ * @input Uses node:fs, node:path, vitest, @testing-library/react, InputClearButton, Icon,+ * theme* @output Unit tests for the shared clear-button primitive* @position Testing; validates InputClearButton.tsx — the single home for the* clearable input family's clear (✕) affordance and its theme target.@@ -10,6 +11,8 @@* SYNC: When InputClearButton.tsx changes, update tests to match new behavior.*/+import {readFileSync} from 'node:fs';+import path from 'node:path';import {describe, it, expect, vi} from 'vitest';import {render, screen, fireEvent} from '@testing-library/react';import {InputClearButton} from './InputClearButton';@@ -22,6 +25,39 @@ function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {return [prose, component].filter(Boolean).join('\n\n');}+interface InjectedRule {+ selector: string;+ text: string;+ media: string | null;+}++/**+ * Every style rule StyleX has injected at runtime (`runtimeInjection` in the+ * root vitest config), flattened out of any at-rule wrapper and tagged with+ * that wrapper's condition — so a `@media (pointer: coarse)` rule stays+ * distinguishable from the unconditional one.+ */+function injectedRules(): InjectedRule[] {+ const walk = (rules: CSSRuleList, condition: string | null) =>+ [...rules].flatMap((rule): InjectedRule[] => {+ const {selectorText} = rule as CSSStyleRule;+ if (typeof selectorText === 'string') {+ return [{selector: selectorText, text: rule.cssText, media: condition}];+ }+ // A grouping rule (@media, @keyframes, ...): descend, carrying the+ // innermost condition down. jsdom hangs an empty `cssRules` off plain+ // style rules too, which is why the leaf check comes first.+ const nested = (rule as CSSGroupingRule).cssRules;+ if (nested == null) {+ return [];+ }+ const own = (rule as CSSMediaRule).media?.mediaText;+ return walk(nested, own != null && own !== '' ? own : condition);+ });++ return [...document.styleSheets].flatMap(sheet => walk(sheet.cssRules, null));+}+const getGlyph = (): HTMLElement => {const button = screen.getByRole('button', {name: 'Clear'});const icon = button.querySelector('.astryx-icon');@@ -121,6 +157,77 @@ describe('InputClearButton', () => {expect(button).toHaveClass('astryx-input-clear-button');});+ it('grows the hit area to 24px on a coarse pointer only (WCAG 2.5.8 AA)', () => {+ // The visual glyph stays 20px; an ::after overlay expands only the+ // tappable region, and only under a coarse pointer. Asserted against the+ // CSS StyleX injects at runtime, because jsdom resolves neither media+ // queries nor pseudo-element boxes.+ render(<InputClearButton label="Clear" onClick={() => {}} />);+ const button = screen.getByRole('button', {name: 'Clear'});+ const classes = new Set(button.className.split(' ').filter(Boolean));++ const css = injectedRules();+ const rulesForButton = css.filter(({selector}) =>+ [...classes].some(c => selector.includes(`.${c}`)),+ );+ // Guards every assertion below against silently passing if StyleX's+ // runtime injection or the class plumbing ever changes shape.+ expect(rulesForButton.length).toBeGreaterThan(0);++ const decl = (pattern: RegExp, inMedia?: string) =>+ rulesForButton.some(+ ({text, media}) =>+ pattern.test(text) &&+ (inMedia == null ? media == null : (media ?? '').includes(inMedia)),+ );++ // Fine pointer: hit area == the 20px visual glyph, no expansion.+ expect(decl(/--_input-clear-hit-inset\s*:\s*0px/)).toBe(true);+ // Coarse pointer: 20px + 2px on each side = 24x24, the AA floor.+ expect(decl(/--_input-clear-hit-inset\s*:\s*-2px/, 'pointer: coarse')).toBe(+ true,+ );+ // ...and nothing wider than that, which would reach into the adornment+ // gap and the input's caret area. Scoped to the coarse block, because+ // that is the only place a wider value can appear.+ expect(+ decl(+ /--_input-clear-hit-inset\s*:\s*-(?:[3-9]|\d{2,})px/,+ 'pointer: coarse',+ ),+ ).toBe(false);+ // The overlay exists only on touch. On a fine pointer a generated+ // ::after would cover the button and take hover away from the glyph+ // target, so `content` is gated the same way the inset is.+ expect(decl(/--_input-clear-hit-content\s*:\s*none/)).toBe(true);+ expect(decl(/--_input-clear-hit-content\s*:\s*""/, 'pointer: coarse')).toBe(+ true,+ );+ // The overlay itself is what carries the expansion, and what is gated.+ expect(+ decl(/::after\s*\{[^}]*inset\s*:\s*var\(--_input-clear-hit-inset\)/),+ ).toBe(true);+ expect(+ decl(/::after\s*\{[^}]*content\s*:\s*var\(--_input-clear-hit-content\)/),+ ).toBe(true);+ });++ it('declares its own containing block for the hit overlay', () => {+ // The ::after overlay must resolve against this button. Button happens to+ // set `position: relative` on itself, so at runtime the overlay is+ // correctly placed either way — and StyleX compiles both declarations to+ // the same atomic class, so the rendered CSS cannot tell the two apart.+ // Assert on the source instead, so a future edit can't quietly leave the+ // overlay depending on another component's internal.+ const source = readFileSync(+ path.resolve(__dirname, './InputClearButton.tsx'),+ 'utf-8',+ );+ const buttonStyle = source.match(/button:\s*\{[\s\S]*?\n {2}\},/)?.[0];+ expect(buttonStyle).toBeDefined();+ expect(buttonStyle).toMatch(/position:\s*'relative'/);+ });+it('exposes input-clear-button so a theme controls the button size and hover', () => {const theme = defineTheme({name: 'input-clear-button-test',diff --git a/packages/core/src/Field/InputClearButton.tsx b/packages/core/src/Field/InputClearButton.tsxindex 7b005d5760..448d252a05 100644--- a/packages/core/src/Field/InputClearButton.tsx+++ b/packages/core/src/Field/InputClearButton.tsx@@ -25,6 +25,44 @@ const styles = stylex.create({button: {height: '20px',flexShrink: 0,+ // Containing block for the ::after hit overlay below. Button sets its own+ // `position: relative`, so the overlay would resolve correctly without+ // this — but that is another component's internal, and if it ever changes+ // the overlay silently reattaches to some ancestor and the hit area lands+ // in the wrong place. Declaring it here keeps the containing block+ // colocated with the thing that depends on it. No-op at runtime.+ position: 'relative',+ // Expand the tap target to 24px ONLY on touch (WCAG 2.5.8 AA is a touch+ // requirement, and its floor is 24x24). On a fine pointer the 20px glyph+ // is precise enough, and an unconditional overlay could overlap+ // neighboring controls in dense layouts. The inset is 0 by default (hit+ // area == visual glyph) and grows to -2px (=> 24x24) under a coarse+ // pointer — no further, because at -4px the overlay reaches into the 8px+ // adornment gap and, on the inline-start side, over the input's own caret+ // area. Driven through a custom property because StyleX only allows plain+ // values inside a pseudo-element; the conditional lives on this top-level+ // property instead. Private (--_) because it is an internal implementation+ // detail, not a themeable target.+ '--_input-clear-hit-inset': {+ default: '0px',+ '@media (pointer: coarse)': '-2px',+ },+ // The overlay itself is gated too, not just its size. An ::after that is+ // generated on a fine pointer sits exactly over the button at inset 0,+ // where it adds no hit area but is the topmost hit-test box — so hover+ // stops reaching the descendants and `.astryx-input-clear-icon:hover`, a+ // public theme target, no longer matches. `content: none` means the+ // pseudo-element is not generated at all, so on a fine pointer the+ // overlay does not exist.+ '--_input-clear-hit-content': {+ default: 'none',+ '@media (pointer: coarse)': '""',+ },+ '::after': {+ content: 'var(--_input-clear-hit-content)',+ position: 'absolute',+ inset: 'var(--_input-clear-hit-inset)',+ },},});diff --git a/packages/core/src/theme/derivedVarRegistry.test.ts b/packages/core/src/theme/derivedVarRegistry.test.tsindex 845a117da1..892bf64ee7 100644--- a/packages/core/src/theme/derivedVarRegistry.test.ts+++ b/packages/core/src/theme/derivedVarRegistry.test.ts@@ -273,9 +273,12 @@ const VARS_WITHOUT_DERIVED_MAPPING = new Set(['--_avatar-group-overlap','--_codeblock-gutter-width','--_tab-indicator-bottom',- // Hit-area outset on a ::after overlay — `inset` on a pseudo-element is not- // a property a theme author sets on the component.+ // Hit-area outset on a ::after overlay, and whether that overlay is+ // generated at all — `inset` and `content` on a pseudo-element are not+ // properties a theme author sets on the component.'--_thumbnail-hit-inset',+ '--_input-clear-hit-inset',+ '--_input-clear-hit-content',// Indentation and row-spacing metrics: --tree-list-indent is the authorable// step, --_tree-indent the per-row distance TreeListItem computes from it.// --tree-list-row-gap is applied as half a padding-block on each row wrapper,