@human-kit/ui 1.0.0-beta.3 → 1.0.0-beta.4
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 +15 -6
- package/dist/FOCUS_STATE_CONTRACT.md +2 -0
- package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +3 -1
- package/dist/button/root/button-root.svelte +19 -3
- package/dist/button/root/button-root.svelte.d.ts +10 -0
- package/dist/combobox/list/combobox-listbox.svelte.d.ts +3 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/environment.d.ts +11 -0
- package/dist/internal/environment.js +14 -0
- package/dist/internal/localized-strings.d.ts +80 -0
- package/dist/internal/localized-strings.js +80 -0
- package/dist/listbox/README.md +27 -1
- package/dist/listbox/item/listbox-item.svelte +11 -2
- package/dist/listbox/root/context.d.ts +25 -0
- package/dist/listbox/root/context.js +108 -1
- package/dist/listbox/root/listbox.svelte +85 -7
- package/dist/listbox/root/listbox.svelte.d.ts +13 -1
- package/dist/menu/README.md +46 -0
- package/dist/menu/content/menu-content.svelte +16 -5
- package/dist/menu/content/menu-content.svelte.d.ts +2 -1
- package/dist/menu/context-trigger/menu-context-trigger.svelte +195 -0
- package/dist/menu/context-trigger/menu-context-trigger.svelte.d.ts +36 -0
- package/dist/menu/index.d.ts +4 -1
- package/dist/menu/index.js +1 -0
- package/dist/menu/index.parts.d.ts +1 -0
- package/dist/menu/index.parts.js +1 -0
- package/dist/menu/root/context.d.ts +18 -0
- package/dist/menu/root/menu-state.svelte.js +19 -0
- package/dist/popover/content/popover-content.svelte +2 -1
- package/dist/primitives/floating.d.ts +33 -5
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/keyboard-navigation.d.ts +23 -5
- package/dist/primitives/keyboard-navigation.js +22 -15
- package/dist/primitives/long-press.d.ts +59 -0
- package/dist/primitives/long-press.js +142 -0
- package/dist/primitives/scroll-lock.d.ts +24 -0
- package/dist/primitives/scroll-lock.js +40 -0
- package/dist/transfer-list/README.md +121 -0
- package/dist/transfer-list/index.d.ts +33 -0
- package/dist/transfer-list/index.js +18 -0
- package/dist/transfer-list/index.parts.d.ts +9 -0
- package/dist/transfer-list/index.parts.js +10 -0
- package/dist/transfer-list/item/transfer-list-item.svelte +74 -0
- package/dist/transfer-list/item/transfer-list-item.svelte.d.ts +35 -0
- package/dist/transfer-list/list/transfer-list-list.svelte +162 -0
- package/dist/transfer-list/list/transfer-list-list.svelte.d.ts +56 -0
- package/dist/transfer-list/list/transfer-list-source.svelte +13 -0
- package/dist/transfer-list/list/transfer-list-source.svelte.d.ts +36 -0
- package/dist/transfer-list/list/transfer-list-target.svelte +14 -0
- package/dist/transfer-list/list/transfer-list-target.svelte.d.ts +36 -0
- package/dist/transfer-list/move/transfer-list-move-all.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-all.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-button.svelte +103 -0
- package/dist/transfer-list/move/transfer-list-move-button.svelte.d.ts +25 -0
- package/dist/transfer-list/move/transfer-list-move-down.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-down.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-selected.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-selected.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-move-up.svelte +14 -0
- package/dist/transfer-list/move/transfer-list-move-up.svelte.d.ts +10 -0
- package/dist/transfer-list/move/transfer-list-reorder-button.svelte +83 -0
- package/dist/transfer-list/move/transfer-list-reorder-button.svelte.d.ts +21 -0
- package/dist/transfer-list/root/context.d.ts +70 -0
- package/dist/transfer-list/root/context.js +24 -0
- package/dist/transfer-list/root/transfer-list-root.svelte +342 -0
- package/dist/transfer-list/root/transfer-list-root.svelte.d.ts +65 -0
- package/dist/transfer-list/root/types.d.ts +22 -0
- package/dist/transfer-list/root/types.js +4 -0
- package/dist/transfer-list/status/transfer-list-status.svelte +70 -0
- package/dist/transfer-list/status/transfer-list-status.svelte.d.ts +15 -0
- package/package.json +6 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends object = object">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
import { untrack } from 'svelte';
|
|
5
|
+
import { dev } from '../../internal/environment';
|
|
6
|
+
import type { ListBoxContext } from '../../listbox/root/context';
|
|
7
|
+
import { setTransferListContext, type TransferListContext } from './context';
|
|
8
|
+
import { oppositeSide, type TransferListKey, type TransferListMoveDetails } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* TransferList.Root - Owns which side each item is on and shares it with the two
|
|
12
|
+
* lists and the move buttons.
|
|
13
|
+
*
|
|
14
|
+
* There is one source of truth: `items` is the whole collection and `value` is the
|
|
15
|
+
* ordered list of keys that sit on the right. The left is everything else. `value`
|
|
16
|
+
* is therefore exactly what a form would submit, and the right-hand order comes for
|
|
17
|
+
* free — items land in the order they were moved.
|
|
18
|
+
*/
|
|
19
|
+
type TransferListRootProps = {
|
|
20
|
+
/** The whole collection, in the order the left-hand list shows it. */
|
|
21
|
+
items?: Iterable<T>;
|
|
22
|
+
/** Identity of an item. Defaults to its `id` field. */
|
|
23
|
+
getKey?: (item: T) => TransferListKey;
|
|
24
|
+
/** Keys on the right, in order. Two-way by default — use `bind:value`. */
|
|
25
|
+
value?: TransferListKey[];
|
|
26
|
+
/** Initial right-hand keys, for when `value` is not supplied. */
|
|
27
|
+
defaultValue?: TransferListKey[];
|
|
28
|
+
/**
|
|
29
|
+
* Opt into fully controlled state: the component stops writing back to `value` and
|
|
30
|
+
* only reports through `onChange`, so the parent can reject a move by not flowing
|
|
31
|
+
* the new value back down. Off by default, because `bind:value` — the common case —
|
|
32
|
+
* needs the write-back to work at all.
|
|
33
|
+
*/
|
|
34
|
+
controlledValue?: boolean;
|
|
35
|
+
/** Called after a move, with the new value and what moved. */
|
|
36
|
+
onChange?: (value: TransferListKey[], details: TransferListMoveDetails) => void;
|
|
37
|
+
/** Keys that cannot be moved off the side they are on. */
|
|
38
|
+
disabledKeys?: Iterable<TransferListKey>;
|
|
39
|
+
/**
|
|
40
|
+
* Name for the hidden inputs that carry `value`, so the field submits with the form
|
|
41
|
+
* without any wiring. One input per key, in order; nothing is rendered when the right
|
|
42
|
+
* is empty, which is how a multi-value field behaves natively.
|
|
43
|
+
*/
|
|
44
|
+
name?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Whether `Ctrl`/`Cmd`+`Enter` inside a list sends its selection to the other one.
|
|
47
|
+
*
|
|
48
|
+
* Each list has exactly one destination, so the shortcut needs no direction — which
|
|
49
|
+
* also means it stays correct when the layout is mirrored. Turn it off if it collides
|
|
50
|
+
* with something in the surrounding app.
|
|
51
|
+
*/
|
|
52
|
+
moveShortcut?: boolean;
|
|
53
|
+
/** CSS class for the wrapper element. */
|
|
54
|
+
class?: string;
|
|
55
|
+
/** The two lists, the move buttons, and anything else the layout needs. */
|
|
56
|
+
children?: Snippet;
|
|
57
|
+
} & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children'>;
|
|
58
|
+
|
|
59
|
+
let {
|
|
60
|
+
items,
|
|
61
|
+
'aria-label': ariaLabel,
|
|
62
|
+
'aria-labelledby': ariaLabelledBy,
|
|
63
|
+
getKey = (item: T) => (item as { id: TransferListKey }).id,
|
|
64
|
+
value = $bindable(),
|
|
65
|
+
defaultValue,
|
|
66
|
+
controlledValue = false,
|
|
67
|
+
onChange,
|
|
68
|
+
disabledKeys,
|
|
69
|
+
name,
|
|
70
|
+
moveShortcut = true,
|
|
71
|
+
class: className = '',
|
|
72
|
+
children,
|
|
73
|
+
...restProps
|
|
74
|
+
}: TransferListRootProps = $props();
|
|
75
|
+
|
|
76
|
+
// Controlled-ness is opt-in, never inferred from `value` being defined: `bind:value={keys}`
|
|
77
|
+
// and `value={keys}` are indistinguishable at runtime, and inferring it silently breaks
|
|
78
|
+
// every `bind:value` (the same trap documented in ListBox and Dialog).
|
|
79
|
+
const isControlled = untrack(() => controlledValue);
|
|
80
|
+
|
|
81
|
+
let internalValue = $state<TransferListKey[]>(untrack(() => [...(value ?? defaultValue ?? [])]));
|
|
82
|
+
const targetKeys = $derived(isControlled ? (value ?? []) : internalValue);
|
|
83
|
+
|
|
84
|
+
// Keeps the uncontrolled copy in step when the parent assigns `value` directly.
|
|
85
|
+
$effect(() => {
|
|
86
|
+
if (isControlled || value === undefined) return;
|
|
87
|
+
const next = value;
|
|
88
|
+
untrack(() => {
|
|
89
|
+
if (!sameKeys(next, internalValue)) internalValue = [...next];
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
function sameKeys(a: TransferListKey[], b: TransferListKey[]) {
|
|
94
|
+
return a.length === b.length && a.every((key, index) => key === b[index]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const allItems = $derived(items ? Array.from(items) : []);
|
|
98
|
+
const disabledSet = $derived(new Set(disabledKeys ?? []));
|
|
99
|
+
|
|
100
|
+
const itemsByKey = $derived.by(() => {
|
|
101
|
+
// A plain Map on purpose: it is rebuilt wholesale whenever `items` changes and never
|
|
102
|
+
// mutated afterwards, so the reactivity is the derived's. A SvelteMap here would proxy
|
|
103
|
+
// every entry to track writes that never happen.
|
|
104
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity
|
|
105
|
+
const map = new Map<TransferListKey, T>();
|
|
106
|
+
for (const item of allItems) {
|
|
107
|
+
const key = getKey(item);
|
|
108
|
+
if (dev && key === undefined) {
|
|
109
|
+
console.warn(
|
|
110
|
+
'[TransferList.Root]: `getKey` returned undefined for an item. Pass a `getKey` ' +
|
|
111
|
+
'that returns a stable string or number — without one the two sides cannot be ' +
|
|
112
|
+
'told apart.'
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
map.set(key, item);
|
|
116
|
+
}
|
|
117
|
+
return map;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const targetKeySet = $derived(new Set(targetKeys));
|
|
121
|
+
const sourceItems = $derived(allItems.filter((item) => !targetKeySet.has(getKey(item))));
|
|
122
|
+
// Rendered in `value` order, not in `items` order: that is what makes the right-hand
|
|
123
|
+
// list keep the sequence the user built.
|
|
124
|
+
const targetItems = $derived(
|
|
125
|
+
targetKeys.map((key) => itemsByKey.get(key)).filter((item): item is T => item !== undefined)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
let sourceSelection = $state<TransferListKey[]>([]);
|
|
129
|
+
let targetSelection = $state<TransferListKey[]>([]);
|
|
130
|
+
let sourceLabel = $state('');
|
|
131
|
+
let targetLabel = $state('');
|
|
132
|
+
let sourceListContext = $state<ListBoxContext | null>(null);
|
|
133
|
+
let targetListContext = $state<ListBoxContext | null>(null);
|
|
134
|
+
let sourceListElement = $state<HTMLElement | null>(null);
|
|
135
|
+
let targetListElement = $state<HTMLElement | null>(null);
|
|
136
|
+
let lastMove = $state<TransferListMoveDetails | null>(null);
|
|
137
|
+
/**
|
|
138
|
+
* The keys each list is actually showing, once its `filter` has run.
|
|
139
|
+
*
|
|
140
|
+
* `null` until a list registers — which on the server is never, since effects do not run
|
|
141
|
+
* there — and the fallback is then everything on that side. Distinguishing that from an
|
|
142
|
+
* empty array matters: a filter that matches nothing must disable the buttons, while a
|
|
143
|
+
* list that has not reported yet must not.
|
|
144
|
+
*/
|
|
145
|
+
let sourceVisibleKeys = $state<TransferListKey[] | null>(null);
|
|
146
|
+
let targetVisibleKeys = $state<TransferListKey[] | null>(null);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Drops keys that are no longer on the side they were selected in.
|
|
150
|
+
*
|
|
151
|
+
* This is what deselects the items a move took away — rather than clearing both
|
|
152
|
+
* selections when the move is requested, which would also wipe the user's selection in
|
|
153
|
+
* controlled mode when the parent rejected the move and nothing actually went anywhere.
|
|
154
|
+
* It also covers the parent reassigning `value` from the outside.
|
|
155
|
+
*
|
|
156
|
+
* Moved items therefore arrive deselected in the destination, so the very next click on
|
|
157
|
+
* the opposite button is never an accidental undo.
|
|
158
|
+
*/
|
|
159
|
+
$effect(() => {
|
|
160
|
+
const onSource = new Set(sourceItems.map(getKey));
|
|
161
|
+
const onTarget = new Set(targetItems.map(getKey));
|
|
162
|
+
untrack(() => {
|
|
163
|
+
const nextSource = sourceSelection.filter((key) => onSource.has(key));
|
|
164
|
+
if (nextSource.length !== sourceSelection.length) sourceSelection = nextSource;
|
|
165
|
+
const nextTarget = targetSelection.filter((key) => onTarget.has(key));
|
|
166
|
+
if (nextTarget.length !== targetSelection.length) targetSelection = nextTarget;
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
function itemsOf(side: 'source' | 'target') {
|
|
171
|
+
return side === 'source' ? sourceItems : targetItems;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Keys that a button is allowed to move off `side`.
|
|
176
|
+
*
|
|
177
|
+
* Measured over what the list is *showing*: with a filter applied, "move all" means the
|
|
178
|
+
* rows the user can see, which is what makes the button honest — moving items that are
|
|
179
|
+
* filtered out would be invisible work.
|
|
180
|
+
*/
|
|
181
|
+
function movableKeysOf(side: 'source' | 'target') {
|
|
182
|
+
const visible = side === 'source' ? sourceVisibleKeys : targetVisibleKeys;
|
|
183
|
+
const keys = visible ?? itemsOf(side).map(getKey);
|
|
184
|
+
return keys.filter((key) => !disabledSet.has(key));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function move(keys: Iterable<TransferListKey>, to: 'source' | 'target') {
|
|
188
|
+
const from = oppositeSide(to);
|
|
189
|
+
const requested = new Set(keys);
|
|
190
|
+
// Ordered by the originating list rather than by the caller's set: items arrive in
|
|
191
|
+
// the order they were shown, which is the order the user meant.
|
|
192
|
+
const moving = movableKeysOf(from).filter((key) => requested.has(key));
|
|
193
|
+
if (moving.length === 0) return;
|
|
194
|
+
|
|
195
|
+
const next =
|
|
196
|
+
to === 'target'
|
|
197
|
+
? [...targetKeys, ...moving]
|
|
198
|
+
: targetKeys.filter((key) => !moving.includes(key));
|
|
199
|
+
|
|
200
|
+
commit(next, { type: 'move', keys: moving, from, to });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function commit(next: TransferListKey[], details: TransferListMoveDetails) {
|
|
204
|
+
lastMove = details;
|
|
205
|
+
|
|
206
|
+
if (!isControlled) {
|
|
207
|
+
internalValue = next;
|
|
208
|
+
value = next;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
onChange?.(next, details);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Shuffles the selected keys one position within the right-hand order.
|
|
216
|
+
*
|
|
217
|
+
* Each selected key swaps with its neighbour unless that neighbour is selected too, so a
|
|
218
|
+
* contiguous block travels together and a selection already flush against the end simply
|
|
219
|
+
* does not move. Walking from the leading edge is what keeps the block's internal order.
|
|
220
|
+
*
|
|
221
|
+
* It works on the whole `value`, not on what a filter happens to be showing: the order
|
|
222
|
+
* being edited is the one that gets submitted.
|
|
223
|
+
*/
|
|
224
|
+
function reorderedTarget(direction: 'up' | 'down') {
|
|
225
|
+
const selected = new Set(targetSelection);
|
|
226
|
+
if (selected.size === 0) return null;
|
|
227
|
+
|
|
228
|
+
const next = [...targetKeys];
|
|
229
|
+
if (direction === 'up') {
|
|
230
|
+
for (let index = 1; index < next.length; index += 1) {
|
|
231
|
+
if (selected.has(next[index]) && !selected.has(next[index - 1])) {
|
|
232
|
+
[next[index - 1], next[index]] = [next[index], next[index - 1]];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
for (let index = next.length - 2; index >= 0; index -= 1) {
|
|
237
|
+
if (selected.has(next[index]) && !selected.has(next[index + 1])) {
|
|
238
|
+
[next[index], next[index + 1]] = [next[index + 1], next[index]];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return sameKeys(next, targetKeys) ? null : next;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function reorder(direction: 'up' | 'down') {
|
|
247
|
+
const next = reorderedTarget(direction);
|
|
248
|
+
if (!next) return;
|
|
249
|
+
|
|
250
|
+
commit(next, {
|
|
251
|
+
type: 'reorder',
|
|
252
|
+
keys: [...targetSelection],
|
|
253
|
+
from: 'target',
|
|
254
|
+
to: 'target',
|
|
255
|
+
direction
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const ctx: TransferListContext<T> = {
|
|
260
|
+
get getKey() {
|
|
261
|
+
return getKey;
|
|
262
|
+
},
|
|
263
|
+
get sourceItems() {
|
|
264
|
+
return sourceItems;
|
|
265
|
+
},
|
|
266
|
+
get targetItems() {
|
|
267
|
+
return targetItems;
|
|
268
|
+
},
|
|
269
|
+
isDisabled: (key) => disabledSet.has(key),
|
|
270
|
+
getItems: itemsOf,
|
|
271
|
+
getMovableKeys: movableKeysOf,
|
|
272
|
+
getMovableSelection: (side) => {
|
|
273
|
+
const selected = new Set(side === 'source' ? sourceSelection : targetSelection);
|
|
274
|
+
return movableKeysOf(side).filter((key) => selected.has(key));
|
|
275
|
+
},
|
|
276
|
+
getSelection: (side) => (side === 'source' ? sourceSelection : targetSelection),
|
|
277
|
+
setSelection: (side, keys) => {
|
|
278
|
+
if (side === 'source') sourceSelection = keys;
|
|
279
|
+
else targetSelection = keys;
|
|
280
|
+
},
|
|
281
|
+
getLabel: (side) => (side === 'source' ? sourceLabel : targetLabel),
|
|
282
|
+
setLabel: (side, label) => {
|
|
283
|
+
if (side === 'source') sourceLabel = label;
|
|
284
|
+
else targetLabel = label;
|
|
285
|
+
},
|
|
286
|
+
getListContext: (side) => (side === 'source' ? sourceListContext : targetListContext),
|
|
287
|
+
setListContext: (side, listContext) => {
|
|
288
|
+
if (side === 'source') sourceListContext = listContext;
|
|
289
|
+
else targetListContext = listContext;
|
|
290
|
+
},
|
|
291
|
+
getListElement: (side) => (side === 'source' ? sourceListElement : targetListElement),
|
|
292
|
+
setListElement: (side, element) => {
|
|
293
|
+
if (side === 'source') sourceListElement = element;
|
|
294
|
+
else targetListElement = element;
|
|
295
|
+
},
|
|
296
|
+
setVisibleKeys: (side, keys) => {
|
|
297
|
+
if (side === 'source') sourceVisibleKeys = keys;
|
|
298
|
+
else targetVisibleKeys = keys;
|
|
299
|
+
},
|
|
300
|
+
move,
|
|
301
|
+
reorder,
|
|
302
|
+
canReorder: (direction) => reorderedTarget(direction) !== null,
|
|
303
|
+
get moveShortcut() {
|
|
304
|
+
return moveShortcut;
|
|
305
|
+
},
|
|
306
|
+
get lastMove() {
|
|
307
|
+
return lastMove;
|
|
308
|
+
},
|
|
309
|
+
focusItemAt: (side, index) => {
|
|
310
|
+
const list = itemsOf(side);
|
|
311
|
+
if (list.length === 0) return false;
|
|
312
|
+
const key = getKey(list[Math.min(Math.max(index, 0), list.length - 1)]);
|
|
313
|
+
(side === 'source' ? sourceListContext : targetListContext)?.keyboardNav.focusById(key);
|
|
314
|
+
return true;
|
|
315
|
+
},
|
|
316
|
+
focusList: (side) => {
|
|
317
|
+
(side === 'source' ? sourceListElement : targetListElement)?.focus();
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
setTransferListContext(ctx);
|
|
322
|
+
</script>
|
|
323
|
+
|
|
324
|
+
<!-- Two listboxes and a row of buttons otherwise reach assistive technology as unrelated
|
|
325
|
+
controls. Named, they are one thing — but only when named: an unlabelled group is skipped,
|
|
326
|
+
so the role is added exactly when there is a name to hang on it. -->
|
|
327
|
+
<div
|
|
328
|
+
class={className}
|
|
329
|
+
role={ariaLabel || ariaLabelledBy ? 'group' : undefined}
|
|
330
|
+
aria-label={ariaLabel}
|
|
331
|
+
aria-labelledby={ariaLabelledBy}
|
|
332
|
+
data-transfer-list
|
|
333
|
+
{...restProps}
|
|
334
|
+
>
|
|
335
|
+
{@render children?.()}
|
|
336
|
+
|
|
337
|
+
{#if name}
|
|
338
|
+
{#each targetKeys as key (key)}
|
|
339
|
+
<input type="hidden" {name} value={key} />
|
|
340
|
+
{/each}
|
|
341
|
+
{/if}
|
|
342
|
+
</div>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import { type TransferListKey, type TransferListMoveDetails } from './types';
|
|
4
|
+
declare function $$render<T extends object = object>(): {
|
|
5
|
+
props: {
|
|
6
|
+
/** The whole collection, in the order the left-hand list shows it. */
|
|
7
|
+
items?: Iterable<T>;
|
|
8
|
+
/** Identity of an item. Defaults to its `id` field. */
|
|
9
|
+
getKey?: (item: T) => TransferListKey;
|
|
10
|
+
/** Keys on the right, in order. Two-way by default — use `bind:value`. */
|
|
11
|
+
value?: TransferListKey[];
|
|
12
|
+
/** Initial right-hand keys, for when `value` is not supplied. */
|
|
13
|
+
defaultValue?: TransferListKey[];
|
|
14
|
+
/**
|
|
15
|
+
* Opt into fully controlled state: the component stops writing back to `value` and
|
|
16
|
+
* only reports through `onChange`, so the parent can reject a move by not flowing
|
|
17
|
+
* the new value back down. Off by default, because `bind:value` — the common case —
|
|
18
|
+
* needs the write-back to work at all.
|
|
19
|
+
*/
|
|
20
|
+
controlledValue?: boolean;
|
|
21
|
+
/** Called after a move, with the new value and what moved. */
|
|
22
|
+
onChange?: (value: TransferListKey[], details: TransferListMoveDetails) => void;
|
|
23
|
+
/** Keys that cannot be moved off the side they are on. */
|
|
24
|
+
disabledKeys?: Iterable<TransferListKey>;
|
|
25
|
+
/**
|
|
26
|
+
* Name for the hidden inputs that carry `value`, so the field submits with the form
|
|
27
|
+
* without any wiring. One input per key, in order; nothing is rendered when the right
|
|
28
|
+
* is empty, which is how a multi-value field behaves natively.
|
|
29
|
+
*/
|
|
30
|
+
name?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether `Ctrl`/`Cmd`+`Enter` inside a list sends its selection to the other one.
|
|
33
|
+
*
|
|
34
|
+
* Each list has exactly one destination, so the shortcut needs no direction — which
|
|
35
|
+
* also means it stays correct when the layout is mirrored. Turn it off if it collides
|
|
36
|
+
* with something in the surrounding app.
|
|
37
|
+
*/
|
|
38
|
+
moveShortcut?: boolean;
|
|
39
|
+
/** CSS class for the wrapper element. */
|
|
40
|
+
class?: string;
|
|
41
|
+
/** The two lists, the move buttons, and anything else the layout needs. */
|
|
42
|
+
children?: Snippet;
|
|
43
|
+
} & Omit<HTMLAttributes<HTMLDivElement>, "children" | "class">;
|
|
44
|
+
exports: {};
|
|
45
|
+
bindings: "value";
|
|
46
|
+
slots: {};
|
|
47
|
+
events: {};
|
|
48
|
+
};
|
|
49
|
+
declare class __sveltets_Render<T extends object = object> {
|
|
50
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
51
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
52
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
53
|
+
bindings(): "value";
|
|
54
|
+
exports(): {};
|
|
55
|
+
}
|
|
56
|
+
interface $$IsomorphicComponent {
|
|
57
|
+
new <T extends object = object>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
58
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
59
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
60
|
+
<T extends object = object>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
61
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
62
|
+
}
|
|
63
|
+
declare const TransferListRoot: $$IsomorphicComponent;
|
|
64
|
+
type TransferListRoot<T extends object = object> = InstanceType<typeof TransferListRoot<T>>;
|
|
65
|
+
export default TransferListRoot;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Which of the two lists something refers to. */
|
|
2
|
+
export type TransferListSide = 'source' | 'target';
|
|
3
|
+
/** Identity of an item, as returned by the Root's `getKey`. */
|
|
4
|
+
export type TransferListKey = string | number;
|
|
5
|
+
/** What a completed change reports to `onChange` and to the live region. */
|
|
6
|
+
export type TransferListMoveDetails = {
|
|
7
|
+
/**
|
|
8
|
+
* Whether items crossed to the other list, or were reordered within the right-hand one.
|
|
9
|
+
* A reorder has `from` and `to` both `'target'`.
|
|
10
|
+
*/
|
|
11
|
+
type: 'move' | 'reorder';
|
|
12
|
+
/** The keys that actually moved — disabled items are never among them. */
|
|
13
|
+
keys: TransferListKey[];
|
|
14
|
+
/** The list they came from. */
|
|
15
|
+
from: TransferListSide;
|
|
16
|
+
/** The list they landed in. */
|
|
17
|
+
to: TransferListSide;
|
|
18
|
+
/** Which way a reorder went. Absent for a move. */
|
|
19
|
+
direction?: 'up' | 'down';
|
|
20
|
+
};
|
|
21
|
+
/** The list opposite `side`. */
|
|
22
|
+
export declare function oppositeSide(side: TransferListSide): TransferListSide;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
import { resolveLocalizedString } from '../../internal/localized-strings';
|
|
4
|
+
import { useLocaleContextOptional } from '../../locale-provider/context';
|
|
5
|
+
import { useTransferListContext } from '../root/context';
|
|
6
|
+
import type { TransferListMoveDetails } from '../root/types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* TransferList.Status - Visually-hidden live region that announces each move.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors ComboBox.Status. Without it a move is silent to a screen reader: the items
|
|
12
|
+
* simply stop existing in one list and appear in another, with nothing said about how
|
|
13
|
+
* many went or where.
|
|
14
|
+
*/
|
|
15
|
+
type TransferListStatusProps = {
|
|
16
|
+
/** Build the announced message from the completed move. */
|
|
17
|
+
formatMessage?: (details: TransferListMoveDetails, label: string) => string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
let { formatMessage }: TransferListStatusProps = $props();
|
|
21
|
+
|
|
22
|
+
const localeContext = useLocaleContextOptional();
|
|
23
|
+
const emptyLocaleStore = readable<string | undefined>(undefined);
|
|
24
|
+
const localeStore = localeContext?.locale ?? emptyLocaleStore;
|
|
25
|
+
|
|
26
|
+
const ctx = useTransferListContext('TransferList.Status');
|
|
27
|
+
|
|
28
|
+
const visuallyHiddenStyle =
|
|
29
|
+
'position:fixed;top:0;left:0;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0;';
|
|
30
|
+
|
|
31
|
+
const message = $derived.by(() => {
|
|
32
|
+
const move = ctx.lastMove;
|
|
33
|
+
if (!move) return '';
|
|
34
|
+
|
|
35
|
+
const label = ctx.getLabel(move.to);
|
|
36
|
+
if (formatMessage) return formatMessage(move, label);
|
|
37
|
+
|
|
38
|
+
const count = move.keys.length;
|
|
39
|
+
|
|
40
|
+
// A reorder is silent otherwise: the rows are still there, only their order changed,
|
|
41
|
+
// and nothing about that reaches a screen reader on its own.
|
|
42
|
+
if (move.type === 'reorder') {
|
|
43
|
+
const key =
|
|
44
|
+
move.direction === 'up'
|
|
45
|
+
? count === 1
|
|
46
|
+
? 'transferList.itemMovedUp'
|
|
47
|
+
: 'transferList.itemsMovedUp'
|
|
48
|
+
: count === 1
|
|
49
|
+
? 'transferList.itemMovedDown'
|
|
50
|
+
: 'transferList.itemsMovedDown';
|
|
51
|
+
return resolveLocalizedString($localeStore, key, { count });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return count === 1
|
|
55
|
+
? resolveLocalizedString($localeStore, 'transferList.itemMoved', { label })
|
|
56
|
+
: resolveLocalizedString($localeStore, 'transferList.itemsMoved', { count, label });
|
|
57
|
+
});
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<!-- Tagged because it is not the only live region on the page: every ButtonRoot ships one
|
|
61
|
+
for its pending state, so "the status element" is ambiguous without this. -->
|
|
62
|
+
<span
|
|
63
|
+
data-transfer-list-status
|
|
64
|
+
role="status"
|
|
65
|
+
aria-live="polite"
|
|
66
|
+
aria-atomic="true"
|
|
67
|
+
style={visuallyHiddenStyle}
|
|
68
|
+
>
|
|
69
|
+
{message}
|
|
70
|
+
</span>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { TransferListMoveDetails } from '../root/types';
|
|
2
|
+
/**
|
|
3
|
+
* TransferList.Status - Visually-hidden live region that announces each move.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors ComboBox.Status. Without it a move is silent to a screen reader: the items
|
|
6
|
+
* simply stop existing in one list and appear in another, with nothing said about how
|
|
7
|
+
* many went or where.
|
|
8
|
+
*/
|
|
9
|
+
type TransferListStatusProps = {
|
|
10
|
+
/** Build the announced message from the completed move. */
|
|
11
|
+
formatMessage?: (details: TransferListMoveDetails, label: string) => string;
|
|
12
|
+
};
|
|
13
|
+
declare const TransferListStatus: import("svelte").Component<TransferListStatusProps, {}, "">;
|
|
14
|
+
type TransferListStatus = ReturnType<typeof TransferListStatus>;
|
|
15
|
+
export default TransferListStatus;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@human-kit/ui",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Agustin Delgado <agustindariodelgado@gmail.com>",
|
|
@@ -128,6 +128,11 @@
|
|
|
128
128
|
"svelte": "./dist/listbox/index.js",
|
|
129
129
|
"default": "./dist/listbox/index.js"
|
|
130
130
|
},
|
|
131
|
+
"./transfer-list": {
|
|
132
|
+
"types": "./dist/transfer-list/index.d.ts",
|
|
133
|
+
"svelte": "./dist/transfer-list/index.js",
|
|
134
|
+
"default": "./dist/transfer-list/index.js"
|
|
135
|
+
},
|
|
131
136
|
"./menu": {
|
|
132
137
|
"types": "./dist/menu/index.d.ts",
|
|
133
138
|
"svelte": "./dist/menu/index.js",
|