@barqjs/aria 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- 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 +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
package/src/listbox.tsx
ADDED
|
@@ -0,0 +1,1247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A listbox: a set of options, one Tab stop, arrow keys inside.
|
|
3
|
+
*
|
|
4
|
+
* Everything that makes it a listbox rather than a styled list is in
|
|
5
|
+
* `selection.ts`; what is here is the roles and the relationships. Two are
|
|
6
|
+
* easy to get wrong:
|
|
7
|
+
*
|
|
8
|
+
* - The options must be the listbox's OWN children in the accessibility tree.
|
|
9
|
+
* A wrapper `<div>` between them without `role="presentation"` breaks the
|
|
10
|
+
* parent/child relationship, and a screen reader stops reporting "2 of 7".
|
|
11
|
+
* - A section is a `role="group"` with `aria-labelledby` pointing at its own
|
|
12
|
+
* heading; the heading itself must not be an option, or it becomes a
|
|
13
|
+
* selectable item that does nothing.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
cells,
|
|
18
|
+
type Accessor,
|
|
19
|
+
type Child,
|
|
20
|
+
createMemo,
|
|
21
|
+
createSignal,
|
|
22
|
+
For,
|
|
23
|
+
getOwner,
|
|
24
|
+
type Incoming,
|
|
25
|
+
install,
|
|
26
|
+
onCleanup,
|
|
27
|
+
Show,
|
|
28
|
+
} from "@barqjs/core";
|
|
29
|
+
import { createContext, getContext, provide } from "@barqjs/core";
|
|
30
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
31
|
+
import { useAutocompleteCollection } from "./autocomplete.tsx";
|
|
32
|
+
import type {
|
|
33
|
+
Collection,
|
|
34
|
+
DisabledBehavior,
|
|
35
|
+
ItemAccessors,
|
|
36
|
+
Key,
|
|
37
|
+
LayoutDelegate,
|
|
38
|
+
ListStateOptions,
|
|
39
|
+
Node,
|
|
40
|
+
SelectionBehavior,
|
|
41
|
+
SelectionMode,
|
|
42
|
+
} 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";
|
|
54
|
+
import { focusRing } from "./focus.ts";
|
|
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";
|
|
61
|
+
import { field, type FieldOptions } from "./label.ts";
|
|
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";
|
|
71
|
+
import {
|
|
72
|
+
provideStaticSection,
|
|
73
|
+
staticAccessors,
|
|
74
|
+
staticCollection,
|
|
75
|
+
staticItem,
|
|
76
|
+
type StaticCollection,
|
|
77
|
+
} from "./static-collection.ts";
|
|
78
|
+
import {
|
|
79
|
+
contentStyle,
|
|
80
|
+
measureRow,
|
|
81
|
+
rowStyle,
|
|
82
|
+
useVirtualizer,
|
|
83
|
+
type LayoutInfo,
|
|
84
|
+
type VirtualizerValue,
|
|
85
|
+
} from "./virtualizer.tsx";
|
|
86
|
+
import {
|
|
87
|
+
access,
|
|
88
|
+
callback,
|
|
89
|
+
type DOMProps,
|
|
90
|
+
filterDOMProps,
|
|
91
|
+
fromProps,
|
|
92
|
+
id,
|
|
93
|
+
type MaybeAccessor,
|
|
94
|
+
mergeIds,
|
|
95
|
+
mergeProps,
|
|
96
|
+
type StyleProps,
|
|
97
|
+
styleProps,
|
|
98
|
+
} from "./utils.ts";
|
|
99
|
+
|
|
100
|
+
export interface ListBoxOptions extends FieldOptions {
|
|
101
|
+
ref: ElementRef;
|
|
102
|
+
/** @default false */
|
|
103
|
+
shouldFocusWrap?: MaybeAccessor<boolean | undefined>;
|
|
104
|
+
/** @default "vertical" */
|
|
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>;
|
|
108
|
+
autoFocus?: MaybeAccessor<boolean | "first" | "last" | undefined>;
|
|
109
|
+
/** Track focus with `aria-activedescendant`, for a combobox. */
|
|
110
|
+
shouldUseVirtualFocus?: MaybeAccessor<boolean | undefined>;
|
|
111
|
+
/** Select on pointer up. A menu-like listbox wants this. */
|
|
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>;
|
|
121
|
+
disallowEmptySelection?: MaybeAccessor<boolean | undefined>;
|
|
122
|
+
disallowTypeAhead?: MaybeAccessor<boolean | undefined>;
|
|
123
|
+
/** @default "action" under replace selection, "override" under toggle */
|
|
124
|
+
linkBehavior?: MaybeAccessor<LinkBehavior | undefined>;
|
|
125
|
+
/** @default "clearSelection" */
|
|
126
|
+
escapeKeyBehavior?: MaybeAccessor<"clearSelection" | "none" | undefined>;
|
|
127
|
+
keyboardDelegate?: KeyboardDelegate;
|
|
128
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
129
|
+
/** Only a WINDOW of the collection is rendered. See `virtualizer.tsx`. */
|
|
130
|
+
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
131
|
+
/** Where the rows are, when the DOM cannot answer because they are not in it. */
|
|
132
|
+
layoutDelegate?: LayoutDelegate;
|
|
133
|
+
/** Where the option ids come from. Shared with whatever names one. */
|
|
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;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface ListBoxResult {
|
|
147
|
+
listBoxProps: DOMProps;
|
|
148
|
+
labelProps: DOMProps;
|
|
149
|
+
descriptionProps: DOMProps;
|
|
150
|
+
errorMessageProps: DOMProps;
|
|
151
|
+
/** The base every option id in this listbox is derived from. */
|
|
152
|
+
baseId: Accessor<string>;
|
|
153
|
+
}
|
|
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
|
+
|
|
174
|
+
export function listBox(options: ListBoxOptions, state: ListState<unknown>): ListBoxResult {
|
|
175
|
+
const baseId = id(options.baseId);
|
|
176
|
+
const locale = useLocale();
|
|
177
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = field({
|
|
178
|
+
...options,
|
|
179
|
+
// A listbox is not a labelable element.
|
|
180
|
+
labelElementType: "span",
|
|
181
|
+
});
|
|
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
|
+
|
|
206
|
+
const { listProps } = selectableList({
|
|
207
|
+
...options,
|
|
208
|
+
keyboardDelegate,
|
|
209
|
+
selectionManager: state.selectionManager,
|
|
210
|
+
collection: state.collection,
|
|
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(),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
baseId,
|
|
234
|
+
labelProps,
|
|
235
|
+
descriptionProps,
|
|
236
|
+
errorMessageProps,
|
|
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
|
+
},
|
|
255
|
+
},
|
|
256
|
+
),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The id an option renders under.
|
|
262
|
+
*
|
|
263
|
+
* `aria-activedescendant` — on the listbox, or on a combobox's input — has to
|
|
264
|
+
* name the option before it exists, so the id is DERIVED from the key rather
|
|
265
|
+
* than generated per element. It is derived from the listbox's own id as well,
|
|
266
|
+
* because two listboxes on one page offering the same key would otherwise
|
|
267
|
+
* write the same id twice.
|
|
268
|
+
*/
|
|
269
|
+
export function optionIdFor(baseId: string, key: Key): string {
|
|
270
|
+
return `${baseId}-option-${idFragment(key)}`;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A key as part of an id.
|
|
275
|
+
*
|
|
276
|
+
* A key is whatever the caller's data uses, and "New York" is a reasonable one;
|
|
277
|
+
* an id holding a space is read by `aria-activedescendant` and
|
|
278
|
+
* `aria-labelledby` as two ids, neither of which exists. Letters, digits and
|
|
279
|
+
* hyphens pass through, so an ordinary key reads the same in the markup, and
|
|
280
|
+
* everything else is written as its code point so that no two keys collide.
|
|
281
|
+
*/
|
|
282
|
+
export function idFragment(key: Key): string {
|
|
283
|
+
return String(key).replace(
|
|
284
|
+
/[^A-Za-z0-9-]/g,
|
|
285
|
+
(character) => `_${(character.codePointAt(0) ?? 0).toString(36)}_`,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface OptionOptions {
|
|
290
|
+
key: Key;
|
|
291
|
+
ref: ElementRef;
|
|
292
|
+
/** The listbox's own id, which the option's is derived from. */
|
|
293
|
+
baseId: MaybeAccessor<string>;
|
|
294
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
295
|
+
/** @default what the list was given */
|
|
296
|
+
shouldSelectOnPressUp?: MaybeAccessor<boolean | undefined>;
|
|
297
|
+
/** @default what the list was given */
|
|
298
|
+
shouldFocusOnHover?: MaybeAccessor<boolean | undefined>;
|
|
299
|
+
/** @default what the list was given */
|
|
300
|
+
shouldUseVirtualFocus?: MaybeAccessor<boolean | undefined>;
|
|
301
|
+
/** @default what the list was given */
|
|
302
|
+
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
303
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
304
|
+
/** The option's own action, run before the list's `onAction`. */
|
|
305
|
+
onAction?: () => void;
|
|
306
|
+
/** Whether `onAction` does anything. @default `onAction !== undefined` */
|
|
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;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export interface OptionResult {
|
|
316
|
+
optionProps: DOMProps;
|
|
317
|
+
labelProps: DOMProps;
|
|
318
|
+
descriptionProps: DOMProps;
|
|
319
|
+
isSelected: Accessor<boolean>;
|
|
320
|
+
isFocused: Accessor<boolean>;
|
|
321
|
+
/** Focused, and reached by keyboard: virtual focus included, which no DOM focus ring sees. */
|
|
322
|
+
isFocusVisible: Accessor<boolean>;
|
|
323
|
+
isPressed: Accessor<boolean>;
|
|
324
|
+
isDisabled: Accessor<boolean>;
|
|
325
|
+
allowsSelection: Accessor<boolean>;
|
|
326
|
+
/** Whether pressing it does something other than select it. */
|
|
327
|
+
hasAction: Accessor<boolean>;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function option(options: OptionOptions, state: ListState<unknown>): OptionResult {
|
|
331
|
+
const labelId = id();
|
|
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();
|
|
345
|
+
|
|
346
|
+
const item = selectableItem({
|
|
347
|
+
key: options.key,
|
|
348
|
+
ref: options.ref,
|
|
349
|
+
id: () => optionIdFor(access(options.baseId), options.key),
|
|
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,
|
|
372
|
+
});
|
|
373
|
+
|
|
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
|
+
});
|
|
385
|
+
|
|
386
|
+
return {
|
|
387
|
+
isSelected: item.isSelected,
|
|
388
|
+
isFocused: item.isFocused,
|
|
389
|
+
isFocusVisible: () => item.isFocused() && visible(),
|
|
390
|
+
isPressed: item.isPressed,
|
|
391
|
+
isDisabled: item.isDisabled,
|
|
392
|
+
allowsSelection: item.allowsSelection,
|
|
393
|
+
hasAction: item.hasAction,
|
|
394
|
+
labelProps: { id: labelId },
|
|
395
|
+
descriptionProps: { id: descriptionId },
|
|
396
|
+
optionProps: mergeProps(item.itemProps, hoverProps, {
|
|
397
|
+
role: "option",
|
|
398
|
+
// `aria-selected` only where selection is possible: on a listbox that
|
|
399
|
+
// only performs actions, the attribute would say every item is
|
|
400
|
+
// unselected rather than that selection is not the point.
|
|
401
|
+
"aria-selected": () =>
|
|
402
|
+
state.selectionManager().selectionMode !== "none" ? item.isSelected() : undefined,
|
|
403
|
+
"aria-disabled": () => item.isDisabled() || undefined,
|
|
404
|
+
"aria-label": () => access(options["aria-label"]) ?? node()?.["aria-label"],
|
|
405
|
+
// A virtualised list renders a window, so the position has to be stated.
|
|
406
|
+
"aria-posinset": () => (isVirtualized() === true ? (node()?.index ?? 0) + 1 : undefined),
|
|
407
|
+
"aria-setsize": () =>
|
|
408
|
+
isVirtualized() === true ? getItemCount(state.collection()) : undefined,
|
|
409
|
+
}),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export interface ListBoxSectionOptions {
|
|
414
|
+
heading?: MaybeAccessor<unknown>;
|
|
415
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function listBoxSection(options: ListBoxSectionOptions): {
|
|
419
|
+
itemProps: DOMProps;
|
|
420
|
+
headingProps: DOMProps;
|
|
421
|
+
groupProps: DOMProps;
|
|
422
|
+
} {
|
|
423
|
+
const headingId = id();
|
|
424
|
+
const hasHeading = (): boolean => {
|
|
425
|
+
const heading = access(options.heading);
|
|
426
|
+
return heading !== undefined && heading !== null && heading !== "";
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
return {
|
|
430
|
+
// The `<li>` wrapping the section is not itself an option.
|
|
431
|
+
itemProps: { role: "presentation" },
|
|
432
|
+
headingProps: {
|
|
433
|
+
id: () => (hasHeading() ? headingId() : undefined),
|
|
434
|
+
// The heading is not a child of the group; it names it.
|
|
435
|
+
"aria-hidden": true,
|
|
436
|
+
},
|
|
437
|
+
groupProps: {
|
|
438
|
+
role: "group",
|
|
439
|
+
"aria-label": () => access(options["aria-label"]),
|
|
440
|
+
"aria-labelledby": () => (hasHeading() ? headingId() : undefined),
|
|
441
|
+
},
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// ---------------------------------------------------------------------------
|
|
446
|
+
// Components
|
|
447
|
+
// ---------------------------------------------------------------------------
|
|
448
|
+
|
|
449
|
+
export interface ListBoxContextValue {
|
|
450
|
+
state: ListState<unknown>;
|
|
451
|
+
baseId: Accessor<string>;
|
|
452
|
+
shouldSelectOnPressUp: Accessor<boolean | undefined>;
|
|
453
|
+
shouldUseVirtualFocus: Accessor<boolean | undefined>;
|
|
454
|
+
/**
|
|
455
|
+
* Where an `<Option>` written as a child registers itself.
|
|
456
|
+
*
|
|
457
|
+
* Present when the list was given no `items`: the options that render ARE the
|
|
458
|
+
* collection, and each one says what it is rather than reading a node.
|
|
459
|
+
*/
|
|
460
|
+
staticItems?: StaticCollection | undefined;
|
|
461
|
+
/** The list's drag and drop, when it was given `dragAndDropHooks`. */
|
|
462
|
+
dragAndDrop?: CollectionDragAndDrop | undefined;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const ListBoxContext = createContext<ListBoxContextValue | null>(null);
|
|
466
|
+
const ItemNodeContext = createContext<Node<unknown> | null>(null);
|
|
467
|
+
|
|
468
|
+
type ItemRender = (item: unknown) => Child;
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* The list's own item callback, so a SECTION can render its children with it.
|
|
472
|
+
*
|
|
473
|
+
* A section's options are built by the same callback as every other option;
|
|
474
|
+
* without this a caller would have to pass the callback to `<Section>` by hand
|
|
475
|
+
* at every use, and the two could disagree.
|
|
476
|
+
*/
|
|
477
|
+
const ItemRenderContext = createContext<ItemRender | null>(null);
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Make `<Option>` usable inside something that is not a `<ListBox>`.
|
|
481
|
+
*
|
|
482
|
+
* A select and a combobox both END in a list of options, and each owns the
|
|
483
|
+
* collection for its own reasons — a select closes on choosing, a combobox
|
|
484
|
+
* filters. Rather than three near-identical option components, they lend
|
|
485
|
+
* `<Option>` the state it reads.
|
|
486
|
+
*
|
|
487
|
+
* A component, not a function taking a callback: the children have to be a
|
|
488
|
+
* BLOCK for the binding to reach them. JSX written inside an ordinary arrow
|
|
489
|
+
* carries the enclosing component's scope, which is outside the one the
|
|
490
|
+
* binding was installed on, so every `<Option>` would look past it.
|
|
491
|
+
*/
|
|
492
|
+
export function ListBoxProvider(props: Incoming<{ value: ListBoxContextValue; children?: Child }>) {
|
|
493
|
+
return provide(
|
|
494
|
+
ListBoxContext,
|
|
495
|
+
() => props.value(),
|
|
496
|
+
() => props.children as unknown,
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** The collection node the row being built is for. */
|
|
501
|
+
export function provideItemNode(node: Node<unknown>): void {
|
|
502
|
+
const owner = getOwner();
|
|
503
|
+
if (owner !== null) install(owner, ItemNodeContext, () => node);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** The item callback the enclosing list builds its rows with. */
|
|
507
|
+
export function provideItemRender(render: ItemRender): void {
|
|
508
|
+
const owner = getOwner();
|
|
509
|
+
if (owner !== null) install(owner, ItemRenderContext, () => render);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export function useListBox(): ListBoxContextValue {
|
|
513
|
+
const value = getContext(ListBoxContext);
|
|
514
|
+
if (value === null || value === undefined) {
|
|
515
|
+
throw new Error("An Option must be rendered inside a ListBox.");
|
|
516
|
+
}
|
|
517
|
+
return value;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** The collection node the enclosing row is for. */
|
|
521
|
+
export function useItemNode(): Node<unknown> {
|
|
522
|
+
const node = getContext(ItemNodeContext);
|
|
523
|
+
if (node === null || node === undefined) {
|
|
524
|
+
throw new Error("An Option must be rendered inside its ListBox's item callback.");
|
|
525
|
+
}
|
|
526
|
+
return node;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export interface SectionComponentProps extends Omit<StyleProps, "title"> {
|
|
530
|
+
/** The heading. A section without one is a box a screen reader cannot name. */
|
|
531
|
+
title?: Child;
|
|
532
|
+
"aria-label"?: string;
|
|
533
|
+
/** The heading element's own slot, for a design system naming it. */
|
|
534
|
+
titleSlot?: string;
|
|
535
|
+
/** And its class, since `class` styles the section rather than the heading. */
|
|
536
|
+
titleClass?: string;
|
|
537
|
+
"data-slot"?: string;
|
|
538
|
+
children?: Child;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* A run of options under a heading.
|
|
543
|
+
*
|
|
544
|
+
* Rendered from the item callback like any other row: a section arrives there
|
|
545
|
+
* as its own node, and this reads the node it was given rather than being told
|
|
546
|
+
* what is in it.
|
|
547
|
+
*
|
|
548
|
+
* `role="group"` on a nested `<ul>` with `aria-labelledby` pointing at the
|
|
549
|
+
* heading, which is what lets a screen reader say "Fruit, group, 3 items". The
|
|
550
|
+
* heading is `role="presentation"` because a `<li>` inside a listbox that is
|
|
551
|
+
* not an option is one the list would otherwise count.
|
|
552
|
+
*
|
|
553
|
+
* Keyboard traversal needs nothing here: the collection links keys in document
|
|
554
|
+
* order ACROSS section boundaries, so Down from the last option of one section
|
|
555
|
+
* already reaches the first of the next.
|
|
556
|
+
*/
|
|
557
|
+
export function Section(props: SectionComponentProps) {
|
|
558
|
+
const registry = getContext(ListBoxContext)?.staticItems;
|
|
559
|
+
if (registry !== undefined) return <StaticSection of={cells(props)} registry={registry} />;
|
|
560
|
+
|
|
561
|
+
const node = useItemNode();
|
|
562
|
+
const render = getContext(ItemRenderContext);
|
|
563
|
+
if (render === null || render === undefined) {
|
|
564
|
+
throw new Error("A Section must be rendered inside its list's item callback.");
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// The hook, not a second copy of it: `listBoxSection` already decides that
|
|
568
|
+
// the wrapper is presentational, that the heading names the group rather
|
|
569
|
+
// than joining it, and that a section with no heading gets no dangling
|
|
570
|
+
// `aria-labelledby`.
|
|
571
|
+
const { itemProps, headingProps, groupProps } = listBoxSection({
|
|
572
|
+
heading: () => (props.title === undefined ? undefined : "titled"),
|
|
573
|
+
"aria-label": () => props["aria-label"],
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
return (
|
|
577
|
+
<li {...itemProps} {...styleProps(cells(props))} data-slot={props["data-slot"] ?? "section"}>
|
|
578
|
+
<span
|
|
579
|
+
{...headingProps}
|
|
580
|
+
data-slot={props.titleSlot ?? "section-title"}
|
|
581
|
+
class={props.titleClass}
|
|
582
|
+
>
|
|
583
|
+
{props.title}
|
|
584
|
+
</span>
|
|
585
|
+
<ul {...groupProps}>
|
|
586
|
+
<For each={() => [...node.childNodes]}>
|
|
587
|
+
{(child: Node<unknown>) => {
|
|
588
|
+
provideItemNode(child);
|
|
589
|
+
return render(child.value);
|
|
590
|
+
}}
|
|
591
|
+
</For>
|
|
592
|
+
</ul>
|
|
593
|
+
{props.children}
|
|
594
|
+
</li>
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
interface SectionHeading {
|
|
599
|
+
id: Accessor<string>;
|
|
600
|
+
readonly name: (has: boolean) => void;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const SectionHeadingContext = createContext<SectionHeading | null>(null);
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* The id the enclosing section's heading must carry, for a heading written as
|
|
607
|
+
* one of the section's children rather than as its `title`.
|
|
608
|
+
*
|
|
609
|
+
* Calling it is what makes the group point at the heading, and leaving undoes
|
|
610
|
+
* that, so a section never names an element that is not there.
|
|
611
|
+
*/
|
|
612
|
+
export function useSectionHeading(): Accessor<string> | null {
|
|
613
|
+
const heading = getContext(SectionHeadingContext);
|
|
614
|
+
if (heading === null || heading === undefined) return null;
|
|
615
|
+
heading.name(true);
|
|
616
|
+
onCleanup(() => heading.name(false));
|
|
617
|
+
return heading.id;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function StaticSection(sectionProps: {
|
|
621
|
+
of: Incoming<SectionComponentProps>;
|
|
622
|
+
registry: StaticCollection;
|
|
623
|
+
}) {
|
|
624
|
+
const props = sectionProps.of;
|
|
625
|
+
const list = useListBox();
|
|
626
|
+
const section = staticItem(sectionProps.registry, { type: "section", textValue: "" });
|
|
627
|
+
const headingId = id();
|
|
628
|
+
const [isNamed, setNamed] = createSignal(false, { ownedWrite: true });
|
|
629
|
+
const hasTitle = (): boolean => props.title?.() !== undefined;
|
|
630
|
+
const heading: SectionHeading = { id: headingId, name: setNamed };
|
|
631
|
+
|
|
632
|
+
return (
|
|
633
|
+
<li
|
|
634
|
+
role="presentation"
|
|
635
|
+
{...styleProps(props)}
|
|
636
|
+
data-slot={props["data-slot"]?.() ?? "section"}
|
|
637
|
+
// A section a filter has emptied still renders its heading, and a design
|
|
638
|
+
// system hides the whole thing off this.
|
|
639
|
+
data-empty={(list.state.collection().getItem(section.key)?.childNodes.length ?? 0) === 0}
|
|
640
|
+
ref={section.ref}
|
|
641
|
+
>
|
|
642
|
+
<span
|
|
643
|
+
id={hasTitle() ? headingId() : undefined}
|
|
644
|
+
aria-hidden="true"
|
|
645
|
+
data-slot={props.titleSlot?.() ?? "section-title"}
|
|
646
|
+
class={props.titleClass?.()}
|
|
647
|
+
>
|
|
648
|
+
{props.title}
|
|
649
|
+
</span>
|
|
650
|
+
<ul
|
|
651
|
+
role="group"
|
|
652
|
+
aria-label={props["aria-label"]?.()}
|
|
653
|
+
aria-labelledby={hasTitle() || isNamed() ? headingId() : undefined}
|
|
654
|
+
>
|
|
655
|
+
{
|
|
656
|
+
provideStaticSection(section.key, () =>
|
|
657
|
+
provide(
|
|
658
|
+
SectionHeadingContext,
|
|
659
|
+
() => heading,
|
|
660
|
+
() => props.children as unknown,
|
|
661
|
+
),
|
|
662
|
+
) as Child
|
|
663
|
+
}
|
|
664
|
+
</ul>
|
|
665
|
+
</li>
|
|
666
|
+
);
|
|
667
|
+
}
|
|
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
|
+
|
|
680
|
+
export interface ListBoxComponentProps<T> extends StyleProps, ItemAccessors<T> {
|
|
681
|
+
/**
|
|
682
|
+
* The values to render, in order.
|
|
683
|
+
*
|
|
684
|
+
* Leave it out and the `<Option>`s written as children are the collection:
|
|
685
|
+
* each one carries its own `value`, and nothing is rendered from data.
|
|
686
|
+
*/
|
|
687
|
+
items?: Iterable<T>;
|
|
688
|
+
/** How one value renders, returning an `<Option>`; or the options themselves. */
|
|
689
|
+
children: ((item: T) => Child) | Child;
|
|
690
|
+
label?: Child;
|
|
691
|
+
description?: Child;
|
|
692
|
+
errorMessage?: Child;
|
|
693
|
+
/** @default "none" */
|
|
694
|
+
selectionMode?: SelectionMode;
|
|
695
|
+
/** @default "toggle" */
|
|
696
|
+
selectionBehavior?: SelectionBehavior;
|
|
697
|
+
selectedKeys?: "all" | Iterable<Key>;
|
|
698
|
+
defaultSelectedKeys?: "all" | Iterable<Key>;
|
|
699
|
+
disabledKeys?: Iterable<Key>;
|
|
700
|
+
/** Whether `disabledKeys` stops everything, or only selection. @default "all" */
|
|
701
|
+
disabledBehavior?: DisabledBehavior;
|
|
702
|
+
disallowEmptySelection?: boolean;
|
|
703
|
+
shouldFocusWrap?: boolean;
|
|
704
|
+
/** @default "vertical" */
|
|
705
|
+
orientation?: "horizontal" | "vertical";
|
|
706
|
+
/** Whether the options are a stack or a grid the arrows move across. @default "stack" */
|
|
707
|
+
layout?: "stack" | "grid";
|
|
708
|
+
autoFocus?: boolean | "first" | "last";
|
|
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;
|
|
720
|
+
"aria-label"?: string;
|
|
721
|
+
"aria-labelledby"?: string;
|
|
722
|
+
"aria-describedby"?: string;
|
|
723
|
+
ref?: RefTarget<HTMLUListElement>;
|
|
724
|
+
onSelectionChange?: (keys: "all" | Set<Key>) => void;
|
|
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;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* ```tsx
|
|
737
|
+
* <ListBox
|
|
738
|
+
* label="Fruit"
|
|
739
|
+
* items={fruits()}
|
|
740
|
+
* selectionMode="multiple"
|
|
741
|
+
* onSelectionChange={(keys) => picked.set(keys)}
|
|
742
|
+
* >
|
|
743
|
+
* {(fruit) => <Option>{fruit.name}</Option>}
|
|
744
|
+
* </ListBox>
|
|
745
|
+
* ```
|
|
746
|
+
*
|
|
747
|
+
* The items are DATA. A component whose children are lazy and whose body runs
|
|
748
|
+
* once has no reason to render the options twice — once to discover them and
|
|
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.
|
|
753
|
+
*/
|
|
754
|
+
export function ListBox<T>(props: ListBoxComponentProps<T>) {
|
|
755
|
+
const domRef = makeRef<HTMLUListElement>();
|
|
756
|
+
const options = fromProps(cells(props) as unknown as Incoming<Record<string, unknown>>);
|
|
757
|
+
const rendered = props.items === undefined ? staticCollection() : null;
|
|
758
|
+
const auto = useAutocompleteCollection();
|
|
759
|
+
|
|
760
|
+
const state = listState<T>({
|
|
761
|
+
...(options as ListStateOptions<T>),
|
|
762
|
+
...(rendered === null
|
|
763
|
+
? {}
|
|
764
|
+
: ({ items: rendered.entries, ...staticAccessors } as unknown as ListStateOptions<T>)),
|
|
765
|
+
filter: auto?.filterNodes as ListStateOptions<T>["filter"],
|
|
766
|
+
onSelectionChange: (keys) => props.onSelectionChange?.(keys),
|
|
767
|
+
});
|
|
768
|
+
const anyState = state as unknown as ListState<unknown>;
|
|
769
|
+
|
|
770
|
+
// Inside a `<Virtualizer>` the list renders a WINDOW of its collection, and
|
|
771
|
+
// the layout — not the DOM — answers what is above, below and a page away.
|
|
772
|
+
const virtual = useVirtualizer();
|
|
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;
|
|
785
|
+
|
|
786
|
+
const { listBoxProps, labelProps, descriptionProps, errorMessageProps, baseId } = listBox(
|
|
787
|
+
{
|
|
788
|
+
...(options as unknown as ListBoxOptions),
|
|
789
|
+
...autocompleteOptions(auto, options as unknown as ListBoxOptions),
|
|
790
|
+
ref: domRef,
|
|
791
|
+
keyboardDelegate: props.keyboardDelegate,
|
|
792
|
+
shouldSelectOnPressUp,
|
|
793
|
+
onAction: (key) => props.onAction?.(key),
|
|
794
|
+
hasAction: () => props.onAction !== undefined,
|
|
795
|
+
isVirtualized: () => virtual !== null,
|
|
796
|
+
layoutDelegate: virtual?.layout,
|
|
797
|
+
},
|
|
798
|
+
anyState,
|
|
799
|
+
);
|
|
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
|
+
|
|
818
|
+
const owner = getOwner();
|
|
819
|
+
if (owner !== null) {
|
|
820
|
+
const value: ListBoxContextValue = {
|
|
821
|
+
state: anyState,
|
|
822
|
+
baseId,
|
|
823
|
+
shouldSelectOnPressUp,
|
|
824
|
+
shouldUseVirtualFocus: () => auto?.collectionProps.shouldUseVirtualFocus(),
|
|
825
|
+
staticItems: rendered ?? undefined,
|
|
826
|
+
dragAndDrop,
|
|
827
|
+
};
|
|
828
|
+
install(owner, ListBoxContext, () => value);
|
|
829
|
+
}
|
|
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
|
+
|
|
845
|
+
const elementProps = mergeProps(
|
|
846
|
+
listBoxProps,
|
|
847
|
+
focusProps,
|
|
848
|
+
dragAndDrop?.collectionProps,
|
|
849
|
+
forwarded,
|
|
850
|
+
styleProps(cells(props)),
|
|
851
|
+
{
|
|
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,
|
|
858
|
+
"data-virtualized": () => virtual !== null,
|
|
859
|
+
"data-drop-target": isDropTarget,
|
|
860
|
+
style: () => (virtual === null ? props.style : contentStyle(virtual.contentSize())),
|
|
861
|
+
},
|
|
862
|
+
);
|
|
863
|
+
|
|
864
|
+
const render = cells(props).children;
|
|
865
|
+
|
|
866
|
+
/** The whole collection, or the window of it the virtualiser says is on screen. */
|
|
867
|
+
const rows = (): Node<T>[] => {
|
|
868
|
+
if (virtual === null) return [...state.collection()];
|
|
869
|
+
const collection = state.collection();
|
|
870
|
+
return virtual
|
|
871
|
+
.visible()
|
|
872
|
+
.map((info: LayoutInfo) => collection.getItem(info.key))
|
|
873
|
+
.filter((node: Node<T> | null): node is Node<T> => node !== null);
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
provideItemRender(render as unknown as (item: unknown) => Child);
|
|
877
|
+
|
|
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;
|
|
890
|
+
return (
|
|
891
|
+
<li role="option" style={{ display: "contents" }}>
|
|
892
|
+
{renderEmpty(renderState)}
|
|
893
|
+
</li>
|
|
894
|
+
);
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
return (
|
|
898
|
+
<>
|
|
899
|
+
<span {...labelProps}>{props.label}</span>
|
|
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}
|
|
914
|
+
</ul>
|
|
915
|
+
{dragAndDrop?.preview}
|
|
916
|
+
<span {...descriptionProps}>{props.description}</span>
|
|
917
|
+
<span {...errorMessageProps}>{props.errorMessage}</span>
|
|
918
|
+
</>
|
|
919
|
+
);
|
|
920
|
+
}
|
|
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
|
+
|
|
945
|
+
export interface OptionComponentProps extends StyleProps {
|
|
946
|
+
children?: Child;
|
|
947
|
+
"aria-label"?: string;
|
|
948
|
+
ref?: RefTarget<HTMLLIElement>;
|
|
949
|
+
/**
|
|
950
|
+
* What the option is, when it is written as a child of a list with no
|
|
951
|
+
* `items`: its key in the selection. Generated when left out.
|
|
952
|
+
*/
|
|
953
|
+
value?: Key;
|
|
954
|
+
/** What a typeahead matches, for a written option. @default its own text */
|
|
955
|
+
textValue?: string;
|
|
956
|
+
/** For a written option; one rendered from data reads its item's. */
|
|
957
|
+
isDisabled?: boolean;
|
|
958
|
+
/** Keep a written option mounted and take it out of the collection. */
|
|
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;
|
|
970
|
+
/**
|
|
971
|
+
* Called when the option is acted on: a press where nothing is selectable,
|
|
972
|
+
* and Enter where something is, which is the split every list here makes
|
|
973
|
+
* between choosing a row and opening it. The list's own `onAction` runs
|
|
974
|
+
* after it.
|
|
975
|
+
*/
|
|
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;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
const LINK_PROPS = ["href", "target", "rel", "download", "ping", "referrerPolicy"] as const;
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* One option. Its key, value and disabled state come from the collection node
|
|
995
|
+
* the enclosing {@link ListBox} is rendering, so nothing is repeated here.
|
|
996
|
+
*/
|
|
997
|
+
export function Option(props: OptionComponentProps) {
|
|
998
|
+
const domRef = makeRef<HTMLLIElement>();
|
|
999
|
+
const list = useListBox();
|
|
1000
|
+
const options = fromProps(cells(props));
|
|
1001
|
+
const written =
|
|
1002
|
+
list.staticItems === undefined
|
|
1003
|
+
? null
|
|
1004
|
+
: staticItem(list.staticItems, {
|
|
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,
|
|
1015
|
+
});
|
|
1016
|
+
const node: Pick<Node<unknown>, "key" | "props"> = written === null
|
|
1017
|
+
? useItemNode()
|
|
1018
|
+
: { key: written.key, props: {} };
|
|
1019
|
+
const virtual = useVirtualizer();
|
|
1020
|
+
|
|
1021
|
+
measureRow(virtual, node.key, domRef);
|
|
1022
|
+
|
|
1023
|
+
const {
|
|
1024
|
+
optionProps,
|
|
1025
|
+
isSelected,
|
|
1026
|
+
isFocused,
|
|
1027
|
+
isFocusVisible,
|
|
1028
|
+
isPressed,
|
|
1029
|
+
isDisabled,
|
|
1030
|
+
allowsSelection,
|
|
1031
|
+
hasAction,
|
|
1032
|
+
} = option(
|
|
1033
|
+
{
|
|
1034
|
+
key: node.key,
|
|
1035
|
+
ref: domRef,
|
|
1036
|
+
baseId: list.baseId,
|
|
1037
|
+
"aria-label": () => props["aria-label"],
|
|
1038
|
+
isDisabled: () => props.isDisabled === true || node.props?.isDisabled === true,
|
|
1039
|
+
shouldSelectOnPressUp: list.shouldSelectOnPressUp,
|
|
1040
|
+
shouldUseVirtualFocus: list.shouldUseVirtualFocus,
|
|
1041
|
+
isVirtualized: () => virtual !== null,
|
|
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,
|
|
1049
|
+
},
|
|
1050
|
+
list.state,
|
|
1051
|
+
);
|
|
1052
|
+
|
|
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;
|
|
1065
|
+
|
|
1066
|
+
const elementProps = mergeProps(
|
|
1067
|
+
optionProps,
|
|
1068
|
+
hoverProps,
|
|
1069
|
+
keyboardProps,
|
|
1070
|
+
focusProps,
|
|
1071
|
+
filterDOMProps(options, { global: true, labelable: true }),
|
|
1072
|
+
styleProps(cells(props)),
|
|
1073
|
+
{
|
|
1074
|
+
"data-selected": isSelected,
|
|
1075
|
+
"data-focused": isFocused,
|
|
1076
|
+
"data-focus-visible": isFocusVisible,
|
|
1077
|
+
"data-pressed": isPressed,
|
|
1078
|
+
"data-hovered": isHovered,
|
|
1079
|
+
"data-disabled": isDisabled,
|
|
1080
|
+
"data-selection-mode": () => (selectionMode() === "none" ? undefined : selectionMode()),
|
|
1081
|
+
hidden: () => props.isHidden === true || undefined,
|
|
1082
|
+
"data-testid": () => props["data-testid"],
|
|
1083
|
+
// Positioned by the LAYOUT: the rows above it are not rendered, so
|
|
1084
|
+
// document flow has nothing to place it against.
|
|
1085
|
+
style: () => {
|
|
1086
|
+
if (virtual === null) return props.style;
|
|
1087
|
+
const info = virtual.layout.getLayoutInfo(node.key);
|
|
1088
|
+
return info === null ? props.style : rowStyle(info);
|
|
1089
|
+
},
|
|
1090
|
+
},
|
|
1091
|
+
dnd?.itemProps,
|
|
1092
|
+
);
|
|
1093
|
+
|
|
1094
|
+
const element = (
|
|
1095
|
+
<li {...elementProps} ref={mergeRefs(domRef.set, written?.ref, props.ref)}>
|
|
1096
|
+
<SelectionIndicatorProvider isSelected={isSelected()}>
|
|
1097
|
+
{props.children}
|
|
1098
|
+
</SelectionIndicatorProvider>
|
|
1099
|
+
</li>
|
|
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
|
+
);
|
|
1247
|
+
}
|