@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
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag and drop for a collection component.
|
|
3
|
+
*
|
|
4
|
+
* `dragAndDrop()` turns the caller's handlers into a hooks object, and a
|
|
5
|
+
* collection that takes `dragAndDropHooks` calls those hooks with its own
|
|
6
|
+
* state, element and keyboard delegate. The collection decides where things
|
|
7
|
+
* go (a listbox draws an indicator as an option, a grid list as a row); the
|
|
8
|
+
* hooks decide what dragging and dropping mean. A tree or a table consumes the
|
|
9
|
+
* same object through {@link collectionDragAndDrop}.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type Accessor,
|
|
14
|
+
type Child,
|
|
15
|
+
cells,
|
|
16
|
+
createContext,
|
|
17
|
+
createSignal,
|
|
18
|
+
flush,
|
|
19
|
+
getContext,
|
|
20
|
+
getOwner,
|
|
21
|
+
type Incoming,
|
|
22
|
+
install,
|
|
23
|
+
onCleanup,
|
|
24
|
+
Show,
|
|
25
|
+
} from "@barqjs/core";
|
|
26
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
27
|
+
import { button, type ButtonOptions } from "./button.tsx";
|
|
28
|
+
import type { Collection, Key, SelectionManager } from "./collections.ts";
|
|
29
|
+
import { rowStyle, type VirtualizerValue } from "./virtualizer.tsx";
|
|
30
|
+
import { contains } from "./dom.ts";
|
|
31
|
+
import {
|
|
32
|
+
draggableCollection,
|
|
33
|
+
type DraggableCollectionState,
|
|
34
|
+
type DraggableCollectionStateOptions,
|
|
35
|
+
draggableCollectionState,
|
|
36
|
+
draggableItem,
|
|
37
|
+
type DraggableItemResult,
|
|
38
|
+
type DragItem,
|
|
39
|
+
type DraggableCollectionProps,
|
|
40
|
+
type DragPreviewRenderer,
|
|
41
|
+
dropIndicator,
|
|
42
|
+
droppableCollection,
|
|
43
|
+
type DroppableCollectionActivateEvent,
|
|
44
|
+
type DroppableCollectionOptions,
|
|
45
|
+
type DroppableCollectionProps,
|
|
46
|
+
type DroppableCollectionResult,
|
|
47
|
+
type DroppableCollectionState,
|
|
48
|
+
type DroppableCollectionStateOptions,
|
|
49
|
+
droppableCollectionState,
|
|
50
|
+
droppableItem,
|
|
51
|
+
type DropTarget,
|
|
52
|
+
type DropTargetDelegate,
|
|
53
|
+
type ItemDropTarget,
|
|
54
|
+
isVirtualDragging,
|
|
55
|
+
ListDropTargetDelegate,
|
|
56
|
+
} from "./dnd/index.ts";
|
|
57
|
+
import { focusRing } from "./focus.ts";
|
|
58
|
+
import { hover } from "./interactions/hover.ts";
|
|
59
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
60
|
+
import type { KeyboardDelegate } from "./selection.ts";
|
|
61
|
+
import {
|
|
62
|
+
access,
|
|
63
|
+
callback,
|
|
64
|
+
chain,
|
|
65
|
+
type DOMProps,
|
|
66
|
+
filterDOMProps,
|
|
67
|
+
fromProps,
|
|
68
|
+
type MaybeAccessor,
|
|
69
|
+
mergeProps,
|
|
70
|
+
type StyleProps,
|
|
71
|
+
styleProps,
|
|
72
|
+
} from "./utils.ts";
|
|
73
|
+
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
// The hooks object
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
/** What a drag preview renders: an element, or one with its offset under the pointer. */
|
|
79
|
+
export type DragPreviewContent = Child | { element: Child; x: number; y: number };
|
|
80
|
+
|
|
81
|
+
export interface DragAndDropOptions<T = unknown>
|
|
82
|
+
extends Omit<DraggableCollectionProps<T>, "preview" | "getItems">, DroppableCollectionProps {
|
|
83
|
+
/** What a drag of `keys` carries. Without it, the collection cannot be dragged from. */
|
|
84
|
+
getItems?: (keys: Set<Key>, items: T[]) => DragItem[];
|
|
85
|
+
/** What follows the pointer. Without it, the browser photographs the dragged element. */
|
|
86
|
+
renderDragPreview?: (items: DragItem[]) => DragPreviewContent;
|
|
87
|
+
/** One indicator, which should be a `<DropIndicator>`. @default `<DropIndicator target={target} />` */
|
|
88
|
+
renderDropIndicator?: (target: DropTarget) => Child;
|
|
89
|
+
/** Finds the drop target under the pointer. @default a `ListDropTargetDelegate` */
|
|
90
|
+
dropTargetDelegate?: DropTargetDelegate;
|
|
91
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
92
|
+
/**
|
|
93
|
+
* Where a mouse or touch drag may start: anywhere on an item, or only on its
|
|
94
|
+
* drag button, which leaves text fields inside an item usable. Keyboard and
|
|
95
|
+
* screen reader drags are not affected.
|
|
96
|
+
*
|
|
97
|
+
* @default "item"
|
|
98
|
+
*/
|
|
99
|
+
pointerDragSource?: "item" | "handle";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface DragAndDropHooks {
|
|
103
|
+
draggableCollectionState?: (
|
|
104
|
+
options: Omit<DraggableCollectionStateOptions, "getItems">,
|
|
105
|
+
) => DraggableCollectionState;
|
|
106
|
+
draggableCollection?: typeof draggableCollection;
|
|
107
|
+
draggableItem?: typeof draggableItem;
|
|
108
|
+
DragPreview?: typeof DragPreview;
|
|
109
|
+
renderDragPreview?: (items: DragItem[]) => DragPreviewContent;
|
|
110
|
+
isVirtualDragging?: () => boolean;
|
|
111
|
+
pointerDragSource?: "item" | "handle";
|
|
112
|
+
droppableCollectionState?: (options: DroppableCollectionStateOptions) => DroppableCollectionState;
|
|
113
|
+
droppableCollection?: (
|
|
114
|
+
options: DroppableCollectionOptions,
|
|
115
|
+
state: DroppableCollectionState,
|
|
116
|
+
ref: ElementRef<HTMLElement>,
|
|
117
|
+
) => DroppableCollectionResult;
|
|
118
|
+
droppableItem?: typeof droppableItem;
|
|
119
|
+
dropIndicator?: typeof dropIndicator;
|
|
120
|
+
renderDropIndicator?: (target: DropTarget) => Child;
|
|
121
|
+
dropTargetDelegate?: DropTargetDelegate;
|
|
122
|
+
ListDropTargetDelegate?: typeof ListDropTargetDelegate;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface DragAndDrop {
|
|
126
|
+
dragAndDropHooks: DragAndDropHooks;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Drag and drop for a collection, as hooks it calls.
|
|
131
|
+
*
|
|
132
|
+
* ```tsx
|
|
133
|
+
* const { dragAndDropHooks } = dragAndDrop<Person>({
|
|
134
|
+
* getItems: (keys, people) => people.map((person) => ({ "text/plain": person.name })),
|
|
135
|
+
* onReorder: (event) => move(event.keys, event.target),
|
|
136
|
+
* });
|
|
137
|
+
* <ListBox aria-label="People" items={people()} dragAndDropHooks={dragAndDropHooks}>…</ListBox>
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* Draggable when `getItems` is given; droppable when any drop handler is.
|
|
141
|
+
*/
|
|
142
|
+
export function dragAndDrop<T = unknown>(options: DragAndDropOptions<T>): DragAndDrop {
|
|
143
|
+
const hooks: DragAndDropHooks = {};
|
|
144
|
+
const getItems = options.getItems;
|
|
145
|
+
const isDroppable =
|
|
146
|
+
options.onDrop !== undefined ||
|
|
147
|
+
options.onInsert !== undefined ||
|
|
148
|
+
options.onItemDrop !== undefined ||
|
|
149
|
+
options.onReorder !== undefined ||
|
|
150
|
+
options.onMove !== undefined ||
|
|
151
|
+
options.onRootDrop !== undefined;
|
|
152
|
+
|
|
153
|
+
if (getItems !== undefined) {
|
|
154
|
+
hooks.draggableCollectionState = (props) =>
|
|
155
|
+
draggableCollectionState<T>({
|
|
156
|
+
...(props as Omit<DraggableCollectionStateOptions<T>, "getItems">),
|
|
157
|
+
isDisabled: options.isDisabled,
|
|
158
|
+
onDragStart: options.onDragStart,
|
|
159
|
+
onDragMove: options.onDragMove,
|
|
160
|
+
onDragEnd: options.onDragEnd,
|
|
161
|
+
getAllowedDropOperations: options.getAllowedDropOperations,
|
|
162
|
+
getItems,
|
|
163
|
+
});
|
|
164
|
+
hooks.draggableCollection = draggableCollection;
|
|
165
|
+
hooks.draggableItem = draggableItem;
|
|
166
|
+
hooks.DragPreview = DragPreview;
|
|
167
|
+
hooks.renderDragPreview = options.renderDragPreview;
|
|
168
|
+
hooks.isVirtualDragging = isVirtualDragging;
|
|
169
|
+
hooks.pointerDragSource = options.pointerDragSource ?? "item";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (isDroppable) {
|
|
173
|
+
hooks.droppableCollectionState = (props) => droppableCollectionState({ ...options, ...props });
|
|
174
|
+
hooks.droppableItem = droppableItem;
|
|
175
|
+
hooks.droppableCollection = (props, state, ref) =>
|
|
176
|
+
droppableCollection(
|
|
177
|
+
{
|
|
178
|
+
...options,
|
|
179
|
+
...props,
|
|
180
|
+
// The collection's own response (a tree expanding the row) and the
|
|
181
|
+
// caller's both happen.
|
|
182
|
+
onDropActivate: chain(props.onDropActivate, options.onDropActivate),
|
|
183
|
+
},
|
|
184
|
+
state,
|
|
185
|
+
ref,
|
|
186
|
+
);
|
|
187
|
+
hooks.dropIndicator = dropIndicator;
|
|
188
|
+
hooks.renderDropIndicator = options.renderDropIndicator;
|
|
189
|
+
hooks.dropTargetDelegate = options.dropTargetDelegate;
|
|
190
|
+
hooks.ListDropTargetDelegate = ListDropTargetDelegate;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return { dragAndDropHooks: hooks };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ---------------------------------------------------------------------------
|
|
197
|
+
// What a collection shares with its items
|
|
198
|
+
// ---------------------------------------------------------------------------
|
|
199
|
+
|
|
200
|
+
export interface DragAndDropContextValue {
|
|
201
|
+
dragAndDropHooks?: DragAndDropHooks | undefined;
|
|
202
|
+
dragState?: DraggableCollectionState | undefined;
|
|
203
|
+
dropState?: DroppableCollectionState | undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const DragAndDropContext = createContext<DragAndDropContextValue | null>(null);
|
|
207
|
+
|
|
208
|
+
/** The drag and drop state of the enclosing collection, if it has any. */
|
|
209
|
+
export function useDragAndDropContext(): DragAndDropContextValue {
|
|
210
|
+
return getContext(DragAndDropContext) ?? {};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface DropIndicatorContextValue {
|
|
214
|
+
/** Draws one indicator the way the enclosing collection's indicators look. */
|
|
215
|
+
render: (props: Incoming<DropIndicatorProps>) => Child;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const DropIndicatorContext = createContext<DropIndicatorContextValue | null>(null);
|
|
219
|
+
|
|
220
|
+
/** Install how `<DropIndicator>` renders inside the calling component. */
|
|
221
|
+
export function provideDropIndicator(value: DropIndicatorContextValue): void {
|
|
222
|
+
const owner = getOwner();
|
|
223
|
+
if (owner !== null) install(owner, DropIndicatorContext, () => value);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface DropIndicatorProps extends StyleProps {
|
|
227
|
+
/** The position the indicator stands for. */
|
|
228
|
+
target: DropTarget;
|
|
229
|
+
children?: Child;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The mark between items where a drop would land.
|
|
234
|
+
*
|
|
235
|
+
* Rendered from a collection's `renderDropIndicator`, which is where `target`
|
|
236
|
+
* comes from. The element is the collection's choice: an option in a listbox,
|
|
237
|
+
* a row in a grid list. It carries `data-drop-target` while it is the target.
|
|
238
|
+
*/
|
|
239
|
+
export function DropIndicator(props: DropIndicatorProps) {
|
|
240
|
+
const context = getContext(DropIndicatorContext);
|
|
241
|
+
if (context === null || context === undefined) {
|
|
242
|
+
throw new Error("A DropIndicator must be rendered by a droppable collection.");
|
|
243
|
+
}
|
|
244
|
+
return <>{context.render(cells(props))}</>;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
// Drag preview
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
export interface DragPreviewProps {
|
|
252
|
+
children: (items: DragItem[]) => DragPreviewContent;
|
|
253
|
+
/** Receives the renderer `drag()` takes as its `preview`. */
|
|
254
|
+
ref?: RefTarget<DragPreviewRenderer>;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isPositioned(value: unknown): value is { element: Child; x: number; y: number } {
|
|
258
|
+
return (
|
|
259
|
+
typeof value === "object" &&
|
|
260
|
+
value !== null &&
|
|
261
|
+
"element" in value &&
|
|
262
|
+
typeof (value as { x?: unknown }).x === "number" &&
|
|
263
|
+
typeof (value as { y?: unknown }).y === "number"
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* What a native drag shows under the pointer.
|
|
269
|
+
*
|
|
270
|
+
* The browser photographs an element DURING `dragstart`, so the preview is
|
|
271
|
+
* rendered and flushed synchronously inside that handler, handed over, and
|
|
272
|
+
* removed a frame later once the photograph is taken.
|
|
273
|
+
*/
|
|
274
|
+
export function DragPreview(props: DragPreviewProps) {
|
|
275
|
+
const container = makeRef<HTMLDivElement>();
|
|
276
|
+
const [items, setItems] = createSignal<DragItem[] | null>(null, { ownedWrite: true });
|
|
277
|
+
let offset: { x?: number; y?: number } = {};
|
|
278
|
+
let frame: number | undefined;
|
|
279
|
+
|
|
280
|
+
// Built where it is inserted rather than in a memo: what the caller renders
|
|
281
|
+
// binds its own text and attributes, and those bindings have to belong to
|
|
282
|
+
// the insertion that shows them.
|
|
283
|
+
const content = (): Child => {
|
|
284
|
+
const dragged = items();
|
|
285
|
+
if (dragged === null) return null;
|
|
286
|
+
// A render function written inline arrives as itself, one passed by name
|
|
287
|
+
// as a Cell around it.
|
|
288
|
+
const render = callback<[DragItem[]], DragPreviewContent>(cells(props).children);
|
|
289
|
+
if (render === undefined) return null;
|
|
290
|
+
const result = render(dragged);
|
|
291
|
+
if (isPositioned(result)) {
|
|
292
|
+
offset = { x: result.x, y: result.y };
|
|
293
|
+
return result.element;
|
|
294
|
+
}
|
|
295
|
+
offset = {};
|
|
296
|
+
return result;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const renderer: DragPreviewRenderer = (dragged, deliver) => {
|
|
300
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
301
|
+
setItems(dragged);
|
|
302
|
+
flush();
|
|
303
|
+
deliver(container(), offset.x, offset.y);
|
|
304
|
+
frame = requestAnimationFrame(() => {
|
|
305
|
+
frame = undefined;
|
|
306
|
+
setItems(null);
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
mergeRefs(props.ref)(renderer);
|
|
311
|
+
onCleanup(() => {
|
|
312
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
return (
|
|
316
|
+
<Show when={items() !== null}>
|
|
317
|
+
<div
|
|
318
|
+
ref={container.set}
|
|
319
|
+
style={{ "z-index": "-100", position: "fixed", top: "0", left: "-100000px" }}
|
|
320
|
+
>
|
|
321
|
+
{content()}
|
|
322
|
+
</div>
|
|
323
|
+
</Show>
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ---------------------------------------------------------------------------
|
|
328
|
+
// The drag button
|
|
329
|
+
// ---------------------------------------------------------------------------
|
|
330
|
+
|
|
331
|
+
export interface DragButtonSlot {
|
|
332
|
+
/** Options for `button()`: the press that starts a drag, its label and description. */
|
|
333
|
+
options: DOMProps;
|
|
334
|
+
/** What goes on the element itself: `draggable`, and the style that lets presses through. */
|
|
335
|
+
elementProps: DOMProps;
|
|
336
|
+
ref?: (element: Element | null) => void;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const DragButtonContext = createContext<DragButtonSlot | null>(null);
|
|
340
|
+
|
|
341
|
+
/** Offer a drag button's props to a `<DragButton>` rendered inside the calling component. */
|
|
342
|
+
export function provideDragButton(slot: DragButtonSlot): void {
|
|
343
|
+
const owner = getOwner();
|
|
344
|
+
if (owner !== null) install(owner, DragButtonContext, () => slot);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The drag button the enclosing item offers, if it is draggable. */
|
|
348
|
+
export function useDragButton(): DragButtonSlot | null {
|
|
349
|
+
return getContext(DragButtonContext) ?? null;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export interface DragButtonProps extends StyleProps {
|
|
353
|
+
children?: Child;
|
|
354
|
+
"aria-label"?: string;
|
|
355
|
+
ref?: RefTarget<HTMLButtonElement>;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The button inside a draggable row that starts a keyboard or screen reader
|
|
360
|
+
* drag. A row of a grid list holds controls of its own, so Enter on the row is
|
|
361
|
+
* not available to start one.
|
|
362
|
+
*
|
|
363
|
+
* ```tsx
|
|
364
|
+
* <GridListItem>
|
|
365
|
+
* <DragButton>≡</DragButton>
|
|
366
|
+
* {file.name}
|
|
367
|
+
* </GridListItem>
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
export function DragButton(props: DragButtonProps) {
|
|
371
|
+
const slot = useDragButton();
|
|
372
|
+
if (slot === null) {
|
|
373
|
+
throw new Error("A DragButton must be rendered inside a draggable collection item.");
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const domRef = makeRef<HTMLButtonElement>();
|
|
377
|
+
const own = fromProps(cells(props));
|
|
378
|
+
const options = mergeProps(slot.options, {
|
|
379
|
+
"aria-label": () =>
|
|
380
|
+
props["aria-label"] ??
|
|
381
|
+
access(slot.options["aria-label"] as MaybeAccessor<string | undefined>),
|
|
382
|
+
}) as ButtonOptions;
|
|
383
|
+
const { buttonProps, isPressed } = button(options, domRef);
|
|
384
|
+
const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
|
|
385
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
386
|
+
|
|
387
|
+
const elementProps = mergeProps(
|
|
388
|
+
buttonProps,
|
|
389
|
+
hoverProps,
|
|
390
|
+
focusProps,
|
|
391
|
+
filterDOMProps(own, { global: true }),
|
|
392
|
+
slot.elementProps,
|
|
393
|
+
styleProps(cells(props)),
|
|
394
|
+
{
|
|
395
|
+
"data-pressed": isPressed,
|
|
396
|
+
"data-hovered": isHovered,
|
|
397
|
+
"data-focused": isFocused,
|
|
398
|
+
"data-focus-visible": isFocusVisible,
|
|
399
|
+
"data-slot": () => props["data-slot"] ?? "drag",
|
|
400
|
+
},
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
return (
|
|
404
|
+
<button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref)}>
|
|
405
|
+
{props.children}
|
|
406
|
+
</button>
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export interface DraggableItemSlotProps {
|
|
411
|
+
/** Props for the item's element. */
|
|
412
|
+
itemProps: DOMProps;
|
|
413
|
+
/** The slot its `<DragButton>` reads. */
|
|
414
|
+
dragButton: DragButtonSlot;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Split a draggable item's props between the item and its drag button.
|
|
419
|
+
*
|
|
420
|
+
* With the default pointer source, a mouse or touch drag starts anywhere on
|
|
421
|
+
* the item and the button lets presses through to it. With `"handle"`, only
|
|
422
|
+
* the button is `draggable`, but the drag events stay on the item so the
|
|
423
|
+
* preview is positioned against the whole item, as when dragging from it.
|
|
424
|
+
*/
|
|
425
|
+
export function draggableItemSlotProps(
|
|
426
|
+
item: DraggableItemResult | null,
|
|
427
|
+
hooks: DragAndDropHooks | undefined,
|
|
428
|
+
dragButtonRef: Accessor<Element | null>,
|
|
429
|
+
setDragButtonRef: (element: Element | null) => void,
|
|
430
|
+
): DraggableItemSlotProps {
|
|
431
|
+
if (hooks?.pointerDragSource !== "handle") {
|
|
432
|
+
return {
|
|
433
|
+
itemProps: item?.dragProps ?? {},
|
|
434
|
+
dragButton: {
|
|
435
|
+
options: item?.dragButtonProps ?? {},
|
|
436
|
+
elementProps: { style: { "pointer-events": "none" } },
|
|
437
|
+
ref: setDragButtonRef,
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const dragProps = item?.dragProps ?? {};
|
|
443
|
+
const onDragStart = dragProps.onDragStart as ((event: DragEvent) => void) | undefined;
|
|
444
|
+
const onDrag = dragProps.onDrag as ((event: DragEvent) => void) | undefined;
|
|
445
|
+
const onDragEnd = dragProps.onDragEnd as ((event: DragEvent) => void) | undefined;
|
|
446
|
+
const isFromDragButton = (event: DragEvent): boolean =>
|
|
447
|
+
contains(dragButtonRef(), event.target instanceof Node ? event.target : null);
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
itemProps: {
|
|
451
|
+
onDragStart: (event: DragEvent) => {
|
|
452
|
+
// Text or an image dragged from inside the item is not an item drag.
|
|
453
|
+
if (!isFromDragButton(event) || onDragStart === undefined) return;
|
|
454
|
+
onDragStart(event);
|
|
455
|
+
// The browser would otherwise photograph only the button.
|
|
456
|
+
if (!event.defaultPrevented && hooks.renderDragPreview === undefined) {
|
|
457
|
+
const element = event.currentTarget as HTMLElement;
|
|
458
|
+
const rect = element.getBoundingClientRect();
|
|
459
|
+
event.dataTransfer?.setDragImage(element, event.clientX - rect.x, event.clientY - rect.y);
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
onDrag: (event: DragEvent) => {
|
|
463
|
+
if (isFromDragButton(event)) onDrag?.(event);
|
|
464
|
+
},
|
|
465
|
+
onDragEnd: (event: DragEvent) => {
|
|
466
|
+
if (isFromDragButton(event)) onDragEnd?.(event);
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
dragButton: {
|
|
470
|
+
options: item?.dragButtonProps ?? {},
|
|
471
|
+
elementProps: { draggable: dragProps.draggable },
|
|
472
|
+
ref: setDragButtonRef,
|
|
473
|
+
},
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// ---------------------------------------------------------------------------
|
|
478
|
+
// The collection side
|
|
479
|
+
// ---------------------------------------------------------------------------
|
|
480
|
+
|
|
481
|
+
export interface CollectionDragAndDropOptions {
|
|
482
|
+
hooks: DragAndDropHooks | undefined;
|
|
483
|
+
collection: Accessor<Collection<unknown>>;
|
|
484
|
+
selectionManager: Accessor<SelectionManager>;
|
|
485
|
+
ref: ElementRef<HTMLElement>;
|
|
486
|
+
/** Moves between drop targets; it should reach disabled items too. */
|
|
487
|
+
keyboardDelegate: KeyboardDelegate;
|
|
488
|
+
layout?: MaybeAccessor<"stack" | "grid" | undefined>;
|
|
489
|
+
orientation?: MaybeAccessor<"horizontal" | "vertical" | undefined>;
|
|
490
|
+
direction?: MaybeAccessor<"ltr" | "rtl" | undefined>;
|
|
491
|
+
/** Wraps the delegate that finds the target under the pointer, as a tree's does. */
|
|
492
|
+
dropTargetDelegate?: (base: DropTargetDelegate) => DropTargetDelegate;
|
|
493
|
+
/** A drag held over, or a keyboard activation of, a target in the collection. */
|
|
494
|
+
onDropActivate?: (event: DroppableCollectionActivateEvent) => void;
|
|
495
|
+
/** Every key of a keyboard drag over the collection, after the target has moved. */
|
|
496
|
+
onKeyDown?: (event: KeyboardEvent) => void;
|
|
497
|
+
/** What takes focus for an item after a drop, where items are not focusable themselves. */
|
|
498
|
+
focusKeyFor?: (key: Key) => Key;
|
|
499
|
+
/**
|
|
500
|
+
* The virtualiser the collection is inside. Its layout then finds the target
|
|
501
|
+
* under the pointer and places the indicators, since most rows a drop may
|
|
502
|
+
* fall between are not in the document.
|
|
503
|
+
*/
|
|
504
|
+
virtual?: VirtualizerValue | null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The delegate without Left and Right where they mean nothing.
|
|
509
|
+
*
|
|
510
|
+
* In a vertical list those keys are not a direction, and a delegate that
|
|
511
|
+
* answers them anyway would walk the drop target with them; in a tree they
|
|
512
|
+
* expand and collapse the row instead.
|
|
513
|
+
*/
|
|
514
|
+
function dropNavigation(
|
|
515
|
+
delegate: KeyboardDelegate,
|
|
516
|
+
layout: MaybeAccessor<"stack" | "grid" | undefined>,
|
|
517
|
+
orientation: MaybeAccessor<"horizontal" | "vertical" | undefined>,
|
|
518
|
+
): KeyboardDelegate {
|
|
519
|
+
const isVerticalStack = (): boolean =>
|
|
520
|
+
(access(layout) ?? "stack") === "stack" && (access(orientation) ?? "vertical") === "vertical";
|
|
521
|
+
const horizontal = (
|
|
522
|
+
method: ((key: Key) => Key | null) | undefined,
|
|
523
|
+
): ((key: Key) => Key | null) | undefined =>
|
|
524
|
+
method === undefined || isVerticalStack() ? undefined : method;
|
|
525
|
+
return {
|
|
526
|
+
getKeyBelow: delegate.getKeyBelow?.bind(delegate),
|
|
527
|
+
getKeyAbove: delegate.getKeyAbove?.bind(delegate),
|
|
528
|
+
getKeyPageBelow: delegate.getKeyPageBelow?.bind(delegate),
|
|
529
|
+
getKeyPageAbove: delegate.getKeyPageAbove?.bind(delegate),
|
|
530
|
+
getFirstKey: delegate.getFirstKey?.bind(delegate),
|
|
531
|
+
getLastKey: delegate.getLastKey?.bind(delegate),
|
|
532
|
+
getKeyForSearch: delegate.getKeyForSearch?.bind(delegate),
|
|
533
|
+
get getKeyLeftOf() {
|
|
534
|
+
return horizontal(delegate.getKeyLeftOf?.bind(delegate));
|
|
535
|
+
},
|
|
536
|
+
get getKeyRightOf() {
|
|
537
|
+
return horizontal(delegate.getKeyRightOf?.bind(delegate));
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export interface CollectionDragAndDrop {
|
|
543
|
+
hooks: DragAndDropHooks | undefined;
|
|
544
|
+
dragState: DraggableCollectionState | undefined;
|
|
545
|
+
dropState: DroppableCollectionState | undefined;
|
|
546
|
+
/** Spread on the collection's element. */
|
|
547
|
+
collectionProps: DOMProps;
|
|
548
|
+
/** Whether the collection itself is where a drop would land. */
|
|
549
|
+
isRootDropTarget: Accessor<boolean>;
|
|
550
|
+
/** The preview to render beside the collection, when the hooks render one. */
|
|
551
|
+
preview: Child;
|
|
552
|
+
/**
|
|
553
|
+
* Whether the indicator for `target` should be in the DOM: while a keyboard
|
|
554
|
+
* or screen reader drag is under way, or while it is the current target.
|
|
555
|
+
*/
|
|
556
|
+
shouldRenderIndicator(target: ItemDropTarget): boolean;
|
|
557
|
+
/** The caller's indicator for `target`, or the default one. */
|
|
558
|
+
renderDropIndicator(target: ItemDropTarget): Child;
|
|
559
|
+
/** Rows a virtualiser has to keep rendered: the one a drop is aimed at. */
|
|
560
|
+
persistedKeys: Accessor<Key[]>;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Where a virtualised collection draws the indicator for `target`, as a style.
|
|
565
|
+
* Nothing outside a virtualiser, where the indicator sits in the flow between
|
|
566
|
+
* the rows it separates.
|
|
567
|
+
*/
|
|
568
|
+
export function dropIndicatorStyle(
|
|
569
|
+
virtual: VirtualizerValue | null,
|
|
570
|
+
target: ItemDropTarget,
|
|
571
|
+
): Record<string, string> | undefined {
|
|
572
|
+
const info = virtual?.layout.getDropTargetLayoutInfo?.(target) ?? null;
|
|
573
|
+
return info === null ? undefined : rowStyle(info);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Call a collection's drag and drop hooks, and share the result with its items.
|
|
578
|
+
*
|
|
579
|
+
* Called from the collection component's body. Provides the context
|
|
580
|
+
* `useDragAndDropContext()` reads in each item.
|
|
581
|
+
*/
|
|
582
|
+
export function collectionDragAndDrop(
|
|
583
|
+
options: CollectionDragAndDropOptions,
|
|
584
|
+
): CollectionDragAndDrop {
|
|
585
|
+
const hooks = options.hooks;
|
|
586
|
+
let dragState: DraggableCollectionState | undefined;
|
|
587
|
+
let dropState: DroppableCollectionState | undefined;
|
|
588
|
+
let collectionProps: DOMProps = {};
|
|
589
|
+
let preview: Child = null;
|
|
590
|
+
|
|
591
|
+
if (hooks?.draggableCollectionState !== undefined) {
|
|
592
|
+
const previewRef = makeRef<DragPreviewRenderer>();
|
|
593
|
+
dragState = hooks.draggableCollectionState({
|
|
594
|
+
collection: options.collection,
|
|
595
|
+
selectionManager: options.selectionManager,
|
|
596
|
+
preview: hooks.renderDragPreview === undefined ? undefined : previewRef,
|
|
597
|
+
});
|
|
598
|
+
hooks.draggableCollection?.({}, dragState, options.ref);
|
|
599
|
+
|
|
600
|
+
const PreviewComponent = hooks.DragPreview;
|
|
601
|
+
const render = hooks.renderDragPreview;
|
|
602
|
+
if (PreviewComponent !== undefined && render !== undefined) {
|
|
603
|
+
preview = <PreviewComponent ref={previewRef.set}>{render}</PreviewComponent>;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (hooks?.droppableCollectionState !== undefined) {
|
|
608
|
+
dropState = hooks.droppableCollectionState({
|
|
609
|
+
collection: options.collection,
|
|
610
|
+
selectionManager: options.selectionManager,
|
|
611
|
+
});
|
|
612
|
+
const Delegate = hooks.ListDropTargetDelegate ?? ListDropTargetDelegate;
|
|
613
|
+
const placed = options.virtual?.layout;
|
|
614
|
+
const base =
|
|
615
|
+
hooks.dropTargetDelegate ??
|
|
616
|
+
(placed?.getDropTargetFromPoint === undefined
|
|
617
|
+
? new Delegate(options.collection, options.ref, {
|
|
618
|
+
layout: options.layout,
|
|
619
|
+
orientation: options.orientation,
|
|
620
|
+
direction: options.direction,
|
|
621
|
+
})
|
|
622
|
+
: {
|
|
623
|
+
getDropTargetFromPoint: (x, y, isValid) =>
|
|
624
|
+
placed.getDropTargetFromPoint?.(x, y, isValid) ?? null,
|
|
625
|
+
});
|
|
626
|
+
const result = hooks.droppableCollection?.(
|
|
627
|
+
{
|
|
628
|
+
keyboardDelegate: dropNavigation(
|
|
629
|
+
options.keyboardDelegate,
|
|
630
|
+
options.layout,
|
|
631
|
+
options.orientation,
|
|
632
|
+
),
|
|
633
|
+
dropTargetDelegate: options.dropTargetDelegate?.(base) ?? base,
|
|
634
|
+
onDropActivate: options.onDropActivate,
|
|
635
|
+
onKeyDown: options.onKeyDown,
|
|
636
|
+
focusKeyFor: options.focusKeyFor,
|
|
637
|
+
},
|
|
638
|
+
dropState,
|
|
639
|
+
options.ref,
|
|
640
|
+
);
|
|
641
|
+
collectionProps = result?.collectionProps ?? {};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
const owner = getOwner();
|
|
645
|
+
if (owner !== null) {
|
|
646
|
+
const value: DragAndDropContextValue = { dragAndDropHooks: hooks, dragState, dropState };
|
|
647
|
+
install(owner, DragAndDropContext, () => value);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const droppable = dropState;
|
|
651
|
+
return {
|
|
652
|
+
persistedKeys: () => {
|
|
653
|
+
const target = droppable?.target() ?? null;
|
|
654
|
+
return target !== null && target.type === "item" ? [target.key] : [];
|
|
655
|
+
},
|
|
656
|
+
hooks,
|
|
657
|
+
dragState,
|
|
658
|
+
dropState,
|
|
659
|
+
collectionProps,
|
|
660
|
+
preview,
|
|
661
|
+
isRootDropTarget: () => droppable?.isDropTarget({ type: "root" }) ?? false,
|
|
662
|
+
shouldRenderIndicator: (target) =>
|
|
663
|
+
droppable !== undefined &&
|
|
664
|
+
hooks?.dropIndicator !== undefined &&
|
|
665
|
+
(hooks.isVirtualDragging?.() === true || droppable.isDropTarget(target)),
|
|
666
|
+
renderDropIndicator: (target) =>
|
|
667
|
+
hooks?.renderDropIndicator?.(target) ?? <DropIndicator target={target} />,
|
|
668
|
+
};
|
|
669
|
+
}
|