Skip to content

Commit eb1d168

Browse files
committed
feat(core): a finger's press on a control is tracked like UIControl
The press model moves onto the gesture host's press tracking (gestures/delegatedPress.ts) with its two profiles. A control (the default) highlights in the frame the finger lands, after UIScrollView's content-touch delay inside a scroller, keeps tracking until the browser takes the touch to scroll (then cancels with no fade and no click), answers a quick tap at the lift and releases on UIButton's clock. A cell (Item and TreeList rows, usePressFeedback({profile: 'cell'})) keeps the list cell's press. The mouse and Space-key half for touch-first devices paints through the shared paintPress on the profile's clock; the old touch state machine goes.
1 parent 1e900ba commit eb1d168

16 files changed

Lines changed: 825 additions & 1668 deletions
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] Under a finger, a button, card, link or other control now highlights in the frame the finger lands, as UIKit's controls do, instead of after a 150 ms delay; inside a scroller it still waits that delay (a flick paints nothing), and a scroll that takes a held press cancels it with no fade and no click. The release fades over UIButton's 250 ms. List rows (Item, TreeList) keep a list cell's press: the 150 ms delay wherever they are, cancelled by 10 px of travel, released on the deselect clock. `usePressFeedback` takes `{profile: 'cell'}` for a local list row; the default is a control. A mouse on a desktop keeps `:active`.
6+
7+
@vjeux

‎apps/storybook/stories/PressFeedback.stories.tsx‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import {VStack} from '@astryxdesign/core/Layout';
2626
* View these with a coarse pointer: in Chromium, open DevTools, toggle the
2727
* device toolbar (a phone preset), and use the touch cursor; or open Storybook
2828
* on a phone. Under a finger the bare `:active` arm is dropped and the press
29-
* follows the clocks a native list uses: nothing paints for 150 ms; a held
30-
* press then paints the full pressed overlay on the next frame; travelling
31-
* 10 px, or a scroll claiming the gesture, cancels it with no fade and nothing
32-
* repaints until a new touch; a tap shorter than the delay paints at the lift;
33-
* the release fades over 200 ms. A mouse keeps `:active`.
29+
* follows UIKit. A control (a button, a card) highlights in the frame the
30+
* finger lands, or 150 ms later inside a scroller, where a flick paints
31+
* nothing; the browser taking the touch to scroll cancels it with no fade; a
32+
* tap shorter than the delay paints at the lift; the release fades over
33+
* 250 ms (UIButton's). A cell (a list row) waits 150 ms, is cancelled by
34+
* 10 px of travel, and releases on the deselect clock. A mouse keeps
35+
* `:active` on a desktop.
3436
*/
3537
const meta: Meta = {
3638
title: 'Core/Press feedback (touch)',
@@ -39,7 +41,7 @@ const meta: Meta = {
3941
docs: {
4042
description: {
4143
component:
42-
'The touch press model on every pressable surface. View with a coarse pointer (DevTools device toolbar, or a phone). Under a finger the bare `:active` arm is dropped and one document-level controller writes `data-astryx-press="on"` once a press is believed (150 ms, no travel, no scroll) and `data-astryx-press="fading"` for the 200 ms release; a scroll or 10 px of travel cancels with no fade, and nothing repaints until a new touch. A mouse keeps `:active`. Scroll the list stories with a finger: no row paints while the list moves.',
44+
'The touch press model on every pressable surface. View with a coarse pointer (DevTools device toolbar, or a phone). Under a finger the bare `:active` arm is dropped and the gesture host writes `data-astryx-press="on"` while a press is held and `data-astryx-press="fading"` for the release. A control highlights in the frame the finger lands (150 ms later inside a scroller) and releases over 250 ms; a cell (a list row) waits 150 ms, is cancelled by 10 px of travel, and releases on the deselect clock. A scroll claiming the touch cancels with no fade. A mouse keeps `:active` on a desktop. Scroll the list stories with a finger: nothing paints while the list moves.',
4345
},
4446
},
4547
},
@@ -51,9 +53,12 @@ type Story = StoryObj;
5153
const touchNote = (what: string) =>
5254
`${what} Hold: paints after 150 ms. Tap: paints at the lift. Drag or scroll: never paints, and stays dark until the finger lifts and lands again.`;
5355

56+
const controlNote = (what: string) =>
57+
`${what} Touch: paints in the frame the finger lands. Lift: fades over 250 ms. A scroll that takes the touch: the paint goes at once, with no fade and no click.`;
58+
5459
export const Buttons: Story = {
5560
parameters: {
56-
docs: {description: {story: touchNote('Buttons under a finger.')}},
61+
docs: {description: {story: controlNote('Buttons under a finger.')}},
5762
},
5863
render: () => (
5964
<VStack gap={3}>
@@ -159,3 +164,30 @@ export const Controls: Story = {
159164
);
160165
},
161166
};
167+
168+
export const ButtonsInScroller: Story = {
169+
parameters: {
170+
docs: {
171+
description: {
172+
story:
173+
'Buttons in a scrolling list, under a finger: a held button paints 150 ms after the finger lands (UIScrollView.delaysContentTouches); a flick scrolls the list and paints nothing; a scroll that starts after the button painted takes the touch, and the paint goes at once with no fade and no click.',
174+
},
175+
},
176+
},
177+
render: () => (
178+
<div
179+
data-testid="button-scroller"
180+
style={{height: 240, overflowY: 'auto', border: '1px solid transparent'}}>
181+
<VStack gap={3}>
182+
{Array.from({length: 16}, (_, index) => (
183+
<Button
184+
key={index}
185+
label={`Action ${index + 1}`}
186+
variant="secondary"
187+
onClick={() => {}}
188+
/>
189+
))}
190+
</VStack>
191+
</div>
192+
),
193+
};
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file ButtonPress.interaction.browser.spec.ts
5+
* @input The built press feedback stories (Buttons, Buttons in a scroller),
6+
* real touch input (Chromium CDP / WebKit's input command), the
7+
* interaction harness, and the control profile's clocks
8+
* (gestures/PressTracker.ts, PRESS_PROFILES)
9+
* @output Interaction proofs for the control press profile: outside a
10+
* scroller a finger's press highlights in the frame it lands and releases
11+
* on UIButton's clock; inside one it waits UIScrollView's content-touch
12+
* delay, a flick paints nothing, and a scroll that takes a held press
13+
* cancels it with no fade and no click; a quick tap highlights at the lift;
14+
* the engine's own tap paints and activates once; a storyboard of a press,
15+
* its trace asserted
16+
* @position Interaction evidence for the press model's control profile
17+
* (gestures/PressTracker.ts, gestures/delegatedPress.ts as installed by
18+
* hooks/usePressFeedback.ts)
19+
*/
20+
21+
import {expect, test, type Locator, type Page} from '@playwright/test';
22+
import {
23+
flick,
24+
freezeTimers,
25+
holdThenDrag,
26+
installTimerGate,
27+
openStory,
28+
tap,
29+
thawTimers,
30+
touchDriver,
31+
touchStoryboard,
32+
withStorybook,
33+
type TouchDriver,
34+
} from '@astryxdesign/interaction-harness';
35+
import {PRESS_PROFILES} from '../../gestures/PressTracker';
36+
import {scroll} from '../../motion/uikit/scroll';
37+
38+
const storybook = withStorybook(test);
39+
40+
/** The control profile's release clock: UIButton's. */
41+
const RELEASE_MS = PRESS_PROFILES.control.releaseMs;
42+
const CONTENT_TOUCH_DELAY_MS = scroll.delaysContentTouchesMs;
43+
/** Long enough that a press in a scroller is believed. */
44+
const HOLD_MS = 400;
45+
/** Two frames at 60 Hz: "in the frame the finger lands", with dispatch slack. */
46+
const SAME_FRAME_MS = 34;
47+
48+
const BUTTONS = 'core-press-feedback-touch--buttons';
49+
const SCROLLER = 'core-press-feedback-touch--buttons-in-scroller';
50+
51+
interface Opened {
52+
driver: TouchDriver;
53+
surface: Locator;
54+
center: {x: number; y: number};
55+
/** What the page saw, on its own clock (performance.now()). */
56+
log: () => Promise<LogEntry[]>;
57+
}
58+
59+
type LogEntry = [kind: string, t: number, value: string | null];
60+
61+
async function openSurface(
62+
page: Page,
63+
story: string,
64+
surface: (page: Page) => Locator,
65+
): Promise<Opened> {
66+
await openStory(page, storybook(), story);
67+
const driver = await touchDriver(page);
68+
const element = surface(page);
69+
await expect(element).toBeVisible();
70+
const box = await element.boundingBox();
71+
if (box == null) {
72+
throw new Error(`${story}: the surface has no box`);
73+
}
74+
await element.evaluate(el => {
75+
const entries: LogEntry[] = [];
76+
(window as unknown as {__press: LogEntry[]}).__press = entries;
77+
for (const type of ['pointerdown', 'pointerup', 'pointercancel', 'click']) {
78+
document.addEventListener(
79+
type,
80+
() => entries.push([type, performance.now(), null]),
81+
true,
82+
);
83+
}
84+
new MutationObserver(records => {
85+
for (const record of records) {
86+
entries.push([
87+
'press',
88+
performance.now(),
89+
(record.target as Element).getAttribute('data-astryx-press'),
90+
]);
91+
}
92+
}).observe(el, {attributes: true, attributeFilter: ['data-astryx-press']});
93+
});
94+
return {
95+
driver,
96+
surface: element,
97+
center: {x: box.x + box.width / 2, y: box.y + box.height / 2},
98+
log: async () =>
99+
page.evaluate(() => (window as unknown as {__press: LogEntry[]}).__press),
100+
};
101+
}
102+
103+
const secondaryButton = (page: Page) =>
104+
page.getByRole('button', {name: 'Secondary', exact: true});
105+
const scrollerButton = (page: Page) =>
106+
page.getByRole('button', {name: 'Action 2', exact: true});
107+
108+
const first = (entries: LogEntry[], kind: string, value?: string | null) =>
109+
entries.find(
110+
([k, , v]) => k === kind && (value === undefined || v === value),
111+
);
112+
113+
test('outside a scroller, a finger’s press highlights in the frame it lands, and the lift fades it on UIButton’s clock', async ({
114+
page,
115+
}) => {
116+
const opened = await openSurface(page, BUTTONS, secondaryButton);
117+
await opened.driver.play(tap(opened.center, {holdMs: 300}));
118+
await expect
119+
.poll(async () => opened.surface.getAttribute('data-astryx-press'))
120+
.toBeNull();
121+
const entries = await opened.log();
122+
const down = first(entries, 'pointerdown');
123+
const on = first(entries, 'press', 'on');
124+
const up = first(entries, 'pointerup');
125+
const fading = first(entries, 'press', 'fading');
126+
const gone = first(entries, 'press', null);
127+
expect(
128+
down && on && up && fading && gone,
129+
JSON.stringify(entries),
130+
).toBeTruthy();
131+
if (!down || !on || !up || !fading || !gone) {
132+
return;
133+
}
134+
expect(on[1] - down[1]).toBeLessThan(SAME_FRAME_MS);
135+
expect(fading[1] - up[1]).toBeLessThan(SAME_FRAME_MS);
136+
// The attribute leaves when UIButton's release clock runs out.
137+
expect(gone[1] - fading[1]).toBeGreaterThanOrEqual(RELEASE_MS - 5);
138+
expect(gone[1] - fading[1]).toBeLessThan(RELEASE_MS + 80);
139+
// WebKit synthesises a click only from its own tap path, not from a raw
140+
// touch sequence (harness README, engine limits); Chromium does from both.
141+
if (opened.driver.engine === 'chromium') {
142+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(1);
143+
}
144+
});
145+
146+
test('inside a scroller, a held press waits UIScrollView’s content-touch delay', async ({
147+
page,
148+
}) => {
149+
const opened = await openSurface(page, SCROLLER, scrollerButton);
150+
await opened.driver.play(tap(opened.center, {holdMs: HOLD_MS}));
151+
const entries = await opened.log();
152+
const down = first(entries, 'pointerdown');
153+
const on = first(entries, 'press', 'on');
154+
expect(down && on, JSON.stringify(entries)).toBeTruthy();
155+
if (!down || !on) {
156+
return;
157+
}
158+
expect(on[1] - down[1]).toBeGreaterThanOrEqual(CONTENT_TOUCH_DELAY_MS - 5);
159+
expect(on[1] - down[1]).toBeLessThan(CONTENT_TOUCH_DELAY_MS + 60);
160+
// WebKit synthesises a click only from its own tap path, not from a raw
161+
// touch sequence (harness README, engine limits); Chromium does from both.
162+
if (opened.driver.engine === 'chromium') {
163+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(1);
164+
}
165+
});
166+
167+
test('inside a scroller, a tap quicker than the delay highlights at the lift, then fades', async ({
168+
page,
169+
}) => {
170+
const opened = await openSurface(page, SCROLLER, scrollerButton);
171+
await opened.driver.play(tap(opened.center, {holdMs: 60}));
172+
await expect
173+
.poll(async () => opened.surface.getAttribute('data-astryx-press'))
174+
.toBeNull();
175+
const entries = await opened.log();
176+
const up = first(entries, 'pointerup');
177+
const on = first(entries, 'press', 'on');
178+
const fading = first(entries, 'press', 'fading');
179+
expect(up && on && fading, JSON.stringify(entries)).toBeTruthy();
180+
if (!up || !on || !fading) {
181+
return;
182+
}
183+
// Nothing before the lift, the highlight at it, held for the quick-tap
184+
// flash, then the release.
185+
expect(on[1]).toBeGreaterThanOrEqual(up[1] - 5);
186+
expect(fading[1] - on[1]).toBeGreaterThanOrEqual(90);
187+
// WebKit synthesises a click only from its own tap path, not from a raw
188+
// touch sequence (harness README, engine limits); Chromium does from both.
189+
if (opened.driver.engine === 'chromium') {
190+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(1);
191+
}
192+
});
193+
194+
test('a tap through the engine’s own tap path paints the press and activates once', async ({
195+
page,
196+
}) => {
197+
const opened = await openSurface(page, BUTTONS, secondaryButton);
198+
await opened.driver.tap(opened.center);
199+
await expect
200+
.poll(async () => opened.surface.getAttribute('data-astryx-press'))
201+
.toBeNull();
202+
const entries = await opened.log();
203+
expect(first(entries, 'press', 'on'), JSON.stringify(entries)).toBeTruthy();
204+
expect(first(entries, 'press', 'fading')).toBeTruthy();
205+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(1);
206+
});
207+
208+
test('inside a scroller, a flick scrolls the list and paints nothing', async ({
209+
page,
210+
browserName,
211+
}) => {
212+
test.skip(
213+
browserName !== 'chromium',
214+
'injected touches start no native scroll in this WebKit build; the iOS simulator covers it',
215+
);
216+
const opened = await openSurface(page, SCROLLER, scrollerButton);
217+
await opened.driver.play(
218+
flick(opened.center, {velocity: {x: 0, y: -1500}, durationMs: 80}),
219+
);
220+
await page.waitForTimeout(300);
221+
const entries = await opened.log();
222+
expect(
223+
entries.filter(([k]) => k === 'press'),
224+
JSON.stringify(entries),
225+
).toEqual([]);
226+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(0);
227+
const scrolled = await page
228+
.getByTestId('button-scroller')
229+
.evaluate(el => el.scrollTop);
230+
expect(scrolled).toBeGreaterThan(0);
231+
});
232+
233+
test('inside a scroller, a scroll that takes a held press cancels it: the paint goes at once, with no fade and no click', async ({
234+
page,
235+
browserName,
236+
}) => {
237+
test.skip(
238+
browserName !== 'chromium',
239+
'injected touches start no native scroll in this WebKit build; the iOS simulator covers it',
240+
);
241+
const opened = await openSurface(page, SCROLLER, scrollerButton);
242+
await opened.driver.play(
243+
holdThenDrag(
244+
opened.center,
245+
{x: opened.center.x, y: opened.center.y - 150},
246+
{holdMs: 300, durationMs: 300},
247+
),
248+
);
249+
await page.waitForTimeout(100);
250+
const entries = await opened.log();
251+
const presses = entries.filter(([k]) => k === 'press').map(([, , v]) => v);
252+
// Highlighted while held; the browser's takeover removes it, never fades it.
253+
expect(presses, JSON.stringify(entries)).toEqual(['on', null]);
254+
expect(first(entries, 'pointercancel')).toBeTruthy();
255+
expect(entries.filter(([k]) => k === 'click')).toHaveLength(0);
256+
const scrolled = await page
257+
.getByTestId('button-scroller')
258+
.evaluate(el => el.scrollTop);
259+
expect(scrolled).toBeGreaterThan(0);
260+
});
261+
262+
test(
263+
'storyboard: a button pressed and lifted outside a scroller',
264+
{tag: '@storyboard'},
265+
async ({page}, testInfo) => {
266+
const opened = await openSurface(page, BUTTONS, secondaryButton);
267+
await installTimerGate(page);
268+
const box = await opened.surface.boundingBox();
269+
if (box == null) {
270+
throw new Error('no button');
271+
}
272+
const clip = {
273+
x: Math.max(0, box.x - 16),
274+
y: Math.max(0, box.y - 16),
275+
width: box.width + 32,
276+
height: box.height + 32,
277+
};
278+
const storyboard = await touchStoryboard(
279+
page,
280+
opened.driver,
281+
tap(opened.center, {holdMs: 300}),
282+
{
283+
name: 'button-press-control-profile',
284+
clip,
285+
measure: async () => ({
286+
state: await opened.surface.getAttribute('data-astryx-press'),
287+
}),
288+
columns: 3,
289+
},
290+
);
291+
await freezeTimers(page);
292+
await storyboard.write(testInfo);
293+
await thawTimers(page);
294+
const states = storyboard.frames.map(f => f.data.state as string | null);
295+
// Before the touch, the touch itself (already highlighted), the lift.
296+
expect(states).toEqual([null, 'on', 'fading']);
297+
},
298+
);

0 commit comments

Comments
 (0)