Skip to content

Commit 504873a

Browse files
committed
feat(List): a touch press hides the dividers beside the pressed row
A highlighted UIKit cell hides its own separator and the one above it, in the frame the highlight appears and with no animation, and both come back in the first frame of the release while the gray is still fading. A divided List row now does the same: the pressed row's divider and its previous sibling's go transparent while the press is on, and the press layer reaches over the pressed row's own, so the row reads as one unbroken highlight. The press-coverage guard learns that a composer styling a marked component through xstyle (ListItem on Item) is covered by that component.
1 parent d104ec1 commit 504873a

7 files changed

Lines changed: 316 additions & 5 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+
[feat] In a `List` with `hasDividers`, a touch press hides the dividers on either side of the pressed row, so the pressed row reads as one unbroken highlight; both come back the moment the finger lifts, while the highlight fades.
6+
7+
@vjeux

‎packages/core/src/List/List.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const docs = {
3838
{
3939
name: 'hasDividers',
4040
type: 'boolean',
41-
description: 'Show dividers between items.',
41+
description: 'Show dividers between items. A touch press hides the dividers on either side of the pressed row until the finger lifts.',
4242
default: 'false',
4343
},
4444
{

‎packages/core/src/List/List.test.tsx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,63 @@ describe('List', () => {
488488
expect(hrs).toHaveLength(0);
489489
});
490490

491+
it('hides a touch-pressed row’s divider and the one above it, with the press layer over its own', () => {
492+
render(
493+
<List hasDividers>
494+
<ListItem label="Item 1" onClick={() => {}} />
495+
<ListItem label="Item 2" onClick={() => {}} />
496+
</List>,
497+
);
498+
const row = screen.getByText('Item 1').closest('li') as HTMLElement;
499+
const rules = rulesDeclaredFor(row);
500+
const selector = (rule: string) => rule.slice(0, rule.indexOf('{'));
501+
const hides = (condition: string) =>
502+
rules.some(
503+
rule =>
504+
selector(rule).includes(condition) &&
505+
!selector(rule).includes('::before') &&
506+
/border-(bottom|block-end)-color:\s*transparent/.test(rule),
507+
);
508+
// Its own divider while it is pressed, and the divider above the row
509+
// being pressed: the row before a pressed sibling.
510+
expect(hides('[data-astryx-press="on"]')).toBe(true);
511+
expect(hides(':has(+ [data-astryx-press="on"])')).toBe(true);
512+
// Only while the press is on: the release shows both again at once.
513+
expect(hides('[data-astryx-press="fading"]')).toBe(false);
514+
// The pressed row's layer reaches over the hidden divider, except on the
515+
// last row, which has none.
516+
const layer = rules.filter(
517+
rule =>
518+
selector(rule).includes('::before') &&
519+
selector(rule).includes('[data-astryx-press="on"]') &&
520+
/(bottom|inset-block-end):/.test(rule),
521+
);
522+
expect(layer.some(rule => /calc\(\s*-1\s*\*\s*var\(/.test(rule))).toBe(
523+
true,
524+
);
525+
expect(
526+
layer.some(
527+
rule =>
528+
selector(rule).includes(':last-child') &&
529+
/(bottom|inset-block-end):\s*0(px)?[;}\s]/.test(rule),
530+
),
531+
).toBe(true);
532+
});
533+
534+
it('leaves a list without dividers alone during a press', () => {
535+
render(
536+
<List>
537+
<ListItem label="Item 1" onClick={() => {}} />
538+
</List>,
539+
);
540+
const row = screen.getByText('Item 1').closest('li') as HTMLElement;
541+
expect(
542+
rulesDeclaredFor(row).some(rule =>
543+
rule.slice(0, rule.indexOf('{')).includes(':has(+ [data-astryx-press'),
544+
),
545+
).toBe(false);
546+
});
547+
491548
// ===========================================================================
492549
// Interactive items — onClick (invisible button pattern)
493550
// ===========================================================================

‎packages/core/src/List/List.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export interface ListProps extends BaseProps<
5858
density?: ListDensity;
5959

6060
/**
61-
* Whether to show dividers between list items.
61+
* Whether to show dividers between list items. A touch press hides the
62+
* dividers on either side of the pressed row until the finger lifts.
6263
* @default false
6364
*/
6465
hasDividers?: boolean;

‎packages/core/src/List/ListItem.tsx‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/**
66
* @file ListItem.tsx
77
* @input Uses React, ReactNode, StyleXStyles, theme tokens, the List row edge
8-
* treatment, CheckboxInput, and the marker or task checkbox a
8+
* treatment, the press layer (dividers beside a touch-pressed row hide and
9+
* the layer reaches over the row's own), CheckboxInput, and the marker or task checkbox a
910
* ListMarkerScope sets (Markdown's nested lists and the task items of mixed
1011
* lists)
1112
* @output Exports ListItem component, ListItemProps type
@@ -186,14 +187,34 @@ const styles = stylex.create({
186187
withDivider: {
187188
borderBlockEndWidth: borderVars['--border-width'],
188189
borderBlockEndStyle: 'solid',
189-
borderBlockEndColor: colorVars['--color-border'],
190+
// A touch press hides this row's divider and the one above it, in the
191+
// frame the press is believed, with no transition, as a highlighted
192+
// UIKit cell hides its separators; both come back in the first frame of
193+
// the release, while the press is still fading.
194+
borderBlockEndColor: {
195+
default: colorVars['--color-border'],
196+
'[data-astryx-press="on"]': 'transparent',
197+
':has(+ [data-astryx-press="on"])': 'transparent',
198+
},
190199
// A longhand, not the `borderBlockEnd` shorthand: StyleX's default
191200
// property-specificity mode drops border shorthands silently, so the
192201
// shorthand never reached the shipped CSS and the last item kept its
193202
// divider.
194203
':last-child': {
195204
borderBlockEndWidth: 0,
196205
},
206+
// While its divider is hidden, the press layer (utils/pressLayer.stylex.ts)
207+
// reaches over it, so the whole row is pressed. The last row has no
208+
// divider to cover.
209+
'::before': {
210+
insetBlockEnd: {
211+
default: null,
212+
'[data-astryx-press="on"]': {
213+
default: `calc(-1 * ${borderVars['--border-width']})`,
214+
':last-child': 0,
215+
},
216+
},
217+
},
197218
},
198219
});
199220

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file ListDividers.interaction.browser.spec.ts
5+
* @input The built List story with dividers, real touch input (Chromium CDP /
6+
* WebKit's input command) and the interaction harness
7+
* @output Interaction proofs for a divided list under a touch press: the
8+
* pressed row's divider and the one above it are hidden in the frame the
9+
* press is believed, with the press layer reaching over the pressed row's
10+
* own; both come back in the first frame of the release; the last row's
11+
* layer never reaches past the list; a storyboard of rest, press and the
12+
* release's first frame
13+
* @position Interaction evidence for ListItem's divider styles together with
14+
* the press layer (utils/pressLayer.stylex.ts)
15+
*
16+
* UIKit's reference: a highlighted cell hides its own separator and the one
17+
* above it, unanimated, in the frame the highlight appears; on an animated
18+
* deselect both return in the first frame while the gray is still fading.
19+
* The states are read from the engine's computed styles, which carry the
20+
* real cascade (layers, specificity), and photographed in the storyboard.
21+
*/
22+
23+
import {expect, test, type Locator, type Page} from '@playwright/test';
24+
import {
25+
holdAnimations,
26+
openStory,
27+
releaseAnimations,
28+
seekAnimations,
29+
Storyboard,
30+
tap,
31+
touchDriver,
32+
withStorybook,
33+
} from '@astryxdesign/interaction-harness';
34+
35+
const storybook = withStorybook(test);
36+
37+
const STORY = 'core-list--with-media';
38+
const HOLD_MS = 400;
39+
/** Past the press onset (150 ms), well before the lift. */
40+
const PRESSED_AT_MS = 260;
41+
42+
const rows = (page: Page) => page.locator('li[data-astryx-pressable]');
43+
44+
interface RowState {
45+
readonly press: string | null;
46+
/** The divider at the row's bottom edge, as painted. */
47+
readonly divider: string;
48+
readonly dividerWidth: string;
49+
/** Where the press layer's bottom edge sits, relative to the row's padding box. */
50+
readonly layerBottom: string;
51+
readonly layerOpacity: string;
52+
}
53+
54+
async function rowStates(page: Page): Promise<RowState[]> {
55+
return rows(page).evaluateAll(elements =>
56+
elements.map(el => {
57+
const style = getComputedStyle(el);
58+
const layer = getComputedStyle(el, '::before');
59+
return {
60+
press: el.getAttribute('data-astryx-press'),
61+
divider: style.borderBottomColor,
62+
dividerWidth: style.borderBottomWidth,
63+
layerBottom: layer.bottom,
64+
layerOpacity: layer.opacity,
65+
};
66+
}),
67+
);
68+
}
69+
70+
const TRANSPARENT = 'rgba(0, 0, 0, 0)';
71+
72+
async function center(row: Locator) {
73+
const box = await row.boundingBox();
74+
if (box == null) {
75+
throw new Error('no row');
76+
}
77+
return {x: box.x + box.width / 2, y: box.y + box.height / 2};
78+
}
79+
80+
/**
81+
* Holds row `index` with a finger and returns the rows' states while the
82+
* press is on and in the release's first frame (its animations held at 0).
83+
*/
84+
async function pressRow(page: Page, index: number) {
85+
const driver = await touchDriver(page);
86+
const at = await center(rows(page).nth(index));
87+
let pressed: RowState[] = [];
88+
let released: RowState[] = [];
89+
await driver.play(tap(at, {holdMs: HOLD_MS}), {
90+
onEvent: async (_, event) => {
91+
if (event === 0) {
92+
await page.waitForTimeout(PRESSED_AT_MS);
93+
pressed = await rowStates(page);
94+
} else {
95+
await holdAnimations(page);
96+
await seekAnimations(page, 0);
97+
released = await rowStates(page);
98+
}
99+
},
100+
});
101+
await releaseAnimations(page);
102+
return {pressed, released};
103+
}
104+
105+
test.describe('a divided list under a touch press', () => {
106+
test.beforeEach(async ({page}) => {
107+
await openStory(page, storybook(), STORY);
108+
await expect(rows(page)).toHaveCount(3);
109+
});
110+
111+
test('hides the pressed row’s divider and the one above it; the release shows both at once', async ({
112+
page,
113+
}) => {
114+
const rest = await rowStates(page);
115+
// At rest: a divider under every row but the last.
116+
expect(rest.map(row => row.divider === TRANSPARENT)).toEqual([
117+
false,
118+
false,
119+
false,
120+
]);
121+
expect(rest.map(row => row.dividerWidth)).toEqual(['1px', '1px', '0px']);
122+
const resting = rest[0].divider;
123+
124+
const {pressed, released} = await pressRow(page, 1);
125+
expect(pressed.map(row => row.press)).toEqual([null, 'on', null]);
126+
// The divider above the pressed row (the first row's) and its own.
127+
expect(pressed[0].divider).toBe(TRANSPARENT);
128+
expect(pressed[1].divider).toBe(TRANSPARENT);
129+
// The layer covers the pressed row down through its hidden divider.
130+
expect(pressed[1].layerOpacity).toBe('1');
131+
expect(pressed[1].layerBottom).toBe(`-${rest[1].dividerWidth}`);
132+
// Rows that are not pressed keep their layer inside their padding box.
133+
expect(pressed[0].layerBottom).toBe('0px');
134+
135+
// The first frame of the release: both dividers are back while the
136+
// press is still at full strength, and the layer is back inside the row.
137+
expect(released[1].press).toBe('fading');
138+
expect(released[1].layerOpacity).toBe('1');
139+
expect(released[0].divider).toBe(resting);
140+
expect(released[1].divider).toBe(resting);
141+
expect(released[1].layerBottom).toBe('0px');
142+
});
143+
144+
test('the last row hides the divider above it and keeps its layer inside the list', async ({
145+
page,
146+
}) => {
147+
const rest = await rowStates(page);
148+
const {pressed} = await pressRow(page, 2);
149+
expect(pressed[2].press).toBe('on');
150+
// The second row's divider is the one above the pressed last row.
151+
expect(pressed[1].divider).toBe(TRANSPARENT);
152+
// The divider above that stays.
153+
expect(pressed[0].divider).toBe(rest[0].divider);
154+
// The last row has no divider, so its layer does not reach below it.
155+
expect(pressed[2].layerBottom).toBe('0px');
156+
});
157+
158+
test('storyboard: rest, press, the release’s first frame @storyboard', async ({
159+
page,
160+
}, testInfo) => {
161+
test.skip(
162+
testInfo.project.name !== 'chromium',
163+
'One engine is enough for the record',
164+
);
165+
// The list the rows live in.
166+
const list = rows(page).first().locator('xpath=..');
167+
const storyboard = new Storyboard(page, {
168+
name: 'list-dividers-press',
169+
clip: list,
170+
columns: 3,
171+
// Full size: the dividers are hairlines.
172+
cellScale: 1,
173+
});
174+
await storyboard.capture('rest', 0, {rows: await rowStates(page)});
175+
const driver = await touchDriver(page);
176+
const at = await center(rows(page).nth(1));
177+
await driver.play(tap(at, {holdMs: HOLD_MS}), {
178+
onEvent: async (_, event) => {
179+
if (event === 0) {
180+
await page.waitForTimeout(PRESSED_AT_MS);
181+
await storyboard.capture('pressed', PRESSED_AT_MS, {
182+
rows: await rowStates(page),
183+
});
184+
} else {
185+
await holdAnimations(page);
186+
await seekAnimations(page, 0);
187+
await storyboard.capture('release, first frame', HOLD_MS, {
188+
rows: await rowStates(page),
189+
});
190+
}
191+
},
192+
});
193+
await releaseAnimations(page);
194+
await storyboard.write(testInfo);
195+
});
196+
});

‎packages/core/src/utils/pressableCoverage.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* element carrying the scope.
2525
* - A styles-only module (`*.stylex.ts`) is covered when every file that
2626
* imports it is marked.
27+
* - A composer whose arms ride `xstyle` onto a marked component's root
28+
* (ListItem's divider arms on Item's row) is covered by that component,
29+
* since the controller writes `data-astryx-press` on that root.
2730
*/
2831

2932
import fs from 'node:fs';
@@ -109,6 +112,29 @@ function importers(file: string): string[] {
109112
.map(([other]) => other);
110113
}
111114

115+
/** Components (by exported name) whose own file is marked. */
116+
const markedComponents = new Set(
117+
[...source.entries()]
118+
.filter(([file]) => isMarked(file))
119+
.flatMap(([, text]) =>
120+
[...text.matchAll(/export function ([A-Z]\w*)\(/g)].map(m => m[1]),
121+
),
122+
);
123+
124+
/**
125+
* Marked components `file` renders and styles through `xstyle`: its arms land
126+
* on their root, the element that carries the marker.
127+
*/
128+
function xstyleHosts(file: string): string[] {
129+
const text = source.get(file) ?? '';
130+
if (!/\bxstyle=\{/.test(text)) {
131+
return [];
132+
}
133+
return [...markedComponents].filter(name =>
134+
new RegExp(`<${name}\\b`).test(text),
135+
);
136+
}
137+
112138
const pressArmFiles = [...source.entries()]
113139
.filter(([file, text]) => !MODEL_FILES.has(file) && PRESS_ARM.test(text))
114140
.map(([file]) => file)
@@ -146,9 +172,12 @@ describe('every surface that paints a press is reachable by the touch press cont
146172
// A component whose arms read an ancestor scope marker: the owners that
147173
// apply the marker are the pressables, and each must be marked.
148174
const scopes = [...text.matchAll(ANCESTOR_ARM)].map(m => m[1]);
175+
if (scopes.length === 0 && xstyleHosts(file).length > 0) {
176+
return;
177+
}
149178
expect(
150179
scopes,
151-
`${file} paints a press without usePressFeedback() and without an ancestor scope`,
180+
`${file} paints a press without usePressFeedback(), an ancestor scope, or a marked component to style through xstyle`,
152181
).not.toHaveLength(0);
153182
for (const scope of new Set(scopes)) {
154183
const owners = referrers(scope, file);

0 commit comments

Comments
 (0)