♿ a11y: Name the Global Focus Outline on Tabs, Table Rows and Dropdown (#16870)

This commit is contained in:
Marco Beretta 2026-10-07 20:53:12 +02:00 • committed by GitHub
parent d397ecc9ff
commit 16d62f2910
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 247 additions and 0 deletions

View file

@ -0,0 +1,180 @@
import { expect, test } from '@playwright/test';
import type { Page } from '@playwright/test';
import { clickHouseTheme } from '../../../../packages/client/src/theme/themes/clickhouse';
import { NEW_CHAT_PATH } from '../helpers';
/**
* The focus outline utilities the primitives compose (`outline-theme-focus`,
* `outline-offset-theme-focus`, `outline-focus-outline`) paint what the global `:focus-visible`
* rule paints, in every palette and at the theme's width and offset, so a primitive that names the
* outline itself reads as it did when it inherited it. A probe carries the class string
* `focusOutlineRole` exports, and the settings theme selector is the real Dropdown trigger.
*/
type Mode = 'light' | 'dark';
const ROLE_CLASSES =
'focus-visible:outline-theme-focus focus-visible:outline-offset-theme-focus focus-visible:outline-focus-outline';
const SIZE_THEME = {
version: 1,
name: 'e2e-focus-role-size',
modes: {
light: { appearance: { focusRingWidth: '3px', focusRingOffset: '1px' } },
dark: { appearance: { focusRingWidth: '4px', focusRingOffset: '0' } },
},
} as const;
const NAMED_THEME = {
version: 1,
name: 'e2e-focus-role-named',
modes: {
light: { colors: { 'rgb-focus-outline': '180 0 110' } },
dark: { colors: { 'rgb-focus-outline': '255 140 200' } },
},
} as const;
async function openChat(page: Page, mode: Mode, definition?: { name: string }) {
await page.addInitScript(
([appearance, stored]) => {
localStorage.setItem('color-theme', appearance as string);
localStorage.removeItem('theme-colors');
localStorage.removeItem('theme-name');
if (stored) {
localStorage.setItem('theme-definition', JSON.stringify(stored));
localStorage.setItem('theme-source', 'definition');
} else {
localStorage.removeItem('theme-definition');
localStorage.removeItem('theme-source');
}
},
[mode, definition ?? null] as [string, unknown],
);
await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
timeout: 30000,
});
const root = page.locator('html');
if (definition) {
await expect(root).toHaveAttribute('data-theme', definition.name);
} else {
await expect(root).not.toHaveAttribute('data-theme');
}
await expect(root).toHaveClass(mode === 'dark' ? /\bdark\b/ : /\blight\b/);
}
/** Tabs onto a button carrying `classes` and reads its outline. */
async function outlineOf(page: Page, id: string, classes: string) {
await page.evaluate(
([probeId, className]) => {
const sentinel = document.createElement('span');
sentinel.tabIndex = -1;
const probe = document.createElement('button');
probe.id = probeId;
probe.className = className;
probe.textContent = 'Probe';
document.body.prepend(sentinel, probe);
sentinel.focus();
},
[id, classes],
);
await page.keyboard.press('Tab');
const probe = page.locator(`#${id}`);
await expect(probe).toBeFocused();
return probe.evaluate((node) => {
const style = getComputedStyle(node);
return [style.outlineStyle, style.outlineWidth, style.outlineColor, style.outlineOffset];
});
}
/** Each tag is written out whole: the runner finds a scenario by its literal tag. */
const CASES: Array<{ title: string; mode: Mode; definition?: { name: string }; width?: string }> = [
{
title:
'the focus outline utilities match the global outline in the default light theme @scenario:focus-role-utilities-default-light',
mode: 'light',
},
{
title:
'the focus outline utilities match the global outline in the default dark theme @scenario:focus-role-utilities-default-dark',
mode: 'dark',
},
{
title:
'the focus outline utilities match the global outline in ClickHouse light @scenario:focus-role-utilities-clickhouse-light',
mode: 'light',
definition: clickHouseTheme,
},
{
title:
'the focus outline utilities match the global outline in ClickHouse dark @scenario:focus-role-utilities-clickhouse-dark',
mode: 'dark',
definition: clickHouseTheme,
},
{
title:
'the focus outline utilities follow a theme that names the outline color @scenario:focus-role-utilities-named-color-dark',
mode: 'dark',
definition: NAMED_THEME,
},
{
title:
'the focus outline utilities follow the theme width and offset in light @scenario:focus-role-utilities-size-light',
mode: 'light',
definition: SIZE_THEME,
width: '3px',
},
{
title:
'the focus outline utilities follow the theme width and offset in dark @scenario:focus-role-utilities-size-dark',
mode: 'dark',
definition: SIZE_THEME,
width: '4px',
},
];
test.describe('focus outline utilities', () => {
for (const { title, mode, definition, width } of CASES) {
test(title, async ({ page }) => {
await openChat(page, mode, definition);
const global = await outlineOf(page, 'focus-role-bare', '');
const role = await outlineOf(page, 'focus-role-utilities', ROLE_CLASSES);
expect(global[0]).toBe('solid');
expect(role).toEqual(global);
/** The settings dialog lays out differently on mobile; the outline rule does not. */
if (test.info().project.name === 'mobile') {
return;
}
/** The real non-field Dropdown trigger, with the classes the primitive ships. */
await page.getByTestId('nav-user').click();
await page.getByRole('menuitem', { name: 'Settings' }).click();
const trigger = page.getByTestId('theme-selector');
await expect(trigger).toBeVisible({ timeout: 10000 });
await page.keyboard.press('Shift');
await trigger.focus();
await expect(trigger).toBeFocused();
expect(await trigger.evaluate((node) => node.matches(':focus-visible'))).toBe(true);
/** The trigger eases its properties in, so the outline is read once it has settled. */
await expect
.poll(() =>
trigger.evaluate((node) => {
const style = getComputedStyle(node);
return [
style.outlineStyle,
style.outlineWidth,
style.outlineColor,
style.outlineOffset,
];
}),
)
.toEqual(global);
if (width) {
expect(role[1]).toBe(width);
}
});
}
});

View file

@ -5,6 +5,7 @@ import * as Select from '@ariakit/react/select';
import * as Combobox from '@ariakit/react/combobox';
import type { Option } from '~/common';
import { cn, disabledFillClasses } from '~/utils';
import { focusOutlineRole } from './Focus';
import { fieldControl } from './Field';
import './Dropdown.css';
@ -171,6 +172,7 @@ const Dropdown: React.FC<DropdownProps> = ({
variant !== 'field' && [
shapeClasses[shape],
'hover:text-text-primary disabled:hover:text-text-primary',
focusOutlineRole,
],
'disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-transparent',
disabledFillClasses,

View file

@ -1,5 +1,13 @@
import { cva } from 'class-variance-authority';
/**
* The global keyboard outline, as utilities: the `focus-outline` color at the `focusRingWidth` and
* `focusRingOffset` roles. A primitive that must name the outline itself, rather than inherit the
* unlayered `.dark :focus-visible` rule, composes this, and it paints what that rule paints.
*/
export const focusOutlineRole: string =
'focus-visible:outline-theme-focus focus-visible:outline-offset-theme-focus focus-visible:outline-focus-outline';
export type FocusOutline = 'native' | 'hidden';
/**

View file

@ -1,5 +1,6 @@
import * as React from 'react';
import { cva } from 'class-variance-authority';
import { focusOutlineRole } from './Focus';
import { cn } from '~/utils';
interface TableProps extends React.HTMLAttributes<HTMLTableElement> {
@ -86,6 +87,7 @@ const TableRow: React.ForwardRefExoticComponent<
/** Rows are separated by their own padding and the hover fill, not by rules:
* a ruled table reads as a grid, and a list of records rarely needs one. */
'hover:bg-surface-hover data-[state=selected]:bg-surface-hover transition-colors',
focusOutlineRole,
className,
)}
{...props}

View file

@ -1,6 +1,7 @@
import * as React from 'react';
import * as TabsPrimitive from '@radix-ui/react-tabs';
import { cn, disabledInkClasses } from '~/utils';
import { focusOutlineRole } from './Focus';
const Tabs: React.ForwardRefExoticComponent<
TabsPrimitive.TabsProps & React.RefAttributes<HTMLDivElement>
@ -35,6 +36,7 @@ const TabsTrigger: React.ForwardRefExoticComponent<
className={cn(
'text-text-secondary data-[state=active]:bg-surface-primary data-[state=active]:text-text-primary rounded-theme-tab min-w-theme-tab inline-flex items-center justify-center px-3 py-1.5 text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50',
disabledInkClasses,
focusOutlineRole,
className,
)}
{...props}

View file

@ -2,10 +2,13 @@ import React from 'react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { Table, TableBody, TableCell, TableRow } from './Table';
import { Tabs, TabsList, TabsTrigger } from './Tabs';
import InputWithDropdown from './InputWithDropDown';
import { InputCombobox } from './InputCombobox';
import { SecretInput } from './SecretInput';
import MultiSearch from './MultiSearch';
import Dropdown from './Dropdown';
jest.mock('./MorphIcon', () => {
const { createMorphIconMock } = jest.requireActual('../test/mockMorphIcon');
@ -104,4 +107,45 @@ describe('shared control keyboard focus', () => {
await user.tab();
expect(ringHost).not.toHaveClass('ring-focus-control');
});
it('names the global outline on the tab triggers, table rows and dropdown trigger', () => {
const roleClasses = [
'focus-visible:outline-theme-focus',
'focus-visible:outline-offset-theme-focus',
'focus-visible:outline-focus-outline',
];
render(
<>
<Tabs defaultValue="a">
<TabsList>
<TabsTrigger value="a">One</TabsTrigger>
</TabsList>
</Tabs>
<Table>
<TableBody>
<TableRow data-testid="row">
<TableCell>cell</TableCell>
</TableRow>
</TableBody>
</Table>
<Dropdown
value="a"
onChange={jest.fn()}
options={['a', 'b']}
ariaLabel="Choice"
testId="choice"
/>
</>,
);
[
screen.getByRole('tab', { name: 'One' }),
screen.getByTestId('row'),
screen.getByTestId('choice'),
].forEach((node) => {
expect(node).toHaveClass(...roleClasses);
expect(node).not.toHaveClass('outline-hidden');
expect(node).not.toHaveClass('outline-none');
});
});
});

View file

@ -63,6 +63,8 @@ const twMerge = extendTailwindMerge({
'font-weight': [{ font: ['theme-control', 'theme-dialog-title-weight', 'theme-label'] }],
'font-family': [{ font: ['theme-dialog-title'] }],
'font-size': [{ text: [...TYPE_STEPS] }],
'outline-w': [{ outline: ['theme-focus'] }],
'outline-offset': [{ 'outline-offset': ['theme-focus'] }],
},
},
});

View file

@ -146,6 +146,13 @@ module.exports = {
'theme-surface':
'var(--theme-elevation-surface, 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1))',
},
/** The width and offset of the keyboard focus outline, the `focusRingWidth` and `focusRingOffset` roles. */
outlineWidth: {
'theme-focus': 'var(--theme-focus-ring-width, 2px)',
},
outlineOffset: {
'theme-focus': 'var(--theme-focus-ring-offset, 2px)',
},
transitionDuration: {
'theme-fast': 'var(--theme-motion-fast, 150ms)',
'theme-normal': 'var(--theme-motion-normal, 200ms)',