Skip to content

Commit eb2775d

Browse files
committed
fix(core): Tab paints the press for every pointer
Under spec:AST-094 FR43 the press controller paints a mouse's or a pen's press too (data-astryx-press), not only a finger's. Tab painted its pressed image only inside @media (pointer: coarse), so a mouse press showed nothing but :active's colour, with no release fade. - Its image arms paint the controller's press for every pointer. - Its hover and pressed colours yield while the press is on, so a press never paints twice, and paint again over the release, so a hovering pointer's release lands on the tab's hover (as the shared overlay does). Checkbox and Radio are #8696's; Switch is #8681's. Test: Tab's compiled CSS paints both arms outside the coarse media.
1 parent e570a85 commit eb2775d

3 files changed

Lines changed: 116 additions & 4 deletions

File tree

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+
[fix] TabList: a mouse or pen press on a tab paints the same press feedback a finger's does, and its release lands on the tab's hover tint.
6+
7+
@cixzhang

‎packages/core/src/TabList/Tab.tsx‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,29 @@ const styles = stylex.create({
157157
// on the tab instead, and paints as an image below.
158158
'@media (pointer: coarse)': 'transparent',
159159
},
160+
// While the press is on, the hover and pressed colours yield to the
161+
// image (same shape as the arm above, so it outranks it). Over the
162+
// release they paint again: a hovering pointer's hover sits beneath the
163+
// fading image, so the release lands on it.
164+
[stylex.when.ancestor('[data-astryx-press="on"]', tabScope)]: {
165+
default: 'transparent',
166+
'@media (hover: hover)': 'transparent',
167+
'@media (pointer: coarse)': 'transparent',
168+
},
160169
},
161170
// The touch press, as an image layer over the colour: an image change is
162171
// discrete, so the layer's colour transition cannot fade the onset in,
163172
// and the strength the tab's `pressedAlpha` arms own (1 while on, 1 → 0
164-
// over the release) is what moves. Coarse pointers only, like the colour
165-
// arms above; see interactionOverlay.stylex.ts.
173+
// over the release) is what moves. Every pointer: a mouse paints at once;
174+
// see interactionOverlay.stylex.ts.
166175
backgroundImage: {
167176
default: null,
168177
[stylex.when.ancestor('[data-astryx-press="on"]', tabScope)]: {
169-
default: null,
178+
default: pressedOverlayImage,
170179
'@media (pointer: coarse)': pressedOverlayImage,
171180
},
172181
[stylex.when.ancestor('[data-astryx-press="fading"]', tabScope)]: {
173-
default: null,
182+
default: pressedOverlayImage,
174183
'@media (pointer: coarse)': pressedOverlayImage,
175184
},
176185
},
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file pressEveryPointer.source-build.test.mjs
5+
* @input Uses Babel, the StyleX plugin as the package stylesheet compiles it,
6+
* and the sources of the controls that paint the press on a descendant
7+
* @output Verifies Tab paints the controller's press for every pointer, not
8+
* only inside `@media (pointer: coarse)`
9+
* @position Regression test for `spec:AST-094` FR43 (one press model for
10+
* every pointer) in the compiled CSS of those modules
11+
*/
12+
13+
import fs from 'node:fs/promises';
14+
import path from 'node:path';
15+
import {fileURLToPath} from 'node:url';
16+
import {transformAsync} from '@babel/core';
17+
import stylexBabelPlugin from '@stylexjs/babel-plugin';
18+
import {describe, expect, it} from 'vitest';
19+
import stylexResolution from '../../../../scripts/stylex-module-resolution.cjs';
20+
21+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
22+
23+
async function compiledCss(fileName) {
24+
const file = path.join(__dirname, '..', fileName);
25+
const result = await transformAsync(await fs.readFile(file, 'utf8'), {
26+
babelrc: false,
27+
configFile: false,
28+
filename: file,
29+
presets: [
30+
['@babel/preset-typescript', {isTSX: true, allExtensions: true}],
31+
['@babel/preset-react', {runtime: 'automatic'}],
32+
],
33+
plugins: [
34+
[
35+
stylexBabelPlugin,
36+
{
37+
dev: false,
38+
runtimeInjection: false,
39+
genConditionalClasses: true,
40+
treeshakeCompensation: true,
41+
unstable_moduleResolution: stylexResolution.stylexModuleResolution,
42+
},
43+
],
44+
],
45+
});
46+
return stylexBabelPlugin.processStylexRules(
47+
result?.metadata?.stylex ?? [],
48+
false,
49+
);
50+
}
51+
52+
/**
53+
* The rules painting the press image on an arm, each with whether it sits
54+
* inside `@media (pointer: coarse)`.
55+
*/
56+
function pressImageRules(css, arm) {
57+
const out = [];
58+
const media = /@media ([^{]+)\{((?:[^{}]*\{[^{}]*\})*)[^{}]*\}/g;
59+
const coarseRanges = [];
60+
for (const match of css.matchAll(media)) {
61+
if (match[1].includes('pointer: coarse')) {
62+
coarseRanges.push([match.index, match.index + match[0].length]);
63+
}
64+
}
65+
for (const match of css.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
66+
if (
67+
match[1].includes(`[data-astryx-press="${arm}"]`) &&
68+
/background-image:\s*var\(--_press-paint-image\)/.test(match[2])
69+
) {
70+
out.push({
71+
coarseOnly: coarseRanges.some(
72+
([from, to]) => match.index >= from && match.index < to,
73+
),
74+
});
75+
}
76+
}
77+
return out;
78+
}
79+
80+
const CONTROLS = ['TabList/Tab.tsx'];
81+
82+
describe('controls that paint the press on a descendant', () => {
83+
for (const control of CONTROLS) {
84+
it(`${control} paints both arms for every pointer, not only a coarse one`, async () => {
85+
const css = await compiledCss(control);
86+
for (const arm of ['on', 'fading']) {
87+
const rules = pressImageRules(css, arm);
88+
expect(rules.length, `${control} ${arm}`).toBeGreaterThan(0);
89+
expect(
90+
rules.some(rule => !rule.coarseOnly),
91+
`${control} ${arm}`,
92+
).toBe(true);
93+
}
94+
});
95+
}
96+
});

0 commit comments

Comments
 (0)