Skip to content

Commit a361da6

Browse files
committed
feat(List): a reorder handle's moves, and ListItem reorderLabel
Activating a reorderable List's handle without a drag (a tap, a click, a screen reader's double tap) opens Move up, Move down, Move to top and Move to bottom, each offered only when it moves the row, through the same moves as the handle's keys: a row can be reordered with a single pointer and no drag (WCAG 2.2 SC 2.5.7). A press that dragged the row opens nothing. The menu is a DropdownMenu anchored over the handle, mounted only while it is open (a mounted menu would lay out with every move of a dragged handle). ListItem's new reorderLabel names the handle of a row whose label is not plain text ("Reorder {reorderLabel}"); without it such a handle is named after the row's place.
1 parent d7e3771 commit a361da6

9 files changed

Lines changed: 312 additions & 59 deletions

File tree

‎.changeset/list-reorder-moves.md‎

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+
[feat] A reorderable `List`'s handle, activated without a drag (a tap, a click, a screen reader's double tap), opens its row's moves: Move up, Move down, Move to top and Move to bottom, each offered only when it moves the row, so a row can be reordered with a single pointer and no drag. `ListItem`'s new `reorderLabel` names the handle of a row whose label is not plain text.
6+
7+
@vjeux

‎apps/storybook/stories/List.stories.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,10 @@ export const Reorderable: Story = {
675675
'With `onReorder`, each row gets a reorder handle. On a touch ' +
676676
'screen, hold a handle for a moment, then drag: the rows you pass ' +
677677
'slide out of the way and the row settles into its new place on ' +
678-
'release. A mouse drags a handle at once; from the keyboard, the ' +
679-
'arrow keys move the focused handle’s row.',
678+
'release. A mouse drags a handle at once; a tap or click on a ' +
679+
'handle opens its moves (Move up, Move down, Move to top, Move to ' +
680+
'bottom); from the keyboard, the arrow keys move the focused ' +
681+
'handle’s row.',
680682
},
681683
},
682684
},

‎packages/core/locales/en.json‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,22 @@
491491
"defaultMessage": "Reorder cancelled, back to position {position, number} of {total, number}",
492492
"description": "Screen-reader announcement when a reorder in a List is cancelled and the row returns to where it was. `{position}` is that 1-based place, `{total}` the number of rows."
493493
},
494-
495-
496-
497-
494+
"@astryx.list.reorderMoveUp": {
495+
"defaultMessage": "Move up",
496+
"description": "Item in the menu a List row's reorder handle opens when activated (tapped or clicked) instead of dragged: moves the row up one place."
497+
},
498+
"@astryx.list.reorderMoveDown": {
499+
"defaultMessage": "Move down",
500+
"description": "Item in the menu a List row's reorder handle opens when activated instead of dragged: moves the row down one place."
501+
},
502+
"@astryx.list.reorderMoveToTop": {
503+
"defaultMessage": "Move to top",
504+
"description": "Item in the menu a List row's reorder handle opens when activated instead of dragged: moves the row to the first place it can take."
505+
},
506+
"@astryx.list.reorderMoveToBottom": {
507+
"defaultMessage": "Move to bottom",
508+
"description": "Item in the menu a List row's reorder handle opens when activated instead of dragged: moves the row to the last place it can take."
509+
},
498510
"@astryx.listInput.emptyTitle": {
499511
"defaultMessage": "No {itemName}s yet",
500512
"description": "EmptyState title shown inside a lab ListInput when its collection has no records. `{itemName}` is the consumer's singular noun for one record (e.g. \"guest\"); the source appends a literal \"s\" to pluralize it, which only works for regular English plurals. If your language cannot pluralize an interpolated noun this way, rephrase around `{itemName}` instead (e.g. \"No {itemName} added yet\")."

‎packages/core/src/List/List.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const docs = {
5757
name: 'onReorder',
5858
type: '(fromIndex: number, toIndex: number) => void',
5959
description:
60-
"Makes the rows reorderable: each row gets a reorder handle at its end, and a ListItem with isReorderDisabled keeps its place instead. Called with a row's index before and after a move; render the rows in the new order to apply it. On a touch screen, a finger held still on a handle for a moment picks up its row (a flick on it still scrolls): the row follows the finger, the rows it passes slide out of its way, the page scrolls near the edges, and on release the row settles into its new place on UIKit's springs, as an iOS table reorders. A mouse drags a handle at once, and the rows take their places without motion. From the keyboard, a handle's arrow keys move its row one place, Home and End to either end; Space picks it up and Escape puts it back. Every move is announced once the new order renders; a move you decline (by keeping the order) is announced as cancelled. A disabled row's handle stays focusable but moves nothing.",
60+
"Makes the rows reorderable: each row gets a reorder handle at its end, and a ListItem with isReorderDisabled keeps its place instead. Called with a row's index before and after a move; render the rows in the new order to apply it. On a touch screen, a finger held still on a handle for a moment picks up its row (a flick on it still scrolls): the row follows the finger, the rows it passes slide out of its way, the page scrolls near the edges, and on release the row settles into its new place on UIKit's springs, as an iOS table reorders. A mouse drags a handle at once, and the rows take their places without motion. Activating a handle without dragging it (a tap, a click, a screen reader's double tap) opens its moves: Move up, Move down, Move to top and Move to bottom, each offered only when it moves the row. From the keyboard, a handle's arrow keys move its row one place, Home and End to either end; Space picks it up and Escape puts it back. Every move is announced once the new order renders; a move you decline (by keeping the order) is announced as cancelled. A disabled row's handle stays focusable but moves nothing.",
6161
},
6262
{
6363
name: 'edge',

‎packages/core/src/List/ListItem.doc.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ export const docs = {
114114
"In a List with onReorder, this row has no reorder handle and keeps its place: other rows cannot be dragged or moved past it, so it splits the list into groups that each reorder on their own. Its end content stays in line with the other rows' (the handle's place is kept empty). No effect elsewhere.",
115115
default: 'false',
116116
},
117+
{
118+
name: 'reorderLabel',
119+
type: 'string',
120+
description:
121+
'In a List with onReorder, the text this row\'s reorder handle is named after ("Reorder {reorderLabel}"), for a row whose label is not plain text. Without it such a handle is named after the row\'s place.',
122+
},
117123
{
118124
name: 'swipeActions',
119125
type: '{leading?: ItemSwipeAction[]; trailing?: ItemSwipeAction[]}',

‎packages/core/src/List/ListItem.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ export interface ListItemProps extends BaseProps<HTMLLIElement> {
148148
*/
149149
isReorderDisabled?: boolean;
150150

151+
/**
152+
* In a List with `onReorder`, the text this row's reorder handle is named
153+
* after ("Reorder {reorderLabel}"), for a row whose `label` is not plain
154+
* text. Without it such a handle is named after the row's place.
155+
*/
156+
reorderLabel?: string;
157+
151158
/**
152159
* Swipe actions for touch, passed through to `Item` unchanged: the verbs a
153160
* sideways drag uncovers on each side. See `Item.swipeActions`; `List`
@@ -403,6 +410,7 @@ export function ListItem({
403410
isDisabled = false,
404411
isSelected = false,
405412
isReorderDisabled = false,
413+
reorderLabel,
406414
align,
407415
xstyle,
408416
className,
@@ -488,7 +496,10 @@ export function ListItem({
488496
hasReorderHandle ? (
489497
<>
490498
{endContent}
491-
<ListReorderHandle label={label} isDisabled={isDisabled} />
499+
<ListReorderHandle
500+
label={reorderLabel ?? label}
501+
isDisabled={isDisabled}
502+
/>
492503
</>
493504
) : isReorderable && isRenderable(endContent) ? (
494505
// A fixed row keeps its end content in the movable rows' column.

‎packages/core/src/List/ListReorderHandle.tsx‎

Lines changed: 146 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* press for a mouse, the translator and theme tokens
1010
* @output `ListReorderHandle`: the trailing handle of a reorderable List row,
1111
* a button that drags its row (held still for a moment by a finger, at
12-
* once by a mouse) and moves it with the arrow keys
12+
* once by a mouse), moves it with the arrow keys, and, activated without a
13+
* drag, opens its moves (a DropdownMenu anchored to the handle)
1314
* @position Internal to List; ListItem renders it at the end of each row of
1415
* a List with `onReorder`
1516
*
@@ -20,6 +21,10 @@
2021
* table instead, so the handle lets the browser pan vertically and
2122
* pinch-zoom (`pan-y pinch-zoom`) until the claim. A mouse drags at once.
2223
*
24+
* A drag is not everyone's: activating the handle without one — a tap, a
25+
* click, a screen reader's double tap — opens Move up, Move down, Move to top
26+
* and Move to bottom (WCAG 2.2 SC 2.5.7, a single pointer and no drag),
27+
* through the same moves as the keys. A press that dragged opens nothing.
2328
*
2429
* SYNC: When modified, update List/useListReorder.ts and List/ListItem.tsx.
2530
*/
@@ -28,6 +33,7 @@ import {
2833
use,
2934
useEffect,
3035
useRef,
36+
useState,
3137
type KeyboardEvent,
3238
type PointerEvent,
3339
type ReactNode,
@@ -39,12 +45,21 @@ import {focusOutlineProps} from '../utils/focusOutline.stylex';
3945
import {useTranslator} from '../i18n/useTranslator';
4046
import {LongPressGestureRecognizer} from '../gestures/LongPressGestureRecognizer';
4147
import {useGestureRecognizer} from '../gestures/useGestureRecognizer';
48+
import {DropdownMenu} from '../DropdownMenu/DropdownMenu';
4249
import {
4350
HANDLE_ATTRIBUTE,
4451
ListReorderContext,
4552
type ListReorderController,
53+
type ReorderMove,
4654
} from './useListReorder';
4755

56+
const MOVE_LABELS = {
57+
up: '@astryx.list.reorderMoveUp',
58+
down: '@astryx.list.reorderMoveDown',
59+
top: '@astryx.list.reorderMoveToTop',
60+
bottom: '@astryx.list.reorderMoveToBottom',
61+
} as const satisfies Record<ReorderMove['kind'], string>;
62+
4863
/** A touch target's side: the handle's width and height. */
4964
const HANDLE_SIZE = 44;
5065
/** The three lines' height: the handle's share of its row's height. */
@@ -106,6 +121,20 @@ const styles = stylex.create({
106121
width: HANDLE_SIZE,
107122
marginInlineEnd: -8,
108123
},
124+
// Over the handle at the row's end, out of the row's layout.
125+
menuHost: {
126+
position: 'absolute',
127+
insetBlock: 0,
128+
insetInlineEnd: 0,
129+
width: HANDLE_SIZE,
130+
pointerEvents: 'none',
131+
},
132+
menuAnchor: {
133+
display: 'block',
134+
width: '100%',
135+
height: '100%',
136+
outline: 'none',
137+
},
109138
});
110139

111140
/**
@@ -126,15 +155,20 @@ function useHandleGestures(
126155
ref: React.RefObject<HTMLButtonElement | null>,
127156
controller: ListReorderController | undefined,
128157
isDisabled: boolean,
158+
onDrag: () => void,
129159
): void {
130160
const isEnabled = controller != null && !isDisabled;
161+
const onDragRef = useRef(onDrag);
162+
useIsomorphicLayoutEffect(() => {
163+
onDragRef.current = onDrag;
164+
});
131165
// A finger: the reorder's touch layer, through the session.
132166
useIsomorphicLayoutEffect(() => {
133167
const handle = ref.current;
134168
if (!isEnabled || handle == null || controller == null) {
135169
return undefined;
136170
}
137-
return controller.attachTouch(handle);
171+
return controller.attachTouch(handle, () => onDragRef.current());
138172
}, [ref, controller, isEnabled]);
139173
// A mouse drags at once.
140174
useGestureRecognizer(
@@ -146,7 +180,8 @@ function useHandleGestures(
146180
allowableMovement: Number.POSITIVE_INFINITY,
147181
allowedPointerTypes: ['mouse'],
148182
}),
149-
recognizer => controller?.recognized(ref.current, recognizer),
183+
recognizer =>
184+
controller?.recognized(ref.current, recognizer, onDragRef.current),
150185
{manageStyle: false, isEnabled},
151186
);
152187
}
@@ -174,7 +209,22 @@ export function ListReorderHandle({
174209
const ref = useRef<HTMLButtonElement>(null);
175210
const t = useTranslator();
176211
const controller = context?.controller;
177-
useHandleGestures(ref, controller, isDisabled);
212+
// The moves menu exists only while it is open: a mounted menu anchors to
213+
// the handle, and a dragged handle would then lay out on every move.
214+
// Mounted closed, then opened, so the menu moves focus into its rows.
215+
const [menu, setMenu] = useState<'closed' | 'mounting' | 'open'>('closed');
216+
useEffect(() => {
217+
if (menu !== 'mounting') {
218+
return;
219+
}
220+
const frame = requestAnimationFrame(() => setMenu('open'));
221+
return () => cancelAnimationFrame(frame);
222+
}, [menu]);
223+
// A press that dragged the row does not also open the moves menu.
224+
const draggedRef = useRef(false);
225+
useHandleGestures(ref, controller, isDisabled, () => {
226+
draggedRef.current = true;
227+
});
178228
const named = typeof label === 'string' || typeof label === 'number';
179229
// A row without a text label is named after its place, read from the DOM
180230
// after each commit (the row does not know its index). A passive effect:
@@ -193,45 +243,102 @@ export function ListReorderHandle({
193243
if (context == null) {
194244
return null;
195245
}
246+
const rowMoves = () =>
247+
controller?.movesFor(controller.rowOf(ref.current)) ?? [];
196248
const name = named
197249
? t('@astryx.list.reorderHandle', {label: String(label)})
198250
: undefined;
251+
// Activating the handle — a tap, a click, a screen reader's double tap —
252+
// opens its moves: a way to reorder with a single pointer and no drag
253+
// (WCAG 2.5.7). The keys keep their own moves (Space picks up).
199254
return (
200-
<button
201-
ref={ref}
202-
type="button"
203-
{...{[HANDLE_ATTRIBUTE]: ''}}
204-
// The handle takes its own press, so the row does not highlight under
205-
// it (it paints nothing itself).
206-
data-astryx-pressable=""
207-
// It rings itself: the row's focus-within ring stays off for it.
208-
data-astryx-own-ring=""
209-
aria-label={name}
210-
aria-describedby={context.hintId}
211-
aria-disabled={isDisabled || undefined}
212-
onKeyDown={(event: KeyboardEvent<HTMLButtonElement>) => {
213-
if (!isDisabled) {
214-
controller?.keyDown(controller.rowOf(ref.current), event);
215-
}
216-
}}
217-
onBlur={() => controller?.blur(controller.rowOf(ref.current))}
218-
// The row's own pointer handling (its swipe) never sees the handle's
219-
// touches; the gesture system listens on the document, before this.
220-
onPointerDown={(event: PointerEvent<HTMLButtonElement>) => {
221-
event.stopPropagation();
222-
}}
223-
{...focusOutlineProps.focusVisible(styles.handle)}>
224-
<svg
225-
aria-hidden="true"
226-
viewBox="0 0 20 14"
227-
fill="none"
228-
stroke="currentColor"
229-
strokeWidth={2}
230-
strokeLinecap="round"
231-
{...stylex.props(styles.lines)}>
232-
<path d="M1 1h18M1 7h18M1 13h18" />
233-
</svg>
234-
</button>
255+
<>
256+
<button
257+
ref={ref}
258+
type="button"
259+
{...{[HANDLE_ATTRIBUTE]: ''}}
260+
// The handle takes its own press, so the row does not highlight under
261+
// it (it paints nothing itself).
262+
data-astryx-pressable=""
263+
// It rings itself: the row's focus-within ring stays off for it.
264+
data-astryx-own-ring=""
265+
aria-label={name}
266+
aria-describedby={context.hintId}
267+
aria-disabled={isDisabled || undefined}
268+
aria-haspopup="menu"
269+
aria-expanded={menu === 'open'}
270+
onClick={() => {
271+
if (draggedRef.current) {
272+
draggedRef.current = false;
273+
return;
274+
}
275+
if (!isDisabled && menu === 'closed' && rowMoves().length > 0) {
276+
setMenu('mounting');
277+
}
278+
}}
279+
onKeyDown={(event: KeyboardEvent<HTMLButtonElement>) => {
280+
if (!isDisabled) {
281+
controller?.keyDown(controller.rowOf(ref.current), event);
282+
}
283+
}}
284+
onBlur={() => controller?.blur(controller.rowOf(ref.current))}
285+
// The row's own pointer handling (its swipe) never sees the handle's
286+
// touches; the gesture system listens on the document, before this.
287+
onPointerDown={(event: PointerEvent<HTMLButtonElement>) => {
288+
draggedRef.current = false;
289+
event.stopPropagation();
290+
}}
291+
{...focusOutlineProps.focusVisible(styles.handle)}>
292+
<svg
293+
aria-hidden="true"
294+
viewBox="0 0 20 14"
295+
fill="none"
296+
stroke="currentColor"
297+
strokeWidth={2}
298+
strokeLinecap="round"
299+
{...stylex.props(styles.lines)}>
300+
<path d="M1 1h18M1 7h18M1 13h18" />
301+
</svg>
302+
</button>
303+
{menu !== 'closed' && (
304+
// The menu's events stay out of the row (its own press, its keys).
305+
<span
306+
onClick={event => event.stopPropagation()}
307+
onKeyDown={event => event.stopPropagation()}
308+
onPointerDown={event => event.stopPropagation()}
309+
{...stylex.props(styles.menuHost)}>
310+
<DropdownMenu
311+
isMenuOpen={menu === 'open'}
312+
onOpenChange={isOpen => {
313+
if (!isOpen) {
314+
setMenu('closed');
315+
}
316+
}}
317+
hasChevron={false}
318+
items={rowMoves().map(move => ({
319+
id: move.kind,
320+
label: t(MOVE_LABELS[move.kind]),
321+
onClick: () =>
322+
controller?.moveRow(controller.rowOf(ref.current), move.to),
323+
}))}
324+
// Anchored over the handle; named after it; focus coming back to
325+
// it goes on to the handle.
326+
renderTrigger={trigger => (
327+
<span
328+
ref={trigger.ref}
329+
id={trigger.id}
330+
tabIndex={-1}
331+
aria-label={
332+
name ?? ref.current?.getAttribute('aria-label') ?? undefined
333+
}
334+
onFocus={() => ref.current?.focus({preventScroll: true})}
335+
{...stylex.props(styles.menuAnchor)}
336+
/>
337+
)}
338+
/>
339+
</span>
340+
)}
341+
</>
235342
);
236343
}
237344

0 commit comments

Comments
 (0)