Skip to content

Commit f564f48

Browse files
committed
feat(List): editingControl="select", a UIKit table's multiple selection during editing
With allowsMultipleSelectionDuringEditing, a UIKit table in edit mode shows a selection circle in each row's leading column, filled when the row is selected. On iOS 26.2 the control (25.67 pt) sits at x 19.17, its centre 12 pt past a plain table's text margin, and slides in from 44 pt away while fading from 0 to 1 on the edit-mode spring, the table update spring; on leaving it slides back out and fades. List gains editingControl (proposed public API, pending owner review), with 'select' the first value. In edit mode each row shows the theme's checkbox indicator (useIndicator('checkbox')), checked when the row is isSelected, in the indent's leading column at list.metrics.editingControlCenter; a theme can map the indicator to a circle. It is decorative: the row keeps Item's selected semantics and its one tab stop, and activating the row calls its onClick as before, for the app to toggle the selection. Under a finger the edit-mode touch layer slides the control in from list.metrics.editingControlTravel and fades it in; on leaving the room goes at once (the content springs back) while the control slides out from where it stood, and the list unmounts it once it has arrived. A toggle mid-transition turns it around; reduced motion takes the animator's own policy; a mouse shows and hides it at once.
1 parent c1582e6 commit f564f48

12 files changed

Lines changed: 846 additions & 64 deletions

File tree

‎.changeset/list-editing-select.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` gains `editingControl="select"`, a UIKit table's multiple selection during editing: in edit mode each row shows the theme's checkbox indicator in its leading column, checked when the row is `isSelected` (a theme can map the indicator to a circle). It is decorative: the row keeps its selected semantics and its one tab stop, and activating the row calls its `onClick` as before, for the app to toggle the row's selection. Under a finger (iOS and Android alike) the control slides in with the rows and fades in on UIKit's table spring, and slides out on leaving; with reduced motion on it cross-fades; with a mouse it comes and goes at once.
6+
7+
@vjeux

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,69 @@ export const Editing: Story = {
282282
},
283283
};
284284

285+
function EditingSelectionDemo() {
286+
const [isEditing, setIsEditing] = useState(false);
287+
const [selected, setSelected] = useState<ReadonlySet<string>>(
288+
() => new Set(),
289+
);
290+
const toggle = (label: string) =>
291+
setSelected(current => {
292+
const next = new Set(current);
293+
if (next.has(label)) {
294+
next.delete(label);
295+
} else {
296+
next.add(label);
297+
}
298+
return next;
299+
});
300+
return (
301+
<VStack gap={2}>
302+
<div>
303+
<Button
304+
label={isEditing ? 'Done' : 'Edit'}
305+
onClick={() => {
306+
// Done ends the selection, as an app's edit mode usually does.
307+
if (isEditing) {
308+
setSelected(new Set());
309+
}
310+
setIsEditing(!isEditing);
311+
}}
312+
/>
313+
</div>
314+
<List isEditing={isEditing} editingControl="select" hasDividers>
315+
{['Inbox', 'Sent', 'Drafts', 'Archive'].map(label => (
316+
<ListItem
317+
key={label}
318+
label={label}
319+
isSelected={selected.has(label)}
320+
onClick={() => {
321+
if (isEditing) {
322+
toggle(label);
323+
}
324+
}}
325+
endContent={<Icon icon={ChevronRightIcon} />}
326+
/>
327+
))}
328+
</List>
329+
</VStack>
330+
);
331+
}
332+
333+
export const EditingSelection: Story = {
334+
render: () => <EditingSelectionDemo />,
335+
parameters: {
336+
docs: {
337+
description: {
338+
story:
339+
'With `editingControl="select"`, edit mode shows the theme’s ' +
340+
'checkbox indicator in each row, checked when the row is selected; ' +
341+
'tapping a row toggles it here. Under a finger the controls slide ' +
342+
'in with the rows and fade in, and slide out on Done.',
343+
},
344+
},
345+
},
346+
};
347+
285348
export const Links: Story = {
286349
render: args => (
287350
<List {...args}>

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

Lines changed: 199 additions & 46 deletions
Large diffs are not rendered by default.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ describe('List', () => {
640640
`${list.metrics.controlSize}px`,
641641
);
642642
expect(listRowConsts.editingIndent).toBe(`${list.metrics.editingIndent}px`);
643+
expect(listRowConsts.editingControlCenter).toBe(
644+
`${list.metrics.editingControlCenter}px`,
645+
);
643646
});
644647

645648
it('gives rows UIKit’s table heights under a finger only, on every touch platform', () => {

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ListContext,
2727
type ListDensity,
2828
type ListEdge,
29+
type ListEditingControl,
2930
type ListMarkerStyle,
3031
} from './ListContext';
3132
import {mergeProps} from '../utils';
@@ -104,6 +105,18 @@ export interface ListProps extends BaseProps<
104105
*/
105106
isEditing?: boolean;
106107

108+
/**
109+
* The control edit mode shows in each row's leading column. `'select'`:
110+
* UIKit's multiple selection during editing, the theme's checkbox
111+
* indicator (a theme can map it to a circle), checked when the row is
112+
* `isSelected`. It is decorative: the row keeps its selected semantics
113+
* and its one tab stop, and activating the row calls its `onClick` as
114+
* before, for the app to toggle the row's selection. Under a finger it
115+
* slides in with the rows and fades in, and slides out on leaving.
116+
* Proposed public API, pending owner review.
117+
*/
118+
editingControl?: ListEditingControl;
119+
107120
/**
108121
* Whether to show dividers between list items. Under a finger, a touch
109122
* press hides the dividers on either side of the pressed row until the
@@ -261,6 +274,7 @@ export function List({
261274
hasAnimatedChanges = false,
262275
followingContent = 'moves',
263276
isEditing = false,
277+
editingControl,
264278
density = 'balanced',
265279
hasDividers = false,
266280
edge: edgeProp,
@@ -279,18 +293,23 @@ export function List({
279293
const listRef = useRef<HTMLUListElement & HTMLOListElement>(null);
280294
useListUpdateMotion(listRef, hasAnimatedChanges, followingContent);
281295
// Edit mode's trailing content stays while it fades out under a finger,
282-
// and leaves once faded; leaving edit mode brings it back at once.
296+
// and leaves once faded; leaving edit mode brings it back at once. The
297+
// editing control, the other way round, stays while it slides out.
283298
const editingAnimates = useTouchPresentation().isTouch;
284299
const [trailingFading, setTrailingFading] = useState(false);
300+
const [controlLeaving, setControlLeaving] = useState(false);
285301
const [editingSeen, setEditingSeen] = useState(isEditing);
286302
if (editingSeen !== isEditing) {
287303
setEditingSeen(isEditing);
288304
setTrailingFading(isEditing && editingAnimates);
305+
setControlLeaving(!isEditing && editingAnimates && editingControl != null);
289306
}
290-
useListEditingMotion(listRef, isEditing, editingAnimates, () =>
291-
setTrailingFading(false),
292-
);
307+
useListEditingMotion(listRef, isEditing, editingAnimates, {
308+
onTrailingGone: () => setTrailingFading(false),
309+
onControlGone: () => setControlLeaving(false),
310+
});
293311
const showsTrailing = !isEditing || trailingFading;
312+
const showsEditingControl = isEditing || controlLeaving;
294313
const mergedRef = useMergedRefs(
295314
listRef,
296315
ref as React.Ref<HTMLUListElement & HTMLOListElement>,
@@ -318,6 +337,8 @@ export function List({
318337
keepRowPresentation: hasAnimatedChanges ? keepPresentation : undefined,
319338
isEditing,
320339
showsTrailing,
340+
editingControl,
341+
showsEditingControl,
321342
}),
322343
[
323344
density,
@@ -327,6 +348,8 @@ export function List({
327348
hasAnimatedChanges,
328349
isEditing,
329350
showsTrailing,
351+
editingControl,
352+
showsEditingControl,
330353
],
331354
);
332355

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,25 @@ export interface ListContextValue {
7373
* mode, once it has faded out (List/useListEditingMotion.ts); internal.
7474
*/
7575
showsTrailing?: boolean;
76+
/**
77+
* The control edit mode puts in each row's leading column
78+
* (`editingControl`); internal. `'select'`: the theme's checkbox
79+
* indicator, checked when the row is selected.
80+
*/
81+
editingControl?: ListEditingControl;
82+
/**
83+
* Whether rows show their editing control: in edit mode, and after it
84+
* while the control slides out (List/useListEditingMotion.ts); internal.
85+
*/
86+
showsEditingControl?: boolean;
7687
}
7788

89+
/** The control edit mode shows in each row's leading column. */
90+
export type ListEditingControl = 'select';
91+
92+
/** Marks a row's editing control, inside its edit-mode indent. */
93+
export const EDITING_CONTROL_ATTRIBUTE = 'data-astryx-editing-control';
94+
7895
/** Marks the in-flow box that makes a row's edit-mode indent. */
7996
export const EDITING_INDENT_ATTRIBUTE = 'data-astryx-editing-indent';
8097

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file ListEditingSelect.test.tsx
5+
* @input vitest, List with `isEditing` and `editingControl="select"`, a stubbed
6+
* pointer (a finger, iOS and Android alike, or a mouse) and the list table
7+
* (motion/uikit/list.ts)
8+
* @output Unit tests for edit mode's selection control: each row shows the
9+
* theme's checkbox indicator in its leading column, checked when the row
10+
* is selected, decorative (the row keeps its state and its one tab stop);
11+
* activating a row still calls its `onClick`; under a finger the control
12+
* slides in from UIKit's travel and fades in on the table spring, and on
13+
* leaving slides out and fades, staying until it has faded while the
14+
* content moves back at once; a toggle mid-transition turns it around;
15+
* with a mouse it comes and goes at once
16+
* @position Beside List/List.tsx; the motion itself is proven in a real
17+
* engine by List/__tests__/ListEditingSelect.interaction.browser.spec.ts
18+
*/
19+
20+
import {act, fireEvent, render, screen, waitFor} from '@testing-library/react';
21+
import {afterEach, describe, expect, it, vi} from 'vitest';
22+
import {list} from '../motion/uikit/list';
23+
import {motionOf, presentation, type RowMotion} from '../utils/rowMotion';
24+
import {List} from './List';
25+
import {ListItem} from './ListItem';
26+
27+
const TRAVEL = list.metrics.editingControlTravel;
28+
29+
function stubPointer(pointer: 'touch' | 'mouse') {
30+
vi.stubGlobal(
31+
'matchMedia',
32+
vi.fn((query: string) => ({
33+
matches: pointer === 'touch' && query.includes('pointer: coarse'),
34+
media: query,
35+
onchange: null,
36+
addEventListener: () => undefined,
37+
removeEventListener: () => undefined,
38+
addListener: () => undefined,
39+
removeListener: () => undefined,
40+
dispatchEvent: () => false,
41+
})),
42+
);
43+
}
44+
45+
afterEach(() => {
46+
vi.unstubAllGlobals();
47+
});
48+
49+
const SENT_SELECTED = ['Sent'];
50+
const ignoreClick = () => undefined;
51+
52+
function Rows({
53+
isEditing,
54+
editingControl = 'select',
55+
selected = SENT_SELECTED,
56+
onClick = ignoreClick,
57+
}: {
58+
isEditing?: boolean;
59+
editingControl?: 'select';
60+
selected?: string[];
61+
onClick?: (label: string) => void;
62+
}) {
63+
return (
64+
<List isEditing={isEditing} editingControl={editingControl}>
65+
{['Inbox', 'Sent', 'Drafts'].map(label => (
66+
<ListItem
67+
key={label}
68+
label={label}
69+
isSelected={selected.includes(label)}
70+
onClick={() => onClick(label)}
71+
/>
72+
))}
73+
</List>
74+
);
75+
}
76+
77+
const rowOf = (label: string) =>
78+
screen.getByText(label).closest('li') as HTMLElement;
79+
const controlOf = (label: string) =>
80+
rowOf(label).querySelector<HTMLElement>('[data-astryx-editing-control]');
81+
const indicatorOf = (label: string) =>
82+
controlOf(label)?.querySelector<HTMLElement>('.astryx-checkbox-indicator') ??
83+
null;
84+
85+
describe('List edit mode, selection control', () => {
86+
it('shows nothing outside edit mode', () => {
87+
stubPointer('mouse');
88+
render(<Rows isEditing={false} />);
89+
expect(controlOf('Sent')).toBeNull();
90+
});
91+
92+
it('shows the theme’s checkbox indicator in each row, checked when the row is selected', () => {
93+
stubPointer('mouse');
94+
expect(matchMedia('(pointer: coarse)').matches).toBe(false);
95+
render(<Rows isEditing />);
96+
for (const label of ['Inbox', 'Sent', 'Drafts']) {
97+
expect(indicatorOf(label)).not.toBeNull();
98+
}
99+
expect(indicatorOf('Sent')?.getAttribute('data-checked')).toBe('checked');
100+
expect(indicatorOf('Inbox')?.hasAttribute('data-checked')).toBe(false);
101+
});
102+
103+
it('is decorative: the row keeps its state and its one tab stop', () => {
104+
stubPointer('mouse');
105+
render(<Rows isEditing />);
106+
const control = controlOf('Sent') as HTMLElement;
107+
expect(control.getAttribute('aria-hidden')).toBe('true');
108+
expect(control.querySelector('input, button, [tabindex]')).toBeNull();
109+
// Item's own selected semantics carry the state for assistive technology.
110+
expect(rowOf('Sent').getAttribute('aria-current')).toBe('true');
111+
expect(rowOf('Inbox').hasAttribute('aria-current')).toBe(false);
112+
});
113+
114+
it('activating a row still calls its onClick, for the app to toggle its selection', () => {
115+
stubPointer('mouse');
116+
const onClick = vi.fn();
117+
render(<Rows isEditing onClick={onClick} />);
118+
fireEvent.click(screen.getByText('Drafts'));
119+
expect(onClick).toHaveBeenCalledWith('Drafts');
120+
});
121+
122+
it('with a mouse, comes and goes with edit mode at once', () => {
123+
stubPointer('mouse');
124+
const {rerender} = render(<Rows isEditing={false} />);
125+
rerender(<Rows isEditing />);
126+
expect(controlOf('Sent')).not.toBeNull();
127+
expect(motionOf(controlOf('Sent') as HTMLElement, 'x')).toBeUndefined();
128+
rerender(<Rows isEditing={false} />);
129+
expect(controlOf('Sent')).toBeNull();
130+
});
131+
132+
it('under a finger, slides in from UIKit’s travel and fades in on the table spring', async () => {
133+
stubPointer('touch');
134+
const {rerender} = render(<Rows isEditing={false} />);
135+
await act(async () => undefined);
136+
rerender(<Rows isEditing />);
137+
const control = controlOf('Sent') as HTMLElement;
138+
const slide = motionOf(control, 'x') as RowMotion;
139+
expect(slide).toBeDefined();
140+
expect(slide.spring).toBe(list.update.tableSpring);
141+
expect(motionOf(control, 'opacity')).toBeDefined();
142+
const start = await slide.handle.started;
143+
expect(presentation(control, 'x', start)).toBeCloseTo(-TRAVEL, 6);
144+
expect(presentation(control, 'opacity', start)).toBeCloseTo(0, 6);
145+
expect(presentation(control, 'x', start + 700)).toBeCloseTo(0, 1);
146+
expect(presentation(control, 'opacity', start + 700)).toBeCloseTo(1, 1);
147+
});
148+
149+
it('under a finger, leaving slides it out and fades it while the content moves back at once, then it leaves', async () => {
150+
stubPointer('touch');
151+
const {rerender} = render(<Rows isEditing />);
152+
await act(async () => undefined);
153+
rerender(<Rows isEditing={false} />);
154+
const control = controlOf('Sent') as HTMLElement;
155+
expect(control).not.toBeNull();
156+
// The indent left layout at once: the content springs back from it.
157+
const content = screen.getByText('Sent').closest('button') as HTMLElement;
158+
expect(motionOf(content, 'x')).toBeDefined();
159+
const slide = motionOf(control, 'x') as RowMotion;
160+
const start = await slide.handle.started;
161+
expect(presentation(control, 'x', start)).toBeCloseTo(0, 6);
162+
expect(presentation(control, 'x', start + 700)).toBeCloseTo(-TRAVEL, 1);
163+
expect(presentation(control, 'opacity', start + 700)).toBeCloseTo(0, 1);
164+
await slide.handle.finished;
165+
await waitFor(() => expect(controlOf('Sent')).toBeNull());
166+
});
167+
168+
it('a toggle mid-transition turns the control around from where it is', async () => {
169+
stubPointer('touch');
170+
const {rerender} = render(<Rows isEditing={false} />);
171+
await act(async () => undefined);
172+
rerender(<Rows isEditing />);
173+
const control = controlOf('Sent') as HTMLElement;
174+
const slide = motionOf(control, 'x') as RowMotion;
175+
await slide.handle.started;
176+
await new Promise(resolve => setTimeout(resolve, 100));
177+
const at = performance.now();
178+
const before = presentation(control, 'x', at);
179+
expect(before).toBeGreaterThan(-TRAVEL);
180+
expect(before).toBeLessThan(0);
181+
const clock = vi.spyOn(performance, 'now').mockReturnValue(at);
182+
rerender(<Rows isEditing={false} />);
183+
clock.mockRestore();
184+
// The same element, from the same place, now heading out.
185+
expect(controlOf('Sent')).toBe(control);
186+
expect(presentation(control, 'x', at)).toBeCloseTo(before, 3);
187+
expect(presentation(control, 'x', at + 900)).toBeCloseTo(-TRAVEL, 1);
188+
});
189+
});

0 commit comments

Comments
 (0)