Skip to content

Commit 3fecfc2

Browse files
committed
refactor(core): AlertDialog's touch tracking runs on the shared action-group tracker
useActionGroupTracking binds the shared tracker (hooks/actionGroupTracker.ts) to its one long press instead of carrying its own copy of the tracking: the rects read once at begin and hit-tested 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. Its tests are unchanged, and so is AlertDialog's behavior. The same tracker is driven by a menu trigger whose held finger slides into the menu it opened, a segmented control and a page control.
1 parent 942776e commit 3fecfc2

2 files changed

Lines changed: 30 additions & 142 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] AlertDialog's touch tracking runs on the shared action-group tracker, the one any gesture recognizer can drive (a menu tracking the finger that opened it from its trigger, a segmented control, a page control). AlertDialog's behavior is unchanged.
6+
7+
@vjeux

‎packages/core/src/hooks/useActionGroupTracking.ts‎

Lines changed: 23 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
*
4040
* Nothing reads layout after the press begins: the actions' rects are read
4141
* once when it begins, then hit-tested. A move costs at most two attribute
42-
* writes and never a render.
42+
* writes and never a render. The tracking itself is actionGroupTracker.ts;
43+
* this hook binds it to the one recognizer.
4344
*
4445
* SYNC: When modified, update useActionGroupTracking.test.tsx
4546
*/
@@ -49,8 +50,10 @@ import {LongPressGestureRecognizer} from '../gestures/LongPressGestureRecognizer
4950
import type {GestureTouch, PointerKind} from '../gestures/types';
5051
import {useGestureRecognizer} from '../gestures/useGestureRecognizer';
5152
import {UIKIT_GESTURES} from '../motion/uikit/gestures';
52-
import {PRESSED_ATTRIBUTE} from '../utils/pressFeedback';
53-
import {PRESS_FADE_MS} from '../utils/pressGesture';
53+
import {
54+
createActionGroupTracker,
55+
type ActionGroupTracker,
56+
} from './actionGroupTracker';
5457
import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';
5558

5659
export interface ActionGroupTrackingOptions {
@@ -84,22 +87,6 @@ export interface ActionGroupTrackingOptions {
8487
onActivate?: (item: HTMLElement) => void;
8588
}
8689

87-
/** An action that is not enabled takes no highlight and does not run. */
88-
const DISABLED = ':disabled, [aria-disabled="true"]';
89-
90-
interface TrackedAction {
91-
readonly element: HTMLElement;
92-
readonly left: number;
93-
readonly top: number;
94-
readonly right: number;
95-
readonly bottom: number;
96-
}
97-
98-
interface TrackingState {
99-
actions: TrackedAction[];
100-
highlighted: HTMLElement | null;
101-
}
102-
10390
const PRESS = UIKIT_GESTURES.press;
10491

10592
/**
@@ -116,87 +103,20 @@ export function useActionGroupTracking(
116103
useIsomorphicLayoutEffect(() => {
117104
optionsRef.current = options;
118105
});
119-
const stateRef = useRef<TrackingState>({actions: [], highlighted: null});
120-
const fadesRef = useRef(new Map<Element, ReturnType<typeof setTimeout>>());
106+
const trackerRef = useRef<ActionGroupTracker | null>(null);
107+
trackerRef.current ??= createActionGroupTracker(() => ({
108+
roots: () => [ref.current],
109+
itemSelector: optionsRef.current.itemSelector,
110+
highlightExpansion: optionsRef.current.highlightExpansion,
111+
onActivate: optionsRef.current.onActivate,
112+
}));
113+
const tracker = trackerRef.current;
121114
const isEnabled = options.isEnabled ?? true;
122-
// Ends the release fades this hook started (their timers, and the paint
115+
// Ends the release fades the tracker started (their timers, and the paint
123116
// each would remove) when tracking turns off and when the group unmounts:
124117
// the lift that runs an action usually closes the alert before its fade
125118
// ends.
126-
useEffect(() => {
127-
const fades = fadesRef.current;
128-
return () => {
129-
for (const [element, timer] of fades) {
130-
clearTimeout(timer);
131-
if (element.getAttribute(PRESSED_ATTRIBUTE) === 'fading') {
132-
element.removeAttribute(PRESSED_ATTRIBUTE);
133-
}
134-
}
135-
fades.clear();
136-
};
137-
}, [isEnabled]);
138-
139-
const clearFade = (element: Element) => {
140-
const fade = fadesRef.current.get(element);
141-
if (fade != null) {
142-
clearTimeout(fade);
143-
fadesRef.current.delete(element);
144-
}
145-
};
146-
const paint = (element: HTMLElement) => {
147-
clearFade(element);
148-
element.setAttribute(PRESSED_ATTRIBUTE, 'on');
149-
};
150-
const cut = (element: HTMLElement) => {
151-
clearFade(element);
152-
element.removeAttribute(PRESSED_ATTRIBUTE);
153-
};
154-
const release = (element: HTMLElement) => {
155-
clearFade(element);
156-
element.setAttribute(PRESSED_ATTRIBUTE, 'fading');
157-
fadesRef.current.set(
158-
element,
159-
setTimeout(() => {
160-
fadesRef.current.delete(element);
161-
if (element.getAttribute(PRESSED_ATTRIBUTE) === 'fading') {
162-
element.removeAttribute(PRESSED_ATTRIBUTE);
163-
}
164-
}, PRESS_FADE_MS),
165-
);
166-
};
167-
168-
const highlight = (next: HTMLElement | null) => {
169-
const state = stateRef.current;
170-
if (next === state.highlighted) {
171-
return;
172-
}
173-
if (state.highlighted != null) {
174-
cut(state.highlighted);
175-
}
176-
state.highlighted = next;
177-
if (next != null) {
178-
paint(next);
179-
}
180-
};
181-
182-
const actionAt = (x: number, y: number): HTMLElement | null => {
183-
const margin = optionsRef.current.highlightExpansion;
184-
for (const action of stateRef.current.actions) {
185-
if (
186-
x >= action.left - margin &&
187-
x <= action.right + margin &&
188-
y >= action.top - margin &&
189-
y <= action.bottom + margin
190-
) {
191-
return action.element;
192-
}
193-
}
194-
return null;
195-
};
196-
197-
const reset = () => {
198-
stateRef.current = {actions: [], highlighted: null};
199-
};
119+
useEffect(() => () => trackerRef.current?.dispose(), [isEnabled]);
200120

201121
return useGestureRecognizer(
202122
ref,
@@ -218,61 +138,22 @@ export function useActionGroupTracking(
218138
recognizer => {
219139
switch (recognizer.state) {
220140
case 'began': {
221-
const root = ref.current;
222-
const actions: TrackedAction[] = [];
223-
// An inert press tracks no action: nothing to paint, nothing to run.
224-
if (root != null && optionsRef.current.isInert?.() !== true) {
225-
for (const element of root.querySelectorAll<HTMLElement>(
226-
optionsRef.current.itemSelector,
227-
)) {
228-
if (element.matches(DISABLED)) {
229-
continue;
230-
}
231-
const rect = element.getBoundingClientRect();
232-
actions.push({
233-
element,
234-
left: rect.left,
235-
top: rect.top,
236-
right: rect.right,
237-
bottom: rect.bottom,
238-
});
239-
}
240-
}
241-
stateRef.current = {actions, highlighted: null};
242141
const {x, y} = recognizer.location();
243-
highlight(actionAt(x, y));
142+
// An inert press is taken and tracks nothing.
143+
tracker.begin(x, y, {inert: optionsRef.current.isInert?.() === true});
244144
break;
245145
}
246146
case 'changed': {
247147
const {x, y} = recognizer.location();
248-
highlight(actionAt(x, y));
148+
tracker.move(x, y);
249149
break;
250150
}
251-
case 'ended': {
252-
const lifted = stateRef.current.highlighted;
253-
reset();
254-
if (lifted == null) {
255-
break;
256-
}
257-
release(lifted);
258-
if (!lifted.matches(DISABLED)) {
259-
const activate = optionsRef.current.onActivate;
260-
if (activate != null) {
261-
activate(lifted);
262-
} else {
263-
lifted.click();
264-
}
265-
}
151+
case 'ended':
152+
tracker.end();
266153
break;
267-
}
268-
case 'cancelled': {
269-
const highlighted = stateRef.current.highlighted;
270-
reset();
271-
if (highlighted != null) {
272-
cut(highlighted);
273-
}
154+
case 'cancelled':
155+
tracker.cancel();
274156
break;
275-
}
276157
case 'possible':
277158
case 'failed':
278159
// Listeners are not called for these.

0 commit comments

Comments
 (0)