Skip to content

Commit b57e894

Browse files
feat(core): a Lab collection that runs its own reorder mode shares List's reorder parts
A new internal entry, @astryxdesign/core/internal/reorder-mode, gives Lab the parts of List's reorder: the mode hint, which now offers Up and Down or Left and Right, the strings, the pointer input with a finger's hold, the edge autoscroll, the drop bounds and the preview copy. internal/reorder gains mountReorderStarter, so a reorderable object's start(key) reaches such a collection. List's session uses the same parts, so List does not change. The catalog gains Move left and Move right.
1 parent 4595ece commit b57e894

16 files changed

Lines changed: 1113 additions & 483 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+
[chore] Internal: a new internal entry, `@astryxdesign/core/internal/reorder-mode`, reserved for Astryx's own packages, gives a collection that runs its own reorder mode the parts a List's uses: its mode hint, along either axis, its strings, its pointer input with a finger's hold, its edge autoscroll, scroll watch and bounds, the preview copy, and the text it names items by. The internal reorder entry gains a way for the object's `start(key)` to reach such a collection. The catalog gains Move left and Move right for the hint's inline moves. The entry is not part of the public API. Apps see no change.
6+
7+
@humbertovirtudes

‎docs/specs/components/List/List.spec.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ verified_by:
2020
packages/core/src/List/listLayoutUpdates.test.tsx,
2121
packages/core/src/List/__tests__/ListUpdates.interaction.browser.spec.ts,
2222
packages/core/src/List/__tests__/ListReorder.test.tsx,
23+
packages/core/src/internal/reorderShared.test.tsx,
2324
packages/core/src/List/__tests__/ListReorderLayout.test.tsx,
2425
packages/core/src/List/__tests__/ListReorderTouch.interaction.browser.spec.ts,
2526
packages/core/src/List/__tests__/ListReorderPointer.interaction.browser.spec.ts,
@@ -116,6 +117,28 @@ Consumer migration instructions belong in consumer docs and release notes.
116117
through `@astryxdesign/core/internal/reorder`. It tells whether an element
117118
is a reorder handle, so a Lab collection that puts the handle beside its
118119
own controls tells them apart: ListInput's field focus skips the handle.
120+
- The parts of List's reorder that a Lab collection running its own reorder
121+
mode shares, through the internal entry
122+
`@astryxdesign/core/internal/reorder-mode` (`internal/reorder-mode.ts`):
123+
- the mode's hint, `ReorderModeHint` (`List/ReorderModeHint.tsx`), with
124+
Move up and Move down on the block axis or Move left and Move right on
125+
the inline axis;
126+
- its strings, `reorderMessagesFrom` (`List/reorderMessages.ts`);
127+
- its pointer input, `attachReorderMouse` and `attachReorderHold`
128+
(`List/reorderPointer.ts`): a mouse, or a pen where the primary pointer
129+
is fine, drags at once; a finger, or a pen where it is coarse, after the
130+
hold;
131+
- its edge autoscroll, `reorderAutoscroll` (`List/reorderAutoscroll.ts`)
132+
with `autoscrollStep`, and its watch of the scroller,
133+
`watchReorderScroll` (`List/reorderScrollWatch.ts`), which folds a
134+
scroll the drag did not cause into the drag in a later frame;
135+
- its drop bounds, `movableRange`, and its preview copy, `inertClone` and
136+
`paintedBackground`;
137+
- the text it names an item's handle by, `getNodeText`.
138+
139+
`mountReorderStarter`, in `@astryxdesign/core/internal/reorder`, lets the
140+
object's `start(key)` reach that collection (`spec:AST-103` FR4a).
141+
119142
- `@astryxdesign/core/internal/reorderHandle.stylex` holds the reorder
120143
handle's grip and reach sizes (`grip`, `targetReach`, `touchReach`), which
121144
List, ListInput and TransferList share. It is internal, like the other

‎packages/core/locales/en.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@
651651
"defaultMessage": "Move down",
652652
"description": "Accessible label of the down-arrow button in the hint a List row's reorder mode shows: it moves the row one place down, as the Down arrow key does."
653653
},
654+
"@astryx.list.reorderMoveLeft": {
655+
"defaultMessage": "Move left",
656+
"description": "Accessible label of the left-arrow button in the hint that reorder mode shows for items in a row, such as a Table's columns: it moves the item one place to the left, as the Left arrow key does."
657+
},
658+
"@astryx.list.reorderMoveRight": {
659+
"defaultMessage": "Move right",
660+
"description": "Accessible label of the right-arrow button in the hint that reorder mode shows for items in a row, such as a Table's columns: it moves the item one place to the right, as the Right arrow key does."
661+
},
654662
"@astryx.list.reorderDone": {
655663
"defaultMessage": "Done",
656664
"description": "Button in the hint a List row's reorder mode shows: it ends the mode and keeps the moves, as Escape does."

‎packages/core/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,11 @@
849849
"types": "./dist/internal/reorder.d.ts",
850850
"default": "./dist/internal/reorder.js"
851851
},
852+
"./internal/reorder-mode": {
853+
"source": "./src/internal/reorder-mode.ts",
854+
"types": "./dist/internal/reorder-mode.d.ts",
855+
"default": "./dist/internal/reorder-mode.js"
856+
},
852857
"./internal/reorderHandle.stylex": {
853858
"source": "./src/internal/reorderHandle.stylex.ts",
854859
"types": "./dist/internal/reorderHandle.stylex.d.ts",
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
/**
6+
* @file ReorderModeHint.tsx
7+
* @input The handle of the item in reorder mode, its collection's axis, and
8+
* Core's keyboard hint layer (hooks/anchoredHint.tsx)
9+
* @output `ReorderModeHint`: reorder mode's hint, anchored below the start of
10+
* the handle. Its glyphs and Done are buttons that do what their keys do
11+
* @position Rendered by List's reorder session (List/listReorderEngine.tsx),
12+
* and by Lab collections through `@astryxdesign/core/internal/reorder-mode`
13+
*/
14+
15+
import {useRef, type ReactNode} from 'react';
16+
import * as stylex from '@stylexjs/stylex';
17+
import {anchoredHintStyles, renderAnchoredHint} from '../hooks/anchoredHint';
18+
import {useCommittedRef} from '../hooks/useCommittedRef';
19+
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
20+
import english from '../i18n/generated-english/List/ReorderModeHint.generated';
21+
import {
22+
registerEnglishFallback,
23+
useComponentTranslator,
24+
} from '../i18n/useComponentTranslator';
25+
import {touchHitAreaStyles} from '../internal/touchHitArea.stylex';
26+
import {Kbd} from '../Kbd/Kbd';
27+
import {useLayer} from '../Layer/useLayer';
28+
import {useLayerDismissal} from '../Layer/useLayerDismissal';
29+
import {spacingVars} from '../theme/tokens.stylex';
30+
import {focusOutlineProps} from '../utils/focusOutline.stylex';
31+
import {useTouchHitArea} from '../utils/touchHitArea';
32+
33+
registerEnglishFallback(english);
34+
35+
/** A move reorder mode's hint offers: one place in a direction on screen. */
36+
export type ReorderHintMove = 'up' | 'down' | 'left' | 'right';
37+
38+
/** What `ReorderModeHint` takes. */
39+
export interface ReorderModeHintProps {
40+
/** The handle of the item in reorder mode, or null outside the mode. */
41+
readonly target: HTMLElement | null;
42+
/**
43+
* The axis the collection orders along: `block` offers Up and Down,
44+
* `inline` offers Left and Right.
45+
*/
46+
readonly axis: 'block' | 'inline';
47+
/** Does what the move's arrow key does. */
48+
readonly onMove: (move: ReorderHintMove) => void;
49+
/** Ends reorder mode, as Escape does. */
50+
readonly onDone: () => void;
51+
/** Receives the hint's element, which holds its buttons. */
52+
readonly onHintElement?: (element: HTMLElement | null) => void;
53+
}
54+
55+
const styles = stylex.create({
56+
// A glyph or Done: the hint's text, with nothing of a button's own, the
57+
// same size under every pointer. A finger's 44 px target is the shared
58+
// touch hit layer, which stops short of a neighbouring target.
59+
button: {
60+
position: 'relative',
61+
display: 'inline-flex',
62+
alignItems: 'center',
63+
justifyContent: 'center',
64+
margin: 0,
65+
padding: 0,
66+
borderWidth: 0,
67+
borderRadius: 4,
68+
backgroundColor: 'transparent',
69+
color: 'inherit',
70+
fontFamily: 'inherit',
71+
fontSize: 'inherit',
72+
lineHeight: 'inherit',
73+
cursor: {
74+
default: 'pointer',
75+
':is(:disabled,[aria-disabled="true"])': 'default',
76+
},
77+
},
78+
done: {
79+
paddingInline: spacingVars['--spacing-1'],
80+
},
81+
});
82+
83+
/**
84+
* One of the hint's buttons: it never takes the focus from the handle (a
85+
* press does not move it), and does what its key does.
86+
*/
87+
function HintButton({
88+
label,
89+
onPress,
90+
isText = false,
91+
children,
92+
}: {
93+
label?: string;
94+
onPress: () => void;
95+
isText?: boolean;
96+
children: ReactNode;
97+
}) {
98+
// A finger's target: the shared touch hit area grows the button's hit
99+
// layer where the primary pointer is coarse.
100+
const hitAreaRef = useTouchHitArea(true);
101+
return (
102+
<button
103+
ref={hitAreaRef}
104+
type="button"
105+
tabIndex={-1}
106+
aria-label={label}
107+
onPointerDown={event => event.preventDefault()}
108+
onMouseDown={event => event.preventDefault()}
109+
onClick={onPress}
110+
{...focusOutlineProps.focusVisible([
111+
styles.button,
112+
isText && styles.done,
113+
touchHitAreaStyles.layer,
114+
])}>
115+
{children}
116+
</button>
117+
);
118+
}
119+
120+
/**
121+
* Reorder mode's hint. It is anchored below the start of the handle whose
122+
* item is in the mode, in Core's keyboard hint's layer and theme roles. It
123+
* shows the moves for the collection's axis and Done until the mode ends.
124+
* Each move is a button that does what its key does, and the focus stays on
125+
* the handle. Done ends the mode as Escape does. So a pointer or a finger
126+
* can reorder by tapping the handle, the moves, then Done.
127+
*/
128+
export function ReorderModeHint({
129+
target,
130+
axis,
131+
onMove,
132+
onDone,
133+
onHintElement,
134+
}: ReorderModeHintProps): ReactNode {
135+
const t = useComponentTranslator();
136+
const layer = useLayer({mode: 'context'});
137+
// The latest layer handle, for the effect below, which re-runs only when
138+
// the mode's item changes.
139+
const layerRef = useCommittedRef(layer);
140+
const hintRef = useRef<HTMLElement | null>(null);
141+
// External system: the hint's popover in the top layer (useLayer),
142+
// anchored to the handle of the item in reorder mode. Shown while that
143+
// item is in the mode; hidden and unanchored when the mode ends or moves on.
144+
useIsomorphicLayoutEffect(() => {
145+
if (target == null) {
146+
return undefined;
147+
}
148+
const shown = layerRef.current;
149+
shown.ref(target);
150+
shown.show();
151+
return () => {
152+
shown.hide();
153+
shown.ref(null);
154+
};
155+
}, [target]);
156+
// The shared dismissal stack. While the hint is up, an Escape that reaches
157+
// the stack, with the focus in the hint, ends reorder mode. A layer below
158+
// it, such as a Dialog, stays open. The handle claims its own Escape first.
159+
useLayerDismissal({
160+
isActive: target != null,
161+
onDismiss: onDone,
162+
getContainer: () => hintRef.current,
163+
});
164+
const [back, forward]: ReadonlyArray<ReorderHintMove> =
165+
axis === 'block' ? ['up', 'down'] : ['left', 'right'];
166+
return renderAnchoredHint(
167+
layer,
168+
target == null ? null : (
169+
<span
170+
ref={element => {
171+
hintRef.current = element;
172+
onHintElement?.(element);
173+
}}
174+
{...stylex.props(anchoredHintStyles.keys)}>
175+
<HintButton
176+
label={
177+
axis === 'block'
178+
? t('@astryx.list.reorderMoveUp')
179+
: t('@astryx.list.reorderMoveLeft')
180+
}
181+
onPress={() => onMove(back)}>
182+
<Kbd keys={back} aria-hidden />
183+
</HintButton>
184+
<HintButton
185+
label={
186+
axis === 'block'
187+
? t('@astryx.list.reorderMoveDown')
188+
: t('@astryx.list.reorderMoveRight')
189+
}
190+
onPress={() => onMove(forward)}>
191+
<Kbd keys={forward} aria-hidden />
192+
</HintButton>
193+
<HintButton isText onPress={onDone}>
194+
{t('@astryx.list.reorderDone')}
195+
</HintButton>
196+
</span>
197+
),
198+
{isInteractive: true},
199+
);
200+
}

‎packages/core/src/List/listReorderContext.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ export interface ReorderRows {
212212
holds(element: Element, container: HTMLElement): boolean;
213213
}
214214

215+
/** A collection that starts reorder mode for one of its items, by key. */
216+
export interface ReorderStarter {
217+
/** Starts reorder mode for the item with `key`; returns whether it did. */
218+
startByKey(key: Key): boolean;
219+
}
220+
215221
/** The machinery a reorderable object carries. */
216222
export interface ListReorderEngine {
217223
/**
@@ -230,6 +236,14 @@ export interface ListReorderEngine {
230236
* one mounted last if several.
231237
*/
232238
start(options: ListReorderOptions, key: Key): void;
239+
/**
240+
* Lets `start` reach a collection that runs its own reorder mode, mounted
241+
* with the object with `options`. Returns the removal.
242+
*/
243+
mountStarter(
244+
options: ListReorderOptions,
245+
starter: ReorderStarter,
246+
): () => void;
233247
}
234248

235249
/** What a reorderable object holds (reorderable/createReorderable.ts). */

0 commit comments

Comments
 (0)