Skip to content

Commit d7e3771

Browse files
committed
feat(List): onReorder reorders rows the way an iOS table does
List takes onReorder(fromIndex, toIndex): each row gets a trailing reorder handle, and ListItem's isReorderDisabled keeps a row in place (its end content kept in line with the other rows'). A finger held still on a handle for the scroll view's content-touch delay picks the row up (a flick on it scrolls; two fingers are a pinch); the row follows the finger one to one, opaque, the rows it passes slide out of its way on the table spring, the page autoscrolls by whole points on a 20 ms clock near the edges, and the drop springs from rest into its slot on a spring scaled by the distance left. A dragged row stays inside its list. The motion is the same on every touch screen (a coarse pointer); a mouse drags at once and rows take their places without motion. The slide spring is read through its motion role (resolveMotion), once per drag. The handle's keys move the row, with Space and Escape to pick it up and put it back; on a touch screen a key move slides its rows. Every move is announced once the new order renders, and a declined one as kept. A focused handle shows one whole ring, and the handle fits a compact row. What runs only on a touch screen (the hold that claims a finger and the springs) is ListReorderTouch.ts, imported by the reorder session alone. The gesture host owns the document's touch listeners: the hold claims only a lone finger (fingersDown) and asks the host to lock text selection under the held finger (preventsTextSelection), so the handle keeps no selection or callout styles of its own and a mouse keeps the page's selection. The autoscroll runs on the shared edge autoscroll timer (utils/edgeAutoscroll.ts) on UIKit's 20 ms clock. The values are in the UIKit constants table (motion/uikit/reorder.ts), measured on iOS 26.2.
1 parent 8255c8b commit d7e3771

12 files changed

Lines changed: 2266 additions & 7 deletions

File tree

‎.changeset/list-on-reorder.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] `List` takes `onReorder(fromIndex, toIndex)`: each row gets a reorder handle at its end, and `ListItem`'s new `isReorderDisabled` keeps a row in place. On a touch screen a finger held still on a handle picks up its row the way an iOS table's reorder control does (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. A mouse drags a handle at once, and rows take their places without motion. From the keyboard, a handle's arrow keys, Home and End move its row, Space picks it up and Escape puts it back; every move is announced once it lands.
6+
7+
@vjeux

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,89 @@ export const AnimatedChangesSwipe: Story = {
630630
},
631631
},
632632
};
633+
634+
function moveRow<T>(rows: ReadonlyArray<T>, from: number, to: number): T[] {
635+
const next = [...rows];
636+
const [moved] = next.splice(from, 1);
637+
next.splice(to, 0, moved);
638+
return next;
639+
}
640+
641+
function ReorderDemo({
642+
initial,
643+
fixed,
644+
}: {
645+
initial: ReadonlyArray<string>;
646+
fixed?: string;
647+
}) {
648+
const [rows, setRows] = useState(initial);
649+
return (
650+
<List
651+
hasDividers
652+
hasAnimatedChanges
653+
onReorder={(from, to) => setRows(current => moveRow(current, from, to))}>
654+
{rows.map(label => (
655+
<ListItem
656+
key={label}
657+
label={label}
658+
isReorderDisabled={label === fixed}
659+
/>
660+
))}
661+
</List>
662+
);
663+
}
664+
665+
export const Reorderable: Story = {
666+
render: () => (
667+
<ReorderDemo
668+
initial={['Inbox', 'Sent', 'Drafts', 'Archive', 'Spam', 'Trash']}
669+
/>
670+
),
671+
parameters: {
672+
docs: {
673+
description: {
674+
story:
675+
'With `onReorder`, each row gets a reorder handle. On a touch ' +
676+
'screen, hold a handle for a moment, then drag: the rows you pass ' +
677+
'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.',
680+
},
681+
},
682+
},
683+
};
684+
685+
export const ReorderWithFixedRow: Story = {
686+
render: () => (
687+
<ReorderDemo
688+
initial={['Favorites', 'Inbox', 'Sent', 'All mail', 'Drafts', 'Spam']}
689+
fixed="All mail"
690+
/>
691+
),
692+
parameters: {
693+
docs: {
694+
description: {
695+
story:
696+
'A ListItem with `isReorderDisabled` has no handle and keeps its ' +
697+
'place: rows above and below it reorder within their own group.',
698+
},
699+
},
700+
},
701+
};
702+
703+
export const ReorderLongList: Story = {
704+
render: () => (
705+
<ReorderDemo
706+
initial={Array.from({length: 30}, (_, index) => `Item ${index + 1}`)}
707+
/>
708+
),
709+
parameters: {
710+
docs: {
711+
description: {
712+
story:
713+
'Dragging a row near the top or bottom of the screen scrolls the ' +
714+
'page, faster the further past the edge the row goes.',
715+
},
716+
},
717+
},
718+
};

‎packages/core/locales/en.json‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,38 @@
463463
"defaultMessage": "{count, number} {count, plural, one {other} other {others}}",
464464
"description": "Overflow element for the chat typing status when three or more people type: the first name is shown and the rest collapse into this phrase, which is then joined to the name by Intl.ListFormat. Renders as the last list item, e.g. \"Ana and 2 others\"."
465465
},
466+
"@astryx.list.reorderHandle": {
467+
"defaultMessage": "Reorder {label}",
468+
"description": "Accessible label on the handle at the end of a List row that drags the row to a new place or moves it with the arrow keys. `{label}` is the row's text (e.g. \"Reorder Inbox\")."
469+
},
470+
"@astryx.list.reorderHandleAt": {
471+
"defaultMessage": "Reorder item {position, number}",
472+
"description": "Accessible label on a List row's reorder handle when the row has no text label to name it after. `{position}` is the row's 1-based place in the list (e.g. \"Reorder item 3\")."
473+
},
474+
"@astryx.list.reorderHint": {
475+
"defaultMessage": "Drag to move this item, or press Arrow Up or Arrow Down. Press Space to pick it up and Escape to put it back.",
476+
"description": "Description read after the label of every reorder handle in a List: how to move a row by dragging or from the keyboard."
477+
},
478+
"@astryx.list.reorderPosition": {
479+
"defaultMessage": "Position {position, number} of {total, number}",
480+
"description": "Screen-reader announcement each time a row being reordered in a List reaches a new place. `{position}` is its 1-based place, `{total}` the number of rows."
481+
},
482+
"@astryx.list.reorderPickedUp": {
483+
"defaultMessage": "Picked up, position {position, number} of {total, number}. Press Arrow Up or Arrow Down to move, Space to drop, Escape to cancel.",
484+
"description": "Screen-reader announcement when a List row's reorder handle picks the row up from the keyboard. `{position}` is the row's 1-based place, `{total}` the number of rows."
485+
},
486+
"@astryx.list.reorderDropped": {
487+
"defaultMessage": "Dropped, position {position, number} of {total, number}",
488+
"description": "Screen-reader announcement when a row being reordered in a List is dropped. `{position}` is its new 1-based place, `{total}` the number of rows."
489+
},
490+
"@astryx.list.reorderCancelled": {
491+
"defaultMessage": "Reorder cancelled, back to position {position, number} of {total, number}",
492+
"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."
493+
},
494+
495+
496+
497+
466498
"@astryx.listInput.emptyTitle": {
467499
"defaultMessage": "No {itemName}s yet",
468500
"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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export const docs = {
5353
description: "With hasAnimatedChanges, page content below the list moves with its rows (a transient translate, cleared at rest) instead of jumping when the list changes height, each piece by what layout moved it. While it moves, that translate makes it the containing block of any position: fixed element inside it. Use 'stays' to keep it still when the app manages that area itself.",
5454
default: "'moves'",
5555
},
56+
{
57+
name: 'onReorder',
58+
type: '(fromIndex: number, toIndex: number) => void',
59+
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.",
61+
},
5662
{
5763
name: 'edge',
5864
type: "'padded' | 'flush' | 'outset' | 'bleed'",

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* types
1111
* @position Core implementation; consumed by index.ts, tested by List.test.tsx
1212
*
13+
* `hasAnimatedChanges` runs the update motion (utils/useListUpdateMotion.ts);
14+
* `onReorder` runs the reorder session (List/useListReorder.ts) after it.
15+
*
1316
* SYNC: When modified, update these files to stay in sync:
1417
* - /packages/core/src/List/List.doc.mjs
1518
* - /packages/core/src/List/List.test.tsx
@@ -36,6 +39,8 @@ import {
3639
} from '../utils/useListUpdateMotion';
3740
import {themeProps} from '../utils/themeProps';
3841
import {useDevWarning} from '../hooks/useDevWarning';
42+
import {useTranslator} from '../i18n/useTranslator';
43+
import {ListReorderContext, useListReorder} from './useListReorder';
3944

4045
export {
4146
type ListDensity,
@@ -79,6 +84,23 @@ export interface ListProps extends BaseProps<
7984
*/
8085
hasAnimatedChanges?: boolean;
8186

87+
/**
88+
* Makes the rows reorderable: each row gets a reorder handle at its end
89+
* (a ListItem with `isReorderDisabled` keeps its place instead). Called
90+
* with a row's index before and after a move; apply it by rendering the
91+
* rows in their new order. A drag that ends on its starting place, or a
92+
* move the order does not take, changes nothing.
93+
*
94+
* On a touch screen a finger held still on a handle for a moment picks up
95+
* its row (a flick on it still scrolls); the row follows the finger, the
96+
* rows it passes slide out of its way, the list scrolls near the edges, and
97+
* on release the row settles into its new place, the way an iOS table
98+
* reorders. A mouse drags a handle at once. From the keyboard, a handle's
99+
* arrow keys move its row one place, Home and End to the ends; Space picks
100+
* it up, and Escape puts it back.
101+
*/
102+
onReorder?: (fromIndex: number, toIndex: number) => void;
103+
82104
/**
83105
* With `hasAnimatedChanges`, page content below the list moves with its
84106
* rows (a transient `translate`, cleared at rest) instead of jumping when
@@ -244,6 +266,7 @@ const dynamicStyles = stylex.create({
244266
*/
245267
export function List({
246268
children,
269+
onReorder,
247270
hasAnimatedChanges = false,
248271
followingContent = 'moves',
249272
density = 'balanced',
@@ -261,8 +284,12 @@ export function List({
261284
...props
262285
}: ListProps) {
263286
const headerId = useId();
287+
const reorderHintId = useId();
288+
const t = useTranslator();
264289
const listRef = useRef<HTMLUListElement & HTMLOListElement>(null);
265290
useListUpdateMotion(listRef, hasAnimatedChanges, followingContent);
291+
// After the update motion, so a drop's commit is the reorder's.
292+
const reorderContext = useListReorder(listRef, onReorder, reorderHintId);
266293
const mergedRef = useMergedRefs(
267294
listRef,
268295
ref as React.Ref<HTMLUListElement & HTMLOListElement>,
@@ -329,18 +356,36 @@ export function List({
329356
</Tag>
330357
);
331358

359+
// The handles' instructions, read by each handle's description.
360+
const reorderHint =
361+
reorderContext == null ? null : (
362+
<span id={reorderHintId} hidden>
363+
{t('@astryx.list.reorderHint')}
364+
</span>
365+
);
366+
332367
if (header == null) {
333-
return <ListContext value={contextValue}>{listElement}</ListContext>;
368+
return (
369+
<ListContext value={contextValue}>
370+
<ListReorderContext value={reorderContext}>
371+
{listElement}
372+
{reorderHint}
373+
</ListReorderContext>
374+
</ListContext>
375+
);
334376
}
335377

336378
return (
337379
<ListContext value={contextValue}>
338-
<div {...stylex.props(styles.root)}>
339-
<div id={headerId} {...stylex.props(styles.header)}>
340-
{header}
380+
<ListReorderContext value={reorderContext}>
381+
<div {...stylex.props(styles.root)}>
382+
<div id={headerId} {...stylex.props(styles.header)}>
383+
{header}
384+
</div>
385+
{listElement}
386+
{reorderHint}
341387
</div>
342-
{listElement}
343-
</div>
388+
</ListReorderContext>
344389
</ListContext>
345390
);
346391
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ export const docs = {
107107
description: 'Selected state; marks the item as the current one with aria-current="true" (a list item cannot carry aria-selected).',
108108
default: 'false',
109109
},
110+
{
111+
name: 'isReorderDisabled',
112+
type: 'boolean',
113+
description:
114+
"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.",
115+
default: 'false',
116+
},
110117
{
111118
name: 'swipeActions',
112119
type: '{leading?: ItemSwipeAction[]; trailing?: ItemSwipeAction[]}',
@@ -223,6 +230,8 @@ export const docsDense = {
223230
propDescriptions: {
224231
align:
225232
"Start/end content alignment, passed to Item: 'center' (default), 'start', or 'first-line' (centered on the label's first line).",
233+
isReorderDisabled:
234+
'In a List with onReorder: no reorder handle; the row keeps its place and bounds the rows around it.',
226235
label:
227236
'Primary content. String auto-truncates; ReactNode renders as given.',
228237
description: 'Secondary text below label.',

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* @output Exports ListItem component, ListItemProps type
1515
* @position Core implementation; consumed by List, index.ts, tested by List.test.tsx
1616
*
17+
* In a List with `onReorder` (List/useListReorder.ts), each row ends with a
18+
* ListReorderHandle, or keeps its place with `isReorderDisabled`.
19+
*
1720
* Composes Item for the shared start content + label + description + end content layout
1821
* and the invisible button/anchor interactive pattern.
1922
*
@@ -41,9 +44,12 @@ import {isRenderable, mergeProps} from '../utils';
4144
import {Item} from '../Item';
4245
import type {ItemAlign, ItemSwipeActions, ItemSwipeBehavior} from '../Item';
4346
import {themeProps} from '../utils/themeProps';
47+
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
4448
import {listRowStyles} from './listRow.stylex';
4549
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
4650
import {useMergedRefs} from '../hooks/useMergedRefs';
51+
import {ListReorderHandle, ListReorderSlot} from './ListReorderHandle';
52+
import {FIXED_ROW_ATTRIBUTE, ListReorderContext} from './useListReorder';
4753

4854
// =============================================================================
4955
// Types
@@ -134,6 +140,14 @@ export interface ListItemProps extends BaseProps<HTMLLIElement> {
134140
*/
135141
isSelected?: boolean;
136142

143+
/**
144+
* In a List with `onReorder`, this row has no reorder handle and keeps its
145+
* place: other rows cannot be dragged or moved past it, so it splits the
146+
* list into groups that each reorder on their own. No effect elsewhere.
147+
* @default false
148+
*/
149+
isReorderDisabled?: boolean;
150+
137151
/**
138152
* Swipe actions for touch, passed through to `Item` unchanged: the verbs a
139153
* sideways drag uncovers on each side. See `Item.swipeActions`; `List`
@@ -388,6 +402,7 @@ export function ListItem({
388402
rel,
389403
isDisabled = false,
390404
isSelected = false,
405+
isReorderDisabled = false,
391406
align,
392407
xstyle,
393408
className,
@@ -417,6 +432,9 @@ export function ListItem({
417432
}
418433
};
419434
}, []);
435+
// In a List with `onReorder`: a trailing handle, or a row that stays put.
436+
const isReorderable = use(ListReorderContext) != null;
437+
const hasReorderHandle = isReorderable && !isReorderDisabled;
420438
const density = ctx?.density ?? 'balanced';
421439
const hasDividers = ctx?.hasDividers ?? false;
422440
const edge = ctx?.edge ?? 'padded';
@@ -466,7 +484,22 @@ export function ListItem({
466484
startContent={startContent}
467485
label={label}
468486
description={description}
469-
endContent={endContent}
487+
endContent={
488+
hasReorderHandle ? (
489+
<>
490+
{endContent}
491+
<ListReorderHandle label={label} isDisabled={isDisabled} />
492+
</>
493+
) : isReorderable && isRenderable(endContent) ? (
494+
// A fixed row keeps its end content in the movable rows' column.
495+
<>
496+
{endContent}
497+
<ListReorderSlot />
498+
</>
499+
) : (
500+
endContent
501+
)
502+
}
470503
onClick={onClick}
471504
interactiveRef={interactiveRef}
472505
href={href}
@@ -494,10 +527,16 @@ export function ListItem({
494527
edge === 'flush' && styles.edgeFlush,
495528
edge === 'outset' && styles.edgeOutset,
496529
edge === 'bleed' && styles.edgeBleed,
530+
// A focused handle rings itself; the row's focus-within ring stays
531+
// off for it (two rings, and the list clips the row's sides).
532+
hasReorderHandle && focusOutlineStyles.focusWithinExceptOwnRing,
497533
xstyle,
498534
] as StyleXStyles
499535
}
500536
{...mergeProps(themeProps('list-item'), {className, style})}
537+
{...(isReorderable && isReorderDisabled
538+
? {[FIXED_ROW_ATTRIBUTE]: ''}
539+
: null)}
501540
{...restProps}
502541
/>
503542
);

0 commit comments

Comments
 (0)