LibreChat/e2e/specs/mock/scenarios/focus-primitives.spec.ts
Marco Beretta 6f74636375
♿ a11y: Let Dark Mode Honor Outline Utilities on Focus (#16861)
* a11y: Ring Tabs, Table, Dropdown and Menu Items in the Focus Control Role

* test: Tag the Focus Primitives Scenarios Literally

* test: Match the Dark Focus Rule That Yields to Outline Utilities

* refactor: Keep Menu Items and Table Rows on Their Existing Focus Treatment

* refactor: Leave Tabs and Dropdown on the Global Focus Outline

* test: Assert Both Outline Utility Exclusions in the Dark Focus Rule
2026-10-07 18:25:11 +02:00

149 lines
5.1 KiB
TypeScript

import { expect, test } from '@playwright/test';
import type { Page } from '@playwright/test';
import { NEW_CHAT_PATH } from '../helpers';
/**
* The primitives that draw their own keyboard ring (Tabs, Table rows, Dropdown and its menu items)
* name the `focus-control` role, which the bundled themes keep equal to the primary ink, and a
* control that declares an outline utility keeps it in dark mode instead of losing it to the global
* `.dark :focus-visible` rule.
*/
type Mode = 'light' | 'dark';
type Check = 'role' | 'hidden' | 'utility' | 'global';
const RING_CLASSES =
'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-focus-control';
const HIDDEN_CLASSES =
'focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-focus-control';
const UTILITY_CLASSES =
'focus-visible:outline-focus-subtle focus-visible:outline focus-visible:outline-2';
const PROBE = 'focus-primitives-probe';
async function openChat(page: Page, mode: Mode) {
await page.addInitScript((appearance) => {
localStorage.setItem('color-theme', appearance);
localStorage.removeItem('theme-colors');
localStorage.removeItem('theme-name');
localStorage.removeItem('theme-definition');
localStorage.removeItem('theme-source');
}, mode);
await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
timeout: 30000,
});
await expect(page.locator('html')).toHaveClass(mode === 'dark' ? /\bdark\b/ : /\blight\b/);
}
/** Tabs onto a bare button carrying `classes` and reads the outline and ring it draws. */
async function keyboardFocus(page: Page, classes: string) {
await page.evaluate(
([id, className]) => {
const sentinel = document.createElement('span');
sentinel.tabIndex = -1;
const probe = document.createElement('button');
probe.id = id;
probe.className = className;
probe.textContent = 'Probe';
document.body.prepend(sentinel, probe);
sentinel.focus();
},
[PROBE, classes],
);
await page.keyboard.press('Tab');
const probe = page.locator(`#${PROBE}`);
await expect(probe).toBeFocused();
return probe.evaluate((node) => {
const style = getComputedStyle(node);
const root = getComputedStyle(document.documentElement);
return {
outlineStyle: style.outlineStyle,
outlineColor: style.outlineColor,
ring: style.getPropertyValue('--tw-ring-color').trim(),
focusControl: root.getPropertyValue('--focus-control').trim(),
textPrimary: root.getPropertyValue('--text-primary').trim(),
};
});
}
/** Each tag is written out whole: the runner finds a scenario by its literal tag. */
const CASES: Array<{ title: string; mode: Mode; check: Check }> = [
{
title:
'the ring role equals the primary ink in the default light theme @scenario:focus-primitives-role-default-light',
mode: 'light',
check: 'role',
},
{
title:
'the ring role equals the primary ink in the default dark theme @scenario:focus-primitives-role-default-dark',
mode: 'dark',
check: 'role',
},
{
title:
'an outline-hidden control draws no global outline in light @scenario:focus-primitives-outline-hidden-light',
mode: 'light',
check: 'hidden',
},
{
title:
'an outline-hidden control draws no global outline in dark @scenario:focus-primitives-outline-hidden-dark',
mode: 'dark',
check: 'hidden',
},
{
title:
'a control with its own outline utility keeps it in light @scenario:focus-primitives-outline-utility-light',
mode: 'light',
check: 'utility',
},
{
title:
'a control with its own outline utility keeps it in dark @scenario:focus-primitives-outline-utility-dark',
mode: 'dark',
check: 'utility',
},
{
title:
'a bare control keeps the global outline role in light @scenario:focus-primitives-global-outline-light',
mode: 'light',
check: 'global',
},
{
title:
'a bare control keeps the global outline role in dark @scenario:focus-primitives-global-outline-dark',
mode: 'dark',
check: 'global',
},
];
test.describe('primitives focus ring', () => {
for (const { title, mode, check } of CASES) {
test(title, async ({ page }) => {
await openChat(page, mode);
if (check === 'role') {
const { focusControl, textPrimary } = await keyboardFocus(page, RING_CLASSES);
expect(focusControl).not.toBe('');
expect(focusControl).toBe(textPrimary);
return;
}
if (check === 'hidden') {
expect((await keyboardFocus(page, HIDDEN_CLASSES)).outlineStyle).toBe('none');
return;
}
if (check === 'utility') {
const { outlineStyle, outlineColor } = await keyboardFocus(page, UTILITY_CLASSES);
expect(outlineStyle).toBe('solid');
expect(outlineColor).toBe(mode === 'dark' ? 'rgb(89, 89, 89)' : 'rgb(153, 150, 150)');
return;
}
const { outlineStyle, outlineColor } = await keyboardFocus(page, '');
expect(outlineStyle).toBe('solid');
expect(outlineColor).toBe(mode === 'dark' ? 'rgb(255, 255, 255)' : 'rgb(0, 0, 0)');
});
}
});