Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/core-internal-entries-lab-tabbar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[chore] Internal: Core's internal touch entry re-exports the action-group tracking hook, which gains a no-paint option and press callbacks for Lab's TabBar. A tab-bar string joins Core's message catalog. Apps import nothing new.

@vjeux
4 changes: 4 additions & 0 deletions packages/core/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@
"defaultMessage": "Maximum value",
"description": "Accessible name of the upper thumb of a range Slider (two thumbs), announced together with the Slider's own label, for example \"Price range, Maximum value, 80\". Never shown on screen."
},
"@astryx.tabBar.badgedItem": {
"defaultMessage": "{label}, {badge}",
"description": "Screen-reader name of a bottom tab bar item that shows a badge (\"Library, 3\"). {label} is the tab's visible name; {badge} is the badge's count or its own label (\"3 unread\"). Translators may reorder the two or change the separator."
},
"@astryx.tabList.label": {
"defaultMessage": "Tabs",
"description": "Screen-reader-only fallback name for a horizontal tab bar. Plural noun; \"Tabs\" here = UI tab panels, not browser tabs or the Tab key."
Expand Down
92 changes: 91 additions & 1 deletion packages/core/src/hooks/useActionGroupTracking.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* action is skipped; a second finger or the window losing focus cancels;
* inside a scroller a flick is left to the scroller; a mouse under a fine
* pointer is tracked like a finger; the shared tracker does the tracking;
* no render per move
* a caller's no-paint option and press callbacks; no render per move
* @position jsdom tests of packages/core/src/hooks/useActionGroupTracking.ts
*/

Expand Down Expand Up @@ -313,6 +313,96 @@ describe('useActionGroupTracking', () => {
expect(onDelete).not.toHaveBeenCalled();
});

describe('a group that draws its own highlight (Lab TabBar)', () => {
const nameOf = (element: Element | null) => element?.textContent ?? 'none';

it('paints nothing with paintsHighlight off, and still runs the action it lifts on', () => {
const {cancel, del, onCancel} = mount({
options: {paintsHighlight: false},
});
const f = finger();
f.down(cancel, centerOf(CANCEL));
expect(paintOf(cancel)).toBeNull();
f.move(cancel, centerOf(DELETE));
expect(paintOf(del)).toBeNull();
f.move(cancel, centerOf(CANCEL));
f.up(cancel, centerOf(CANCEL));
expect(paintOf(cancel)).toBeNull();
expect(onCancel).toHaveBeenCalledTimes(1);
});

it('reports the press around the tracker: the action at the begin and after each move, then the lift before its action runs', () => {
const log: string[] = [];
const {cancel} = mount({
options: {
onPressBegin: (_x, _y, action) => log.push(`begin ${nameOf(action)}`),
onPressMove: (_x, _y, action) => log.push(`move ${nameOf(action)}`),
onPressEnd: action => log.push(`end ${nameOf(action)}`),
onPressCancel: () => log.push('cancel'),
onActivate: action => log.push(`activate ${nameOf(action)}`),
},
});
const f = finger();
f.down(cancel, centerOf(CANCEL));
f.move(cancel, {x: 104, y: 124});
f.move(cancel, centerOf(DELETE));
f.up(cancel, centerOf(DELETE));
expect(log).toEqual([
'begin Cancel',
'move none',
'move Delete',
'end Delete',
'activate Delete',
]);
});

it('reports a press that a second finger ends as a cancel, and runs nothing (family:action-groups FR3)', () => {
const log: string[] = [];
const {cancel, del} = mount({
options: {
onPressBegin: (_x, _y, action) => log.push(`begin ${nameOf(action)}`),
onPressEnd: () => log.push('end'),
onPressCancel: () => log.push('cancel'),
onActivate: () => log.push('activate'),
},
});
const first = {id: nextTouchId++, ...centerOf(CANCEL)};
const second = {id: nextTouchId++, ...centerOf(DELETE)};
time += 1000;
touch('touchstart', cancel, first, time);
time += 30;
touch('touchstart', del, second, time, [first]);
time += 16;
touch('touchend', del, second, time, [first]);
time += 16;
touch('touchend', cancel, first, time);
expect(log).toEqual(['begin Cancel', 'cancel']);
});

it('reports a press that turning tracking off ends as a cancel, and runs nothing', () => {
const log: string[] = [];
const options: Partial<ActionGroupTrackingOptions> = {
paintsHighlight: false,
onPressBegin: (_x, _y, action) => log.push(`begin ${nameOf(action)}`),
onPressEnd: () => log.push('end'),
onPressCancel: () => log.push('cancel'),
onActivate: () => log.push('activate'),
};
const view = mount({options});
const f = finger();
f.down(view.cancel, centerOf(CANCEL));
view.rerender(
<Group
onCancel={view.onCancel}
onDelete={view.onDelete}
options={{...options, isEnabled: false}}
/>,
);
f.up(view.cancel, centerOf(CANCEL));
expect(log).toEqual(['begin Cancel', 'cancel']);
});
});

it('ends the gesture with nothing acting when the window loses focus (family:action-groups FR3)', () => {
const {cancel, onCancel} = mount();
const f = finger();
Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/hooks/useActionGroupTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* @position Internal hook, not exported from the hooks entry. A long press
* drives the shared action-group tracker (hooks/actionGroupTracker.ts) for
* a group that a held pointer slides across. AlertDialog and
* useGroupPressTracking consume it.
* useGroupPressTracking consume it, and Lab components through the internal
* touch entry.
*
* SYNC: When modified, update useActionGroupTracking.test.tsx
*/
Expand Down Expand Up @@ -71,6 +72,22 @@ export interface ActionGroupTrackingOptions {
manageStyle?: boolean;
/** Runs the action under the finger at the lift. @default its own click */
onActivate?: (item: HTMLElement) => void;
/**
* Paint the tracked action with the press attribute. Off, the group draws
* its own highlight. @default true
*/
paintsHighlight?: boolean;
/** A press began at viewport (x, y) on `action` (null: none). */
onPressBegin?: (x: number, y: number, action: HTMLElement | null) => void;
/** The press moved to viewport (x, y), now on `action` (null: none). */
onPressMove?: (x: number, y: number, action: HTMLElement | null) => void;
/** The lift, before its action runs, with the tracked action (null: none). */
onPressEnd?: (action: HTMLElement | null) => void;
/**
* The press ended with nothing acting: a cancel, a second finger, or
* tracking turned off.
Comment on lines +87 to +88

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the Effect cleanup (lines 132-139) also calls onPressCancel when the group unmounts mid-press. Naming that case keeps a caller from assuming it never runs during its own unmount:

   * The press ended with nothing acting: a cancel, a second finger,
   * tracking turned off, or the group unmounting.

*/
onPressCancel?: () => void;
}

const PRESS = UIKIT_PRESS;
Expand Down Expand Up @@ -185,6 +202,7 @@ export function useActionGroupTracking(
optionsRef.current.resolveTarget,
neighboursRef.current,
),
paintsHighlight: optionsRef.current.paintsHighlight,
onActivate: optionsRef.current.onActivate,
// The browser's own click for a press the group ran is taken, so the
// action runs once. The gesture host swallows it for a press it claims.
Expand All @@ -196,11 +214,16 @@ export function useActionGroupTracking(
// Synchronizes the tracker with tracking on and mounted. Turning tracking
// off, or unmounting, drops a tracked press and ends its release fades.
// The host reports no cancellation to a disabled recognizer, and the lift
// that runs an action often closes the owner before its fade ends.
// that runs an action often closes the owner before its fade ends. The
// group hears the dropped press as a cancel, so its own highlight goes out.
useEffect(
() => () => {
const wasTracking = tracker.isTracking;
tracker.cancel();
tracker.dispose();
if (wasTracking) {
optionsRef.current.onPressCancel?.();
}
},
[tracker, isEnabled],
);
Expand Down Expand Up @@ -238,14 +261,17 @@ export function useActionGroupTracking(
? measureNeighbours(ref.current, optionsRef.current)
: NO_TARGETS;
tracker.begin(x, y, {landed: landedRef.current});
optionsRef.current.onPressBegin?.(x, y, tracker.highlighted);
break;
}
case 'changed': {
const {x, y} = recognizer.location();
tracker.move(x, y);
optionsRef.current.onPressMove?.(x, y, tracker.highlighted);
break;
}
case 'ended': {
optionsRef.current.onPressEnd?.(tracker.highlighted);
const doc = ref.current?.ownerDocument;
const focusedBefore = doc?.activeElement ?? null;
const beganOn = tracker.beganOn;
Expand Down Expand Up @@ -278,6 +304,7 @@ export function useActionGroupTracking(
case 'cancelled':
neighboursRef.current = NO_TARGETS;
tracker.cancel();
optionsRef.current.onPressCancel?.();
break;
case 'possible':
case 'failed':
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/internal/touch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ describe('internal/touch', () => {
expect(entry.navigation).toBeDefined();
});

it('re-exports the action-group tracking hook by name', async () => {
const entry: Record<string, unknown> = await import('./touch');
// Identity alone passes when both sides are undefined.
expect(typeof entry.useActionGroupTracking).toBe('function');
const modules: Record<string, Record<string, unknown>> = {
useActionGroupTracking: await import('../hooks/useActionGroupTracking'),
};
for (const [name, module] of Object.entries(modules)) {
expect(entry[name], name).toBe(module[name]);
}
});

it('attaches no window or document listeners when it loads', async () => {
vi.resetModules();
const onWindow = vi.spyOn(window, 'addEventListener');
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/internal/touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ export {useInteractionModalityTracking} from '../utils/interactionModality';
// scroll is a finger's, and act when that scroll ends.
export {useFingerScroll} from '../gestures/useFingerScroll';
export type {UseFingerScrollOptions} from '../gestures/useFingerScroll';

// Lab's TabBar tracks its press with the shared action-group tracker.
export {useActionGroupTracking} from '../hooks/useActionGroupTracking';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the header's SYNC note also names scripts/touch-support-bundle.test.mjs. Its LAB_ONLY_NAMES list does not include this name yet, so the check that Lab's names stay off the root entry does not cover it:

  'TOUCH_QUERY',
  'useActionGroupTracking',
];

export type {ActionGroupTrackingOptions} from '../hooks/useActionGroupTracking';
Loading