Skip to content

Commit ea9fbf5

Browse files
committed
test(core): one internal definition pins core's touch style keys
The touch keys (the coarse-pointer, touch-callout and compact-size media keys core's styles read) get one internal definition, and a scan pins every quoted at-rule key in core to its reference spelling: a key that names the pointer, the callout or a compact size, or that is a few characters off a reference, must be that spelling exactly. A compiled-CSS check keeps the press paint above the coarse-pointer :active reset, and a browser proof shows a Tab's pressed overlay differing from its hover under a fine pointer and, under a coarse pointer, a finger's press painting as an image while the colour layer stays transparent in :active. #8331's change, rebased onto main as one commit. Core declares the interaction harness as a dev dependency, which its browser proof imports (the harness is on main; this is its first core user).
1 parent d73908f commit ea9fbf5

8 files changed

Lines changed: 442 additions & 4 deletions

File tree

‎.changeset/touch-style-keys.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[chore] Internal: core's touch style keys stay literal, and a test pins every one to the touch presentation resolver's constants (`touchKeys`, internal to core). An imported `defineConsts` key compiles to a different StyleX priority than the same literal. No public export and no style changes.
6+
7+
@vjeux

‎packages/core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@
864864
"devDependencies": {
865865
"@astryxdesign/a11y-spec": "workspace:*",
866866
"@astryxdesign/cli": "workspace:*",
867+
"@astryxdesign/interaction-harness": "workspace:*",
867868
"@babel/cli": "catalog:",
868869
"@babel/core": "catalog:",
869870
"@babel/preset-react": "catalog:",
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file TabPressPointer.interaction.browser.spec.ts
5+
* @input Uses @playwright/test and the interaction harness (a built
6+
* Storybook)
7+
* @output Proves a Tab's hover and press layer under a fine and a coarse
8+
* primary pointer
9+
* @position Interaction proof for packages/core/src/TabList/Tab.tsx
10+
*
11+
* Tab's press layer has a hover branch and a coarse-pointer rule, which
12+
* StyleX makes exclusive. The outcome on every pointer: a fine pointer
13+
* paints the hover, then a pressed overlay that differs from it; under a
14+
* coarse pointer a finger's press paints as an image (`data-astryx-press`),
15+
* and the colour layer stays transparent while the tab is `:active`.
16+
* Chromium only: it holds a finger, and forces `:active`, through the
17+
* DevTools protocol.
18+
*/
19+
20+
import {expect, test, type CDPSession, type Page} from '@playwright/test';
21+
import {openStory, withStorybook} from '@astryxdesign/interaction-harness';
22+
23+
const storybook = withStorybook(test);
24+
25+
const TRANSPARENT = 'rgba(0, 0, 0, 0)';
26+
27+
/** The "Projects" tab: not the story's current one. */
28+
function projectsTab(page: Page) {
29+
return page.locator('button', {hasText: 'Projects'}).first();
30+
}
31+
32+
/** The tab's colour layer (its first aria-hidden span) and the pointer. */
33+
async function tabState(page: Page): Promise<{
34+
colour: string;
35+
image: string;
36+
press: string | null;
37+
coarse: boolean;
38+
hover: boolean;
39+
}> {
40+
return projectsTab(page).evaluate(element => {
41+
const layer = element.querySelector('span[aria-hidden="true"]');
42+
const style = layer == null ? null : getComputedStyle(layer);
43+
return {
44+
colour: style?.backgroundColor ?? '',
45+
image: style?.backgroundImage ?? '',
46+
press: element.getAttribute('data-astryx-press'),
47+
coarse: matchMedia('(pointer: coarse)').matches,
48+
hover: matchMedia('(hover: hover)').matches,
49+
};
50+
});
51+
}
52+
53+
/** The layer's colour once its transition has landed (three equal reads). */
54+
async function settledColour(page: Page): Promise<string> {
55+
let last = '';
56+
let same = 0;
57+
for (let read = 0; read < 60 && same < 3; read++) {
58+
const colour = (await tabState(page)).colour;
59+
same = colour === last ? same + 1 : 1;
60+
last = colour;
61+
await page.waitForTimeout(50);
62+
}
63+
return last;
64+
}
65+
66+
/** The tab's centre, in page coordinates. */
67+
async function tabCentre(page: Page): Promise<{x: number; y: number}> {
68+
const box = await projectsTab(page).boundingBox();
69+
if (box == null) {
70+
throw new Error('the Projects tab has no box');
71+
}
72+
return {x: box.x + box.width / 2, y: box.y + box.height / 2};
73+
}
74+
75+
/** A finger held on the tab's centre; returns the lift. */
76+
async function holdFinger(
77+
page: Page,
78+
cdp: CDPSession,
79+
): Promise<() => Promise<void>> {
80+
await cdp.send('Input.dispatchTouchEvent', {
81+
type: 'touchStart',
82+
touchPoints: [await tabCentre(page)],
83+
});
84+
return async () => {
85+
await cdp.send('Input.dispatchTouchEvent', {
86+
type: 'touchEnd',
87+
touchPoints: [],
88+
});
89+
};
90+
}
91+
92+
/** Puts the tab in `:active` (or out of it) through the DevTools protocol. */
93+
async function forceActive(cdp: CDPSession, isActive: boolean): Promise<void> {
94+
await cdp.send('DOM.enable');
95+
await cdp.send('CSS.enable');
96+
const {root} = await cdp.send('DOM.getDocument', {depth: -1});
97+
const {nodeIds} = await cdp.send('DOM.querySelectorAll', {
98+
nodeId: root.nodeId,
99+
selector: 'button',
100+
});
101+
for (const nodeId of nodeIds) {
102+
const {outerHTML} = await cdp.send('DOM.getOuterHTML', {nodeId});
103+
if (outerHTML.includes('Projects')) {
104+
await cdp.send('CSS.forcePseudoState', {
105+
nodeId,
106+
forcedPseudoClasses: isActive ? ['active'] : [],
107+
});
108+
return;
109+
}
110+
}
111+
throw new Error('the Projects tab has no node');
112+
}
113+
114+
test.describe('Tab press layer by pointer', () => {
115+
test.skip(
116+
({browserName}) => browserName !== 'chromium',
117+
'Chromium holds a finger and forces :active through the DevTools protocol',
118+
);
119+
120+
test.describe('fine pointer with hover', () => {
121+
test.use({isMobile: false, hasTouch: false});
122+
test('paints the hover, then a pressed overlay that differs from it', async ({
123+
page,
124+
}) => {
125+
await openStory(page, storybook(), 'core-tablist--default');
126+
await projectsTab(page).hover();
127+
const state = await tabState(page);
128+
expect(state.coarse).toBe(false);
129+
expect(state.hover).toBe(true);
130+
await expect
131+
.poll(async () => (await tabState(page)).colour)
132+
.not.toBe(TRANSPARENT);
133+
const hovered = await settledColour(page);
134+
await page.mouse.down();
135+
// The pressed overlay is its own colour, not the hover paint held on.
136+
await expect
137+
.poll(async () => (await tabState(page)).colour)
138+
.not.toBe(hovered);
139+
expect(await settledColour(page)).not.toBe(hovered);
140+
await page.mouse.up();
141+
});
142+
});
143+
144+
test("coarse pointer: a finger's press paints as an image, and the colour layer stays transparent while the tab is :active", async ({
145+
page,
146+
}) => {
147+
await openStory(page, storybook(), 'core-tablist--default');
148+
const cdp = await page.context().newCDPSession(page);
149+
expect((await tabState(page)).coarse).toBe(true);
150+
// The finger's press: the press model marks the tab, and the layer
151+
// paints the pressed overlay as an image.
152+
const lift = await holdFinger(page, cdp);
153+
await expect.poll(async () => (await tabState(page)).press).toBe('on');
154+
expect((await tabState(page)).image).not.toBe('none');
155+
await lift();
156+
await expect.poll(async () => (await tabState(page)).press).not.toBe('on');
157+
// The tab in :active under the coarse pointer: the reset keeps the
158+
// colour layer transparent, past any transition.
159+
await forceActive(cdp, true);
160+
expect(await settledColour(page)).toBe(TRANSPARENT);
161+
await forceActive(cdp, false);
162+
});
163+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file pressPaintPriority.test.ts
5+
* @input Uses vitest, Babel with the StyleX plugin (the repository's shared
6+
* module resolution) and interactionOverlay.stylex.ts
7+
* @output Checks, in the compiled CSS, that the press paint outranks the
8+
* coarse-pointer `:active` reset of the overlay
9+
* @position Regression test for the cascade order of the press overlay
10+
*
11+
* WebKit applies `:active` during a touch. Under a coarse pointer the overlay
12+
* turns the bare `:active` image off, and the press paint
13+
* (`[data-astryx-press="on"]`) must still win: its rule has to sit in a
14+
* higher StyleX priority than the reset. The order is a property of how
15+
* each key is spelled: an imported `defineConsts` key compiles to a higher
16+
* priority than the same literal, which put the reset above the paint and
17+
* erased the touch-down frame.
18+
*/
19+
20+
import fs from 'node:fs';
21+
import {createRequire} from 'node:module';
22+
import path from 'node:path';
23+
import {describe, expect, it} from 'vitest';
24+
import stylexResolution from '../../../../scripts/stylex-module-resolution.cjs';
25+
26+
type Rule = [string, {ltr: string}, number];
27+
28+
// Babel's types are not part of core's typecheck; type the one call used.
29+
const {transformSync} = createRequire(import.meta.url)('@babel/core') as {
30+
transformSync: (
31+
code: string,
32+
options: Record<string, unknown>,
33+
) => {metadata?: unknown} | null;
34+
};
35+
36+
function compiledRules(file: string): Rule[] {
37+
const output = transformSync(fs.readFileSync(file, 'utf8'), {
38+
filename: file,
39+
babelrc: false,
40+
configFile: false,
41+
parserOpts: {plugins: ['typescript', 'jsx']},
42+
plugins: [
43+
[
44+
'@stylexjs/babel-plugin',
45+
{
46+
dev: false,
47+
runtimeInjection: false,
48+
genConditionalClasses: true,
49+
treeshakeCompensation: true,
50+
unstable_moduleResolution: stylexResolution.stylexModuleResolution,
51+
},
52+
],
53+
],
54+
});
55+
return (output?.metadata as {stylex?: Rule[]}).stylex ?? [];
56+
}
57+
58+
describe('press overlay cascade', () => {
59+
it('ranks the press paint above the coarse-pointer :active reset', () => {
60+
const rules = compiledRules(
61+
path.resolve(__dirname, 'interactionOverlay.stylex.ts'),
62+
);
63+
const resets = rules.filter(
64+
// Matched without its condition's text: an imported key compiles to a
65+
// placeholder until the bundle step.
66+
([, {ltr}]) =>
67+
ltr.includes(':active') && ltr.includes('background-image:none'),
68+
);
69+
const paints = rules.filter(
70+
([, {ltr}]) =>
71+
ltr.includes('[data-astryx-press="on"]') &&
72+
ltr.includes('background-image:'),
73+
);
74+
expect(resets.length).toBeGreaterThan(0);
75+
expect(paints.length).toBeGreaterThan(0);
76+
const highestReset = Math.max(...resets.map(([, , priority]) => priority));
77+
const lowestPaint = Math.min(...paints.map(([, , priority]) => priority));
78+
expect(highestReset).toBeLessThan(lowestPaint);
79+
});
80+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file touchPresentation.stylex.ts
5+
* @input Uses StyleX defineConsts
6+
* @output Exports touchKeys: the touch presentation resolver's queries as
7+
* StyleX at-rule keys
8+
* @position The reference spellings of the touch, compact and iOS-gate
9+
* conditions. Core styles write these as literal keys wherever they need
10+
* them (`spec:AST-094` FR13, `spec:AST-012` FR9), and a test pins each
11+
* literal to these values; utils/touchPresentation.ts holds the same
12+
* queries for script (matchMedia)
13+
*
14+
* These are the spellings to match. A style that changes with these
15+
* conditions writes the same literal key (`'@media (pointer: coarse)'`, the
16+
* iOS gate as that media key outside `'@supports (-webkit-touch-callout:
17+
* none)'`), and a test pins every literal in core to these values. Do not use
18+
* an imported key as a computed StyleX key: it compiles to a different
19+
* priority than the literal (6000 against 3200) and reorders the cascade.
20+
* The iOS supports gate is only for a style iOS WebKit itself requires (the
21+
* 16px input floor), never a behaviour.
22+
*
23+
* SYNC: When modified, update touchPresentation.ts and
24+
* touchPresentation.test.ts.
25+
*/
26+
27+
import * as stylex from '@stylexjs/stylex';
28+
29+
export const touchKeys = stylex.defineConsts({
30+
/** The primary pointer is coarse (TOUCH_QUERY). */
31+
touch: '@media (pointer: coarse)',
32+
/** The viewport is compact (COMPACT_QUERY, `spec:AST-012` FR9). */
33+
compact: '@media (width < 768px), (height < 500px)',
34+
/** Both: a presentation such as a sheet in place of a popover. */
35+
compactTouch:
36+
'@media (width < 768px) and (pointer: coarse), (height < 500px) and (pointer: coarse)',
37+
/**
38+
* The iOS WebKit gate, inside `touch` (IOS_SUPPORTS_QUERY): only for a
39+
* style iOS WebKit itself requires (the 16px input floor), never a
40+
* behaviour; touch behaviour is one model on every touch device.
41+
*/
42+
iosSupports: '@supports (-webkit-touch-callout: none)',
43+
});

0 commit comments

Comments
 (0)