Skip to content

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

TypeScript

fix(MultiSelector): the active action cell draws the standard focus ring (#7542)

Christopher Chedeau3aca324d4a0a
diff --git a/.changeset/multiselector-action-cell-ring.md b/.changeset/multiselector-action-cell-ring.md
new file mode 100644
index 0000000000..3434faa496
--- /dev/null
+++ b/.changeset/multiselector-action-cell-ring.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `MultiSelector`: the row action cell the keyboard reaches with the inline-end arrow draws the standard focus ring. Before, its outline read four CSS variables that nothing declared, so no ring showed on the active action cell.
+
+@vjeux
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index d461bedee9..c2016dde39 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -35,6 +35,7 @@ import {generateThemeCSS} from '../theme/generateThemeRules';
import {selectorPresentationStyles} from '../Selector/selectorPresentation.stylex';
import {groupStyles} from '../InputGroup/groupStyles';
import {InputGroup, InputGroupText} from '../InputGroup';
+import {focusVars} from '../theme/tokens.stylex';
function generateThemeTestCSS(theme: Parameters<typeof generateThemeCSS>[0]) {
const {prose, component} = generateThemeCSS(theme);
@@ -4083,6 +4084,164 @@ describe('MultiSelector option actions (grid)', () => {
expect(onChange).not.toHaveBeenCalled();
});
+ it('draws the standard focus ring on the active action cell, from the focus tokens', async () => {
+ // jsdom resolves no custom property, so the ring is read from StyleX
+ // classes: a class name hashes one property and its value, and the same
+ // declaration yields the same class wherever it is written.
+ const ring = stylex.create({
+ active: {
+ outlineWidth: focusVars['--focus-outline-width'],
+ outlineStyle: focusVars['--focus-outline-style'],
+ outlineColor: focusVars['--focus-outline-color'],
+ outlineOffset: focusVars['--focus-outline-offset'],
+ },
+ });
+ const ringClasses = stylex
+ .props(ring.active)
+ .className!.split(' ')
+ .filter(token => /^x[a-z0-9]+$/.test(token));
+ expect(ringClasses).toHaveLength(4);
+
+ const user = userEvent.setup();
+ render(
+ <MultiSelector
+ label="Labels"
+ options={OPTIONS}
+ value={[]}
+ onChange={() => {}}
+ />,
+ );
+ const trigger = screen.getByRole('combobox', {name: 'Labels'});
+ await user.click(trigger);
+ const bug = screen.getByRole('row', {name: 'Bug', ...h});
+ const actionCell = bug.children[1] as HTMLElement;
+ for (const token of ringClasses) {
+ expect(actionCell).not.toHaveClass(token);
+ }
+ fireEvent.keyDown(trigger, {key: 'ArrowRight'});
+ expect(trigger).toHaveAttribute('aria-activedescendant', actionCell.id);
+ for (const token of ringClasses) {
+ expect(actionCell).toHaveClass(token);
+ }
+ });
+
+ it('draws no focus ring on an action cell highlighted after pointer input, until a key moves the highlight', async () => {
+ const ring = stylex.create({
+ active: {
+ outlineWidth: focusVars['--focus-outline-width'],
+ outlineStyle: focusVars['--focus-outline-style'],
+ outlineColor: focusVars['--focus-outline-color'],
+ outlineOffset: focusVars['--focus-outline-offset'],
+ },
+ });
+ const ringClasses = stylex
+ .props(ring.active)
+ .className!.split(' ')
+ .filter(token => /^x[a-z0-9]+$/.test(token));
+ expect(ringClasses).toHaveLength(4);
+
+ const user = userEvent.setup();
+ render(
+ <MultiSelector
+ label="Labels"
+ options={OPTIONS}
+ value={[]}
+ onChange={() => {}}
+ />,
+ );
+ const trigger = screen.getByRole('combobox', {name: 'Labels'});
+ await user.click(trigger);
+ const bug = screen.getByRole('row', {name: 'Bug', ...h});
+ const actionCell = bug.children[1] as HTMLElement;
+ fireEvent.mouseEnter(actionCell);
+ // The hover moved the highlight onto the action cell, without the ring.
+ expect(trigger).toHaveAttribute('aria-activedescendant', actionCell.id);
+ for (const token of ringClasses) {
+ expect(actionCell).not.toHaveClass(token);
+ }
+ // Keyboard navigation back onto the cell draws it.
+ fireEvent.keyDown(trigger, {key: 'ArrowLeft'});
+ fireEvent.keyDown(trigger, {key: 'ArrowRight'});
+ expect(trigger).toHaveAttribute('aria-activedescendant', actionCell.id);
+ for (const token of ringClasses) {
+ expect(actionCell).toHaveClass(token);
+ }
+ });
+
+ describe('ring under mixed input', () => {
+ const ringClasses = () => {
+ const ring = stylex.create({
+ active: {
+ outlineWidth: focusVars['--focus-outline-width'],
+ outlineStyle: focusVars['--focus-outline-style'],
+ outlineColor: focusVars['--focus-outline-color'],
+ outlineOffset: focusVars['--focus-outline-offset'],
+ },
+ });
+ return stylex
+ .props(ring.active)
+ .className!.split(' ')
+ .filter(token => /^x[a-z0-9]+$/.test(token));
+ };
+ const hasRing = (cell: HTMLElement) =>
+ ringClasses().every(token => cell.classList.contains(token));
+
+ async function openWithKeyboardOnBugAction() {
+ const user = userEvent.setup();
+ render(
+ <MultiSelector
+ label="Labels"
+ options={OPTIONS}
+ value={[]}
+ onChange={() => {}}
+ />,
+ );
+ const trigger = screen.getByRole('combobox', {name: 'Labels'});
+ await user.click(trigger);
+ const row = (name: string) => screen.getByRole('row', {name, ...h});
+ fireEvent.keyDown(trigger, {key: 'ArrowRight'});
+ const bugAction = row('Bug').children[1] as HTMLElement;
+ expect(trigger).toHaveAttribute('aria-activedescendant', bugAction.id);
+ expect(hasRing(bugAction)).toBe(true);
+ return {trigger, row, bugAction};
+ }
+
+ it('keeps the ring when the pointer crosses a disabled option the highlight ignores', async () => {
+ const {trigger, row, bugAction} = await openWithKeyboardOnBugAction();
+ fireEvent.mouseEnter(row('Docs').children[0]);
+ expect(trigger).toHaveAttribute('aria-activedescendant', bugAction.id);
+ expect(hasRing(bugAction)).toBe(true);
+ });
+
+ it('follows the keyboard modality when a hover moves the highlight to another action', async () => {
+ const {trigger, row} = await openWithKeyboardOnBugAction();
+ const docsAction = row('Docs').children[1] as HTMLElement;
+ fireEvent.mouseEnter(docsAction);
+ expect(trigger).toHaveAttribute('aria-activedescendant', docsAction.id);
+ expect(hasRing(docsAction)).toBe(true);
+ });
+
+ it('draws no ring for a modifier chord after pointer input', async () => {
+ const user = userEvent.setup();
+ render(
+ <MultiSelector
+ label="Labels"
+ options={OPTIONS}
+ value={[]}
+ onChange={() => {}}
+ />,
+ );
+ const trigger = screen.getByRole('combobox', {name: 'Labels'});
+ await user.click(trigger);
+ const bugAction = screen.getByRole('row', {name: 'Bug', ...h})
+ .children[1] as HTMLElement;
+ fireEvent.mouseEnter(bugAction);
+ expect(hasRing(bugAction)).toBe(false);
+ fireEvent.keyDown(trigger, {key: 'ArrowRight', ctrlKey: true});
+ expect(hasRing(bugAction)).toBe(false);
+ });
+ });
+
it('the inline-end arrow reaches the action cell, Enter fires it, inline-start returns', async () => {
const user = userEvent.setup();
const onChange = vi.fn();
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index 1a6159d764..9fbe560e0c 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -50,6 +50,7 @@ import type {IndicatorPosition} from '../Indicator';
import {Badge} from '../Badge';
import {
colorVars,
+ focusVars,
sizeVars,
spacingVars,
radiusVars,
@@ -74,7 +75,10 @@ import {
import {useMultiCombobox} from './hooks';
import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';
import {devWarn, warnOnce} from '../utils/devWarning';
-import {FOCUS_OUTLINE_PARTS} from '../utils/focusOutline.stylex';
+import {
+ getInteractionModality,
+ useInteractionModalityTracking,
+} from '../utils/interactionModality';
import {useAnnounce} from '../hooks/useAnnounce';
import {FOCUSABLE_SELECTOR} from '../hooks/focusableSelector';
import {useAnnounceRenderedText} from '../hooks/useAnnounceRenderedText';
@@ -436,11 +440,15 @@ const styles = stylex.create({
flexShrink: 0,
borderRadius: radiusVars['--radius-element'],
},
+ // The standard ring, from the focus tokens themselves: inside
+ // `stylex.create` StyleX resolves an imported `defineVars` group, and a
+ // plain object such as `FOCUS_OUTLINE_PARTS` compiles to variables nothing
+ // defines.
actionCellActive: {
- outlineWidth: FOCUS_OUTLINE_PARTS.outlineWidth,
- outlineStyle: FOCUS_OUTLINE_PARTS.outlineStyle,
- outlineColor: FOCUS_OUTLINE_PARTS.outlineColor,
- outlineOffset: FOCUS_OUTLINE_PARTS.outlineOffset,
+ outlineWidth: focusVars['--focus-outline-width'],
+ outlineStyle: focusVars['--focus-outline-style'],
+ outlineColor: focusVars['--focus-outline-color'],
+ outlineOffset: focusVars['--focus-outline-offset'],
},
// Empty state
@@ -1752,8 +1760,8 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
getActionCellId,
activeDescendantId,
onTriggerClick,
- onKeyDown,
- onItemMouseEnter,
+ onKeyDown: onComboboxKeyDown,
+ onItemMouseEnter: onComboboxItemMouseEnter,
} = useMultiCombobox({
selectableItems: sortedItems,
isDisabled: isDisabled || isEffectivelyReadOnly,
@@ -1784,6 +1792,29 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
),
});
+ // A hover moves the highlight too, but the focus ring follows the shared
+ // interaction modality (architecture:interaction-modality INV1): it is read
+ // whenever a key or the pointer moves the highlight, so an action cell
+ // highlighted after pointer input keeps the hover treatment and no ring.
+ useInteractionModalityTracking();
+ const [isKeyboardHighlight, setIsKeyboardHighlight] = useState(
+ () => getInteractionModality() === 'keyboard',
+ );
+ const onKeyDown = useCallback(
+ (event: React.KeyboardEvent<HTMLElement>) => {
+ onComboboxKeyDown(event);
+ setIsKeyboardHighlight(getInteractionModality() === 'keyboard');
+ },
+ [onComboboxKeyDown],
+ );
+ const onItemMouseEnter = useCallback(
+ (...args: Parameters<typeof onComboboxItemMouseEnter>) => {
+ onComboboxItemMouseEnter(...args);
+ setIsKeyboardHighlight(getInteractionModality() === 'keyboard');
+ },
+ [onComboboxItemMouseEnter],
+ );
+
// The trigger button owns its click so a consumer `onClick`, which lands on
// the button, composes after the built-in toggle. A press elsewhere in the
// control box (padding, start icon, chevron) still opens the panel.
@@ -2117,6 +2148,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
styles.actionCell,
isHighlighted &&
highlightedCell === 'action' &&
+ isKeyboardHighlight &&
styles.actionCellActive,
)}>
{isSelectAll || isCreate ? null : item.action}
@@ -2131,6 +2163,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
[
isGrid,
highlightedCell,
+ isKeyboardHighlight,
getActionCellId,
renderOption,
indicatorPosition,
diff --git a/packages/core/src/MultiSelector/OptionActions.a11y.chromium.spec.ts b/packages/core/src/MultiSelector/OptionActions.a11y.chromium.spec.ts
index 24cc610d75..f056c26232 100644
--- a/packages/core/src/MultiSelector/OptionActions.a11y.chromium.spec.ts
+++ b/packages/core/src/MultiSelector/OptionActions.a11y.chromium.spec.ts
@@ -6,8 +6,10 @@
* built Storybook.
* @output Real-engine proof for option actions: the action is visible at rest
* beside its row (`spec:AST-058` FR8), the keyboard reaches it through the
- * inline-end arrow in both directions and fires it with Enter, and the
- * highlight follows a real mouse across rows and actions.
+ * inline-end arrow in both directions and fires it with Enter, the active
+ * action cell draws the standard focus ring when the shared input modality
+ * is keyboard and not after pointer input, and the highlight follows a real
+ * mouse across rows and actions.
* @position Run with `pnpm test:a11y-contract`; paint, pointer paths and the
* active descendant as an engine exposes them are not jsdom claims.
*/
@@ -143,6 +145,114 @@ test('the keyboard reaches an action through the inline-end arrow and fires it w
await expect.poll(async () => activeDescendant(page)).toBe('row:Bug');
});
+test('the active action cell draws the standard focus ring', async ({page}) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-multiselector--row-actions&viewMode=story`,
+ );
+ const search = page.getByRole('combobox');
+ await expect(search).toBeFocused();
+ await search.press('ArrowDown'); // select-all
+ await search.press('ArrowDown'); // Feature (no action)
+ await search.press('ArrowDown'); // Bug
+ await search.press('ArrowRight');
+ await expect
+ .poll(async () => activeDescendant(page))
+ .toBe('gridcell:Edit Bug');
+ const ring = await page.evaluate(() => {
+ const id = document.activeElement?.getAttribute('aria-activedescendant');
+ const cell = id ? document.getElementById(id) : null;
+ if (!cell) {
+ return null;
+ }
+ const cs = getComputedStyle(cell);
+ // The ring the focus tokens describe, resolved by the browser on a probe.
+ const probe = document.createElement('div');
+ probe.style.cssText =
+ 'outline-width:var(--focus-outline-width);outline-style:var(--focus-outline-style);outline-color:var(--focus-outline-color);outline-offset:var(--focus-outline-offset)';
+ document.body.appendChild(probe);
+ const want = getComputedStyle(probe);
+ const result = {
+ style: cs.outlineStyle,
+ width: cs.outlineWidth,
+ color: cs.outlineColor,
+ offset: cs.outlineOffset,
+ wantStyle: want.outlineStyle,
+ wantWidth: want.outlineWidth,
+ wantColor: want.outlineColor,
+ wantOffset: want.outlineOffset,
+ };
+ probe.remove();
+ return result;
+ });
+ expect(ring).not.toBeNull();
+ expect(ring!.style).not.toBe('none');
+ expect(ring!.width).not.toBe('0px');
+ expect(ring!.style).toBe(ring!.wantStyle);
+ expect(ring!.width).toBe(ring!.wantWidth);
+ expect(ring!.color).toBe(ring!.wantColor);
+ expect(ring!.offset).toBe(ring!.wantOffset);
+ // Back on the option cell, the action cell drops the ring.
+ await search.press('ArrowLeft');
+ await expect.poll(async () => activeDescendant(page)).toBe('row:Bug');
+ const after = await page
+ .getByRole('row', {name: 'Bug'})
+ .locator('[data-multi-selector-action-cell]')
+ .evaluate(cell => getComputedStyle(cell).outlineStyle);
+ expect(after).toBe('none');
+});
+
+test('the action-cell ring follows the shared input modality', async ({
+ page,
+}) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-multiselector--row-actions&viewMode=story`,
+ );
+ const search = page.getByRole('combobox');
+ await expect(search).toBeFocused();
+ const actionCell = (name: string) =>
+ page.getByRole('row', {name}).locator('[data-multi-selector-action-cell]');
+ const outlineStyle = async (name: string) =>
+ actionCell(name).evaluate(cell => getComputedStyle(cell).outlineStyle);
+ const hover = async (name: string) => {
+ const cell = await box(actionCell(name));
+ await page.mouse.move(cell.x + cell.width / 2, cell.y + cell.height / 2, {
+ steps: 6,
+ });
+ };
+
+ // Pointer input: a click into the search field, then a hover onto an
+ // action, moves the highlight without the ring, and leaves none behind.
+ await search.click();
+ await hover('Bug');
+ await expect
+ .poll(async () => activeDescendant(page))
+ .toBe('gridcell:Edit Bug');
+ expect(await outlineStyle('Bug')).toBe('none');
+ // A modifier chord is not keyboard navigation for the shared tracker.
+ await search.press('Control+ArrowRight');
+ expect(await outlineStyle('Bug')).toBe('none');
+ await page.mouse.move(1, 1, {steps: 6});
+ expect(await outlineStyle('Bug')).toBe('none');
+
+ // Keyboard input: back onto the same cell, the ring is drawn.
+ await search.press('ArrowLeft');
+ await expect.poll(async () => activeDescendant(page)).toBe('row:Bug');
+ await search.press('ArrowRight');
+ await expect
+ .poll(async () => activeDescendant(page))
+ .toBe('gridcell:Edit Bug');
+ expect(await outlineStyle('Bug')).not.toBe('none');
+
+ // Still keyboard-driven, a hover that moves the highlight to another
+ // row's action carries the ring with it.
+ await hover('P0');
+ await expect
+ .poll(async () => activeDescendant(page))
+ .toBe('gridcell:Edit P0');
+ expect(await outlineStyle('P0')).not.toBe('none');
+ expect(await outlineStyle('Bug')).toBe('none');
+});
+
test('under RTL the inline-end arrow is ArrowLeft', async ({page}) => {
await page.goto(
`${storybook.origin}/iframe.html?id=core-multiselector--row-actions-rtl&viewMode=story`,
diff --git a/packages/core/src/utils/focusOutline.stylex.ts b/packages/core/src/utils/focusOutline.stylex.ts
index 3a0b664cd9..62fb233e36 100644
--- a/packages/core/src/utils/focusOutline.stylex.ts
+++ b/packages/core/src/utils/focusOutline.stylex.ts
@@ -70,6 +70,10 @@ const focusOutlineLonghands = {
* independently overridable — and every part stays a var, so a theme's
* `--focus-outline-*` overrides still flow through.
*
+ * NOT usable inside another file's `stylex.create`, for the same reason as
+ * {@link FOCUS_OUTLINE}: StyleX compiles each member there to a CSS variable
+ * nothing defines. Compose the `focusVars` tokens instead.
+ *
* Keys are camelCase to match `HTMLElement.style`, so this spreads straight
* onto an element:
*