@mistriwork/ui 1.0.0-rc.58 → 1.0.0-rc.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -12
- package/dist/Avatar.js +3 -3
- package/dist/Button.d.ts +4 -14
- package/dist/Button.js +26 -13
- package/dist/Carousel.d.ts +6 -5
- package/dist/Carousel.js +58 -67
- package/dist/Choices.d.ts +2 -9
- package/dist/Choices.js +127 -64
- package/dist/DateField.d.ts +9 -4
- package/dist/DateField.js +102 -119
- package/dist/Drawer.d.ts +1 -19
- package/dist/Drawer.js +8 -9
- package/dist/DropMenu.d.ts +8 -21
- package/dist/DropMenu.js +84 -30
- package/dist/Dropdown.d.ts +9 -8
- package/dist/Dropdown.js +69 -65
- package/dist/Field.d.ts +2 -1
- package/dist/Field.js +12 -18
- package/dist/FileField.d.ts +3 -10
- package/dist/FileField.js +12 -9
- package/dist/FormParts.d.ts +17 -10
- package/dist/FormParts.js +26 -41
- package/dist/HeroBlocks.d.ts +7 -3
- package/dist/HeroBlocks.js +11 -6
- package/dist/Modal.d.ts +6 -17
- package/dist/Modal.js +90 -21
- package/dist/Pagination.d.ts +1 -12
- package/dist/Pagination.js +17 -3
- package/dist/Popover.d.ts +1 -15
- package/dist/Popover.js +10 -15
- package/dist/Resizable.d.ts +3 -21
- package/dist/Resizable.js +14 -91
- package/dist/ScrollArea.d.ts +4 -2
- package/dist/ScrollArea.js +11 -11
- package/dist/Select.d.ts +1 -6
- package/dist/Select.js +16 -7
- package/dist/SensitiveField.d.ts +1 -8
- package/dist/SensitiveField.js +6 -4
- package/dist/Slider.d.ts +1 -5
- package/dist/Slider.js +13 -5
- package/dist/Switch.d.ts +1 -11
- package/dist/Switch.js +10 -9
- package/dist/Tabs.d.ts +3 -3
- package/dist/Tabs.js +3 -3
- package/dist/TextArea.d.ts +4 -6
- package/dist/TextArea.js +14 -5
- package/dist/TextField.d.ts +5 -9
- package/dist/TextField.js +67 -9
- package/dist/Tooltip.d.ts +1 -21
- package/dist/Tooltip.js +13 -31
- package/dist/Typeahead.d.ts +15 -18
- package/dist/Typeahead.js +151 -86
- package/dist/UiKit.d.ts +63 -4
- package/dist/UiKit.js +46 -5
- package/dist/behaviors/DaysParts.js +28 -0
- package/dist/behaviors/EditableParts.js +21 -0
- package/dist/behaviors/PickParts.js +30 -0
- package/dist/behaviors/ScrubParts.js +21 -0
- package/dist/behaviors/SortableParts.js +32 -0
- package/dist/behaviors/SpansParts.js +12 -0
- package/dist/behaviors/TransferParts.js +37 -0
- package/dist/behaviors/TurnsParts.js +31 -0
- package/dist/behaviors/WidthsParts.js +27 -0
- package/dist/behaviors/days.js +216 -0
- package/dist/behaviors/dress.js +86 -0
- package/dist/behaviors/editable.js +104 -0
- package/dist/behaviors/heard.js +22 -0
- package/dist/behaviors/index.d.ts +310 -0
- package/dist/behaviors/index.js +44 -0
- package/dist/behaviors/pick.js +158 -0
- package/dist/behaviors/scrub.js +152 -0
- package/dist/behaviors/sortable.js +170 -0
- package/dist/behaviors/spans.js +240 -0
- package/dist/behaviors/testing.js +48 -0
- package/dist/behaviors/transfer.js +202 -0
- package/dist/behaviors/turns.js +106 -0
- package/dist/behaviors/widths.js +150 -0
- package/dist/behaviors.css +309 -0
- package/dist/changeDetails.d.ts +4 -8
- package/dist/changeDetails.js +0 -6
- package/dist/conformance.js +107 -1
- package/dist/controls.css +36 -0
- package/dist/fieldFrame.d.ts +2 -15
- package/dist/fieldFrame.js +1 -17
- package/dist/index.d.ts +6 -5
- package/dist/index.js +6 -5
- package/dist/internal/Frame.d.ts +6 -0
- package/dist/internal/Frame.js +25 -0
- package/dist/internal/HelpTip.d.ts +5 -0
- package/dist/internal/HelpTip.js +37 -0
- package/dist/internal/layers.d.ts +21 -0
- package/dist/internal/layers.js +66 -0
- package/dist/internal/popups.d.ts +12 -10
- package/dist/internal/popups.js +18 -14
- package/dist/internal/told.d.ts +14 -0
- package/dist/internal/told.js +20 -0
- package/dist/internal/uiKitContext.js +6 -4
- package/dist/preferences.css +2 -3
- package/dist/style.css +5 -16
- package/dist/styles/extras.css +461 -0
- package/dist/styles/forms.css +346 -0
- package/dist/styles/guard.css +114 -0
- package/dist/styles/kit-fields.css +181 -0
- package/dist/styles/kit-parts.css +130 -0
- package/dist/styles/kit.css +558 -0
- package/dist/styles/layers.css +4 -0
- package/dist/styles/looks.css +374 -0
- package/dist/styles/m-tokens.css +173 -0
- package/dist/styles/overlays.css +107 -0
- package/dist/styles/parts.css +456 -0
- package/dist/styles/screens.css +76 -0
- package/dist/styles/ui-sdk.css +1635 -0
- package/dist/tokens.css +15 -4
- package/dist/uiKitSlots.d.ts +1 -1
- package/dist/uiKitSlots.js +18 -2
- package/package.json +13 -11
- package/dist/forms.css +0 -225
- package/dist/hero.css +0 -23
- package/dist/internal/anchor.d.ts +0 -19
- package/dist/internal/anchor.js +0 -52
- package/dist/primitives.css +0 -200
- package/dist/standalone.css +0 -478
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/* Stretches of something along a line, each on a track of its own or sharing one: the bars of a Gantt chart,
|
|
2
|
+
the visits of a day by the hour. As a UI SDK primitive over the spans `@ui-sdk/core` ships, the stretches are
|
|
3
|
+
a value with a change, a stretch is carried along its track by a pointer or lengthened from either end, and
|
|
4
|
+
the same is done from the keyboard.
|
|
5
|
+
|
|
6
|
+
The keyboard's part is this primitive's own, so that a stretch is not moved by a key meant to walk: the spans
|
|
7
|
+
are one stop in the Tab order, the arrows walk between them (along a track and across tracks), and a stretch
|
|
8
|
+
is moved with Alt and an arrow along its track and lengthened or shortened with Shift as well, each said as
|
|
9
|
+
it lands; across the tracks, Alt with an arrow takes it to the track beside, where it is not held to its own.
|
|
10
|
+
Enter or Space picks one, for a page that opens something from it. Stretches that share a track and overlap
|
|
11
|
+
stand side by side in it.
|
|
12
|
+
|
|
13
|
+
With `stay`, a stretch keeps to its track: a task does not become another task by being dragged over its row. */
|
|
14
|
+
import { announce, createChange, createSpans, definePrimitive, stackSpans, } from '@ui-sdk/core';
|
|
15
|
+
export const spans = definePrimitive({
|
|
16
|
+
name: 'spans',
|
|
17
|
+
options: {
|
|
18
|
+
value: 'value',
|
|
19
|
+
defaultValue: 'value',
|
|
20
|
+
onValueChange: 'callback',
|
|
21
|
+
min: 'number',
|
|
22
|
+
max: 'number',
|
|
23
|
+
step: 'number',
|
|
24
|
+
minSize: 'number',
|
|
25
|
+
axis: 'string',
|
|
26
|
+
stay: 'boolean',
|
|
27
|
+
locked: 'action',
|
|
28
|
+
label: 'action',
|
|
29
|
+
onPick: 'callback',
|
|
30
|
+
},
|
|
31
|
+
controlled: ['value'],
|
|
32
|
+
state: (options) => ({ value: options.value ?? options.defaultValue ?? [], draft: null, focused: null }),
|
|
33
|
+
sync: (options) => (options.value === undefined ? {} : { value: options.value }),
|
|
34
|
+
api: (context) => {
|
|
35
|
+
const change = createChange(context);
|
|
36
|
+
const options = () => context.options();
|
|
37
|
+
const step = () => options().step ?? 1;
|
|
38
|
+
const said = (span) => options().label?.(span) ?? span.id;
|
|
39
|
+
/* What is asked for, with every stretch back on its own track where they are held to them. */
|
|
40
|
+
const held = (next) => {
|
|
41
|
+
if (!options().stay)
|
|
42
|
+
return next;
|
|
43
|
+
const home = new Map(context.store.get().value.map((span) => [span.id, span.track]));
|
|
44
|
+
return next.map((span) => home.get(span.id) === undefined || home.get(span.id) === span.track ? span : { ...span, track: home.get(span.id) });
|
|
45
|
+
};
|
|
46
|
+
/* The key behind a change asked of core, which takes none with an update and so calls every one the page's own
|
|
47
|
+
(`imperative`): a stretch moved with Alt and an arrow is a person's, and a caller that skips the page's
|
|
48
|
+
changes must not skip it. */
|
|
49
|
+
let keyed;
|
|
50
|
+
const byKey = (event, update) => {
|
|
51
|
+
keyed = event;
|
|
52
|
+
try {
|
|
53
|
+
update();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
keyed = undefined;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const core = createSpans(context, {
|
|
60
|
+
key: 'value',
|
|
61
|
+
draft: 'draft',
|
|
62
|
+
set: (value, details) => change('value', held(value), keyed ? { ...details, reason: 'move-key', event: keyed } : details),
|
|
63
|
+
min: () => options().min ?? 0,
|
|
64
|
+
max: () => options().max ?? 100,
|
|
65
|
+
step,
|
|
66
|
+
minSize: () => options().minSize ?? step(),
|
|
67
|
+
axis: (options().axis ?? 'x'),
|
|
68
|
+
overlap: true,
|
|
69
|
+
disabled: (span) => options().locked?.(span) ?? false,
|
|
70
|
+
label: said,
|
|
71
|
+
});
|
|
72
|
+
const find = (id) => context.store.get().value.find((span) => span.id === id);
|
|
73
|
+
return {
|
|
74
|
+
core,
|
|
75
|
+
find,
|
|
76
|
+
step,
|
|
77
|
+
/* Where a press on a stretch began, to tell a press from a carry once it is let go. */
|
|
78
|
+
pressed: { x: 0, y: 0 },
|
|
79
|
+
locked: (id) => {
|
|
80
|
+
const span = find(id);
|
|
81
|
+
return span ? (options().locked?.(span) ?? false) : true;
|
|
82
|
+
},
|
|
83
|
+
focused: (state) => state.focused !== null && state.value.some((span) => span.id === state.focused) ? state.focused : (state.value[0]?.id ?? null),
|
|
84
|
+
focus: (id) => context.store.set({ focused: id }),
|
|
85
|
+
pick: (id, event) => options().onPick?.(id, event),
|
|
86
|
+
/* Alt with an arrow along the track: the whole stretch a step, or with Shift its far end. */
|
|
87
|
+
nudge(id, by, end, event) {
|
|
88
|
+
const span = find(id);
|
|
89
|
+
if (!span || (options().locked?.(span) ?? false))
|
|
90
|
+
return false;
|
|
91
|
+
const low = options().min ?? 0;
|
|
92
|
+
const high = options().max ?? 100;
|
|
93
|
+
const size = span.end - span.start;
|
|
94
|
+
const next = end
|
|
95
|
+
? { ...span, end: Math.min(Math.max(span.end + by, span.start + (options().minSize ?? step())), high) }
|
|
96
|
+
: {
|
|
97
|
+
...span,
|
|
98
|
+
start: Math.min(Math.max(span.start + by, low), high - size),
|
|
99
|
+
end: Math.min(Math.max(span.start + by, low), high - size) + size,
|
|
100
|
+
};
|
|
101
|
+
if (next.start === span.start && next.end === span.end)
|
|
102
|
+
return false;
|
|
103
|
+
// A composer that draws its stretches from its own records turns the change down and makes it itself
|
|
104
|
+
// (a Gantt, a week): what was asked for is said either way.
|
|
105
|
+
byKey(event, () => core.update(id, { start: next.start, end: next.end }));
|
|
106
|
+
announce(said(next));
|
|
107
|
+
return true;
|
|
108
|
+
},
|
|
109
|
+
/* Alt with an arrow across the tracks: the same stretch on the track beside, where it may leave its own. */
|
|
110
|
+
across(el, id, forward, event) {
|
|
111
|
+
const span = find(id);
|
|
112
|
+
const root = el.closest('[data-slot="spans"]');
|
|
113
|
+
if (!span || !root || options().stay || (options().locked?.(span) ?? false))
|
|
114
|
+
return false;
|
|
115
|
+
const tracks = [...root.querySelectorAll('[data-slot="spans-track"]')].map((track) => track.dataset.track ?? '');
|
|
116
|
+
const next = tracks[tracks.indexOf(span.track) + (forward ? 1 : -1)];
|
|
117
|
+
if (next === undefined)
|
|
118
|
+
return false;
|
|
119
|
+
byKey(event, () => core.update(id, { track: next }));
|
|
120
|
+
announce(said({ ...span, track: next }));
|
|
121
|
+
return true;
|
|
122
|
+
},
|
|
123
|
+
vertical: () => options().axis === 'y',
|
|
124
|
+
/* The stretch the arrows walk to: the next along this track, or the nearest on the track above or below. */
|
|
125
|
+
toward(el, key) {
|
|
126
|
+
const root = el.closest('[data-slot="spans"]');
|
|
127
|
+
if (!root)
|
|
128
|
+
return undefined;
|
|
129
|
+
const all = [...root.querySelectorAll('[data-slot="spans-span"]')];
|
|
130
|
+
const here = el.getBoundingClientRect();
|
|
131
|
+
const along = options().axis === 'y' ? ['ArrowUp', 'ArrowDown'] : ['ArrowLeft', 'ArrowRight'];
|
|
132
|
+
const track = el.closest('[data-slot="spans-track"]');
|
|
133
|
+
if (along.includes(key)) {
|
|
134
|
+
const same = all.filter((span) => span.closest('[data-slot="spans-track"]') === track);
|
|
135
|
+
return same[same.indexOf(el) + (key === along[1] ? 1 : -1)];
|
|
136
|
+
}
|
|
137
|
+
const tracks = [...root.querySelectorAll('[data-slot="spans-track"]')];
|
|
138
|
+
const forward = key === 'ArrowDown' || key === 'ArrowRight';
|
|
139
|
+
for (let at = tracks.indexOf(track) + (forward ? 1 : -1); at >= 0 && at < tracks.length; at += forward ? 1 : -1) {
|
|
140
|
+
const there = all.filter((span) => span.closest('[data-slot="spans-track"]') === tracks[at]);
|
|
141
|
+
if (there.length === 0)
|
|
142
|
+
continue;
|
|
143
|
+
const middle = (box) => (options().axis === 'y' ? box.top + box.height / 2 : box.left + box.width / 2);
|
|
144
|
+
return there.reduce((best, span) => Math.abs(middle(span.getBoundingClientRect()) - middle(here)) < Math.abs(middle(best.getBoundingClientRect()) - middle(here))
|
|
145
|
+
? span
|
|
146
|
+
: best);
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
slots: (slot) => ({
|
|
153
|
+
root: slot({
|
|
154
|
+
state: (state, part) => ({ spans: part.api.core.spans(state), changing: state.draft?.id ?? null }),
|
|
155
|
+
attrs: (state) => ({ 'data-changing': state.draft ? '' : undefined }),
|
|
156
|
+
}),
|
|
157
|
+
track: slot({
|
|
158
|
+
props: { value: 'string' },
|
|
159
|
+
state: (state, part) => ({ spans: part.api.core.spansOn(state, part.key) }),
|
|
160
|
+
attrs: (_state, part) => ({ 'data-track': part.key }),
|
|
161
|
+
bind: (el, part) => part.api.core.bindTrack(el, part.key),
|
|
162
|
+
}),
|
|
163
|
+
span: slot({
|
|
164
|
+
props: { value: 'string' },
|
|
165
|
+
state: (state, part) => ({
|
|
166
|
+
span: part.api.core.spans(state).find((span) => span.id === part.key),
|
|
167
|
+
action: part.api.core.action(state, part.key),
|
|
168
|
+
}),
|
|
169
|
+
attrs: (state, part) => {
|
|
170
|
+
const span = part.api.core.spans(state).find((each) => each.id === part.key);
|
|
171
|
+
return {
|
|
172
|
+
...part.api.core.spanAttrs(state, part.key),
|
|
173
|
+
// Beside the stretches it overlaps on its track, each a share of the track's breadth.
|
|
174
|
+
style: span ? part.api.core.spanStyle(span, stackSpans(part.api.core.spansOn(state, span.track)).get(span.id)) : undefined,
|
|
175
|
+
tabindex: part.api.focused(state) === part.key ? '0' : '-1',
|
|
176
|
+
'data-locked': part.api.locked(part.key) ? '' : undefined,
|
|
177
|
+
'aria-keyshortcuts': part.api.locked(part.key)
|
|
178
|
+
? undefined
|
|
179
|
+
: 'Alt+ArrowLeft Alt+ArrowRight Alt+Shift+ArrowLeft Alt+Shift+ArrowRight',
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
bind: (el, part) => part.api.core.bindSpan(el, part.key),
|
|
183
|
+
on: {
|
|
184
|
+
pointerdown: (event, { api, key }) => {
|
|
185
|
+
api.focus(key);
|
|
186
|
+
api.pressed.x = event.clientX;
|
|
187
|
+
api.pressed.y = event.clientY;
|
|
188
|
+
api.core.spanPointerdown(event, key);
|
|
189
|
+
},
|
|
190
|
+
focus: (_event, { api, key }) => api.focus(key),
|
|
191
|
+
click: (event, { api, key }) => {
|
|
192
|
+
// A press that carried the stretch somewhere is not a press on it.
|
|
193
|
+
if (event.detail !== 0 && Math.hypot(event.clientX - api.pressed.x, event.clientY - api.pressed.y) > 4)
|
|
194
|
+
return;
|
|
195
|
+
api.pick(key, event);
|
|
196
|
+
},
|
|
197
|
+
keydown: (event, { api, key, el }) => {
|
|
198
|
+
if (!el)
|
|
199
|
+
return;
|
|
200
|
+
const arrows = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'];
|
|
201
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
api.pick(key, event);
|
|
204
|
+
}
|
|
205
|
+
else if (arrows.includes(event.key) && event.altKey) {
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
const back = event.key === 'ArrowLeft' || event.key === 'ArrowUp';
|
|
208
|
+
const along = api.vertical()
|
|
209
|
+
? event.key === 'ArrowUp' || event.key === 'ArrowDown'
|
|
210
|
+
: event.key === 'ArrowLeft' || event.key === 'ArrowRight';
|
|
211
|
+
const id = key;
|
|
212
|
+
if (along)
|
|
213
|
+
api.nudge(id, (back ? -1 : 1) * api.step(), event.shiftKey, event);
|
|
214
|
+
else if (api.across(el, id, !back, event)) {
|
|
215
|
+
// It is drawn again on its new track, which may take the caller a moment: the keyboard goes with
|
|
216
|
+
// it once it is there.
|
|
217
|
+
const follow = (tries) => {
|
|
218
|
+
const there = el.ownerDocument.querySelector(`[data-slot="spans-span"][data-span-id="${CSS.escape(id)}"]`);
|
|
219
|
+
if (there && there !== el.ownerDocument.activeElement && there.isConnected)
|
|
220
|
+
there.focus();
|
|
221
|
+
if ((!there || el.ownerDocument.activeElement !== there) && tries > 0)
|
|
222
|
+
requestAnimationFrame(() => follow(tries - 1));
|
|
223
|
+
};
|
|
224
|
+
requestAnimationFrame(() => follow(12));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else if (arrows.includes(event.key)) {
|
|
228
|
+
const next = api.toward(el, event.key);
|
|
229
|
+
if (next) {
|
|
230
|
+
event.preventDefault();
|
|
231
|
+
next.focus();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (event.key === 'Escape')
|
|
235
|
+
api.core.cancel();
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
}),
|
|
239
|
+
}),
|
|
240
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the behaviors' tests draw with: React's root and `act`, and the few ways a test finds a part and presses a key
|
|
3
|
+
* on it. No testing library: a behavior is roles, keys and attributes, and these read exactly those.
|
|
4
|
+
*/
|
|
5
|
+
import { act } from 'react';
|
|
6
|
+
import { createRoot } from 'react-dom/client';
|
|
7
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
8
|
+
let root;
|
|
9
|
+
let host;
|
|
10
|
+
export function draw(node) {
|
|
11
|
+
clear();
|
|
12
|
+
host = document.createElement('div');
|
|
13
|
+
document.body.append(host);
|
|
14
|
+
root = createRoot(host);
|
|
15
|
+
act(() => root?.render(node));
|
|
16
|
+
return host;
|
|
17
|
+
}
|
|
18
|
+
export function clear() {
|
|
19
|
+
act(() => root?.unmount());
|
|
20
|
+
host?.remove();
|
|
21
|
+
root = undefined;
|
|
22
|
+
host = undefined;
|
|
23
|
+
}
|
|
24
|
+
export const all = (role, within = document) => [
|
|
25
|
+
...within.querySelectorAll(`[role="${role}"]`),
|
|
26
|
+
];
|
|
27
|
+
/* The one part with this role and this name: its `aria-label`, else its text. */
|
|
28
|
+
export function one(role, name, within = document) {
|
|
29
|
+
const found = all(role, within).filter((part) => (part.getAttribute('aria-label') ?? part.textContent?.trim()) === name);
|
|
30
|
+
if (found.length !== 1)
|
|
31
|
+
throw new Error(`${found.length} parts with role ${role} named "${name}"`);
|
|
32
|
+
return found[0];
|
|
33
|
+
}
|
|
34
|
+
/* The one part with this `aria-label`, whatever it is: a native button says its role without an attribute. */
|
|
35
|
+
export function labelled(name, within = document) {
|
|
36
|
+
const found = [...within.querySelectorAll(`[aria-label="${name}"]`)];
|
|
37
|
+
if (found.length !== 1)
|
|
38
|
+
throw new Error(`${found.length} parts labelled "${name}"`);
|
|
39
|
+
return found[0];
|
|
40
|
+
}
|
|
41
|
+
export function key(part, pressed, more = {}) {
|
|
42
|
+
act(() => {
|
|
43
|
+
part.dispatchEvent(new KeyboardEvent('keydown', { key: pressed, bubbles: true, cancelable: true, ...more }));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export function focus(part) {
|
|
47
|
+
act(() => part.focus());
|
|
48
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/* Things moved between lists, as a UI SDK primitive: lanes of a board, two columns of a picker, a tray and a
|
|
2
|
+
canvas. It is `createTransfer` given parts, so everything that behavior does arrives with it. An item is carried
|
|
3
|
+
with a pointer, or picked up with Space and moved with the arrows (Left and Right between lists, Up and Down
|
|
4
|
+
within one) and dropped with Space; Escape puts it back; a screen reader hears each step; and the move is a
|
|
5
|
+
change that can be cancelled or controlled.
|
|
6
|
+
|
|
7
|
+
What this adds for a composer is what it knows while something is carried, said as attributes and as part state:
|
|
8
|
+
which lists would take the carried item (`data-takes`), which would not (`data-refuses`), and which one it is
|
|
9
|
+
over now (`data-over`). A board's "letting go here books a visit" is those three, read by whoever draws the
|
|
10
|
+
lane.
|
|
11
|
+
|
|
12
|
+
It also adds one rule to a pointer's drop. `createTransfer` leaves a carried item in the last list that would
|
|
13
|
+
take it, wherever the pointer goes next, so letting go over a list that refuses it, or over nothing, would
|
|
14
|
+
still move it. Here a pointer that has left every list that would take the item is `astray`: no list is lit,
|
|
15
|
+
and letting go puts the item back. What is lit is where it lands, and nothing lands where nothing is lit.
|
|
16
|
+
|
|
17
|
+
An item is carried from anywhere on it by a mouse or a pen. A finger scrolls, so it carries by the handle only,
|
|
18
|
+
which is also what the keyboard picks an item up by: a card with a link and a button in it cannot itself be the
|
|
19
|
+
thing Space acts on. */
|
|
20
|
+
import { createChange, createRovingFocus, createTransfer, definePrimitive, } from '@ui-sdk/core';
|
|
21
|
+
import { dressCopyOf } from './dress.js';
|
|
22
|
+
const listOf = (lists, key) => Object.keys(lists).find((list) => lists[list]?.includes(key));
|
|
23
|
+
export const transfer = definePrimitive({
|
|
24
|
+
name: 'transfer',
|
|
25
|
+
options: {
|
|
26
|
+
value: 'value',
|
|
27
|
+
defaultValue: 'value',
|
|
28
|
+
onValueChange: 'callback',
|
|
29
|
+
accepts: 'action',
|
|
30
|
+
order: 'list',
|
|
31
|
+
label: 'action',
|
|
32
|
+
listLabel: 'action',
|
|
33
|
+
},
|
|
34
|
+
controlled: ['value'],
|
|
35
|
+
state: (options) => ({
|
|
36
|
+
value: options.value ?? options.defaultValue ?? {},
|
|
37
|
+
moving: null,
|
|
38
|
+
focused: null,
|
|
39
|
+
astray: false,
|
|
40
|
+
}),
|
|
41
|
+
sync: (options) => (options.value === undefined ? {} : { value: options.value }),
|
|
42
|
+
api: (context) => {
|
|
43
|
+
const change = createChange(context);
|
|
44
|
+
const accepts = (key, list) => context.options().accepts?.(key, list) ?? true;
|
|
45
|
+
/* Each list's element, to tell which one a pointer is over. */
|
|
46
|
+
const places = new Map();
|
|
47
|
+
const moves = createTransfer(context, {
|
|
48
|
+
key: 'value',
|
|
49
|
+
draft: 'moving',
|
|
50
|
+
set: (value, details) => (details.reason === 'drag' && context.store.get().astray ? false : change('value', value, details)),
|
|
51
|
+
order: () => context.options().order ?? Object.keys(context.store.get().value),
|
|
52
|
+
accepts,
|
|
53
|
+
label: (key) => context.options().label?.(key),
|
|
54
|
+
listLabel: (list) => context.options().listLabel?.(list),
|
|
55
|
+
});
|
|
56
|
+
const roving = createRovingFocus(context, { slot: 'handle', key: 'focused', orientation: () => 'both' });
|
|
57
|
+
/* Follows the pointer that pressed an item until it lifts, and says when it has gone astray. */
|
|
58
|
+
const follow = (key, page) => {
|
|
59
|
+
context.store.set({ astray: false });
|
|
60
|
+
const moved = (event) => {
|
|
61
|
+
const under = page.elementFromPoint(event.clientX, event.clientY);
|
|
62
|
+
const list = [...places].find(([, el]) => el.contains(under))?.[0];
|
|
63
|
+
const home = context.store.get().moving?.from;
|
|
64
|
+
const astray = list === undefined || (list !== home && !accepts(key, list));
|
|
65
|
+
if (astray === context.store.get().astray)
|
|
66
|
+
return;
|
|
67
|
+
context.store.set({ astray });
|
|
68
|
+
// Gone astray, the item is shown back where it came from, which is where letting go would leave it.
|
|
69
|
+
const at = home === undefined ? -1 : (context.store.get().value[home]?.indexOf(key) ?? -1);
|
|
70
|
+
if (astray && home !== undefined && at >= 0)
|
|
71
|
+
moves.moveTo(home, at);
|
|
72
|
+
};
|
|
73
|
+
const lifted = () => {
|
|
74
|
+
page.removeEventListener('pointermove', moved, true);
|
|
75
|
+
page.removeEventListener('pointerup', lifted, true);
|
|
76
|
+
page.removeEventListener('pointercancel', lifted, true);
|
|
77
|
+
};
|
|
78
|
+
page.addEventListener('pointermove', moved, true);
|
|
79
|
+
page.addEventListener('pointerup', lifted, true);
|
|
80
|
+
page.addEventListener('pointercancel', lifted, true);
|
|
81
|
+
context.onDestroy(lifted);
|
|
82
|
+
};
|
|
83
|
+
/* Where the carried item stands now, when that is not where it came from. */
|
|
84
|
+
const over = (state) => {
|
|
85
|
+
if (!state.moving || (state.moving.pointer && state.astray))
|
|
86
|
+
return undefined;
|
|
87
|
+
const now = listOf(state.moving.lists, state.moving.key);
|
|
88
|
+
return now === state.moving.from ? undefined : now;
|
|
89
|
+
};
|
|
90
|
+
/* What the last press on an item landed on. Following a press takes the pointer for the item, so the click
|
|
91
|
+
that ends a press which never moved arrives at the item and not at the link inside it that was pressed;
|
|
92
|
+
the item hands it on (see its `click`). */
|
|
93
|
+
const pressed = {};
|
|
94
|
+
return { moves, roving, accepts, over, pressed, places, follow };
|
|
95
|
+
},
|
|
96
|
+
slots: (slot) => ({
|
|
97
|
+
root: slot({
|
|
98
|
+
state: (state) => ({ carrying: state.moving?.key ?? null }),
|
|
99
|
+
attrs: (state) => ({
|
|
100
|
+
'data-carrying': state.moving ? '' : undefined,
|
|
101
|
+
'data-astray': state.moving?.pointer && state.astray ? '' : undefined,
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
list: slot({
|
|
105
|
+
props: { value: 'string' },
|
|
106
|
+
state: (state, part) => {
|
|
107
|
+
const carried = state.moving;
|
|
108
|
+
const home = carried?.from === part.key;
|
|
109
|
+
const takes = Boolean(carried && !home && part.api.accepts(carried.key, part.key));
|
|
110
|
+
return {
|
|
111
|
+
keys: part.api.moves.lists(state)[part.key] ?? [],
|
|
112
|
+
over: part.api.over(state) === part.key,
|
|
113
|
+
takes,
|
|
114
|
+
refuses: Boolean(carried && !home && !takes),
|
|
115
|
+
carrying: carried?.key ?? null,
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
attrs: (state, part) => {
|
|
119
|
+
const carried = state.moving;
|
|
120
|
+
const home = carried?.from === part.key;
|
|
121
|
+
const takes = Boolean(carried && !home && part.api.accepts(carried.key, part.key));
|
|
122
|
+
return {
|
|
123
|
+
'data-list': part.key,
|
|
124
|
+
'data-over': part.api.over(state) === part.key ? '' : undefined,
|
|
125
|
+
'data-takes': takes ? '' : undefined,
|
|
126
|
+
'data-refuses': carried && !home && !takes ? '' : undefined,
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
bind: (el, part) => {
|
|
130
|
+
part.api.places.set(part.key, el);
|
|
131
|
+
const unbind = part.api.moves.bindList(el, part.key);
|
|
132
|
+
return () => {
|
|
133
|
+
part.api.places.delete(part.key);
|
|
134
|
+
unbind();
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
item: slot({
|
|
139
|
+
props: { value: 'string' },
|
|
140
|
+
state: (state, part) => ({ carried: part.api.moves.moving(state, part.key) }),
|
|
141
|
+
attrs: (state, part) => ({
|
|
142
|
+
'data-carried': part.api.moves.moving(state, part.key) ? '' : undefined,
|
|
143
|
+
'data-dragging': part.api.moves.dragging(state, part.key) ? '' : undefined,
|
|
144
|
+
}),
|
|
145
|
+
on: {
|
|
146
|
+
pointerdown: (event, { api, key, el }) => {
|
|
147
|
+
if (event.pointerType === 'touch' || !el)
|
|
148
|
+
return;
|
|
149
|
+
if (event.target.closest('button, input, select, textarea, [role="menu"]'))
|
|
150
|
+
return;
|
|
151
|
+
api.pressed.on = event.target;
|
|
152
|
+
dressCopyOf(el);
|
|
153
|
+
api.follow(key, el.ownerDocument);
|
|
154
|
+
api.moves.pointerdown(event, key);
|
|
155
|
+
},
|
|
156
|
+
click: (event, { api, el }) => {
|
|
157
|
+
const link = api.pressed.on?.closest('a, [role="link"]');
|
|
158
|
+
api.pressed.on = undefined;
|
|
159
|
+
if (event.target !== el || !link || !el?.contains(link))
|
|
160
|
+
return;
|
|
161
|
+
link.dispatchEvent(new MouseEvent('click', {
|
|
162
|
+
bubbles: true,
|
|
163
|
+
cancelable: true,
|
|
164
|
+
view: window,
|
|
165
|
+
ctrlKey: event.ctrlKey,
|
|
166
|
+
metaKey: event.metaKey,
|
|
167
|
+
shiftKey: event.shiftKey,
|
|
168
|
+
}));
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
bind: (el, part) => part.api.moves.bindItem(el, part.key),
|
|
172
|
+
}),
|
|
173
|
+
handle: slot({
|
|
174
|
+
props: { value: { kind: 'string', from: 'item', required: true } },
|
|
175
|
+
state: (state, part) => ({ carried: part.api.moves.moving(state, part.key) }),
|
|
176
|
+
attrs: (state, part) => ({
|
|
177
|
+
type: 'button',
|
|
178
|
+
'aria-roledescription': 'carry handle',
|
|
179
|
+
'aria-pressed': String(part.api.moves.moving(state, part.key)),
|
|
180
|
+
tabindex: part.api.roving.tabIndex(state, part),
|
|
181
|
+
'data-carried': part.api.moves.moving(state, part.key) ? '' : undefined,
|
|
182
|
+
}),
|
|
183
|
+
on: {
|
|
184
|
+
keydown: (event, { el, api, key }) => {
|
|
185
|
+
if (!api.moves.keydown(event, key) && el)
|
|
186
|
+
api.roving.keydown(event, el);
|
|
187
|
+
},
|
|
188
|
+
pointerdown: (event, { api, key, el }) => {
|
|
189
|
+
const item = el?.closest('[data-slot="transfer-item"]');
|
|
190
|
+
if (item)
|
|
191
|
+
dressCopyOf(item);
|
|
192
|
+
if (el)
|
|
193
|
+
api.follow(key, el.ownerDocument);
|
|
194
|
+
api.moves.pointerdown(event, key);
|
|
195
|
+
// The item around the handle hears the same press; it has been answered here.
|
|
196
|
+
event.stopPropagation();
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
bind: (el, part) => part.api.roving.bind(el, part),
|
|
200
|
+
}),
|
|
201
|
+
}),
|
|
202
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* A list worked through one at a time: the behavior under a queue of things to decide, a review, a set of cards
|
|
2
|
+
to go through. As a UI SDK primitive what has been dealt with is a value with a change, so a page can hold it
|
|
3
|
+
(the ones answered, kept) or leave it alone.
|
|
4
|
+
|
|
5
|
+
One thing is in hand. Finishing it brings the next into its place; skipping leaves it for the end, where what
|
|
6
|
+
was skipped comes round again. When the one in hand changes the keyboard is put on its name, so it is read out
|
|
7
|
+
and the next key is about the new one. What is done to a thing, and whether it worked, is the caller's: it
|
|
8
|
+
says `finish` when it has. */
|
|
9
|
+
import { createChange, definePrimitive } from '@ui-sdk/core';
|
|
10
|
+
export const turns = definePrimitive({
|
|
11
|
+
name: 'turns',
|
|
12
|
+
options: { keys: 'list', value: 'list', defaultValue: 'list', onValueChange: 'callback' },
|
|
13
|
+
controlled: ['value'],
|
|
14
|
+
state: (options) => ({
|
|
15
|
+
value: (options.value ?? options.defaultValue ?? []),
|
|
16
|
+
/* The place, among what is left, of the one in hand. */
|
|
17
|
+
at: 0,
|
|
18
|
+
}),
|
|
19
|
+
sync: (options) => (options.value === undefined ? {} : { value: options.value }),
|
|
20
|
+
api: (context) => {
|
|
21
|
+
const change = createChange(context);
|
|
22
|
+
const left = () => {
|
|
23
|
+
const done = context.store.get().value;
|
|
24
|
+
return (context.options().keys ?? []).filter((key) => !done.includes(key));
|
|
25
|
+
};
|
|
26
|
+
const current = () => {
|
|
27
|
+
const all = left();
|
|
28
|
+
return all[Math.min(context.store.get().at, all.length - 1)] ?? null;
|
|
29
|
+
};
|
|
30
|
+
/* The keyboard goes to the name of the one in hand once it is drawn. */
|
|
31
|
+
const arrive = (from) => {
|
|
32
|
+
const root = from?.closest('[data-slot="turns"]');
|
|
33
|
+
if (!root || typeof requestAnimationFrame === 'undefined')
|
|
34
|
+
return;
|
|
35
|
+
let tries = 0;
|
|
36
|
+
const go = () => {
|
|
37
|
+
const name = root.querySelector('[data-slot="turns-name"]');
|
|
38
|
+
// The name of the last one is still there until the next is drawn: one frame is waited for it.
|
|
39
|
+
if (name && tries > 0)
|
|
40
|
+
name.focus();
|
|
41
|
+
else if (++tries < 12)
|
|
42
|
+
requestAnimationFrame(go);
|
|
43
|
+
};
|
|
44
|
+
go();
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
left,
|
|
48
|
+
current,
|
|
49
|
+
total: () => (context.options().keys ?? []).length,
|
|
50
|
+
now: () => context.store.get(),
|
|
51
|
+
arrive,
|
|
52
|
+
/* The one in hand is left where it stands and the next comes up; past the last, the first that is left. */
|
|
53
|
+
step(by, from) {
|
|
54
|
+
const count = left().length;
|
|
55
|
+
if (count < 2)
|
|
56
|
+
return false;
|
|
57
|
+
const at = Math.min(context.store.get().at, count - 1);
|
|
58
|
+
context.store.set({ at: (at + by + count) % count });
|
|
59
|
+
arrive(from);
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
/* The one in hand (or the one named) has been dealt with: the next slides into its place. */
|
|
63
|
+
finish(key, from, event) {
|
|
64
|
+
const done = key ?? current();
|
|
65
|
+
const now = context.store.get();
|
|
66
|
+
if (done === null || now.value.includes(done))
|
|
67
|
+
return false;
|
|
68
|
+
const before = left();
|
|
69
|
+
if (!change('value', [...now.value, done], { reason: 'imperative', event }))
|
|
70
|
+
return false;
|
|
71
|
+
// Past the last, what was skipped is what is left, from its start.
|
|
72
|
+
if (Math.min(now.at, before.length - 1) >= before.length - 1)
|
|
73
|
+
context.store.set({ at: 0 });
|
|
74
|
+
arrive(from);
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
slots: (slot) => ({
|
|
80
|
+
root: slot({
|
|
81
|
+
state: (state, part) => {
|
|
82
|
+
const left = part.api.left();
|
|
83
|
+
return {
|
|
84
|
+
current: part.api.current(),
|
|
85
|
+
at: Math.min(state.at, Math.max(left.length - 1, 0)),
|
|
86
|
+
handled: state.value,
|
|
87
|
+
left,
|
|
88
|
+
total: part.api.total(),
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
attrs: (_state, part) => ({ 'data-done': part.api.current() === null ? '' : undefined }),
|
|
92
|
+
}),
|
|
93
|
+
/* What the one in hand is called: where the keyboard lands when it changes. */
|
|
94
|
+
name: slot({
|
|
95
|
+
attrs: () => ({ tabindex: '-1' }),
|
|
96
|
+
}),
|
|
97
|
+
skip: slot({
|
|
98
|
+
attrs: (_state, part) => ({ type: 'button', disabled: part.api.left().length < 2 ? '' : undefined }),
|
|
99
|
+
on: {
|
|
100
|
+
click: (_event, { api, el }) => {
|
|
101
|
+
api.step(1, el);
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
}),
|
|
106
|
+
});
|