Skip to content

Commit 6ddc856

Browse files
committed
feat(core): the press controller leaves an action group's tracked actions to its tracker
meta#8637's own change. A press on an action a group tracks (the group item marker) is the group's tracker's to paint, for a finger and, on a touch-first device, for a mouse too; the controller paints nothing there. Absorbs meta#8637 (ed3d750).
1 parent a321e95 commit 6ddc856

3 files changed

Lines changed: 135 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+
[chore] Internal: the touch press controller leaves the press of an action marked as tracked by its group (`data-astryx-press-group-item`) to that group's own tracker, so one writer paints each press. No component carries the marker yet, so nothing visible or behavioral changes.
6+
7+
@vjeux

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import {
2828
pressConsts,
2929
} from './interactionOverlay.stylex';
3030
import {
31+
PRESS_GROUP_ITEM_ATTRIBUTE,
3132
PRESSABLE_ATTRIBUTE,
3233
PRESSED_ATTRIBUTE,
3334
installPressFeedback,
3435
paintPress,
36+
pressGroupItemProps,
3537
} from './pressFeedback';
3638
import {
3739
PRESS_FADE_MS,
@@ -281,6 +283,66 @@ describe('the press reaches the surface the finger landed on', () => {
281283
});
282284
});
283285

286+
describe('an action group whose own tracker paints its actions', () => {
287+
// family:action-groups: the group's tracker follows a held pointer across
288+
// its actions and paints them; the controller must not write the press
289+
// attribute there too.
290+
function group(): {actions: HTMLElement[]; field: HTMLElement} {
291+
const root = document.createElement('div');
292+
const actions = [pressable('button'), pressable('button')];
293+
for (const action of actions) {
294+
for (const [name, value] of Object.entries(pressGroupItemProps)) {
295+
action.setAttribute(name, value);
296+
}
297+
root.appendChild(action);
298+
}
299+
// A member the tracker skips (a menu trigger) carries no marker.
300+
const field = pressable('button');
301+
root.appendChild(field);
302+
document.body.appendChild(root);
303+
return {actions, field};
304+
}
305+
306+
it('paints nothing on an action its group tracks', () => {
307+
const {actions} = group();
308+
expect(actions[0].hasAttribute(PRESS_GROUP_ITEM_ATTRIBUTE)).toBe(true);
309+
press(actions[0], 'pointerdown', 10, 10);
310+
vi.advanceTimersByTime(PRESS_ONSET_MS);
311+
press(actions[0], 'pointerup', 10, 10);
312+
vi.advanceTimersByTime(PRESS_FLASH_MS);
313+
expect(actions[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
314+
});
315+
316+
it('leaves the paint the group writes on it alone', () => {
317+
const {actions} = group();
318+
actions[1].setAttribute(PRESSED_ATTRIBUTE, 'on');
319+
press(actions[1], 'pointerdown', 10, 10);
320+
vi.advanceTimersByTime(PRESS_ONSET_MS);
321+
press(actions[1], 'pointercancel', 10, 10);
322+
expect(actions[1].getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
323+
});
324+
325+
it('still paints a member the group does not track', () => {
326+
const {field} = group();
327+
press(field, 'pointerdown', 10, 10);
328+
vi.advanceTimersByTime(PRESS_ONSET_MS);
329+
expect(field.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
330+
});
331+
332+
it('still records the touch, so a scroll it drives brakes the next one', () => {
333+
const {actions} = group();
334+
scroller.appendChild(actions[0].parentElement as HTMLElement);
335+
press(actions[0], 'pointerdown', 10, 10);
336+
// The browser takes the touch to scroll: the cancel records nothing, so
337+
// the touch-down alone must have recorded the finger.
338+
press(actions[0], 'pointercancel', 10, 10);
339+
scroller.dispatchEvent(new Event('scroll'));
340+
press(rows[1], 'pointerdown', 10, 100);
341+
vi.advanceTimersByTime(PRESS_ONSET_MS * 2);
342+
expect(pressedRow()).toBe(-1);
343+
});
344+
});
345+
284346
describe('a scroll takes it away and nothing brings it back', () => {
285347
it('clears on a scroll from the ancestor scroller', () => {
286348
press(rows[1], 'pointerdown', 10, 10);
@@ -869,6 +931,32 @@ describe('a phone or tablet paints a mouse or Space press itself', () => {
869931
expect(rows[1].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
870932
});
871933

934+
function groupAction(row: HTMLElement): HTMLButtonElement {
935+
const action = focusedButton(row);
936+
for (const [name, value] of Object.entries(pressGroupItemProps)) {
937+
action.setAttribute(name, value);
938+
}
939+
return action;
940+
}
941+
942+
it("leaves a mouse press on an action its group tracks to the group's tracker (family:action-groups)", () => {
943+
const action = groupAction(rows[1]);
944+
press(action, 'pointerdown', 10, 100, 'mouse');
945+
expect(action.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
946+
// The tracker's own paint is left alone through the release.
947+
action.setAttribute(PRESSED_ATTRIBUTE, 'on');
948+
press(action, 'pointerup', 10, 100, 'mouse');
949+
expect(action.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
950+
});
951+
952+
it('still paints a Space press on a focused action its group tracks: the group tracks pointers only', () => {
953+
const action = groupAction(rows[1]);
954+
key(action, 'keydown');
955+
expect(action.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
956+
key(action, 'keyup');
957+
expect(action.getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
958+
});
959+
872960
it('keeps the paint while the mouse is dragged off the surface, as `:active` does', () => {
873961
press(rows[1], 'pointerdown', 10, 100, 'mouse');
874962
press(document.body, 'pointermove', 400, 900, 'mouse');

‎packages/core/src/utils/pressFeedback.ts‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
* visibility events
88
* @output Exports paintPress and readPressPaint (the press attribute's
99
* writer for every press they track), PRESSABLE_ATTRIBUTE,
10-
* PRESSED_ATTRIBUTE, pressableProps and installPressFeedback
10+
* PRESSED_ATTRIBUTE, pressableProps, the action-group item marker
11+
* (PRESS_GROUP_ITEM_ATTRIBUTE, pressGroupItemProps) and
12+
* installPressFeedback
1113
* @position The DOM half of the touch press model: one delegated controller
1214
* per document that writes `data-astryx-press` on the nearest pressable surface.
1315
* Installed by usePressFeedback (hooks/usePressFeedback.ts) on first mount;
@@ -63,6 +65,13 @@
6365
* Its one touch-only listener, `touchcancel`, is on only while the primary
6466
* pointer is coarse (coarsePointer.ts, `spec:AST-094` FR24).
6567
*
68+
* An action of a group whose own tracker follows a held pointer across its
69+
* siblings (`family:action-groups`) carries {@link PRESS_GROUP_ITEM_ATTRIBUTE}:
70+
* that tracker paints its press, so the controller leaves a pointer's press
71+
* there alone, and the two never write the attribute over each other. A
72+
* member the group's tracker skips (a field, a menu trigger) carries no
73+
* marker and keeps the controller's press.
74+
*
6675
* SYNC: When modified, update pressGesture.ts, hooks/usePressFeedback.ts,
6776
* hooks/usePressFeedback.doc.mjs, utils/interactionOverlay.stylex.ts and
6877
* pressFeedback.test.ts.
@@ -110,6 +119,18 @@ export const pressableProps: {readonly [PRESSABLE_ATTRIBUTE]: ''} = {
110119
[PRESSABLE_ATTRIBUTE]: '',
111120
};
112121

122+
/**
123+
* Marks an action whose group tracks a held pointer across its siblings
124+
* (`family:action-groups`): the group's tracker paints a pointer's press on
125+
* it, and the controller paints none there. The owner renders it on each
126+
* action its tracker follows, so it is in place before the first press.
127+
*/
128+
export const PRESS_GROUP_ITEM_ATTRIBUTE = 'data-astryx-press-group-item';
129+
130+
/** Spread onto each action a group's tracker follows. */
131+
export const pressGroupItemProps: {readonly [PRESS_GROUP_ITEM_ATTRIBUTE]: ''} =
132+
{[PRESS_GROUP_ITEM_ATTRIBUTE]: ''};
133+
113134
/**
114135
* How long after a touch a scroll can still be that finger's momentum.
115136
*
@@ -624,6 +645,15 @@ function surfaceAt(target: EventTarget | null): HTMLElement | null {
624645
return found;
625646
}
626647

648+
/**
649+
* Whether a group's own tracker paints this surface's press
650+
* ({@link PRESS_GROUP_ITEM_ATTRIBUTE}). The touch is still recorded, so a
651+
* scroll it drives arms the brake as any finger's does.
652+
*/
653+
function trackedByGroup(element: HTMLElement): boolean {
654+
return element.closest(`[${PRESS_GROUP_ITEM_ATTRIBUTE}]`) != null;
655+
}
656+
627657
/**
628658
* Is this touch a BRAKE on a list the finger set moving, rather than a press?
629659
*
@@ -705,8 +735,9 @@ function stylesheetDroppedActive(): boolean {
705735

706736
/**
707737
* The surface a mouse button's or the Space key's press paints, if any: the
708-
* innermost marked surface, outside a menu. A key's press paints only a
709-
* focused element that is itself the surface, as `:active` does.
738+
* innermost marked surface, outside a menu and off an action a group's
739+
* tracker paints. A key's press paints only a focused element that is
740+
* itself the surface, as `:active` does.
710741
*/
711742
function buttonSurfaceAt(
712743
target: EventTarget | null,
@@ -719,6 +750,11 @@ function buttonSurfaceAt(
719750
if (pointerId == null && element !== target) {
720751
return null;
721752
}
753+
// A group's tracker paints a pointer's press on its actions, a mouse's too;
754+
// a key's press is the action's own.
755+
if (pointerId != null && trackedByGroup(element)) {
756+
return null;
757+
}
722758
return element;
723759
}
724760

@@ -804,7 +840,7 @@ function onPointerDown(event: PointerEvent): void {
804840
scrollerIsBraking(element);
805841
lastTouchAt = Date.now();
806842
lastTouchTarget = event.target instanceof Node ? event.target : null;
807-
if (element == null || braking) {
843+
if (element == null || braking || trackedByGroup(element)) {
808844
return;
809845
}
810846
takeOver(button, element);

0 commit comments

Comments
 (0)