Skip to content

Commit eb157e1

Browse files
committed
feat(core): an experimental List reorder subpath for rows that are not ListItems
The List experimental subpath (List/experimental in the core package's exports) carries core List's reorder session for a list whose rows hold their own content: useListReorder, ListReorderContext, ListReorderHandle and LIST_REORDER_FIXED_ROW_ATTRIBUTE. A row is any direct child of the list, whatever its tag, with its handle anywhere inside it; the handle takes a whole accessible name of its own. It follows spec:AST-017's experimental boundary: never re-exported from the package root or the List subpath, every exposed declaration (the session's controller and types included) marked experimental, and a stability: 'experimental' hook doc.
1 parent bbffaaf commit eb157e1

8 files changed

Lines changed: 365 additions & 16 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+
[experimental] `@astryxdesign/core/List/experimental` exports core `List`'s reorder session for lists whose rows are not `ListItem`s, outside the stable compatibility promise (it may change or be removed in a patch release): `useListReorder`, `ListReorderContext`, `ListReorderHandle` and `LIST_REORDER_FIXED_ROW_ATTRIBUTE`. Nothing is added to the package root or to the `List` subpath.
6+
7+
@vjeux

‎packages/core/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
"types": "./dist/theme/syntax/index.d.ts",
9292
"default": "./dist/theme/syntax/index.js"
9393
},
94+
"./List/experimental": {
95+
"source": "./src/List/experimental.ts",
96+
"types": "./dist/List/experimental.d.ts",
97+
"default": "./dist/List/experimental.js"
98+
},
9499
"./DateInput/tokens.stylex": {
95100
"source": "./src/DateInput/tokens.stylex.ts",
96101
"types": "./dist/DateInput/tokens.stylex.d.ts",

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* a button that drags its row (held still for a moment by a finger, at
1212
* once by a mouse), moves it with the arrow keys, and, activated without a
1313
* drag, opens its moves (a DropdownMenu anchored to the handle)
14-
* @position Internal to List; ListItem renders it at the end of each row of
15-
* a List with `onReorder`
14+
* @position ListItem renders it at the end of each row of a List with
15+
* `onReorder`; also exported, experimental, from the List experimental
16+
* subpath for rows that are not ListItems
1617
*
1718
* UIKit's reorder control (iOS 26.2, measured): the three gray lines at the
1819
* trailing edge of a row in edit mode. A finger held still on it for the
@@ -186,6 +187,7 @@ function useHandleGestures(
186187
);
187188
}
188189

190+
/** @experimental */
189191
export interface ListReorderHandleProps {
190192
/** The row's label: a string names the handle after it. */
191193
readonly label: ReactNode;
@@ -195,15 +197,22 @@ export interface ListReorderHandleProps {
195197
* (spec:AST-085).
196198
*/
197199
readonly isDisabled?: boolean;
200+
/**
201+
* The handle's whole accessible name, for a row whose label does not name
202+
* it (a record of fields: "Reorder guest 2"). Replaces "Reorder {label}".
203+
*/
204+
readonly 'aria-label'?: string;
198205
}
199206

200207
/**
201-
* The reorder handle of a List row. Renders nothing outside a List with
202-
* `onReorder`.
208+
* The reorder handle of a list row. Renders nothing outside a reorder
209+
* session (`ListReorderContext` with `useListReorder`'s value).
210+
* @experimental
203211
*/
204212
export function ListReorderHandle({
205213
label,
206214
isDisabled = false,
215+
'aria-label': ariaLabel,
207216
}: ListReorderHandleProps) {
208217
const context = use(ListReorderContext);
209218
const ref = useRef<HTMLButtonElement>(null);
@@ -225,7 +234,16 @@ export function ListReorderHandle({
225234
useHandleGestures(ref, controller, isDisabled, () => {
226235
draggedRef.current = true;
227236
});
228-
const named = typeof label === 'string' || typeof label === 'number';
237+
const labelText =
238+
typeof label === 'string' || typeof label === 'number'
239+
? String(label)
240+
: null;
241+
const name =
242+
ariaLabel ??
243+
(labelText == null
244+
? undefined
245+
: t('@astryx.list.reorderHandle', {label: labelText}));
246+
const named = name != null;
229247
// A row without a text label is named after its place, read from the DOM
230248
// after each commit (the row does not know its index). A passive effect:
231249
// the list's own ref is attached after its rows' layout effects run.
@@ -245,9 +263,6 @@ export function ListReorderHandle({
245263
}
246264
const rowMoves = () =>
247265
controller?.movesFor(controller.rowOf(ref.current)) ?? [];
248-
const name = named
249-
? t('@astryx.list.reorderHandle', {label: String(label)})
250-
: undefined;
251266
// Activating the handle — a tap, a click, a screen reader's double tap —
252267
// opens its moves: a way to reorder with a single pointer and no drag
253268
// (WCAG 2.5.7). The keys keep their own moves (Space picks up).
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file experimental.test.ts
5+
* @input The `@astryxdesign/core/List/experimental` entry, the package root,
6+
* the ordinary List subpath, the hooks and utils subpaths, and
7+
* package.json's exports map
8+
* @output Proof that the experimental List reorder surface is exported where
9+
* spec:AST-017 FR21 puts it and nowhere else, and that it reorders a list
10+
* whose rows are not ListItems
11+
* @position Testing for /packages/core/src/List/experimental.ts
12+
*/
13+
14+
import {useId, useRef, useState, type ReactNode} from 'react';
15+
import {describe, expect, it, vi} from 'vitest';
16+
import {render, screen} from '@testing-library/react';
17+
import userEvent from '@testing-library/user-event';
18+
import pkg from '../../package.json';
19+
import * as hooks from '../hooks';
20+
import * as root from '../index';
21+
import * as utils from '../utils';
22+
import * as list from './index';
23+
import * as reorder from './experimental';
24+
25+
const NAMES = [
26+
'useListReorder',
27+
'ListReorderContext',
28+
'ListReorderHandle',
29+
'LIST_REORDER_FIXED_ROW_ATTRIBUTE',
30+
] as const;
31+
32+
describe('@astryxdesign/core/List/experimental', () => {
33+
it('exports the reorder surface', () => {
34+
for (const name of NAMES) {
35+
expect(reorder[name], name).toBeDefined();
36+
}
37+
expect(reorder.LIST_REORDER_FIXED_ROW_ATTRIBUTE).toBe(
38+
'data-astryx-reorder-fixed',
39+
);
40+
});
41+
42+
it('is a subpath of its own, never re-exported from the root or an ordinary subpath', () => {
43+
expect(pkg.exports['./List/experimental']).toMatchObject({
44+
source: './src/List/experimental.ts',
45+
});
46+
for (const name of NAMES) {
47+
expect(name in root, `root re-exports ${name}`).toBe(false);
48+
expect(name in list, `List re-exports ${name}`).toBe(false);
49+
expect(name in hooks, `hooks re-exports ${name}`).toBe(false);
50+
expect(name in utils, `utils re-exports ${name}`).toBe(false);
51+
}
52+
});
53+
54+
it('reorders a list whose rows hold their own content', async () => {
55+
const onChange = vi.fn();
56+
function Records(): ReactNode {
57+
const listRef = useRef<HTMLOListElement>(null);
58+
const hintId = useId();
59+
const [items, setItems] = useState(['Alpha', 'Beta', 'Gamma']);
60+
const session = reorder.useListReorder(
61+
listRef,
62+
(fromIndex, toIndex) => {
63+
const next = [...items];
64+
const [moved] = next.splice(fromIndex, 1);
65+
next.splice(toIndex, 0, moved);
66+
onChange(next);
67+
setItems(next);
68+
},
69+
hintId,
70+
);
71+
return (
72+
<reorder.ListReorderContext value={session}>
73+
<ol ref={listRef}>
74+
{items.map(item => (
75+
<li
76+
key={item}
77+
{...(item === 'Gamma'
78+
? {[reorder.LIST_REORDER_FIXED_ROW_ATTRIBUTE]: ''}
79+
: null)}>
80+
<input aria-label={`${item} name`} defaultValue={item} />
81+
{item === 'Gamma' ? null : (
82+
<reorder.ListReorderHandle
83+
label={item}
84+
aria-label={item === 'Beta' ? 'Move record 2' : undefined}
85+
/>
86+
)}
87+
</li>
88+
))}
89+
</ol>
90+
<span id={hintId} hidden>
91+
Reorder instructions
92+
</span>
93+
</reorder.ListReorderContext>
94+
);
95+
}
96+
const user = userEvent.setup();
97+
render(<Records />);
98+
const handle = screen.getByRole('button', {name: 'Reorder Alpha'});
99+
expect(handle).toHaveAccessibleDescription('Reorder instructions');
100+
expect(screen.queryByRole('button', {name: 'Reorder Gamma'})).toBeNull();
101+
// A whole name replaces "Reorder {label}".
102+
expect(screen.getByRole('button', {name: 'Move record 2'})).toBeVisible();
103+
handle.focus();
104+
await user.keyboard('{ArrowDown}');
105+
expect(onChange).toHaveBeenLastCalledWith(['Beta', 'Alpha', 'Gamma']);
106+
// The fixed row bounds the others: Alpha cannot pass Gamma.
107+
await user.keyboard('{ArrowDown}');
108+
expect(onChange).toHaveBeenCalledTimes(1);
109+
expect(screen.getByRole('button', {name: 'Reorder Alpha'})).toHaveFocus();
110+
});
111+
112+
it('moves rows of any tag, with the handle deep inside each', async () => {
113+
const onReorder = vi.fn();
114+
function Cards(): ReactNode {
115+
const listRef = useRef<HTMLDivElement>(null);
116+
const hintId = useId();
117+
const session = reorder.useListReorder(listRef, onReorder, hintId);
118+
return (
119+
<reorder.ListReorderContext value={session}>
120+
<div ref={listRef} role="list">
121+
{['Alpha', 'Beta'].map(item => (
122+
<div key={item} role="listitem">
123+
<span>
124+
{item}
125+
<reorder.ListReorderHandle label={item} />
126+
</span>
127+
</div>
128+
))}
129+
</div>
130+
<span id={hintId} hidden>
131+
Reorder instructions
132+
</span>
133+
</reorder.ListReorderContext>
134+
);
135+
}
136+
const user = userEvent.setup();
137+
render(<Cards />);
138+
screen.getByRole('button', {name: 'Reorder Alpha'}).focus();
139+
await user.keyboard('{ArrowDown}');
140+
expect(onReorder).toHaveBeenCalledWith(0, 1);
141+
});
142+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
/**
6+
* @file experimental.ts
7+
* @input List's reorder session (useListReorder.ts) and its handle
8+
* (ListReorderHandle.tsx)
9+
* @output The `@astryxdesign/core/List/experimental` entry point: the reorder
10+
* session for a list whose rows are not ListItems
11+
* @position Experimental public subpath (spec:AST-017 FR21): outside the
12+
* stable compatibility promise, never re-exported from the package root or
13+
* the ordinary `List` subpath. Lab components whose rows hold their own
14+
* content (ListInput's records) reorder through it.
15+
*
16+
* Everything here is experimental: it may change or be removed in a patch
17+
* release. Every declaration it exposes carries `@experimental`.
18+
*
19+
* SYNC: When modified, update /packages/core/src/List/experimental.test.tsx and
20+
* /packages/core/src/List/useListReorder.doc.mjs.
21+
*/
22+
23+
export {
24+
FIXED_ROW_ATTRIBUTE as LIST_REORDER_FIXED_ROW_ATTRIBUTE,
25+
ListReorderContext,
26+
useListReorder,
27+
} from './useListReorder';
28+
export type {ListReorderContextValue} from './useListReorder';
29+
30+
export {ListReorderHandle} from './ListReorderHandle';
31+
export type {ListReorderHandleProps} from './ListReorderHandle';
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/** @type {import('@astryxdesign/cli/authoring').HookDoc} */
4+
export const docs = {
5+
name: 'useListReorder',
6+
displayName: 'useListReorder',
7+
stability: 'experimental',
8+
keywords: [
9+
'reorder',
10+
'drag',
11+
'sort',
12+
'list',
13+
'handle',
14+
'grip',
15+
'UIKit',
16+
'UITableView',
17+
'touch',
18+
'keyboard',
19+
'experimental',
20+
],
21+
params: [
22+
{
23+
name: 'listRef',
24+
type: 'RefObject<HTMLElement | null>',
25+
description:
26+
'The list element. Its direct children are the rows, in order, of any tag; a row\'s handle may sit anywhere inside it.',
27+
required: true,
28+
},
29+
{
30+
name: 'onReorder',
31+
type: '((fromIndex: number, toIndex: number) => void) | undefined',
32+
description:
33+
'Called once per move with the row\u2019s old and new index. Commit the new order in the same task, rows keyed by item. Without it the session is off and `ListReorderHandle` renders nothing.',
34+
required: true,
35+
},
36+
{
37+
name: 'hintId',
38+
type: 'string',
39+
description:
40+
'The id of an element holding the reorder instructions (the `@astryx.list.reorderHint` string); every handle is described by it.',
41+
required: true,
42+
},
43+
],
44+
returns: [
45+
{
46+
name: 'session',
47+
type: 'ListReorderContextValue | null',
48+
description:
49+
'The reorder session, opaque; null without `onReorder`. Provide it with `<ListReorderContext value={session}>` around the rows.',
50+
},
51+
],
52+
usage: {
53+
description:
54+
'Experimental: this hook and everything exported from `@astryxdesign/core/List/experimental` are outside the stable compatibility promise and may change or be removed in a patch release. The reorder session of core `List` (`onReorder`), for a list whose rows are not `ListItem`s, such as rows of form fields. Put a `ListReorderHandle` in each row (named \u201cReorder {label}\u201d, or by its `aria-label`; `isDisabled` keeps it focusable but inert); a row that keeps its place carries `LIST_REORDER_FIXED_ROW_ATTRIBUTE` and no handle, and no row moves past it. A finger held still on a handle for a moment picks its row up (a flick on it scrolls, a pinch zooms); the row follows the finger, the rows it passes slide out of its way on UIKit\u2019s table spring, the list autoscrolls near its edges, and the drop springs the row into its slot. A mouse drags at once, and rows take their places without motion. Activating a handle without a drag opens its moves (Move up, Move down, Move to top, Move to bottom). A focused handle\u2019s arrow keys, Home and End move its row, Space picks it up and Escape puts it back, and screen readers hear each move once it lands.',
55+
bestPractices: [
56+
{
57+
guidance: true,
58+
description:
59+
'Use core `List` with `onReorder` when the rows are `ListItem`s; reach for this hook only when they cannot be.',
60+
},
61+
{
62+
guidance: true,
63+
description:
64+
'Commit the new order synchronously in `onReorder`, keeping each row\u2019s key, so the drop settles from where the row is.',
65+
},
66+
{
67+
guidance: false,
68+
description:
69+
'Animate the rows\u2019 `translate` yourself while a session can move them, or wrap the list in a View Transition for the drop.',
70+
},
71+
{
72+
guidance: false,
73+
description:
74+
'Rely on this API from stable product code: it is experimental and may change in a patch release.',
75+
},
76+
],
77+
},
78+
relatedComponents: ['List'],
79+
importPath: '@astryxdesign/core/List/experimental',
80+
category: 'interaction',
81+
};
82+
83+
/** @type {import('@astryxdesign/cli/authoring').HookTranslationDoc} */
84+
export const docsDense = {
85+
description:
86+
'Experimental (may change or be removed in a patch release). core List\u2019s reorder session for rows that are not ListItems: ListReorderHandle per row, LIST_REORDER_FIXED_ROW_ATTRIBUTE for a row that keeps its place, ListReorderContext provides the session.',
87+
paramDescriptions: {
88+
listRef: 'the list; its direct children are the rows.',
89+
onReorder: '(fromIndex, toIndex); commit the new order in the same task.',
90+
hintId: 'id of the element holding @astryx.list.reorderHint.',
91+
},
92+
returnDescriptions: {
93+
session: 'opaque; null without onReorder; give it to ListReorderContext.',
94+
},
95+
usage: {
96+
description:
97+
'Experimental. UIKit edit-mode reorder for custom rows: hold then drag (touch), drag (mouse), arrows/Home/End/Space/Escape (keys).',
98+
bestPractices: [
99+
{
100+
guidance: true,
101+
description: 'Prefer List onReorder with ListItem rows.',
102+
},
103+
{
104+
guidance: false,
105+
description: 'Own animations or View Transitions on the rows.',
106+
},
107+
],
108+
},
109+
};

0 commit comments

Comments
 (0)