@barqjs/aria 0.2.0-next.0 → 0.2.0-next.1
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 +24 -13
- package/package.json +127 -37
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +21 -20
- package/src/button.tsx +151 -25
- package/src/calendar.tsx +1019 -92
- package/src/checkbox.tsx +214 -52
- package/src/collections.ts +88 -5
- package/src/color.ts +1580 -23
- package/src/colorpicker.tsx +698 -153
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +635 -113
- package/src/command.tsx +78 -59
- package/src/date.ts +31 -9
- package/src/datefield.tsx +1451 -336
- package/src/datepicker.tsx +465 -114
- package/src/dialog.tsx +302 -115
- package/src/disclosure.tsx +81 -29
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +3 -6
- package/src/drag-and-drop.tsx +669 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/form.tsx +18 -18
- package/src/gridlist.tsx +912 -121
- package/src/group.tsx +146 -0
- package/src/index.ts +402 -0
- package/src/interactions/focus-events.ts +13 -5
- package/src/interactions/keyboard.ts +195 -2
- package/src/interactions/modality.ts +18 -6
- package/src/interactions/open-link.ts +46 -13
- package/src/interactions/press.ts +35 -18
- package/src/label.ts +18 -6
- package/src/link.tsx +146 -50
- package/src/listbox.tsx +615 -89
- package/src/live.ts +4 -0
- package/src/menu.tsx +182 -89
- package/src/menubar.tsx +21 -17
- package/src/navigation-tree.tsx +357 -0
- package/src/numberfield.tsx +164 -60
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +364 -62
- package/src/platform.ts +2 -1
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +109 -104
- package/src/radio.tsx +101 -27
- package/src/select.tsx +225 -145
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +73 -16
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +139 -44
- package/src/switch.tsx +47 -21
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +198 -89
- package/src/tag.tsx +217 -32
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +103 -37
- package/src/toast-components.tsx +589 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +38 -0
- package/src/toggle.ts +68 -5
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +15 -10
- package/src/tooltip.tsx +46 -29
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +30 -1
- package/src/validation.ts +79 -22
- package/src/virtualizer.tsx +186 -23
package/src/listbox.tsx
CHANGED
|
@@ -14,31 +14,60 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
|
+
cells,
|
|
17
18
|
type Accessor,
|
|
18
19
|
type Child,
|
|
20
|
+
createMemo,
|
|
19
21
|
createSignal,
|
|
20
22
|
For,
|
|
21
23
|
getOwner,
|
|
22
24
|
type Incoming,
|
|
23
25
|
install,
|
|
24
26
|
onCleanup,
|
|
27
|
+
Show,
|
|
25
28
|
} from "@barqjs/core";
|
|
26
29
|
import { createContext, getContext, provide } from "@barqjs/core";
|
|
27
30
|
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
31
|
+
import { useAutocompleteCollection } from "./autocomplete.tsx";
|
|
28
32
|
import type {
|
|
33
|
+
Collection,
|
|
34
|
+
DisabledBehavior,
|
|
29
35
|
ItemAccessors,
|
|
30
36
|
Key,
|
|
31
37
|
LayoutDelegate,
|
|
32
38
|
ListStateOptions,
|
|
33
39
|
Node,
|
|
40
|
+
SelectionBehavior,
|
|
34
41
|
SelectionMode,
|
|
35
42
|
} from "./collections.ts";
|
|
36
|
-
import { listState, type ListState } from "./collections.ts";
|
|
43
|
+
import { getItemCount, listState, type ListState } from "./collections.ts";
|
|
44
|
+
import {
|
|
45
|
+
type CollectionDragAndDrop,
|
|
46
|
+
collectionDragAndDrop,
|
|
47
|
+
type DragAndDropHooks,
|
|
48
|
+
dropIndicatorStyle,
|
|
49
|
+
type DropIndicatorProps,
|
|
50
|
+
provideDropIndicator,
|
|
51
|
+
useDragAndDropContext,
|
|
52
|
+
} from "./drag-and-drop.tsx";
|
|
53
|
+
import { afterDropIndicatorTargets, type ItemDropTarget } from "./dnd/index.ts";
|
|
37
54
|
import { focusRing } from "./focus.ts";
|
|
38
|
-
import {
|
|
39
|
-
import
|
|
55
|
+
import { collator, useLocale } from "./i18n.ts";
|
|
56
|
+
import { focused, focusWithin } from "./interactions/focus-events.ts";
|
|
57
|
+
import { hover, type HoverEvent } from "./interactions/hover.ts";
|
|
58
|
+
import { type BaseEvent, keyboard } from "./interactions/keyboard.ts";
|
|
59
|
+
import { focusVisible, isFocusVisible as focusIsVisible } from "./interactions/modality.ts";
|
|
60
|
+
import type { ElementRef, PressEvent } from "./interactions/press.ts";
|
|
40
61
|
import { field, type FieldOptions } from "./label.ts";
|
|
41
|
-
import {
|
|
62
|
+
import { SelectionIndicatorProvider } from "./selection-indicator.tsx";
|
|
63
|
+
import {
|
|
64
|
+
type KeyboardDelegate,
|
|
65
|
+
ListKeyboardDelegate,
|
|
66
|
+
selectableItem,
|
|
67
|
+
selectableList,
|
|
68
|
+
type LinkBehavior,
|
|
69
|
+
} from "./selection.ts";
|
|
70
|
+
import { SharedElementTransition } from "./shared-element-transition.tsx";
|
|
42
71
|
import {
|
|
43
72
|
provideStaticSection,
|
|
44
73
|
staticAccessors,
|
|
@@ -52,14 +81,17 @@ import {
|
|
|
52
81
|
rowStyle,
|
|
53
82
|
useVirtualizer,
|
|
54
83
|
type LayoutInfo,
|
|
84
|
+
type VirtualizerValue,
|
|
55
85
|
} from "./virtualizer.tsx";
|
|
56
86
|
import {
|
|
57
87
|
access,
|
|
88
|
+
callback,
|
|
58
89
|
type DOMProps,
|
|
59
90
|
filterDOMProps,
|
|
60
91
|
fromProps,
|
|
61
92
|
id,
|
|
62
93
|
type MaybeAccessor,
|
|
94
|
+
mergeIds,
|
|
63
95
|
mergeProps,
|
|
64
96
|
type StyleProps,
|
|
65
97
|
styleProps,
|
|
@@ -71,14 +103,28 @@ export interface ListBoxOptions extends FieldOptions {
|
|
|
71
103
|
shouldFocusWrap?: MaybeAccessor<boolean | undefined>;
|
|
72
104
|
/** @default "vertical" */
|
|
73
105
|
orientation?: MaybeAccessor<"horizontal" | "vertical" | undefined>;
|
|
106
|
+
/** Whether the options are a stack or a grid the arrows move across. @default "stack" */
|
|
107
|
+
layout?: MaybeAccessor<"stack" | "grid" | undefined>;
|
|
74
108
|
autoFocus?: MaybeAccessor<boolean | "first" | "last" | undefined>;
|
|
75
109
|
/** Track focus with `aria-activedescendant`, for a combobox. */
|
|
76
110
|
shouldUseVirtualFocus?: MaybeAccessor<boolean | undefined>;
|
|
77
111
|
/** Select on pointer up. A menu-like listbox wants this. */
|
|
78
112
|
shouldSelectOnPressUp?: MaybeAccessor<boolean | undefined>;
|
|
113
|
+
/** Move focus to an option the pointer moves over. */
|
|
114
|
+
shouldFocusOnHover?: MaybeAccessor<boolean | undefined>;
|
|
115
|
+
/**
|
|
116
|
+
* The selection behaviour the list was given, which decides how a link
|
|
117
|
+
* behaves. Read from the option rather than the state, which a long press
|
|
118
|
+
* changes. @default "toggle"
|
|
119
|
+
*/
|
|
120
|
+
selectionBehavior?: MaybeAccessor<SelectionBehavior | undefined>;
|
|
79
121
|
disallowEmptySelection?: MaybeAccessor<boolean | undefined>;
|
|
80
122
|
disallowTypeAhead?: MaybeAccessor<boolean | undefined>;
|
|
123
|
+
/** @default "action" under replace selection, "override" under toggle */
|
|
81
124
|
linkBehavior?: MaybeAccessor<LinkBehavior | undefined>;
|
|
125
|
+
/** @default "clearSelection" */
|
|
126
|
+
escapeKeyBehavior?: MaybeAccessor<"clearSelection" | "none" | undefined>;
|
|
127
|
+
keyboardDelegate?: KeyboardDelegate;
|
|
82
128
|
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
83
129
|
/** Only a WINDOW of the collection is rendered. See `virtualizer.tsx`. */
|
|
84
130
|
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
@@ -86,6 +132,15 @@ export interface ListBoxOptions extends FieldOptions {
|
|
|
86
132
|
layoutDelegate?: LayoutDelegate;
|
|
87
133
|
/** Where the option ids come from. Shared with whatever names one. */
|
|
88
134
|
baseId?: MaybeAccessor<string | undefined>;
|
|
135
|
+
/** Called when an option is acted on, with its key. */
|
|
136
|
+
onAction?: (key: Key) => void;
|
|
137
|
+
/** Whether `onAction` does anything. @default `onAction !== undefined` */
|
|
138
|
+
hasAction?: MaybeAccessor<boolean | undefined>;
|
|
139
|
+
/** Focus entering the list from outside it. */
|
|
140
|
+
onFocus?: (event: FocusEvent) => void;
|
|
141
|
+
/** Focus leaving the list altogether. */
|
|
142
|
+
onBlur?: (event: FocusEvent) => void;
|
|
143
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
89
144
|
}
|
|
90
145
|
|
|
91
146
|
export interface ListBoxResult {
|
|
@@ -97,19 +152,81 @@ export interface ListBoxResult {
|
|
|
97
152
|
baseId: Accessor<string>;
|
|
98
153
|
}
|
|
99
154
|
|
|
155
|
+
/** What a list tells its options, whichever component renders them. */
|
|
156
|
+
interface ListData {
|
|
157
|
+
hasAction: Accessor<boolean>;
|
|
158
|
+
onAction: (key: Key) => void;
|
|
159
|
+
linkBehavior: Accessor<LinkBehavior>;
|
|
160
|
+
shouldSelectOnPressUp: Accessor<boolean | undefined>;
|
|
161
|
+
shouldFocusOnHover: Accessor<boolean | undefined>;
|
|
162
|
+
shouldUseVirtualFocus: Accessor<boolean | undefined>;
|
|
163
|
+
isVirtualized: Accessor<boolean | undefined>;
|
|
164
|
+
focusVisible: Accessor<boolean>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Keyed by the state rather than handed through context, so an option built
|
|
169
|
+
* by a select or a combobox, which supply their own context, still hears what
|
|
170
|
+
* the list it is in was told.
|
|
171
|
+
*/
|
|
172
|
+
const listData = new WeakMap<ListState<unknown>, ListData>();
|
|
173
|
+
|
|
100
174
|
export function listBox(options: ListBoxOptions, state: ListState<unknown>): ListBoxResult {
|
|
101
175
|
const baseId = id(options.baseId);
|
|
176
|
+
const locale = useLocale();
|
|
102
177
|
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = field({
|
|
103
178
|
...options,
|
|
104
179
|
// A listbox is not a labelable element.
|
|
105
180
|
labelElementType: "span",
|
|
106
181
|
});
|
|
107
182
|
|
|
183
|
+
// Nothing inside a listbox can select a link under toggle selection, since
|
|
184
|
+
// checkboxes are not allowed in one, so there a link always overrides.
|
|
185
|
+
const linkBehavior = (): LinkBehavior => {
|
|
186
|
+
const behavior = access(options.selectionBehavior) ?? "toggle";
|
|
187
|
+
const given = access(options.linkBehavior) ?? (behavior === "replace" ? "action" : "override");
|
|
188
|
+
return behavior === "toggle" && given === "action" ? "override" : given;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const search = collator({ usage: "search", sensitivity: "base" });
|
|
192
|
+
const keyboardDelegate =
|
|
193
|
+
options.keyboardDelegate ??
|
|
194
|
+
new ListKeyboardDelegate({
|
|
195
|
+
collection: state.collection,
|
|
196
|
+
ref: options.ref,
|
|
197
|
+
disabledKeys: state.disabledKeys,
|
|
198
|
+
disabledBehavior: () => state.selectionManager().disabledBehavior,
|
|
199
|
+
collator: search,
|
|
200
|
+
layout: access(options.layout) === "grid" ? "grid" : "stack",
|
|
201
|
+
orientation: options.orientation,
|
|
202
|
+
direction: () => locale().direction,
|
|
203
|
+
layoutDelegate: options.layoutDelegate,
|
|
204
|
+
});
|
|
205
|
+
|
|
108
206
|
const { listProps } = selectableList({
|
|
109
207
|
...options,
|
|
208
|
+
keyboardDelegate,
|
|
110
209
|
selectionManager: state.selectionManager,
|
|
111
210
|
collection: state.collection,
|
|
112
211
|
disabledKeys: state.disabledKeys,
|
|
212
|
+
linkBehavior,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const { focusWithinProps } = focusWithin({
|
|
216
|
+
onFocusWithin: options.onFocus,
|
|
217
|
+
onBlurWithin: options.onBlur,
|
|
218
|
+
onFocusWithinChange: options.onFocusChange,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
listData.set(state, {
|
|
222
|
+
hasAction: () => access(options.hasAction) ?? options.onAction !== undefined,
|
|
223
|
+
onAction: (key) => options.onAction?.(key),
|
|
224
|
+
linkBehavior,
|
|
225
|
+
shouldSelectOnPressUp: () => access(options.shouldSelectOnPressUp),
|
|
226
|
+
shouldFocusOnHover: () => access(options.shouldFocusOnHover),
|
|
227
|
+
shouldUseVirtualFocus: () => access(options.shouldUseVirtualFocus),
|
|
228
|
+
isVirtualized: () => access(options.isVirtualized),
|
|
229
|
+
focusVisible: focusVisible(),
|
|
113
230
|
});
|
|
114
231
|
|
|
115
232
|
return {
|
|
@@ -117,21 +234,26 @@ export function listBox(options: ListBoxOptions, state: ListState<unknown>): Lis
|
|
|
117
234
|
labelProps,
|
|
118
235
|
descriptionProps,
|
|
119
236
|
errorMessageProps,
|
|
120
|
-
listBoxProps: mergeProps(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
237
|
+
listBoxProps: mergeProps(
|
|
238
|
+
filterDOMProps(options, { labelable: true }),
|
|
239
|
+
focusWithinProps,
|
|
240
|
+
listProps,
|
|
241
|
+
fieldProps,
|
|
242
|
+
{
|
|
243
|
+
role: "listbox",
|
|
244
|
+
"aria-multiselectable": () =>
|
|
245
|
+
state.selectionManager().selectionMode === "multiple" ? true : undefined,
|
|
246
|
+
"aria-disabled": () => access(options.isDisabled) === true || undefined,
|
|
247
|
+
"aria-orientation": () => access(options.orientation) ?? "vertical",
|
|
248
|
+
// Under virtual focus the listbox never has DOM focus, so the item that
|
|
249
|
+
// does is named here instead.
|
|
250
|
+
"aria-activedescendant": () => {
|
|
251
|
+
if (access(options.shouldUseVirtualFocus) !== true) return undefined;
|
|
252
|
+
const focusedKey = state.selectionManager().focusedKey;
|
|
253
|
+
return focusedKey === null ? undefined : optionIdFor(baseId(), focusedKey);
|
|
254
|
+
},
|
|
133
255
|
},
|
|
134
|
-
|
|
256
|
+
),
|
|
135
257
|
};
|
|
136
258
|
}
|
|
137
259
|
|
|
@@ -170,13 +292,24 @@ export interface OptionOptions {
|
|
|
170
292
|
/** The listbox's own id, which the option's is derived from. */
|
|
171
293
|
baseId: MaybeAccessor<string>;
|
|
172
294
|
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
295
|
+
/** @default what the list was given */
|
|
173
296
|
shouldSelectOnPressUp?: MaybeAccessor<boolean | undefined>;
|
|
297
|
+
/** @default what the list was given */
|
|
298
|
+
shouldFocusOnHover?: MaybeAccessor<boolean | undefined>;
|
|
299
|
+
/** @default what the list was given */
|
|
174
300
|
shouldUseVirtualFocus?: MaybeAccessor<boolean | undefined>;
|
|
301
|
+
/** @default what the list was given */
|
|
175
302
|
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
176
303
|
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
304
|
+
/** The option's own action, run before the list's `onAction`. */
|
|
177
305
|
onAction?: () => void;
|
|
178
306
|
/** Whether `onAction` does anything. @default `onAction !== undefined` */
|
|
179
307
|
hasAction?: MaybeAccessor<boolean | undefined>;
|
|
308
|
+
onPressStart?: (event: PressEvent) => void;
|
|
309
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
310
|
+
onPressUp?: (event: PressEvent) => void;
|
|
311
|
+
onPress?: (event: PressEvent) => void;
|
|
312
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
180
313
|
}
|
|
181
314
|
|
|
182
315
|
export interface OptionResult {
|
|
@@ -185,32 +318,82 @@ export interface OptionResult {
|
|
|
185
318
|
descriptionProps: DOMProps;
|
|
186
319
|
isSelected: Accessor<boolean>;
|
|
187
320
|
isFocused: Accessor<boolean>;
|
|
321
|
+
/** Focused, and reached by keyboard: virtual focus included, which no DOM focus ring sees. */
|
|
322
|
+
isFocusVisible: Accessor<boolean>;
|
|
188
323
|
isPressed: Accessor<boolean>;
|
|
189
324
|
isDisabled: Accessor<boolean>;
|
|
190
325
|
allowsSelection: Accessor<boolean>;
|
|
326
|
+
/** Whether pressing it does something other than select it. */
|
|
327
|
+
hasAction: Accessor<boolean>;
|
|
191
328
|
}
|
|
192
329
|
|
|
193
330
|
export function option(options: OptionOptions, state: ListState<unknown>): OptionResult {
|
|
194
331
|
const labelId = id();
|
|
195
332
|
const descriptionId = id();
|
|
333
|
+
const data = listData.get(state);
|
|
334
|
+
|
|
335
|
+
const node = (): Node<unknown> | null => state.collection().getItem(options.key);
|
|
336
|
+
const ownAction = (): unknown => node()?.props?.onAction;
|
|
337
|
+
const shouldSelectOnPressUp = (): boolean | undefined =>
|
|
338
|
+
access(options.shouldSelectOnPressUp) ?? data?.shouldSelectOnPressUp();
|
|
339
|
+
const shouldFocusOnHover = (): boolean | undefined =>
|
|
340
|
+
access(options.shouldFocusOnHover) ?? data?.shouldFocusOnHover();
|
|
341
|
+
const shouldUseVirtualFocus = (): boolean | undefined =>
|
|
342
|
+
access(options.shouldUseVirtualFocus) ?? data?.shouldUseVirtualFocus();
|
|
343
|
+
const isVirtualized = (): boolean | undefined =>
|
|
344
|
+
access(options.isVirtualized) ?? data?.isVirtualized();
|
|
196
345
|
|
|
197
346
|
const item = selectableItem({
|
|
198
|
-
|
|
347
|
+
key: options.key,
|
|
348
|
+
ref: options.ref,
|
|
199
349
|
id: () => optionIdFor(access(options.baseId), options.key),
|
|
200
350
|
selectionManager: state.selectionManager,
|
|
351
|
+
isDisabled: options.isDisabled,
|
|
352
|
+
shouldSelectOnPressUp,
|
|
353
|
+
allowsDifferentPressOrigin: () =>
|
|
354
|
+
shouldSelectOnPressUp() === true && shouldFocusOnHover() === true,
|
|
355
|
+
shouldUseVirtualFocus,
|
|
356
|
+
linkBehavior: data?.linkBehavior,
|
|
357
|
+
hasAction: () =>
|
|
358
|
+
(access(options.hasAction) ?? options.onAction !== undefined) ||
|
|
359
|
+
typeof ownAction() === "function" ||
|
|
360
|
+
data?.hasAction() === true,
|
|
361
|
+
onAction: () => {
|
|
362
|
+
options.onAction?.();
|
|
363
|
+
const own = ownAction();
|
|
364
|
+
if (typeof own === "function") (own as () => void)();
|
|
365
|
+
if (data?.hasAction() === true) data.onAction(options.key);
|
|
366
|
+
},
|
|
367
|
+
onPressStart: options.onPressStart,
|
|
368
|
+
onPressEnd: options.onPressEnd,
|
|
369
|
+
onPressUp: options.onPressUp,
|
|
370
|
+
onPress: options.onPress,
|
|
371
|
+
onPressChange: options.onPressChange,
|
|
201
372
|
});
|
|
202
373
|
|
|
203
|
-
const
|
|
374
|
+
const visible = data?.focusVisible ?? focusVisible();
|
|
375
|
+
const { hoverProps } = hover({
|
|
376
|
+
isDisabled: () => item.isDisabled() || shouldFocusOnHover() !== true,
|
|
377
|
+
onHoverStart: () => {
|
|
378
|
+
// Read now rather than from the signal: a pointer moving changes the
|
|
379
|
+
// modality without announcing it.
|
|
380
|
+
if (focusIsVisible()) return;
|
|
381
|
+
state.selectionManager().setFocused(true);
|
|
382
|
+
state.selectionManager().setFocusedKey(options.key);
|
|
383
|
+
},
|
|
384
|
+
});
|
|
204
385
|
|
|
205
386
|
return {
|
|
206
387
|
isSelected: item.isSelected,
|
|
207
388
|
isFocused: item.isFocused,
|
|
389
|
+
isFocusVisible: () => item.isFocused() && visible(),
|
|
208
390
|
isPressed: item.isPressed,
|
|
209
391
|
isDisabled: item.isDisabled,
|
|
210
392
|
allowsSelection: item.allowsSelection,
|
|
393
|
+
hasAction: item.hasAction,
|
|
211
394
|
labelProps: { id: labelId },
|
|
212
395
|
descriptionProps: { id: descriptionId },
|
|
213
|
-
optionProps: mergeProps(item.itemProps, {
|
|
396
|
+
optionProps: mergeProps(item.itemProps, hoverProps, {
|
|
214
397
|
role: "option",
|
|
215
398
|
// `aria-selected` only where selection is possible: on a listbox that
|
|
216
399
|
// only performs actions, the attribute would say every item is
|
|
@@ -220,10 +403,9 @@ export function option(options: OptionOptions, state: ListState<unknown>): Optio
|
|
|
220
403
|
"aria-disabled": () => item.isDisabled() || undefined,
|
|
221
404
|
"aria-label": () => access(options["aria-label"]) ?? node()?.["aria-label"],
|
|
222
405
|
// A virtualised list renders a window, so the position has to be stated.
|
|
223
|
-
"aria-posinset": () =>
|
|
224
|
-
access(options.isVirtualized) === true ? (node()?.index ?? 0) + 1 : undefined,
|
|
406
|
+
"aria-posinset": () => (isVirtualized() === true ? (node()?.index ?? 0) + 1 : undefined),
|
|
225
407
|
"aria-setsize": () =>
|
|
226
|
-
|
|
408
|
+
isVirtualized() === true ? getItemCount(state.collection()) : undefined,
|
|
227
409
|
}),
|
|
228
410
|
};
|
|
229
411
|
}
|
|
@@ -276,6 +458,8 @@ export interface ListBoxContextValue {
|
|
|
276
458
|
* collection, and each one says what it is rather than reading a node.
|
|
277
459
|
*/
|
|
278
460
|
staticItems?: StaticCollection | undefined;
|
|
461
|
+
/** The list's drag and drop, when it was given `dragAndDropHooks`. */
|
|
462
|
+
dragAndDrop?: CollectionDragAndDrop | undefined;
|
|
279
463
|
}
|
|
280
464
|
|
|
281
465
|
const ListBoxContext = createContext<ListBoxContextValue | null>(null);
|
|
@@ -370,9 +554,9 @@ export interface SectionComponentProps extends Omit<StyleProps, "title"> {
|
|
|
370
554
|
* order ACROSS section boundaries, so Down from the last option of one section
|
|
371
555
|
* already reaches the first of the next.
|
|
372
556
|
*/
|
|
373
|
-
export function Section(props:
|
|
557
|
+
export function Section(props: SectionComponentProps) {
|
|
374
558
|
const registry = getContext(ListBoxContext)?.staticItems;
|
|
375
|
-
if (registry !== undefined) return <StaticSection of={props} registry={registry} />;
|
|
559
|
+
if (registry !== undefined) return <StaticSection of={cells(props)} registry={registry} />;
|
|
376
560
|
|
|
377
561
|
const node = useItemNode();
|
|
378
562
|
const render = getContext(ItemRenderContext);
|
|
@@ -385,16 +569,16 @@ export function Section(props: Incoming<SectionComponentProps>) {
|
|
|
385
569
|
// than joining it, and that a section with no heading gets no dangling
|
|
386
570
|
// `aria-labelledby`.
|
|
387
571
|
const { itemProps, headingProps, groupProps } = listBoxSection({
|
|
388
|
-
heading: () => (props.title
|
|
389
|
-
"aria-label": () => props["aria-label"]
|
|
572
|
+
heading: () => (props.title === undefined ? undefined : "titled"),
|
|
573
|
+
"aria-label": () => props["aria-label"],
|
|
390
574
|
});
|
|
391
575
|
|
|
392
576
|
return (
|
|
393
|
-
<li {...itemProps} {...styleProps(props)} data-slot={props["data-slot"]
|
|
577
|
+
<li {...itemProps} {...styleProps(cells(props))} data-slot={props["data-slot"] ?? "section"}>
|
|
394
578
|
<span
|
|
395
579
|
{...headingProps}
|
|
396
|
-
data-slot={props.titleSlot
|
|
397
|
-
class={props.titleClass
|
|
580
|
+
data-slot={props.titleSlot ?? "section-title"}
|
|
581
|
+
class={props.titleClass}
|
|
398
582
|
>
|
|
399
583
|
{props.title}
|
|
400
584
|
</span>
|
|
@@ -433,12 +617,13 @@ export function useSectionHeading(): Accessor<string> | null {
|
|
|
433
617
|
return heading.id;
|
|
434
618
|
}
|
|
435
619
|
|
|
436
|
-
function StaticSection(
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
620
|
+
function StaticSection(sectionProps: {
|
|
621
|
+
of: Incoming<SectionComponentProps>;
|
|
622
|
+
registry: StaticCollection;
|
|
623
|
+
}) {
|
|
624
|
+
const props = sectionProps.of;
|
|
440
625
|
const list = useListBox();
|
|
441
|
-
const section = staticItem(sectionProps.registry
|
|
626
|
+
const section = staticItem(sectionProps.registry, { type: "section", textValue: "" });
|
|
442
627
|
const headingId = id();
|
|
443
628
|
const [isNamed, setNamed] = createSignal(false, { ownedWrite: true });
|
|
444
629
|
const hasTitle = (): boolean => props.title?.() !== undefined;
|
|
@@ -481,6 +666,17 @@ function StaticSection(
|
|
|
481
666
|
);
|
|
482
667
|
}
|
|
483
668
|
|
|
669
|
+
/** What a list hands to its empty state. Every field follows the list. */
|
|
670
|
+
export interface ListBoxRenderState {
|
|
671
|
+
isEmpty: Accessor<boolean>;
|
|
672
|
+
isFocused: Accessor<boolean>;
|
|
673
|
+
isFocusVisible: Accessor<boolean>;
|
|
674
|
+
isDropTarget: Accessor<boolean>;
|
|
675
|
+
layout: Accessor<"stack" | "grid">;
|
|
676
|
+
orientation: Accessor<"horizontal" | "vertical">;
|
|
677
|
+
state: ListState<unknown>;
|
|
678
|
+
}
|
|
679
|
+
|
|
484
680
|
export interface ListBoxComponentProps<T> extends StyleProps, ItemAccessors<T> {
|
|
485
681
|
/**
|
|
486
682
|
* The values to render, in order.
|
|
@@ -496,19 +692,44 @@ export interface ListBoxComponentProps<T> extends StyleProps, ItemAccessors<T> {
|
|
|
496
692
|
errorMessage?: Child;
|
|
497
693
|
/** @default "none" */
|
|
498
694
|
selectionMode?: SelectionMode;
|
|
695
|
+
/** @default "toggle" */
|
|
696
|
+
selectionBehavior?: SelectionBehavior;
|
|
499
697
|
selectedKeys?: "all" | Iterable<Key>;
|
|
500
698
|
defaultSelectedKeys?: "all" | Iterable<Key>;
|
|
501
699
|
disabledKeys?: Iterable<Key>;
|
|
700
|
+
/** Whether `disabledKeys` stops everything, or only selection. @default "all" */
|
|
701
|
+
disabledBehavior?: DisabledBehavior;
|
|
502
702
|
disallowEmptySelection?: boolean;
|
|
503
703
|
shouldFocusWrap?: boolean;
|
|
704
|
+
/** @default "vertical" */
|
|
504
705
|
orientation?: "horizontal" | "vertical";
|
|
706
|
+
/** Whether the options are a stack or a grid the arrows move across. @default "stack" */
|
|
707
|
+
layout?: "stack" | "grid";
|
|
505
708
|
autoFocus?: boolean | "first" | "last";
|
|
506
709
|
isDisabled?: boolean;
|
|
710
|
+
/** Select on pointer up rather than down. */
|
|
711
|
+
shouldSelectOnPressUp?: boolean;
|
|
712
|
+
/** Move focus to an option the pointer moves over. */
|
|
713
|
+
shouldFocusOnHover?: boolean;
|
|
714
|
+
/** @default "clearSelection" */
|
|
715
|
+
escapeKeyBehavior?: "clearSelection" | "none";
|
|
716
|
+
/** What to show when there are no options. */
|
|
717
|
+
renderEmptyState?: (list: ListBoxRenderState) => Child;
|
|
718
|
+
/** Which option each key moves to. @default list navigation for `layout` and `orientation` */
|
|
719
|
+
keyboardDelegate?: KeyboardDelegate;
|
|
507
720
|
"aria-label"?: string;
|
|
508
721
|
"aria-labelledby"?: string;
|
|
722
|
+
"aria-describedby"?: string;
|
|
509
723
|
ref?: RefTarget<HTMLUListElement>;
|
|
510
724
|
onSelectionChange?: (keys: "all" | Set<Key>) => void;
|
|
511
725
|
onAction?: (key: Key) => void;
|
|
726
|
+
/** Focus entering the list from outside it. */
|
|
727
|
+
onFocus?: (event: FocusEvent) => void;
|
|
728
|
+
/** Focus leaving the list altogether. */
|
|
729
|
+
onBlur?: (event: FocusEvent) => void;
|
|
730
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
731
|
+
/** Drag and drop, from `dragAndDrop()`. */
|
|
732
|
+
dragAndDropHooks?: DragAndDropHooks;
|
|
512
733
|
}
|
|
513
734
|
|
|
514
735
|
/**
|
|
@@ -526,60 +747,121 @@ export interface ListBoxComponentProps<T> extends StyleProps, ItemAccessors<T> {
|
|
|
526
747
|
* The items are DATA. A component whose children are lazy and whose body runs
|
|
527
748
|
* once has no reason to render the options twice — once to discover them and
|
|
528
749
|
* once to show them — so the collection is derived from the values directly.
|
|
750
|
+
*
|
|
751
|
+
* Inside an `<Autocomplete>` the list is filtered by what is typed, and
|
|
752
|
+
* highlighted from the field.
|
|
529
753
|
*/
|
|
530
|
-
export function ListBox<T>(props:
|
|
754
|
+
export function ListBox<T>(props: ListBoxComponentProps<T>) {
|
|
531
755
|
const domRef = makeRef<HTMLUListElement>();
|
|
532
|
-
const options = fromProps(props as unknown as Incoming<Record<string, unknown>>);
|
|
756
|
+
const options = fromProps(cells(props) as unknown as Incoming<Record<string, unknown>>);
|
|
533
757
|
const rendered = props.items === undefined ? staticCollection() : null;
|
|
758
|
+
const auto = useAutocompleteCollection();
|
|
534
759
|
|
|
535
760
|
const state = listState<T>({
|
|
536
761
|
...(options as ListStateOptions<T>),
|
|
537
762
|
...(rendered === null
|
|
538
763
|
? {}
|
|
539
764
|
: ({ items: rendered.entries, ...staticAccessors } as unknown as ListStateOptions<T>)),
|
|
540
|
-
|
|
765
|
+
filter: auto?.filterNodes as ListStateOptions<T>["filter"],
|
|
766
|
+
onSelectionChange: (keys) => props.onSelectionChange?.(keys),
|
|
541
767
|
});
|
|
768
|
+
const anyState = state as unknown as ListState<unknown>;
|
|
542
769
|
|
|
543
770
|
// Inside a `<Virtualizer>` the list renders a WINDOW of its collection, and
|
|
544
771
|
// the layout — not the DOM — answers what is above, below and a page away.
|
|
545
772
|
const virtual = useVirtualizer();
|
|
546
|
-
|
|
773
|
+
const dropping: { held?: CollectionDragAndDrop } = {};
|
|
774
|
+
virtual?.attach(
|
|
775
|
+
state.collection,
|
|
776
|
+
domRef,
|
|
777
|
+
() => state.selectionManager().focusedKey,
|
|
778
|
+
() => dropping.held?.persistedKeys() ?? [],
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
// A press selects on release in a draggable list, so that pressing one of
|
|
782
|
+
// several selected items to drag them all does not first select only it.
|
|
783
|
+
const shouldSelectOnPressUp = (): boolean | undefined =>
|
|
784
|
+
dragAndDrop?.dragState !== undefined ? true : props.shouldSelectOnPressUp;
|
|
547
785
|
|
|
548
786
|
const { listBoxProps, labelProps, descriptionProps, errorMessageProps, baseId } = listBox(
|
|
549
787
|
{
|
|
550
788
|
...(options as unknown as ListBoxOptions),
|
|
789
|
+
...autocompleteOptions(auto, options as unknown as ListBoxOptions),
|
|
551
790
|
ref: domRef,
|
|
791
|
+
keyboardDelegate: props.keyboardDelegate,
|
|
792
|
+
shouldSelectOnPressUp,
|
|
793
|
+
onAction: (key) => props.onAction?.(key),
|
|
794
|
+
hasAction: () => props.onAction !== undefined,
|
|
552
795
|
isVirtualized: () => virtual !== null,
|
|
553
796
|
layoutDelegate: virtual?.layout,
|
|
554
797
|
},
|
|
555
|
-
|
|
798
|
+
anyState,
|
|
556
799
|
);
|
|
557
800
|
|
|
801
|
+
const dragAndDrop = listBoxDragAndDrop(props.dragAndDropHooks, anyState, domRef, {
|
|
802
|
+
orientation: () => props.orientation,
|
|
803
|
+
layoutDelegate: virtual?.layout,
|
|
804
|
+
virtual,
|
|
805
|
+
});
|
|
806
|
+
dropping.held = dragAndDrop;
|
|
807
|
+
|
|
808
|
+
if (auto !== null) {
|
|
809
|
+
onCleanup(
|
|
810
|
+
auto.connect({
|
|
811
|
+
ref: domRef,
|
|
812
|
+
state: anyState,
|
|
813
|
+
idFor: (key) => optionIdFor(baseId(), key),
|
|
814
|
+
}),
|
|
815
|
+
);
|
|
816
|
+
}
|
|
817
|
+
|
|
558
818
|
const owner = getOwner();
|
|
559
819
|
if (owner !== null) {
|
|
560
820
|
const value: ListBoxContextValue = {
|
|
561
|
-
state:
|
|
821
|
+
state: anyState,
|
|
562
822
|
baseId,
|
|
563
|
-
shouldSelectOnPressUp
|
|
564
|
-
shouldUseVirtualFocus: () =>
|
|
823
|
+
shouldSelectOnPressUp,
|
|
824
|
+
shouldUseVirtualFocus: () => auto?.collectionProps.shouldUseVirtualFocus(),
|
|
565
825
|
staticItems: rendered ?? undefined,
|
|
826
|
+
dragAndDrop,
|
|
566
827
|
};
|
|
567
828
|
install(owner, ListBoxContext, () => value);
|
|
568
829
|
}
|
|
569
830
|
|
|
831
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
832
|
+
const isEmpty = (): boolean => state.collection().size === 0;
|
|
833
|
+
const layout = (): "stack" | "grid" => props.layout ?? "stack";
|
|
834
|
+
const orientation = (): "horizontal" | "vertical" => props.orientation ?? "vertical";
|
|
835
|
+
const isDropTarget = (): boolean => dragAndDrop?.isRootDropTarget() === true;
|
|
836
|
+
|
|
837
|
+
// Focus entering and leaving is the list's, heard through `listBox`, rather
|
|
838
|
+
// than each element's own focus and blur.
|
|
839
|
+
const {
|
|
840
|
+
onFocus: _onFocus,
|
|
841
|
+
onBlur: _onBlur,
|
|
842
|
+
...forwarded
|
|
843
|
+
} = filterDOMProps(fromProps(cells(props)), { global: true });
|
|
844
|
+
|
|
570
845
|
const elementProps = mergeProps(
|
|
571
846
|
listBoxProps,
|
|
572
|
-
|
|
573
|
-
|
|
847
|
+
focusProps,
|
|
848
|
+
dragAndDrop?.collectionProps,
|
|
849
|
+
forwarded,
|
|
850
|
+
styleProps(cells(props)),
|
|
574
851
|
{
|
|
575
|
-
"data-testid": () => props["data-testid"]
|
|
576
|
-
"data-empty":
|
|
852
|
+
"data-testid": () => props["data-testid"],
|
|
853
|
+
"data-empty": isEmpty,
|
|
854
|
+
"data-focused": isFocused,
|
|
855
|
+
"data-focus-visible": isFocusVisible,
|
|
856
|
+
"data-layout": layout,
|
|
857
|
+
"data-orientation": orientation,
|
|
577
858
|
"data-virtualized": () => virtual !== null,
|
|
578
|
-
|
|
859
|
+
"data-drop-target": isDropTarget,
|
|
860
|
+
style: () => (virtual === null ? props.style : contentStyle(virtual.contentSize())),
|
|
579
861
|
},
|
|
580
862
|
);
|
|
581
863
|
|
|
582
|
-
const render = props.children;
|
|
864
|
+
const render = cells(props).children;
|
|
583
865
|
|
|
584
866
|
/** The whole collection, or the window of it the virtualiser says is on screen. */
|
|
585
867
|
const rows = (): Node<T>[] => {
|
|
@@ -593,36 +875,73 @@ export function ListBox<T>(props: Incoming<ListBoxComponentProps<T>>) {
|
|
|
593
875
|
|
|
594
876
|
provideItemRender(render as unknown as (item: unknown) => Child);
|
|
595
877
|
|
|
596
|
-
|
|
878
|
+
const renderState: ListBoxRenderState = {
|
|
879
|
+
isEmpty,
|
|
880
|
+
isFocused,
|
|
881
|
+
isFocusVisible,
|
|
882
|
+
isDropTarget,
|
|
883
|
+
layout,
|
|
884
|
+
orientation,
|
|
885
|
+
state: anyState,
|
|
886
|
+
};
|
|
887
|
+
const renderEmpty = callback(cells(props).renderEmptyState);
|
|
888
|
+
const emptyState = (): Child => {
|
|
889
|
+
if (!isEmpty() || renderEmpty === undefined) return null;
|
|
597
890
|
return (
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
{props.children}
|
|
602
|
-
</ul>
|
|
603
|
-
<span {...descriptionProps}>{props.description}</span>
|
|
604
|
-
<span {...errorMessageProps}>{props.errorMessage}</span>
|
|
605
|
-
</>
|
|
891
|
+
<li role="option" style={{ display: "contents" }}>
|
|
892
|
+
{renderEmpty(renderState)}
|
|
893
|
+
</li>
|
|
606
894
|
);
|
|
607
|
-
}
|
|
895
|
+
};
|
|
608
896
|
|
|
609
897
|
return (
|
|
610
898
|
<>
|
|
611
899
|
<span {...labelProps}>{props.label}</span>
|
|
612
|
-
<ul {...elementProps} ref={mergeRefs(domRef.set, props.ref
|
|
613
|
-
<
|
|
614
|
-
{
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
900
|
+
<ul {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
|
|
901
|
+
<SharedElementTransition>
|
|
902
|
+
{rendered !== null ? (
|
|
903
|
+
props.children
|
|
904
|
+
) : (
|
|
905
|
+
<For each={rows}>
|
|
906
|
+
{(node: Node<T>) => {
|
|
907
|
+
provideItemNode(node);
|
|
908
|
+
return render(node.value as T);
|
|
909
|
+
}}
|
|
910
|
+
</For>
|
|
911
|
+
)}
|
|
912
|
+
</SharedElementTransition>
|
|
913
|
+
{emptyState}
|
|
619
914
|
</ul>
|
|
915
|
+
{dragAndDrop?.preview}
|
|
620
916
|
<span {...descriptionProps}>{props.description}</span>
|
|
621
917
|
<span {...errorMessageProps}>{props.errorMessage}</span>
|
|
622
918
|
</>
|
|
623
919
|
);
|
|
624
920
|
}
|
|
625
921
|
|
|
922
|
+
/**
|
|
923
|
+
* What an enclosing autocomplete asks of the list: its id, a name, virtual
|
|
924
|
+
* focus, no typeahead of its own. The caller's own name and id come first.
|
|
925
|
+
*/
|
|
926
|
+
function autocompleteOptions(
|
|
927
|
+
auto: ReturnType<typeof useAutocompleteCollection>,
|
|
928
|
+
options: ListBoxOptions & { id?: MaybeAccessor<string | undefined> },
|
|
929
|
+
): Partial<ListBoxOptions> & { id?: MaybeAccessor<string> } {
|
|
930
|
+
if (auto === null) return {};
|
|
931
|
+
const given = auto.collectionProps;
|
|
932
|
+
const own = options.id;
|
|
933
|
+
return {
|
|
934
|
+
// Merged rather than replaced, so the field's `aria-controls` follows the
|
|
935
|
+
// caller's id.
|
|
936
|
+
id: own === undefined ? given.id : mergeIds(own as MaybeAccessor<string>, given.id),
|
|
937
|
+
"aria-label": () => access(options["aria-label"]) ?? given["aria-label"],
|
|
938
|
+
shouldUseVirtualFocus: () =>
|
|
939
|
+
access(options.shouldUseVirtualFocus) ?? given.shouldUseVirtualFocus(),
|
|
940
|
+
disallowTypeAhead: () => access(options.disallowTypeAhead) ?? given.disallowTypeAhead(),
|
|
941
|
+
autoFocus: () => access(options.autoFocus) ?? given.autoFocus(),
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
|
|
626
945
|
export interface OptionComponentProps extends StyleProps {
|
|
627
946
|
children?: Child;
|
|
628
947
|
"aria-label"?: string;
|
|
@@ -638,29 +957,61 @@ export interface OptionComponentProps extends StyleProps {
|
|
|
638
957
|
isDisabled?: boolean;
|
|
639
958
|
/** Keep a written option mounted and take it out of the collection. */
|
|
640
959
|
isHidden?: boolean;
|
|
960
|
+
/**
|
|
961
|
+
* Where a written option leads. One rendered from data reads its item's
|
|
962
|
+
* `href` and the rest.
|
|
963
|
+
*/
|
|
964
|
+
href?: string;
|
|
965
|
+
target?: string;
|
|
966
|
+
rel?: string;
|
|
967
|
+
download?: boolean | string;
|
|
968
|
+
ping?: string;
|
|
969
|
+
referrerPolicy?: string;
|
|
641
970
|
/**
|
|
642
971
|
* Called when the option is acted on: a press where nothing is selectable,
|
|
643
972
|
* and Enter where something is, which is the split every list here makes
|
|
644
|
-
* between choosing a row and opening it.
|
|
973
|
+
* between choosing a row and opening it. The list's own `onAction` runs
|
|
974
|
+
* after it.
|
|
645
975
|
*/
|
|
646
976
|
onAction?: () => void;
|
|
977
|
+
onPress?: (event: PressEvent) => void;
|
|
978
|
+
onPressStart?: (event: PressEvent) => void;
|
|
979
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
980
|
+
onPressUp?: (event: PressEvent) => void;
|
|
981
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
982
|
+
onHoverStart?: (event: HoverEvent) => void;
|
|
983
|
+
onHoverEnd?: (event: HoverEvent) => void;
|
|
984
|
+
onHoverChange?: (isHovered: boolean) => void;
|
|
985
|
+
/** Call `event.continuePropagation()` to let the list handle the key too. */
|
|
986
|
+
onKeyDown?: (event: BaseEvent<KeyboardEvent>) => void;
|
|
987
|
+
onKeyUp?: (event: BaseEvent<KeyboardEvent>) => void;
|
|
988
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
647
989
|
}
|
|
648
990
|
|
|
991
|
+
const LINK_PROPS = ["href", "target", "rel", "download", "ping", "referrerPolicy"] as const;
|
|
992
|
+
|
|
649
993
|
/**
|
|
650
994
|
* One option. Its key, value and disabled state come from the collection node
|
|
651
995
|
* the enclosing {@link ListBox} is rendering, so nothing is repeated here.
|
|
652
996
|
*/
|
|
653
|
-
export function Option(props:
|
|
997
|
+
export function Option(props: OptionComponentProps) {
|
|
654
998
|
const domRef = makeRef<HTMLLIElement>();
|
|
655
999
|
const list = useListBox();
|
|
1000
|
+
const options = fromProps(cells(props));
|
|
656
1001
|
const written =
|
|
657
1002
|
list.staticItems === undefined
|
|
658
1003
|
? null
|
|
659
1004
|
: staticItem(list.staticItems, {
|
|
660
|
-
key: props.value
|
|
661
|
-
textValue: () => props.textValue
|
|
662
|
-
props: () =>
|
|
663
|
-
|
|
1005
|
+
key: props.value,
|
|
1006
|
+
textValue: () => props.textValue,
|
|
1007
|
+
props: () => {
|
|
1008
|
+
const own: Record<string, unknown> = { isDisabled: props.isDisabled === true };
|
|
1009
|
+
for (const name of LINK_PROPS) {
|
|
1010
|
+
if (props[name] !== undefined) own[name] = props[name];
|
|
1011
|
+
}
|
|
1012
|
+
return own;
|
|
1013
|
+
},
|
|
1014
|
+
isHidden: () => props.isHidden,
|
|
664
1015
|
});
|
|
665
1016
|
const node: Pick<Node<unknown>, "key" | "props"> = written === null
|
|
666
1017
|
? useItemNode()
|
|
@@ -669,31 +1020,56 @@ export function Option(props: Incoming<OptionComponentProps>) {
|
|
|
669
1020
|
|
|
670
1021
|
measureRow(virtual, node.key, domRef);
|
|
671
1022
|
|
|
672
|
-
const {
|
|
1023
|
+
const {
|
|
1024
|
+
optionProps,
|
|
1025
|
+
isSelected,
|
|
1026
|
+
isFocused,
|
|
1027
|
+
isFocusVisible,
|
|
1028
|
+
isPressed,
|
|
1029
|
+
isDisabled,
|
|
1030
|
+
allowsSelection,
|
|
1031
|
+
hasAction,
|
|
1032
|
+
} = option(
|
|
673
1033
|
{
|
|
674
1034
|
key: node.key,
|
|
675
1035
|
ref: domRef,
|
|
676
1036
|
baseId: list.baseId,
|
|
677
|
-
"aria-label": () => props["aria-label"]
|
|
678
|
-
isDisabled: () => props.isDisabled
|
|
1037
|
+
"aria-label": () => props["aria-label"],
|
|
1038
|
+
isDisabled: () => props.isDisabled === true || node.props?.isDisabled === true,
|
|
679
1039
|
shouldSelectOnPressUp: list.shouldSelectOnPressUp,
|
|
680
1040
|
shouldUseVirtualFocus: list.shouldUseVirtualFocus,
|
|
681
1041
|
isVirtualized: () => virtual !== null,
|
|
682
|
-
onAction: () => props.onAction?.()
|
|
683
|
-
hasAction: () => props.onAction
|
|
1042
|
+
onAction: () => props.onAction?.(),
|
|
1043
|
+
hasAction: () => props.onAction !== undefined,
|
|
1044
|
+
onPress: options.onPress,
|
|
1045
|
+
onPressStart: options.onPressStart,
|
|
1046
|
+
onPressEnd: options.onPressEnd,
|
|
1047
|
+
onPressUp: options.onPressUp,
|
|
1048
|
+
onPressChange: options.onPressChange,
|
|
684
1049
|
},
|
|
685
1050
|
list.state,
|
|
686
1051
|
);
|
|
687
1052
|
|
|
688
|
-
const
|
|
689
|
-
const
|
|
1053
|
+
const dnd = optionDragAndDrop(list, node.key, domRef, hasAction);
|
|
1054
|
+
const isDraggable = (): boolean =>
|
|
1055
|
+
dnd?.collection.dragState !== undefined && !list.state.selectionManager().isDisabled(node.key);
|
|
1056
|
+
const { hoverProps, isHovered } = hover({
|
|
1057
|
+
isDisabled: () => !allowsSelection() && !hasAction() && !isDraggable(),
|
|
1058
|
+
onHoverStart: options.onHoverStart,
|
|
1059
|
+
onHoverEnd: options.onHoverEnd,
|
|
1060
|
+
onHoverChange: options.onHoverChange,
|
|
1061
|
+
});
|
|
1062
|
+
const { keyboardProps } = keyboard({ onKeyDown: options.onKeyDown, onKeyUp: options.onKeyUp });
|
|
1063
|
+
const { focusProps } = focused({ onFocusChange: options.onFocusChange });
|
|
1064
|
+
const selectionMode = (): SelectionMode => list.state.selectionManager().selectionMode;
|
|
690
1065
|
|
|
691
1066
|
const elementProps = mergeProps(
|
|
692
1067
|
optionProps,
|
|
693
1068
|
hoverProps,
|
|
1069
|
+
keyboardProps,
|
|
694
1070
|
focusProps,
|
|
695
|
-
filterDOMProps(
|
|
696
|
-
styleProps(props),
|
|
1071
|
+
filterDOMProps(options, { global: true, labelable: true }),
|
|
1072
|
+
styleProps(cells(props)),
|
|
697
1073
|
{
|
|
698
1074
|
"data-selected": isSelected,
|
|
699
1075
|
"data-focused": isFocused,
|
|
@@ -701,21 +1077,171 @@ export function Option(props: Incoming<OptionComponentProps>) {
|
|
|
701
1077
|
"data-pressed": isPressed,
|
|
702
1078
|
"data-hovered": isHovered,
|
|
703
1079
|
"data-disabled": isDisabled,
|
|
704
|
-
|
|
705
|
-
|
|
1080
|
+
"data-selection-mode": () => (selectionMode() === "none" ? undefined : selectionMode()),
|
|
1081
|
+
hidden: () => props.isHidden === true || undefined,
|
|
1082
|
+
"data-testid": () => props["data-testid"],
|
|
706
1083
|
// Positioned by the LAYOUT: the rows above it are not rendered, so
|
|
707
1084
|
// document flow has nothing to place it against.
|
|
708
1085
|
style: () => {
|
|
709
|
-
if (virtual === null) return props.style
|
|
1086
|
+
if (virtual === null) return props.style;
|
|
710
1087
|
const info = virtual.layout.getLayoutInfo(node.key);
|
|
711
|
-
return info === null ? props.style
|
|
1088
|
+
return info === null ? props.style : rowStyle(info);
|
|
712
1089
|
},
|
|
713
1090
|
},
|
|
1091
|
+
dnd?.itemProps,
|
|
714
1092
|
);
|
|
715
1093
|
|
|
716
|
-
|
|
717
|
-
<li {...elementProps} ref={mergeRefs(domRef.set, written?.ref, props.ref
|
|
718
|
-
{
|
|
1094
|
+
const element = (
|
|
1095
|
+
<li {...elementProps} ref={mergeRefs(domRef.set, written?.ref, props.ref)}>
|
|
1096
|
+
<SelectionIndicatorProvider isSelected={isSelected()}>
|
|
1097
|
+
{props.children}
|
|
1098
|
+
</SelectionIndicatorProvider>
|
|
719
1099
|
</li>
|
|
720
1100
|
);
|
|
1101
|
+
if (dnd === null) return element;
|
|
1102
|
+
|
|
1103
|
+
return (
|
|
1104
|
+
<>
|
|
1105
|
+
<Show when={dnd.collection.shouldRenderIndicator(dnd.before)}>
|
|
1106
|
+
{dnd.collection.renderDropIndicator(dnd.before)}
|
|
1107
|
+
</Show>
|
|
1108
|
+
{element}
|
|
1109
|
+
<For each={dnd.after()}>
|
|
1110
|
+
{(target: ItemDropTarget) => (
|
|
1111
|
+
<Show when={dnd.collection.shouldRenderIndicator(target)}>
|
|
1112
|
+
{dnd.collection.renderDropIndicator(target)}
|
|
1113
|
+
</Show>
|
|
1114
|
+
)}
|
|
1115
|
+
</For>
|
|
1116
|
+
</>
|
|
1117
|
+
);
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
interface OptionDragAndDrop {
|
|
1121
|
+
collection: CollectionDragAndDrop;
|
|
1122
|
+
itemProps: DOMProps;
|
|
1123
|
+
before: ItemDropTarget;
|
|
1124
|
+
after: Accessor<ItemDropTarget[]>;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
/**
|
|
1128
|
+
* An option's part in its list's drag and drop: dragging it, dropping on it,
|
|
1129
|
+
* and the indicators for the positions before and after it.
|
|
1130
|
+
*/
|
|
1131
|
+
function optionDragAndDrop(
|
|
1132
|
+
list: ListBoxContextValue,
|
|
1133
|
+
key: Key,
|
|
1134
|
+
domRef: Accessor<HTMLLIElement | null>,
|
|
1135
|
+
hasAction: Accessor<boolean>,
|
|
1136
|
+
): OptionDragAndDrop | null {
|
|
1137
|
+
const collection = list.dragAndDrop;
|
|
1138
|
+
if (collection === undefined) return null;
|
|
1139
|
+
const { hooks, dragState, dropState } = collection;
|
|
1140
|
+
|
|
1141
|
+
const draggable =
|
|
1142
|
+
dragState === undefined || hooks?.draggableItem === undefined
|
|
1143
|
+
? null
|
|
1144
|
+
: hooks.draggableItem({ key, hasAction }, dragState);
|
|
1145
|
+
const droppable =
|
|
1146
|
+
dropState === undefined || hooks?.droppableItem === undefined
|
|
1147
|
+
? null
|
|
1148
|
+
: hooks.droppableItem(
|
|
1149
|
+
{ target: { type: "item", key, dropPosition: "on" } },
|
|
1150
|
+
dropState,
|
|
1151
|
+
domRef,
|
|
1152
|
+
);
|
|
1153
|
+
|
|
1154
|
+
const after = createMemo(() => {
|
|
1155
|
+
const items = list.state.collection();
|
|
1156
|
+
const node = items.getItem(key);
|
|
1157
|
+
return node === null ? [] : afterDropIndicatorTargets(items, node);
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1160
|
+
return {
|
|
1161
|
+
collection,
|
|
1162
|
+
before: { type: "item", key, dropPosition: "before" },
|
|
1163
|
+
after,
|
|
1164
|
+
itemProps: mergeProps(draggable?.dragProps, droppable?.dropProps, {
|
|
1165
|
+
"data-allows-dragging": () => dragState !== undefined,
|
|
1166
|
+
"data-dragging": () => dragState?.isDragging(key) === true,
|
|
1167
|
+
"data-drop-target": () => droppable?.isDropTarget() === true,
|
|
1168
|
+
}),
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
function listBoxDragAndDrop(
|
|
1173
|
+
hooks: DragAndDropHooks | undefined,
|
|
1174
|
+
state: ListState<unknown>,
|
|
1175
|
+
domRef: Accessor<HTMLUListElement | null>,
|
|
1176
|
+
layout: {
|
|
1177
|
+
orientation: Accessor<"horizontal" | "vertical" | undefined>;
|
|
1178
|
+
layoutDelegate: LayoutDelegate | undefined;
|
|
1179
|
+
virtual: VirtualizerValue | null;
|
|
1180
|
+
},
|
|
1181
|
+
): CollectionDragAndDrop | undefined {
|
|
1182
|
+
if (hooks === undefined) return undefined;
|
|
1183
|
+
const locale = useLocale();
|
|
1184
|
+
const direction = (): "ltr" | "rtl" => locale().direction;
|
|
1185
|
+
const collection = state.collection as Accessor<Collection<unknown>>;
|
|
1186
|
+
|
|
1187
|
+
const dragAndDrop = collectionDragAndDrop({
|
|
1188
|
+
hooks,
|
|
1189
|
+
collection,
|
|
1190
|
+
selectionManager: state.selectionManager,
|
|
1191
|
+
ref: domRef,
|
|
1192
|
+
// A position next to a disabled option is still a place to drop.
|
|
1193
|
+
keyboardDelegate: new ListKeyboardDelegate({
|
|
1194
|
+
collection,
|
|
1195
|
+
ref: domRef,
|
|
1196
|
+
disabledBehavior: () => "selection",
|
|
1197
|
+
orientation: layout.orientation,
|
|
1198
|
+
direction,
|
|
1199
|
+
layoutDelegate: layout.layoutDelegate,
|
|
1200
|
+
}),
|
|
1201
|
+
orientation: layout.orientation,
|
|
1202
|
+
direction,
|
|
1203
|
+
virtual: layout.virtual,
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
if (dragAndDrop.dropState !== undefined) {
|
|
1207
|
+
provideDropIndicator({ render: (props) => <ListBoxDropIndicator of={props} /> });
|
|
1208
|
+
}
|
|
1209
|
+
return dragAndDrop;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/** A drop indicator in a listbox: an option, so it is in the listbox's own children. */
|
|
1213
|
+
function ListBoxDropIndicator(indicator: { of: Incoming<DropIndicatorProps> }) {
|
|
1214
|
+
const props = indicator.of;
|
|
1215
|
+
const { dragAndDropHooks, dropState } = useDragAndDropContext();
|
|
1216
|
+
if (dragAndDropHooks?.dropIndicator === undefined || dropState === undefined) {
|
|
1217
|
+
throw new Error("A DropIndicator must be rendered inside a droppable ListBox.");
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
const domRef = makeRef<HTMLLIElement>();
|
|
1221
|
+
const virtual = useVirtualizer();
|
|
1222
|
+
const indicatorStyle = (): Record<string, string> | undefined => {
|
|
1223
|
+
const target = props.target();
|
|
1224
|
+
return target.type === "item" ? dropIndicatorStyle(virtual, target) : undefined;
|
|
1225
|
+
};
|
|
1226
|
+
const { dropIndicatorProps, isDropTarget, isHidden } = dragAndDropHooks.dropIndicator(
|
|
1227
|
+
{ target: props.target() },
|
|
1228
|
+
dropState,
|
|
1229
|
+
domRef,
|
|
1230
|
+
);
|
|
1231
|
+
|
|
1232
|
+
return (
|
|
1233
|
+
<Show when={!isHidden()}>
|
|
1234
|
+
<li
|
|
1235
|
+
{...dropIndicatorProps}
|
|
1236
|
+
{...styleProps(props)}
|
|
1237
|
+
{...(virtual === null ? {} : { style: indicatorStyle })}
|
|
1238
|
+
role="option"
|
|
1239
|
+
data-slot={props["data-slot"]?.() ?? "drop-indicator"}
|
|
1240
|
+
data-drop-target={isDropTarget()}
|
|
1241
|
+
ref={domRef.set}
|
|
1242
|
+
>
|
|
1243
|
+
{props.children?.()}
|
|
1244
|
+
</li>
|
|
1245
|
+
</Show>
|
|
1246
|
+
);
|
|
721
1247
|
}
|