Skip to content

Commit 190b6a1

Browse files
committed
refactor(core): the action-group tracking is a tracker any recognizer can drive
useActionGroupTracking's tracking (rects read once at begin, hit-test per move, the press paint cut on leave and faded at the lift, disabled actions skipped, the action's own click run inside the lift, inert presses taken and dropped) moves into actionGroupTracker.ts as createActionGroupTracker: begin, move, end, cancel and offsetBy (for an autoscroll), with several roots, an onHighlightChange callback and an opt-out of the paint. The hook binds it to its one long press exactly as before; its tests are unchanged. A recognizer that lives elsewhere can now drive the same tracking: a menu trigger whose held finger slides into the menu it opened, or a context menu's long press.
1 parent f3b37d2 commit 190b6a1

5 files changed

Lines changed: 705 additions & 130 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+
[internal] The action-group tracking behind AlertDialog's touch handling is a plain tracker that any gesture recognizer can drive (begin, move, end, cancel, offsetBy), so a menu can track the finger that opened it from its trigger. AlertDialog's behavior is unchanged.
6+
7+
@vjeux
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file actionGroupTracker.test.ts
5+
* @input vitest (jsdom), createActionGroupTracker over actions laid out with
6+
* stubbed rects
7+
* @output The tracker's contract, driven directly as a recognizer elsewhere
8+
* would drive it: begin highlights the action under the point and reads
9+
* layout only then; move follows the finger and cuts (not fades) the action
10+
* it leaves; end fades and runs only the action under the finger, once;
11+
* cancel and an inert press run nothing; disabled actions are skipped;
12+
* several roots are one group; offsetBy shifts the cached rects; the
13+
* highlight callback and the opt-out of the paint
14+
* @position jsdom tests of packages/core/src/hooks/actionGroupTracker.ts; the
15+
* hook binding is covered by useActionGroupTracking.test.tsx
16+
*/
17+
18+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
19+
import {PRESSED_ATTRIBUTE} from '../utils/pressFeedback';
20+
import {PRESS_FADE_MS} from '../utils/pressGesture';
21+
import {
22+
createActionGroupTracker,
23+
type ActionGroupTrackerOptions,
24+
} from './actionGroupTracker';
25+
26+
function place(
27+
element: Element,
28+
box: {left: number; top: number; width: number; height: number},
29+
) {
30+
const rect = DOMRect.fromRect({
31+
x: box.left,
32+
y: box.top,
33+
width: box.width,
34+
height: box.height,
35+
});
36+
const spy = vi.fn(() => rect);
37+
element.getBoundingClientRect = spy;
38+
return spy;
39+
}
40+
41+
function action(label: string, box: Parameters<typeof place>[1]) {
42+
const button = document.createElement('button');
43+
button.type = 'button';
44+
button.textContent = label;
45+
button.setAttribute('data-action', '');
46+
const rectSpy = place(button, box);
47+
const onClick = vi.fn();
48+
button.addEventListener('click', onClick);
49+
return {button, onClick, rectSpy};
50+
}
51+
52+
let root: HTMLDivElement;
53+
let first: ReturnType<typeof action>;
54+
let second: ReturnType<typeof action>;
55+
56+
beforeEach(() => {
57+
vi.useFakeTimers();
58+
root = document.createElement('div');
59+
first = action('First', {left: 0, top: 0, width: 200, height: 40});
60+
second = action('Second', {left: 0, top: 50, width: 200, height: 40});
61+
root.append(first.button, second.button);
62+
document.body.append(root);
63+
});
64+
65+
afterEach(() => {
66+
root.remove();
67+
vi.useRealTimers();
68+
});
69+
70+
function tracker(options: Partial<ActionGroupTrackerOptions> = {}) {
71+
return createActionGroupTracker(() => ({
72+
roots: () => [root],
73+
itemSelector: '[data-action]',
74+
highlightExpansion: 0,
75+
...options,
76+
}));
77+
}
78+
79+
describe('createActionGroupTracker', () => {
80+
it('begin highlights the action under the point and reads layout only then', () => {
81+
const group = tracker();
82+
group.begin(100, 20);
83+
expect(group.highlighted).toBe(first.button);
84+
expect(first.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
85+
group.move(100, 25);
86+
group.move(100, 70);
87+
group.move(100, 72);
88+
expect(first.rectSpy).toHaveBeenCalledTimes(1);
89+
expect(second.rectSpy).toHaveBeenCalledTimes(1);
90+
});
91+
92+
it('move follows the finger: the action it leaves is cut, not faded', () => {
93+
const group = tracker();
94+
group.begin(100, 20);
95+
group.move(100, 70);
96+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
97+
expect(second.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
98+
// Between the actions nothing is highlighted.
99+
group.move(100, 45);
100+
expect(group.highlighted).toBeNull();
101+
expect(second.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
102+
});
103+
104+
it('end fades the action under the finger and runs it once, from inside the call', () => {
105+
const group = tracker();
106+
group.begin(100, 20);
107+
group.move(100, 70);
108+
expect(group.end()).toBe(second.button);
109+
expect(second.onClick).toHaveBeenCalledTimes(1);
110+
expect(first.onClick).not.toHaveBeenCalled();
111+
expect(second.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
112+
vi.advanceTimersByTime(PRESS_FADE_MS);
113+
expect(second.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
114+
// A second end has nothing to run.
115+
expect(group.end()).toBeNull();
116+
expect(second.onClick).toHaveBeenCalledTimes(1);
117+
});
118+
119+
it('a lift with no action under the finger runs nothing', () => {
120+
const group = tracker();
121+
group.begin(100, 20);
122+
group.move(100, 200);
123+
expect(group.end()).toBeNull();
124+
expect(first.onClick).not.toHaveBeenCalled();
125+
});
126+
127+
it('cancel cuts the highlight, reports it gone and runs nothing', () => {
128+
const onHighlightChange = vi.fn();
129+
const group = tracker({onHighlightChange});
130+
group.begin(100, 20);
131+
group.cancel();
132+
expect(onHighlightChange).toHaveBeenLastCalledWith(null);
133+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
134+
expect(group.isTracking).toBe(false);
135+
expect(group.end()).toBeNull();
136+
expect(first.onClick).not.toHaveBeenCalled();
137+
});
138+
139+
it('an inert press is taken but paints and runs nothing', () => {
140+
const group = tracker();
141+
group.begin(100, 20, {inert: true});
142+
expect(group.isTracking).toBe(true);
143+
expect(group.highlighted).toBeNull();
144+
expect(first.rectSpy).not.toHaveBeenCalled();
145+
group.move(100, 70);
146+
expect(group.end()).toBeNull();
147+
expect(first.onClick).not.toHaveBeenCalled();
148+
expect(second.onClick).not.toHaveBeenCalled();
149+
});
150+
151+
it('skips disabled actions', () => {
152+
second.button.disabled = true;
153+
const group = tracker();
154+
group.begin(100, 70);
155+
expect(group.highlighted).toBeNull();
156+
expect(group.end()).toBeNull();
157+
});
158+
159+
it('tracks the actions of several roots as one group', () => {
160+
const flyout = document.createElement('div');
161+
const third = action('Third', {left: 220, top: 0, width: 200, height: 40});
162+
flyout.append(third.button);
163+
document.body.append(flyout);
164+
const group = tracker({roots: () => [root, flyout]});
165+
group.begin(100, 20);
166+
group.move(300, 20);
167+
expect(group.highlighted).toBe(third.button);
168+
group.end();
169+
expect(third.onClick).toHaveBeenCalledTimes(1);
170+
flyout.remove();
171+
});
172+
173+
it('offsetBy shifts the cached rects when the content moves under a still finger', () => {
174+
const group = tracker();
175+
group.begin(100, 20);
176+
// The list scrolled up by 50: the second action is now under the finger.
177+
group.offsetBy(0, -50);
178+
group.move(100, 20);
179+
expect(group.highlighted).toBe(second.button);
180+
expect(second.rectSpy).toHaveBeenCalledTimes(1);
181+
});
182+
183+
it('reports highlight changes, and can leave the paint to its owner', () => {
184+
const onHighlightChange = vi.fn();
185+
const onActivate = vi.fn();
186+
const group = tracker({
187+
paintsHighlight: false,
188+
onHighlightChange,
189+
onActivate,
190+
});
191+
group.begin(100, 20);
192+
group.move(100, 70);
193+
group.end();
194+
// The lift leaves the highlight to the activation: no change reported.
195+
expect(onHighlightChange.mock.calls.map(([item]) => item)).toEqual([
196+
first.button,
197+
second.button,
198+
]);
199+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
200+
expect(second.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
201+
// onActivate replaces the action's own click.
202+
expect(onActivate).toHaveBeenCalledWith(second.button);
203+
expect(second.onClick).not.toHaveBeenCalled();
204+
});
205+
206+
it('a press that begins while another is tracked takes the paint off the first one', () => {
207+
const onHighlightChange = vi.fn();
208+
const group = tracker({onHighlightChange});
209+
group.begin(100, 20);
210+
expect(first.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
211+
// A second recognizer takes over the finger without an end or a cancel.
212+
group.begin(100, 70);
213+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
214+
expect(second.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
215+
// Taken over onto nothing: the highlight is gone, and the owner is told.
216+
group.begin(100, 200);
217+
expect(second.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
218+
expect(onHighlightChange.mock.calls.map(([item]) => item)).toEqual([
219+
first.button,
220+
second.button,
221+
null,
222+
]);
223+
});
224+
225+
it('dispose finishes a pending release fade', () => {
226+
const group = tracker();
227+
group.begin(100, 20);
228+
group.end();
229+
expect(first.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
230+
group.dispose();
231+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
232+
vi.advanceTimersByTime(PRESS_FADE_MS);
233+
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
234+
});
235+
236+
it('expands each action\u2019s hit area by the given margin', () => {
237+
const group = tracker({highlightExpansion: 6});
238+
group.begin(100, 44);
239+
expect(group.highlighted).toBe(first.button);
240+
});
241+
});

0 commit comments

Comments
 (0)