@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,801 @@
|
|
|
1
|
+
/** `drag()` and `drop()`: one element dragged, and one element dropped on. */
|
|
2
|
+
|
|
3
|
+
import { type Accessor, createEffect, createSignal } from "@barqjs/core";
|
|
4
|
+
import { tryCleanup } from "@barqjs/primitives/utils";
|
|
5
|
+
import { contains, isScrollable, ownerDocument, scrollParents } from "../dom.ts";
|
|
6
|
+
import { stringFormatter, type StringFormatter } from "../i18n.ts";
|
|
7
|
+
import { description } from "../interactions/description.ts";
|
|
8
|
+
import { globalListeners } from "../interactions/listeners.ts";
|
|
9
|
+
import { isVirtualClick, isVirtualPointerEvent } from "../interactions/modality.ts";
|
|
10
|
+
import type { ElementRef, PressEvent } from "../interactions/press.ts";
|
|
11
|
+
import { isIOS, isIPad, isMac, isWebKit } from "../platform.ts";
|
|
12
|
+
import { access, type DOMProps, type MaybeAccessor, mergeProps } from "../utils.ts";
|
|
13
|
+
import {
|
|
14
|
+
ALL,
|
|
15
|
+
ALLOWED_BITS,
|
|
16
|
+
allowedOperation,
|
|
17
|
+
COPY,
|
|
18
|
+
dndState,
|
|
19
|
+
type DropEffect,
|
|
20
|
+
EFFECT_ALLOWED,
|
|
21
|
+
EFFECT_TO_OPERATION,
|
|
22
|
+
globalAllowedDropOperations,
|
|
23
|
+
globalDropEffect,
|
|
24
|
+
LINK,
|
|
25
|
+
MOVE,
|
|
26
|
+
NONE,
|
|
27
|
+
OPERATION_BITS,
|
|
28
|
+
OPERATION_TO_EFFECT,
|
|
29
|
+
operationsToArray,
|
|
30
|
+
readFromDataTransfer,
|
|
31
|
+
DataTransferTypes,
|
|
32
|
+
refElement,
|
|
33
|
+
setGlobalAllowedDropOperations,
|
|
34
|
+
setGlobalDndState,
|
|
35
|
+
setGlobalDropEffect,
|
|
36
|
+
writeToDataTransfer,
|
|
37
|
+
} from "./data.ts";
|
|
38
|
+
import {
|
|
39
|
+
beginDragging,
|
|
40
|
+
centre,
|
|
41
|
+
type DragModality,
|
|
42
|
+
dragModality,
|
|
43
|
+
dragSession,
|
|
44
|
+
registerDropTarget,
|
|
45
|
+
} from "./manager.ts";
|
|
46
|
+
import { type DndMessage, dndStrings } from "./strings.ts";
|
|
47
|
+
import type {
|
|
48
|
+
DragEndEvent,
|
|
49
|
+
DragItem,
|
|
50
|
+
DragMoveEvent,
|
|
51
|
+
DragPreviewRenderer,
|
|
52
|
+
DragStartEvent,
|
|
53
|
+
DragTypes,
|
|
54
|
+
DropActivateEvent,
|
|
55
|
+
DropEnterEvent,
|
|
56
|
+
DropEvent,
|
|
57
|
+
DropExitEvent,
|
|
58
|
+
DropMoveEvent,
|
|
59
|
+
DropOperation,
|
|
60
|
+
} from "./types.ts";
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// drag
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
export interface DragOptions {
|
|
67
|
+
onDragStart?: (event: DragStartEvent) => void;
|
|
68
|
+
onDragMove?: (event: DragMoveEvent) => void;
|
|
69
|
+
/** After a drop or a cancel. */
|
|
70
|
+
onDragEnd?: (event: DragEndEvent) => void;
|
|
71
|
+
getItems: () => DragItem[];
|
|
72
|
+
/** Renders what follows the pointer. Without one, the browser photographs the element. */
|
|
73
|
+
preview?: MaybeAccessor<DragPreviewRenderer | null | undefined>;
|
|
74
|
+
/** @default every operation */
|
|
75
|
+
getAllowedDropOperations?: () => DropOperation[];
|
|
76
|
+
/**
|
|
77
|
+
* Whether a separate button starts keyboard and screen reader drags. The
|
|
78
|
+
* Enter and click handling then goes on `dragButtonProps`, not the element.
|
|
79
|
+
*/
|
|
80
|
+
hasDragButton?: boolean;
|
|
81
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface DragResult {
|
|
85
|
+
dragProps: DOMProps;
|
|
86
|
+
/** Options for `button()`, for the drag button when there is one. */
|
|
87
|
+
dragButtonProps: DOMProps;
|
|
88
|
+
isDragging: Accessor<boolean>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const DRAG_MESSAGES: Record<DragModality, { start: DndMessage; end: DndMessage }> = {
|
|
92
|
+
keyboard: { start: "dragDescriptionKeyboard", end: "endDragKeyboard" },
|
|
93
|
+
touch: { start: "dragDescriptionTouch", end: "endDragTouch" },
|
|
94
|
+
virtual: { start: "dragDescriptionVirtual", end: "endDragVirtual" },
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export interface DragInteraction {
|
|
98
|
+
/** `draggable` and the native drag events. */
|
|
99
|
+
nativeProps: DOMProps;
|
|
100
|
+
/** Enter, and a screen reader's click or long press, on the element itself. */
|
|
101
|
+
accessibleProps: DOMProps;
|
|
102
|
+
onPress: (event: PressEvent) => void;
|
|
103
|
+
isDragging: Accessor<boolean>;
|
|
104
|
+
message: Accessor<string>;
|
|
105
|
+
strings: Accessor<StringFormatter<DndMessage>>;
|
|
106
|
+
modality: Accessor<DragModality>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function dragInteraction(options: DragOptions): DragInteraction {
|
|
110
|
+
const strings = stringFormatter(dndStrings);
|
|
111
|
+
const currentModality = dragModality();
|
|
112
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
113
|
+
const listeners = globalListeners();
|
|
114
|
+
|
|
115
|
+
const state = { x: 0, y: 0 };
|
|
116
|
+
let draggingElement: Element | null = null;
|
|
117
|
+
const [isDragging, setDraggingState] = createSignal(false, { ownedWrite: true });
|
|
118
|
+
const setDragging = (element: Element | null): void => {
|
|
119
|
+
draggingElement = element;
|
|
120
|
+
setDraggingState(element !== null);
|
|
121
|
+
};
|
|
122
|
+
let modalityOnPointerDown: string | null = null;
|
|
123
|
+
|
|
124
|
+
const allowedOperations = (): DropOperation[] =>
|
|
125
|
+
options.getAllowedDropOperations?.() ?? ["move", "copy", "link"];
|
|
126
|
+
|
|
127
|
+
const startDragging = (target: HTMLElement): void => {
|
|
128
|
+
if (options.onDragStart !== undefined) {
|
|
129
|
+
const { x, y } = centre(target);
|
|
130
|
+
options.onDragStart({ type: "dragstart", x, y });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
beginDragging(
|
|
134
|
+
{
|
|
135
|
+
element: target,
|
|
136
|
+
items: options.getItems(),
|
|
137
|
+
allowedDropOperations: allowedOperations(),
|
|
138
|
+
onDragEnd(event) {
|
|
139
|
+
setDragging(null);
|
|
140
|
+
options.onDragEnd?.(event);
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
strings,
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
setDragging(target);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const onDragStart = (event: DragEvent): void => {
|
|
150
|
+
if (event.defaultPrevented || isDisabled()) return;
|
|
151
|
+
// A draggable inside a draggable: only the innermost drags.
|
|
152
|
+
event.stopPropagation();
|
|
153
|
+
|
|
154
|
+
// A screen reader's long press arrives as a native drag; it becomes a
|
|
155
|
+
// virtual one.
|
|
156
|
+
if (modalityOnPointerDown === "virtual") {
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
startDragging(event.target as HTMLElement);
|
|
159
|
+
modalityOnPointerDown = null;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
options.onDragStart?.({ type: "dragstart", x: event.clientX, y: event.clientY });
|
|
164
|
+
|
|
165
|
+
const items = options.getItems();
|
|
166
|
+
const dataTransfer = event.dataTransfer;
|
|
167
|
+
if (dataTransfer === null) return;
|
|
168
|
+
// Selected text on the page would otherwise ride along in some browsers.
|
|
169
|
+
dataTransfer.clearData?.();
|
|
170
|
+
writeToDataTransfer(dataTransfer, items);
|
|
171
|
+
|
|
172
|
+
let allowed = ALL;
|
|
173
|
+
if (options.getAllowedDropOperations !== undefined) {
|
|
174
|
+
allowed = NONE;
|
|
175
|
+
for (const operation of options.getAllowedDropOperations()) {
|
|
176
|
+
allowed |= OPERATION_BITS[operation] ?? NONE;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
setGlobalAllowedDropOperations(allowed);
|
|
180
|
+
dataTransfer.effectAllowed = EFFECT_ALLOWED[allowed] ?? "none";
|
|
181
|
+
|
|
182
|
+
const renderer = access(options.preview);
|
|
183
|
+
if (typeof renderer === "function") {
|
|
184
|
+
const source = event.currentTarget as HTMLElement;
|
|
185
|
+
renderer(items, (node, userX, userY) => {
|
|
186
|
+
if (node === null) return;
|
|
187
|
+
// Under the pointer where the element was grabbed, unless the preview
|
|
188
|
+
// is too small for that, in which case centred.
|
|
189
|
+
const size = node.getBoundingClientRect();
|
|
190
|
+
const rect = source.getBoundingClientRect();
|
|
191
|
+
let x = event.clientX - rect.x;
|
|
192
|
+
let y = event.clientY - rect.y;
|
|
193
|
+
if (x > size.width || y > size.height) {
|
|
194
|
+
x = size.width / 2;
|
|
195
|
+
y = size.height / 2;
|
|
196
|
+
}
|
|
197
|
+
if (typeof userX === "number" && typeof userY === "number") {
|
|
198
|
+
x = userX;
|
|
199
|
+
y = userY;
|
|
200
|
+
}
|
|
201
|
+
// Clamped here so the browser does not snap it after the first move.
|
|
202
|
+
x = Math.max(0, Math.min(x, size.width));
|
|
203
|
+
y = Math.max(0, Math.min(y, size.height));
|
|
204
|
+
// An odd height blurs the preview on some displays.
|
|
205
|
+
node.style.height = `${2 * Math.round(size.height / 2)}px`;
|
|
206
|
+
dataTransfer.setDragImage(node, x, y);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// A drag started here may only land on a `drop()` target, which is what
|
|
211
|
+
// guarantees a keyboard alternative exists for wherever it lands.
|
|
212
|
+
listeners.add(
|
|
213
|
+
window,
|
|
214
|
+
"drop",
|
|
215
|
+
(dropEvent: Event) => {
|
|
216
|
+
dropEvent.preventDefault();
|
|
217
|
+
dropEvent.stopPropagation();
|
|
218
|
+
console.warn(
|
|
219
|
+
"A drag started with drag() may only be dropped on a target created with drop(), so that a keyboard and screen reader alternative exists.",
|
|
220
|
+
);
|
|
221
|
+
},
|
|
222
|
+
{ once: true },
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
state.x = event.clientX;
|
|
226
|
+
state.y = event.clientY;
|
|
227
|
+
|
|
228
|
+
// A frame later, so the browser photographs the element before its
|
|
229
|
+
// dragging style lands.
|
|
230
|
+
const target = event.target as Element;
|
|
231
|
+
requestAnimationFrame(() => setDragging(target));
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const onDrag = (event: DragEvent): void => {
|
|
235
|
+
event.stopPropagation();
|
|
236
|
+
if (event.clientX === state.x && event.clientY === state.y) return;
|
|
237
|
+
options.onDragMove?.({ type: "dragmove", x: event.clientX, y: event.clientY });
|
|
238
|
+
state.x = event.clientX;
|
|
239
|
+
state.y = event.clientY;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const resetGlobals = (): void => {
|
|
243
|
+
setGlobalAllowedDropOperations(NONE);
|
|
244
|
+
setGlobalDropEffect(undefined);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const onDragEnd = (event: DragEvent): void => {
|
|
248
|
+
event.stopPropagation();
|
|
249
|
+
if (options.onDragEnd !== undefined) {
|
|
250
|
+
// Chrome on Android always reports `none`, so the effect the drop target
|
|
251
|
+
// settled on is used when there is one.
|
|
252
|
+
// https://bugs.chromium.org/p/chromium/issues/detail?id=1353951
|
|
253
|
+
const effect = globalDropEffect() ?? event.dataTransfer?.dropEffect ?? "none";
|
|
254
|
+
options.onDragEnd({
|
|
255
|
+
type: "dragend",
|
|
256
|
+
x: event.clientX,
|
|
257
|
+
y: event.clientY,
|
|
258
|
+
dropOperation: EFFECT_TO_OPERATION[effect],
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
setDragging(null);
|
|
262
|
+
listeners.removeAll();
|
|
263
|
+
resetGlobals();
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// A dragged element removed by its own drop never receives `dragend`
|
|
267
|
+
// (https://bugzilla.mozilla.org/show_bug.cgi?id=460801), so the drag is
|
|
268
|
+
// ended here if the element did not survive.
|
|
269
|
+
tryCleanup(() => {
|
|
270
|
+
const element = draggingElement;
|
|
271
|
+
if (element === null) return;
|
|
272
|
+
queueMicrotask(() => {
|
|
273
|
+
if (element.isConnected) return;
|
|
274
|
+
options.onDragEnd?.({
|
|
275
|
+
type: "dragend",
|
|
276
|
+
x: 0,
|
|
277
|
+
y: 0,
|
|
278
|
+
dropOperation: EFFECT_TO_OPERATION[globalDropEffect() ?? "none"],
|
|
279
|
+
});
|
|
280
|
+
draggingElement = null;
|
|
281
|
+
resetGlobals();
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const onPress = (event: PressEvent): void => {
|
|
286
|
+
if (isDisabled()) return;
|
|
287
|
+
if (event.pointerType !== "keyboard" && event.pointerType !== "virtual") return;
|
|
288
|
+
startDragging(event.target as HTMLElement);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const accessibleProps: DOMProps = {
|
|
292
|
+
onPointerDown(event: PointerEvent) {
|
|
293
|
+
if (isDisabled()) return;
|
|
294
|
+
if (isVirtualPointerEvent(event)) {
|
|
295
|
+
modalityOnPointerDown = "virtual";
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (event.width < 1 && event.height < 1 && isIOS() && isWebKit()) {
|
|
299
|
+
// VoiceOver on iOS.
|
|
300
|
+
modalityOnPointerDown = "virtual";
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const rect = (event.currentTarget as Element).getBoundingClientRect();
|
|
304
|
+
const offsetX = event.clientX - rect.x;
|
|
305
|
+
const offsetY = event.clientY - rect.y;
|
|
306
|
+
// TalkBack presses the exact centre.
|
|
307
|
+
if (Math.abs(offsetX - rect.width / 2) <= 0.5 && Math.abs(offsetY - rect.height / 2) <= 0.5) {
|
|
308
|
+
modalityOnPointerDown = "virtual";
|
|
309
|
+
} else {
|
|
310
|
+
modalityOnPointerDown = event.pointerType;
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
// Enter is taken in the capture phase so that selection does not also see it.
|
|
314
|
+
onKeyDownCapture(event: KeyboardEvent) {
|
|
315
|
+
if (isDisabled()) return;
|
|
316
|
+
if (event.target === event.currentTarget && event.key === "Enter") {
|
|
317
|
+
event.preventDefault();
|
|
318
|
+
event.stopPropagation();
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
onKeyUpCapture(event: KeyboardEvent) {
|
|
322
|
+
if (isDisabled()) return;
|
|
323
|
+
if (event.target === event.currentTarget && event.key === "Enter") {
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
event.stopPropagation();
|
|
326
|
+
startDragging(event.target as HTMLElement);
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
// NVDA and JAWS in browse mode, and touch screen readers, send no keys.
|
|
330
|
+
onClick(event: MouseEvent) {
|
|
331
|
+
if (isDisabled()) return;
|
|
332
|
+
if (isVirtualClick(event) || modalityOnPointerDown === "virtual") {
|
|
333
|
+
event.preventDefault();
|
|
334
|
+
event.stopPropagation();
|
|
335
|
+
startDragging(event.target as HTMLElement);
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const nativeProps: DOMProps = {
|
|
341
|
+
draggable: () => (isDisabled() ? "false" : "true"),
|
|
342
|
+
onDragStart,
|
|
343
|
+
onDrag,
|
|
344
|
+
onDragEnd,
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
nativeProps,
|
|
349
|
+
accessibleProps,
|
|
350
|
+
onPress,
|
|
351
|
+
isDragging: () => !isDisabled() && isDragging(),
|
|
352
|
+
message: () => {
|
|
353
|
+
const messages = DRAG_MESSAGES[currentModality()];
|
|
354
|
+
return strings().format(isDragging() ? messages.end : messages.start);
|
|
355
|
+
},
|
|
356
|
+
strings,
|
|
357
|
+
modality: currentModality,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Make an element draggable, by pointer, keyboard and screen reader.
|
|
363
|
+
*
|
|
364
|
+
* ```tsx
|
|
365
|
+
* const { dragProps, isDragging } = drag({
|
|
366
|
+
* getItems: () => [{ "text/plain": "salama" }],
|
|
367
|
+
* });
|
|
368
|
+
* <div {...dragProps} role="button" tabIndex={0} data-dragging={isDragging}>Drag me</div>
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
371
|
+
export function drag(options: DragOptions): DragResult {
|
|
372
|
+
const interaction = dragInteraction(options);
|
|
373
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
374
|
+
const describedBy = description(() => (isDisabled() ? undefined : interaction.message()));
|
|
375
|
+
|
|
376
|
+
const dragProps = mergeProps(
|
|
377
|
+
options.hasDragButton === true ? {} : interaction.accessibleProps,
|
|
378
|
+
options.hasDragButton === true ? {} : describedBy,
|
|
379
|
+
interaction.nativeProps,
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
dragProps,
|
|
384
|
+
dragButtonProps: {
|
|
385
|
+
...describedBy,
|
|
386
|
+
onPress: interaction.onPress,
|
|
387
|
+
isDisabled,
|
|
388
|
+
},
|
|
389
|
+
isDragging: interaction.isDragging,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// ---------------------------------------------------------------------------
|
|
394
|
+
// drop
|
|
395
|
+
// ---------------------------------------------------------------------------
|
|
396
|
+
|
|
397
|
+
export interface DropOptions {
|
|
398
|
+
ref: ElementRef<HTMLElement>;
|
|
399
|
+
/** The operation a drop of `types` would perform. @default the first allowed */
|
|
400
|
+
getDropOperation?: (types: DragTypes, allowedOperations: DropOperation[]) => DropOperation;
|
|
401
|
+
/** As `getDropOperation`, for a point inside the target. Pointer drags only. */
|
|
402
|
+
getDropOperationForPoint?: (
|
|
403
|
+
types: DragTypes,
|
|
404
|
+
allowedOperations: DropOperation[],
|
|
405
|
+
x: number,
|
|
406
|
+
y: number,
|
|
407
|
+
) => DropOperation;
|
|
408
|
+
onDropEnter?: (event: DropEnterEvent) => void;
|
|
409
|
+
onDropMove?: (event: DropMoveEvent) => void;
|
|
410
|
+
/** A drag held over the target for a while, which typically opens it. */
|
|
411
|
+
onDropActivate?: (event: DropActivateEvent) => void;
|
|
412
|
+
onDropExit?: (event: DropExitEvent) => void;
|
|
413
|
+
onDrop?: (event: DropEvent) => void;
|
|
414
|
+
/** Whether a separate button is the target for keyboard and screen reader drops. */
|
|
415
|
+
hasDropButton?: boolean;
|
|
416
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export interface DropResult {
|
|
420
|
+
dropProps: DOMProps;
|
|
421
|
+
isDropTarget: Accessor<boolean>;
|
|
422
|
+
/** Options for `button()`, for the drop button when there is one. */
|
|
423
|
+
dropButtonProps: DOMProps;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const DROP_ACTIVATE_TIMEOUT = 800;
|
|
427
|
+
|
|
428
|
+
const DROP_MESSAGES: Record<DragModality, DndMessage> = {
|
|
429
|
+
keyboard: "dropDescriptionKeyboard",
|
|
430
|
+
touch: "dropDescriptionTouch",
|
|
431
|
+
virtual: "dropDescriptionVirtual",
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
/** The description a drop target carries while a drag is under way, and the click it needs. */
|
|
435
|
+
export function virtualDrop(describe: boolean): DOMProps {
|
|
436
|
+
const strings = stringFormatter(dndStrings);
|
|
437
|
+
const currentModality = dragModality();
|
|
438
|
+
return {
|
|
439
|
+
...(describe
|
|
440
|
+
? description(() =>
|
|
441
|
+
dragSession() === null ? undefined : strings().format(DROP_MESSAGES[currentModality()]),
|
|
442
|
+
)
|
|
443
|
+
: {}),
|
|
444
|
+
// Mobile Safari only delivers a click to an element that has a click
|
|
445
|
+
// listener of its own, and the drag manager reads clicks at the document.
|
|
446
|
+
// Bubble-phase clicks are delegated to the root, so the listener that
|
|
447
|
+
// lands on the element itself is the capturing one.
|
|
448
|
+
// https://www.quirksmode.org/blog/archives/2010/09/click_event_del.html
|
|
449
|
+
onClickCapture: acceptClick,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function acceptClick(): void {
|
|
454
|
+
// Present only so the element has a click listener; see `virtualDrop`.
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function allowedOperationsOf(event: DragEvent): number {
|
|
458
|
+
const effectAllowed = event.dataTransfer?.effectAllowed ?? "all";
|
|
459
|
+
let allowed = ALLOWED_BITS[effectAllowed] ?? ALL;
|
|
460
|
+
|
|
461
|
+
// WebKit reports `copyMove` on macOS and `all` on iOS whatever the drag
|
|
462
|
+
// allowed (https://bugs.webkit.org/show_bug.cgi?id=178058), and Chrome on
|
|
463
|
+
// Android always `copyMove`. A drag started on this page recorded what it
|
|
464
|
+
// really allows.
|
|
465
|
+
if (globalAllowedDropOperations() !== NONE) allowed &= globalAllowedDropOperations();
|
|
466
|
+
|
|
467
|
+
// Chrome and Safari on macOS narrow `effectAllowed` for the modifier keys
|
|
468
|
+
// themselves; Firefox and every Windows browser do not, so the narrowing is
|
|
469
|
+
// done here with each platform's keys.
|
|
470
|
+
let modifiers = NONE;
|
|
471
|
+
if (isMac()) {
|
|
472
|
+
if (event.altKey) modifiers |= COPY;
|
|
473
|
+
// iPadOS never fires `drop` with a link effect.
|
|
474
|
+
// https://bugs.webkit.org/show_bug.cgi?id=244701
|
|
475
|
+
if (event.ctrlKey && !isIPad()) modifiers |= LINK;
|
|
476
|
+
if (event.metaKey) modifiers |= MOVE;
|
|
477
|
+
} else {
|
|
478
|
+
if (event.altKey) modifiers |= LINK;
|
|
479
|
+
if (event.shiftKey) modifiers |= MOVE;
|
|
480
|
+
if (event.ctrlKey) modifiers |= COPY;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return modifiers !== NONE ? allowed & modifiers : allowed;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export function dropBehaviour(options: DropOptions, describe: boolean): DropResult {
|
|
487
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
488
|
+
const [isDropTarget, setDropTarget] = createSignal(false, { ownedWrite: true });
|
|
489
|
+
const state = {
|
|
490
|
+
x: 0,
|
|
491
|
+
y: 0,
|
|
492
|
+
dragOverElements: new Set<Element>(),
|
|
493
|
+
dropEffect: "none" as DropEffect,
|
|
494
|
+
allowedOperations: ALL,
|
|
495
|
+
dropActivateTimer: undefined as ReturnType<typeof setTimeout> | undefined,
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
tryCleanup(() => clearTimeout(state.dropActivateTimer));
|
|
499
|
+
|
|
500
|
+
const relative = (event: DragEvent, x = event.clientX, y = event.clientY) => {
|
|
501
|
+
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
|
|
502
|
+
return { x: x - rect.x, y: y - rect.y };
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
const fireDropEnter = (event: DragEvent): void => {
|
|
506
|
+
setDropTarget(true);
|
|
507
|
+
options.onDropEnter?.({ type: "dropenter", ...relative(event) });
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const fireDropExit = (event: DragEvent): void => {
|
|
511
|
+
setDropTarget(false);
|
|
512
|
+
options.onDropExit?.({ type: "dropexit", ...relative(event) });
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const operationForPoint = (event: DragEvent, allowedBits: number, x: number, y: number) => {
|
|
516
|
+
const forPoint = options.getDropOperationForPoint;
|
|
517
|
+
if (forPoint === undefined || event.dataTransfer === null) return null;
|
|
518
|
+
const point = relative(event, x, y);
|
|
519
|
+
return allowedOperation(
|
|
520
|
+
allowedBits,
|
|
521
|
+
forPoint(
|
|
522
|
+
new DataTransferTypes(event.dataTransfer),
|
|
523
|
+
operationsToArray(allowedBits),
|
|
524
|
+
point.x,
|
|
525
|
+
point.y,
|
|
526
|
+
),
|
|
527
|
+
);
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const onDragOver = (event: DragEvent): void => {
|
|
531
|
+
event.preventDefault();
|
|
532
|
+
event.stopPropagation();
|
|
533
|
+
if (isDisabled() || event.dataTransfer === null) return;
|
|
534
|
+
|
|
535
|
+
const allowedBits = allowedOperationsOf(event);
|
|
536
|
+
if (
|
|
537
|
+
event.clientX === state.x &&
|
|
538
|
+
event.clientY === state.y &&
|
|
539
|
+
allowedBits === state.allowedOperations
|
|
540
|
+
) {
|
|
541
|
+
event.dataTransfer.dropEffect = state.dropEffect;
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
state.x = event.clientX;
|
|
546
|
+
state.y = event.clientY;
|
|
547
|
+
const previousEffect = state.dropEffect;
|
|
548
|
+
|
|
549
|
+
// A modifier key pressed mid-drag changes what is allowed.
|
|
550
|
+
if (allowedBits !== state.allowedOperations) {
|
|
551
|
+
const allowed = operationsToArray(allowedBits);
|
|
552
|
+
let operation: DropOperation = allowed[0] ?? "cancel";
|
|
553
|
+
if (options.getDropOperation !== undefined) {
|
|
554
|
+
operation = allowedOperation(
|
|
555
|
+
allowedBits,
|
|
556
|
+
options.getDropOperation(new DataTransferTypes(event.dataTransfer), allowed),
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
state.dropEffect = OPERATION_TO_EFFECT[operation];
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
const forPoint = operationForPoint(event, allowedBits, state.x, state.y);
|
|
563
|
+
if (forPoint !== null) state.dropEffect = OPERATION_TO_EFFECT[forPoint];
|
|
564
|
+
|
|
565
|
+
state.allowedOperations = allowedBits;
|
|
566
|
+
event.dataTransfer.dropEffect = state.dropEffect;
|
|
567
|
+
|
|
568
|
+
if (state.dropEffect === "none" && previousEffect !== "none") fireDropExit(event);
|
|
569
|
+
else if (state.dropEffect !== "none" && previousEffect === "none") fireDropEnter(event);
|
|
570
|
+
|
|
571
|
+
if (options.onDropMove !== undefined && state.dropEffect !== "none") {
|
|
572
|
+
options.onDropMove({ type: "dropmove", ...relative(event, state.x, state.y) });
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
clearTimeout(state.dropActivateTimer);
|
|
576
|
+
const onDropActivate = options.onDropActivate;
|
|
577
|
+
if (onDropActivate !== undefined && state.dropEffect !== "none") {
|
|
578
|
+
const point = relative(event, state.x, state.y);
|
|
579
|
+
state.dropActivateTimer = setTimeout(() => {
|
|
580
|
+
onDropActivate({ type: "dropactivate", ...point });
|
|
581
|
+
}, DROP_ACTIVATE_TIMEOUT);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
const onDragEnter = (event: DragEvent): void => {
|
|
586
|
+
event.preventDefault();
|
|
587
|
+
event.stopPropagation();
|
|
588
|
+
if (isDisabled() || event.dataTransfer === null) return;
|
|
589
|
+
|
|
590
|
+
state.dragOverElements.add(event.target as Element);
|
|
591
|
+
if (state.dragOverElements.size > 1) return;
|
|
592
|
+
|
|
593
|
+
const allowedBits = allowedOperationsOf(event);
|
|
594
|
+
const allowed = operationsToArray(allowedBits);
|
|
595
|
+
let operation: DropOperation = allowed[0] ?? "cancel";
|
|
596
|
+
if (options.getDropOperation !== undefined) {
|
|
597
|
+
operation = allowedOperation(
|
|
598
|
+
allowedBits,
|
|
599
|
+
options.getDropOperation(new DataTransferTypes(event.dataTransfer), allowed),
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
const forPoint = operationForPoint(event, allowedBits, event.clientX, event.clientY);
|
|
603
|
+
if (forPoint !== null) operation = forPoint;
|
|
604
|
+
|
|
605
|
+
state.x = event.clientX;
|
|
606
|
+
state.y = event.clientY;
|
|
607
|
+
state.allowedOperations = allowedBits;
|
|
608
|
+
state.dropEffect = OPERATION_TO_EFFECT[operation];
|
|
609
|
+
event.dataTransfer.dropEffect = state.dropEffect;
|
|
610
|
+
|
|
611
|
+
if (operation !== "cancel") fireDropEnter(event);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const onDragLeave = (event: DragEvent): void => {
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
event.stopPropagation();
|
|
617
|
+
if (isDisabled()) return;
|
|
618
|
+
|
|
619
|
+
// `relatedTarget` would answer whether the drag is still inside, but
|
|
620
|
+
// WebKit always reports null (https://bugs.webkit.org/show_bug.cgi?id=66547).
|
|
621
|
+
// So every element entered is counted, and the target is left when the
|
|
622
|
+
// count is empty. An element removed mid-drag (a drop indicator that
|
|
623
|
+
// moved) never reports leaving, so those are dropped too.
|
|
624
|
+
const target = event.target as Element;
|
|
625
|
+
state.dragOverElements.delete(target);
|
|
626
|
+
if (target === event.currentTarget) {
|
|
627
|
+
for (const element of state.dragOverElements) {
|
|
628
|
+
if (!contains(event.currentTarget as Element, element)) {
|
|
629
|
+
state.dragOverElements.delete(element);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (state.dragOverElements.size > 0) return;
|
|
635
|
+
if (state.dropEffect !== "none") fireDropExit(event);
|
|
636
|
+
clearTimeout(state.dropActivateTimer);
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const onDrop = (event: DragEvent): void => {
|
|
640
|
+
event.preventDefault();
|
|
641
|
+
event.stopPropagation();
|
|
642
|
+
if (isDisabled()) return;
|
|
643
|
+
|
|
644
|
+
// Kept for `dragend`, which Chrome on Android reports as `none`.
|
|
645
|
+
setGlobalDropEffect(state.dropEffect);
|
|
646
|
+
|
|
647
|
+
if (options.onDrop !== undefined) {
|
|
648
|
+
options.onDrop({
|
|
649
|
+
type: "drop",
|
|
650
|
+
...relative(event),
|
|
651
|
+
items: readFromDataTransfer(event.dataTransfer),
|
|
652
|
+
dropOperation: EFFECT_TO_OPERATION[state.dropEffect],
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
const snapshot = { ...dndState() };
|
|
657
|
+
state.dragOverElements.clear();
|
|
658
|
+
fireDropExit(event);
|
|
659
|
+
clearTimeout(state.dropActivateTimer);
|
|
660
|
+
// A drag from outside any collection needs nothing kept for its `dragend`.
|
|
661
|
+
// One from a collection does: its `dragend` asks whether the drop was
|
|
662
|
+
// internal, and the exit above cleared the drop collection.
|
|
663
|
+
if (snapshot.draggingCollectionRef === undefined) setGlobalDropEffect(undefined);
|
|
664
|
+
else setGlobalDndState(snapshot);
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
createEffect(
|
|
668
|
+
() => ({ element: refElement(options.ref) as HTMLElement | null, disabled: isDisabled() }),
|
|
669
|
+
({ element, disabled }) => {
|
|
670
|
+
if (disabled || element === null) return undefined;
|
|
671
|
+
return registerDropTarget({
|
|
672
|
+
element,
|
|
673
|
+
getDropOperation: (types, allowed) =>
|
|
674
|
+
options.getDropOperation?.(types, allowed) ?? allowed[0] ?? "cancel",
|
|
675
|
+
onDropEnter(event) {
|
|
676
|
+
setDropTarget(true);
|
|
677
|
+
options.onDropEnter?.(event);
|
|
678
|
+
},
|
|
679
|
+
onDropExit(event) {
|
|
680
|
+
setDropTarget(false);
|
|
681
|
+
options.onDropExit?.(event);
|
|
682
|
+
},
|
|
683
|
+
onDrop: (event) => options.onDrop?.(event),
|
|
684
|
+
onDropActivate: (event) => options.onDropActivate?.(event),
|
|
685
|
+
});
|
|
686
|
+
},
|
|
687
|
+
);
|
|
688
|
+
|
|
689
|
+
const virtual = virtualDrop(describe);
|
|
690
|
+
const hasDropButton = options.hasDropButton === true;
|
|
691
|
+
|
|
692
|
+
return {
|
|
693
|
+
dropProps: {
|
|
694
|
+
...(hasDropButton ? {} : virtual),
|
|
695
|
+
onDragEnter,
|
|
696
|
+
onDragOver,
|
|
697
|
+
onDragLeave,
|
|
698
|
+
onDrop,
|
|
699
|
+
},
|
|
700
|
+
dropButtonProps: { ...(hasDropButton ? virtual : {}), isDisabled },
|
|
701
|
+
isDropTarget: () => !isDisabled() && isDropTarget(),
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Make an element a drop target, by pointer, keyboard and screen reader.
|
|
707
|
+
*
|
|
708
|
+
* ```tsx
|
|
709
|
+
* const domRef = ref<HTMLDivElement>();
|
|
710
|
+
* const { dropProps, isDropTarget } = drop({ ref: domRef, onDrop: (e) => read(e.items) });
|
|
711
|
+
* <div {...dropProps} ref={domRef.set} role="button" tabIndex={0} data-drop-target={isDropTarget} />
|
|
712
|
+
* ```
|
|
713
|
+
*/
|
|
714
|
+
export function drop(options: DropOptions): DropResult {
|
|
715
|
+
return dropBehaviour(options, true);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// ---------------------------------------------------------------------------
|
|
719
|
+
// Auto scroll
|
|
720
|
+
// ---------------------------------------------------------------------------
|
|
721
|
+
|
|
722
|
+
const AUTOSCROLL_AREA_SIZE = 20;
|
|
723
|
+
|
|
724
|
+
export interface AutoScroll {
|
|
725
|
+
move(x: number, y: number): void;
|
|
726
|
+
stop(): void;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* Scroll the element's scroll parent while a drag hovers near its edge.
|
|
731
|
+
*
|
|
732
|
+
* Every engine does this natively except WebKit on macOS.
|
|
733
|
+
* https://bugs.webkit.org/show_bug.cgi?id=222636
|
|
734
|
+
*/
|
|
735
|
+
export function autoScroll(ref: ElementRef): AutoScroll {
|
|
736
|
+
const state = {
|
|
737
|
+
timer: undefined as number | undefined,
|
|
738
|
+
dx: 0,
|
|
739
|
+
dy: 0,
|
|
740
|
+
scrollable: null as Element | null,
|
|
741
|
+
x: true,
|
|
742
|
+
y: true,
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
const resolve = (): Element | null => {
|
|
746
|
+
const element = refElement(ref);
|
|
747
|
+
if (element === null) return null;
|
|
748
|
+
if (state.scrollable === null) {
|
|
749
|
+
state.scrollable = isScrollable(element)
|
|
750
|
+
? element
|
|
751
|
+
: (scrollParents(element.parentElement ?? element)[0] ??
|
|
752
|
+
ownerDocument(element).scrollingElement ??
|
|
753
|
+
ownerDocument(element).documentElement);
|
|
754
|
+
const style = window.getComputedStyle(state.scrollable);
|
|
755
|
+
state.x = /(auto|scroll)/.test(style.overflowX);
|
|
756
|
+
state.y = /(auto|scroll)/.test(style.overflowY);
|
|
757
|
+
}
|
|
758
|
+
return state.scrollable;
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
const stop = (): void => {
|
|
762
|
+
if (state.timer === undefined) return;
|
|
763
|
+
cancelAnimationFrame(state.timer);
|
|
764
|
+
state.timer = undefined;
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
tryCleanup(stop);
|
|
768
|
+
|
|
769
|
+
const scroll = (): void => {
|
|
770
|
+
const scrollable = state.scrollable;
|
|
771
|
+
if (scrollable !== null) {
|
|
772
|
+
if (state.x) scrollable.scrollLeft += state.dx;
|
|
773
|
+
if (state.y) scrollable.scrollTop += state.dy;
|
|
774
|
+
}
|
|
775
|
+
if (state.timer !== undefined) state.timer = requestAnimationFrame(scroll);
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
return {
|
|
779
|
+
move(x, y) {
|
|
780
|
+
if (!isWebKit() || isIOS()) return;
|
|
781
|
+
const scrollable = resolve();
|
|
782
|
+
if (scrollable === null) return;
|
|
783
|
+
|
|
784
|
+
const box = scrollable.getBoundingClientRect();
|
|
785
|
+
const left = AUTOSCROLL_AREA_SIZE;
|
|
786
|
+
const top = AUTOSCROLL_AREA_SIZE;
|
|
787
|
+
const bottom = box.height - AUTOSCROLL_AREA_SIZE;
|
|
788
|
+
const right = box.width - AUTOSCROLL_AREA_SIZE;
|
|
789
|
+
if (x < left || x > right || y < top || y > bottom) {
|
|
790
|
+
if (x < left) state.dx = x - left;
|
|
791
|
+
else if (x > right) state.dx = x - right;
|
|
792
|
+
if (y < top) state.dy = y - top;
|
|
793
|
+
else if (y > bottom) state.dy = y - bottom;
|
|
794
|
+
if (state.timer === undefined) state.timer = requestAnimationFrame(scroll);
|
|
795
|
+
} else {
|
|
796
|
+
stop();
|
|
797
|
+
}
|
|
798
|
+
},
|
|
799
|
+
stop,
|
|
800
|
+
};
|
|
801
|
+
}
|