@wordpress/compose 7.32.0 → 7.32.1-next.ff1cebbba.0
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/build/higher-order/compose.js +25 -20
- package/build/higher-order/compose.js.map +7 -1
- package/build/higher-order/if-condition/index.js +35 -41
- package/build/higher-order/if-condition/index.js.map +7 -1
- package/build/higher-order/pipe.js +32 -66
- package/build/higher-order/pipe.js.map +7 -1
- package/build/higher-order/pure/index.js +44 -37
- package/build/higher-order/pure/index.js.map +7 -1
- package/build/higher-order/with-global-events/index.js +64 -73
- package/build/higher-order/with-global-events/index.js.map +7 -1
- package/build/higher-order/with-global-events/listener.js +34 -21
- package/build/higher-order/with-global-events/listener.js.map +7 -1
- package/build/higher-order/with-instance-id/index.js +45 -27
- package/build/higher-order/with-instance-id/index.js.map +7 -1
- package/build/higher-order/with-safe-timeout/index.js +70 -66
- package/build/higher-order/with-safe-timeout/index.js.map +7 -1
- package/build/higher-order/with-state/index.js +52 -40
- package/build/higher-order/with-state/index.js.map +7 -1
- package/build/hooks/use-async-list/index.js +38 -42
- package/build/hooks/use-async-list/index.js.map +7 -1
- package/build/hooks/use-constrained-tabbing/index.js +54 -75
- package/build/hooks/use-constrained-tabbing/index.js.map +7 -1
- package/build/hooks/use-copy-on-click/index.js +46 -56
- package/build/hooks/use-copy-on-click/index.js.map +7 -1
- package/build/hooks/use-copy-to-clipboard/index.js +41 -51
- package/build/hooks/use-copy-to-clipboard/index.js.map +7 -1
- package/build/hooks/use-debounce/index.js +30 -37
- package/build/hooks/use-debounce/index.js.map +7 -1
- package/build/hooks/use-debounced-input/index.js +39 -27
- package/build/hooks/use-debounced-input/index.js.map +7 -1
- package/build/hooks/use-dialog/index.js +63 -55
- package/build/hooks/use-dialog/index.js.map +7 -1
- package/build/hooks/use-disabled/index.js +74 -82
- package/build/hooks/use-disabled/index.js.map +7 -1
- package/build/hooks/use-dragging/index.js +51 -48
- package/build/hooks/use-dragging/index.js.map +7 -1
- package/build/hooks/use-drop-zone/index.js +133 -168
- package/build/hooks/use-drop-zone/index.js.map +7 -1
- package/build/hooks/use-event/index.js +33 -45
- package/build/hooks/use-event/index.js.map +7 -1
- package/build/hooks/use-fixed-window-list/index.js +109 -97
- package/build/hooks/use-fixed-window-list/index.js.map +7 -1
- package/build/hooks/use-focus-on-mount/index.js +44 -57
- package/build/hooks/use-focus-on-mount/index.js.map +7 -1
- package/build/hooks/use-focus-outside/index.js +44 -102
- package/build/hooks/use-focus-outside/index.js.map +7 -1
- package/build/hooks/use-focus-return/index.js +35 -56
- package/build/hooks/use-focus-return/index.js.map +7 -1
- package/build/hooks/use-focusable-iframe/index.js +38 -35
- package/build/hooks/use-focusable-iframe/index.js.map +7 -1
- package/build/hooks/use-instance-id/index.js +26 -42
- package/build/hooks/use-instance-id/index.js.map +7 -1
- package/build/hooks/use-isomorphic-layout-effect/index.js +25 -17
- package/build/hooks/use-isomorphic-layout-effect/index.js.map +7 -1
- package/build/hooks/use-keyboard-shortcut/index.js +72 -69
- package/build/hooks/use-keyboard-shortcut/index.js.map +7 -1
- package/build/hooks/use-media-query/index.js +39 -35
- package/build/hooks/use-media-query/index.js.map +7 -1
- package/build/hooks/use-merge-refs/index.js +34 -102
- package/build/hooks/use-merge-refs/index.js.map +7 -1
- package/build/hooks/use-observable-value/index.js +31 -28
- package/build/hooks/use-observable-value/index.js.map +7 -1
- package/build/hooks/use-preferred-color-scheme/index.android.js +31 -23
- package/build/hooks/use-preferred-color-scheme/index.android.js.map +7 -1
- package/build/hooks/use-preferred-color-scheme/index.ios.js +25 -17
- package/build/hooks/use-preferred-color-scheme/index.ios.js.map +7 -1
- package/build/hooks/use-previous/index.js +26 -25
- package/build/hooks/use-previous/index.js.map +7 -1
- package/build/hooks/use-reduced-motion/index.js +35 -18
- package/build/hooks/use-reduced-motion/index.js.map +7 -1
- package/build/hooks/use-ref-effect/index.js +25 -35
- package/build/hooks/use-ref-effect/index.js.map +7 -1
- package/build/hooks/use-resize-observer/index.js +35 -67
- package/build/hooks/use-resize-observer/index.js.map +7 -1
- package/build/hooks/use-resize-observer/legacy/index.js +47 -103
- package/build/hooks/use-resize-observer/legacy/index.js.map +7 -1
- package/build/hooks/use-resize-observer/use-resize-observer.js +44 -36
- package/build/hooks/use-resize-observer/use-resize-observer.js.map +7 -1
- package/build/hooks/use-state-with-history/index.js +68 -63
- package/build/hooks/use-state-with-history/index.js.map +7 -1
- package/build/hooks/use-throttle/index.js +30 -37
- package/build/hooks/use-throttle/index.js.map +7 -1
- package/build/hooks/use-viewport-match/index.js +51 -70
- package/build/hooks/use-viewport-match/index.js.map +7 -1
- package/build/hooks/use-warn-on-change/index.js +50 -40
- package/build/hooks/use-warn-on-change/index.js.map +7 -1
- package/build/index.js +156 -358
- package/build/index.js.map +7 -1
- package/build/utils/create-higher-order-component/index.js +30 -34
- package/build/utils/create-higher-order-component/index.js.map +7 -1
- package/build/utils/debounce/index.js +38 -99
- package/build/utils/debounce/index.js.map +7 -1
- package/build/utils/observable-map/index.js +29 -19
- package/build/utils/observable-map/index.js.map +7 -1
- package/build/utils/throttle/index.js +30 -81
- package/build/utils/throttle/index.js.map +7 -1
- package/build-module/higher-order/compose.js +6 -15
- package/build-module/higher-order/compose.js.map +7 -1
- package/build-module/higher-order/if-condition/index.js +16 -37
- package/build-module/higher-order/if-condition/index.js.map +7 -1
- package/build-module/higher-order/pipe.js +10 -62
- package/build-module/higher-order/pipe.js.map +7 -1
- package/build-module/higher-order/pure/index.js +12 -29
- package/build-module/higher-order/pure/index.js.map +7 -1
- package/build-module/higher-order/with-global-events/index.js +34 -67
- package/build-module/higher-order/with-global-events/index.js.map +7 -1
- package/build-module/higher-order/with-global-events/listener.js +16 -17
- package/build-module/higher-order/with-global-events/listener.js.map +7 -1
- package/build-module/higher-order/with-instance-id/index.js +17 -22
- package/build-module/higher-order/with-instance-id/index.js.map +7 -1
- package/build-module/higher-order/with-safe-timeout/index.js +52 -62
- package/build-module/higher-order/with-safe-timeout/index.js.map +7 -1
- package/build-module/higher-order/with-state/index.js +23 -35
- package/build-module/higher-order/with-state/index.js.map +7 -1
- package/build-module/hooks/use-async-list/index.js +16 -33
- package/build-module/hooks/use-async-list/index.js.map +7 -1
- package/build-module/hooks/use-constrained-tabbing/index.js +26 -70
- package/build-module/hooks/use-constrained-tabbing/index.js.map +7 -1
- package/build-module/hooks/use-copy-on-click/index.js +14 -47
- package/build-module/hooks/use-copy-on-click/index.js.map +7 -1
- package/build-module/hooks/use-copy-to-clipboard/index.js +11 -44
- package/build-module/hooks/use-copy-to-clipboard/index.js.map +7 -1
- package/build-module/hooks/use-debounce/index.js +12 -33
- package/build-module/hooks/use-debounce/index.js.map +7 -1
- package/build-module/hooks/use-debounced-input/index.js +7 -18
- package/build-module/hooks/use-debounced-input/index.js.map +7 -1
- package/build-module/hooks/use-dialog/index.js +29 -43
- package/build-module/hooks/use-dialog/index.js.map +7 -1
- package/build-module/hooks/use-disabled/index.js +47 -78
- package/build-module/hooks/use-disabled/index.js.map +7 -1
- package/build-module/hooks/use-dragging/index.js +19 -39
- package/build-module/hooks/use-dragging/index.js.map +7 -1
- package/build-module/hooks/use-drop-zone/index.js +100 -158
- package/build-module/hooks/use-drop-zone/index.js.map +7 -1
- package/build-module/hooks/use-event/index.js +13 -39
- package/build-module/hooks/use-event/index.js.map +7 -1
- package/build-module/hooks/use-fixed-window-list/index.js +86 -87
- package/build-module/hooks/use-fixed-window-list/index.js.map +7 -1
- package/build-module/hooks/use-focus-on-mount/index.js +12 -48
- package/build-module/hooks/use-focus-on-mount/index.js.map +7 -1
- package/build-module/hooks/use-focus-outside/index.js +21 -93
- package/build-module/hooks/use-focus-outside/index.js.map +7 -1
- package/build-module/hooks/use-focus-return/index.js +13 -48
- package/build-module/hooks/use-focus-return/index.js.map +7 -1
- package/build-module/hooks/use-focusable-iframe/index.js +11 -31
- package/build-module/hooks/use-focusable-iframe/index.js.map +7 -1
- package/build-module/hooks/use-instance-id/index.js +7 -36
- package/build-module/hooks/use-instance-id/index.js.map +7 -1
- package/build-module/hooks/use-isomorphic-layout-effect/index.js +7 -13
- package/build-module/hooks/use-isomorphic-layout-effect/index.js.map +7 -1
- package/build-module/hooks/use-keyboard-shortcut/index.js +40 -60
- package/build-module/hooks/use-keyboard-shortcut/index.js.map +7 -1
- package/build-module/hooks/use-media-query/index.js +21 -30
- package/build-module/hooks/use-media-query/index.js.map +7 -1
- package/build-module/hooks/use-merge-refs/index.js +10 -92
- package/build-module/hooks/use-merge-refs/index.js.map +7 -1
- package/build-module/hooks/use-observable-value/index.js +13 -24
- package/build-module/hooks/use-observable-value/index.js.map +7 -1
- package/build-module/hooks/use-preferred-color-scheme/index.android.js +15 -18
- package/build-module/hooks/use-preferred-color-scheme/index.android.js.map +7 -1
- package/build-module/hooks/use-preferred-color-scheme/index.ios.js +6 -12
- package/build-module/hooks/use-preferred-color-scheme/index.ios.js.map +7 -1
- package/build-module/hooks/use-previous/index.js +7 -20
- package/build-module/hooks/use-previous/index.js.map +7 -1
- package/build-module/hooks/use-reduced-motion/index.js +7 -13
- package/build-module/hooks/use-reduced-motion/index.js.map +7 -1
- package/build-module/hooks/use-ref-effect/index.js +7 -31
- package/build-module/hooks/use-ref-effect/index.js.map +7 -1
- package/build-module/hooks/use-resize-observer/index.js +7 -61
- package/build-module/hooks/use-resize-observer/index.js.map +7 -1
- package/build-module/hooks/use-resize-observer/legacy/index.js +28 -96
- package/build-module/hooks/use-resize-observer/legacy/index.js.map +7 -1
- package/build-module/hooks/use-resize-observer/use-resize-observer.js +10 -28
- package/build-module/hooks/use-resize-observer/use-resize-observer.js.map +7 -1
- package/build-module/hooks/use-state-with-history/index.js +47 -55
- package/build-module/hooks/use-state-with-history/index.js.map +7 -1
- package/build-module/hooks/use-throttle/index.js +12 -33
- package/build-module/hooks/use-throttle/index.js.map +7 -1
- package/build-module/hooks/use-viewport-match/index.js +22 -64
- package/build-module/hooks/use-viewport-match/index.js.map +7 -1
- package/build-module/hooks/use-warn-on-change/index.js +21 -34
- package/build-module/hooks/use-warn-on-change/index.js.map +7 -1
- package/build-module/index.js +83 -53
- package/build-module/index.js.map +7 -1
- package/build-module/utils/create-higher-order-component/index.js +9 -30
- package/build-module/utils/create-higher-order-component/index.js.map +7 -1
- package/build-module/utils/debounce/index.js +17 -95
- package/build-module/utils/debounce/index.js.map +7 -1
- package/build-module/utils/observable-map/index.js +8 -16
- package/build-module/utils/observable-map/index.js.map +7 -1
- package/build-module/utils/throttle/index.js +8 -75
- package/build-module/utils/throttle/index.js.map +7 -1
- package/package.json +17 -10
- package/src/hooks/use-debounce/index.js +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/higher-order/with-network-connectivity/index.native.js +0 -27
- package/build/higher-order/with-network-connectivity/index.native.js.map +0 -1
- package/build/higher-order/with-preferred-color-scheme/index.native.js +0 -37
- package/build/higher-order/with-preferred-color-scheme/index.native.js.map +0 -1
- package/build/hooks/use-constrained-tabbing/index.native.js +0 -20
- package/build/hooks/use-constrained-tabbing/index.native.js.map +0 -1
- package/build/hooks/use-focus-outside/index.native.js +0 -175
- package/build/hooks/use-focus-outside/index.native.js.map +0 -1
- package/build/hooks/use-keyboard-shortcut/index.native.js +0 -9
- package/build/hooks/use-keyboard-shortcut/index.native.js.map +0 -1
- package/build/hooks/use-network-connectivity/index.native.js +0 -58
- package/build/hooks/use-network-connectivity/index.native.js.map +0 -1
- package/build/hooks/use-preferred-color-scheme-style/index.native.js +0 -42
- package/build/hooks/use-preferred-color-scheme-style/index.native.js.map +0 -1
- package/build/hooks/use-resize-observer/index.native.js +0 -14
- package/build/hooks/use-resize-observer/index.native.js.map +0 -1
- package/build/hooks/use-resize-observer/legacy/index.native.js +0 -62
- package/build/hooks/use-resize-observer/legacy/index.native.js.map +0 -1
- package/build/index.native.js +0 -297
- package/build/index.native.js.map +0 -1
- package/build-module/higher-order/with-network-connectivity/index.native.js +0 -19
- package/build-module/higher-order/with-network-connectivity/index.native.js.map +0 -1
- package/build-module/higher-order/with-preferred-color-scheme/index.native.js +0 -29
- package/build-module/higher-order/with-preferred-color-scheme/index.native.js.map +0 -1
- package/build-module/hooks/use-constrained-tabbing/index.native.js +0 -13
- package/build-module/hooks/use-constrained-tabbing/index.native.js.map +0 -1
- package/build-module/hooks/use-focus-outside/index.native.js +0 -169
- package/build-module/hooks/use-focus-outside/index.native.js.map +0 -1
- package/build-module/hooks/use-keyboard-shortcut/index.native.js +0 -3
- package/build-module/hooks/use-keyboard-shortcut/index.native.js.map +0 -1
- package/build-module/hooks/use-network-connectivity/index.native.js +0 -52
- package/build-module/hooks/use-network-connectivity/index.native.js.map +0 -1
- package/build-module/hooks/use-preferred-color-scheme-style/index.native.js +0 -35
- package/build-module/hooks/use-preferred-color-scheme-style/index.native.js.map +0 -1
- package/build-module/hooks/use-resize-observer/index.native.js +0 -2
- package/build-module/hooks/use-resize-observer/index.native.js.map +0 -1
- package/build-module/hooks/use-resize-observer/legacy/index.native.js +0 -56
- package/build-module/hooks/use-resize-observer/legacy/index.native.js.map +0 -1
- package/build-module/index.native.js +0 -45
- package/build-module/index.native.js.map +0 -1
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import useRefEffect from '../use-ref-effect';
|
|
5
|
-
import useEvent from '../use-event';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* A hook to facilitate drag and drop handling.
|
|
9
|
-
*
|
|
10
|
-
* @param {Object} props Named parameters.
|
|
11
|
-
* @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.
|
|
12
|
-
* @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.
|
|
13
|
-
* @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.
|
|
14
|
-
* @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.
|
|
15
|
-
* @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.
|
|
16
|
-
* @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.
|
|
17
|
-
* @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.
|
|
18
|
-
* @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.
|
|
19
|
-
*
|
|
20
|
-
* @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.
|
|
21
|
-
*/
|
|
22
|
-
export default function useDropZone({
|
|
1
|
+
import useRefEffect from "../use-ref-effect";
|
|
2
|
+
import useEvent from "../use-event";
|
|
3
|
+
function useDropZone({
|
|
23
4
|
dropZoneElement,
|
|
24
5
|
isDisabled,
|
|
25
6
|
onDrop: _onDrop,
|
|
@@ -35,150 +16,111 @@ export default function useDropZone({
|
|
|
35
16
|
const onDragLeaveEvent = useEvent(_onDragLeave);
|
|
36
17
|
const onDragEndEvent = useEvent(_onDragEnd);
|
|
37
18
|
const onDragOverEvent = useEvent(_onDragOver);
|
|
38
|
-
return useRefEffect(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// If a custom dropZoneRef is passed, use that instead of the element.
|
|
44
|
-
// This allows the dropzone to cover an expanded area, rather than
|
|
45
|
-
// be restricted to the area of the ref returned by this hook.
|
|
46
|
-
const element = dropZoneElement !== null && dropZoneElement !== void 0 ? dropZoneElement : elem;
|
|
47
|
-
let isDragging = false;
|
|
48
|
-
const {
|
|
49
|
-
ownerDocument
|
|
50
|
-
} = element;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Checks if an element is in the drop zone.
|
|
54
|
-
*
|
|
55
|
-
* @param {EventTarget|null} targetToCheck
|
|
56
|
-
*
|
|
57
|
-
* @return {boolean} True if in drop zone, false if not.
|
|
58
|
-
*/
|
|
59
|
-
function isElementInZone(targetToCheck) {
|
|
60
|
-
const {
|
|
61
|
-
defaultView
|
|
62
|
-
} = ownerDocument;
|
|
63
|
-
if (!targetToCheck || !defaultView || !(targetToCheck instanceof defaultView.HTMLElement) || !element.contains(targetToCheck)) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** @type {HTMLElement|null} */
|
|
68
|
-
let elementToCheck = targetToCheck;
|
|
69
|
-
do {
|
|
70
|
-
if (elementToCheck.dataset.isDropZone) {
|
|
71
|
-
return elementToCheck === element;
|
|
72
|
-
}
|
|
73
|
-
} while (elementToCheck = elementToCheck.parentElement);
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
function maybeDragStart(/** @type {DragEvent} */event) {
|
|
77
|
-
if (isDragging) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
isDragging = true;
|
|
81
|
-
|
|
82
|
-
// Note that `dragend` doesn't fire consistently for file and
|
|
83
|
-
// HTML drag events where the drag origin is outside the browser
|
|
84
|
-
// window. In Firefox it may also not fire if the originating
|
|
85
|
-
// node is removed.
|
|
86
|
-
ownerDocument.addEventListener('dragend', maybeDragEnd);
|
|
87
|
-
ownerDocument.addEventListener('mousemove', maybeDragEnd);
|
|
88
|
-
if (_onDragStart) {
|
|
89
|
-
onDragStartEvent(event);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
function onDragEnter(/** @type {DragEvent} */event) {
|
|
93
|
-
event.preventDefault();
|
|
94
|
-
|
|
95
|
-
// The `dragenter` event will also fire when entering child
|
|
96
|
-
// elements, but we only want to call `onDragEnter` when
|
|
97
|
-
// entering the drop zone, which means the `relatedTarget`
|
|
98
|
-
// (element that has been left) should be outside the drop zone.
|
|
99
|
-
if (element.contains(/** @type {Node} */event.relatedTarget)) {
|
|
19
|
+
return useRefEffect(
|
|
20
|
+
(elem) => {
|
|
21
|
+
if (isDisabled) {
|
|
100
22
|
return;
|
|
101
23
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
24
|
+
const element = dropZoneElement ?? elem;
|
|
25
|
+
let isDragging = false;
|
|
26
|
+
const { ownerDocument } = element;
|
|
27
|
+
function isElementInZone(targetToCheck) {
|
|
28
|
+
const { defaultView } = ownerDocument;
|
|
29
|
+
if (!targetToCheck || !defaultView || !(targetToCheck instanceof defaultView.HTMLElement) || !element.contains(targetToCheck)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
let elementToCheck = targetToCheck;
|
|
33
|
+
do {
|
|
34
|
+
if (elementToCheck.dataset.isDropZone) {
|
|
35
|
+
return elementToCheck === element;
|
|
36
|
+
}
|
|
37
|
+
} while (elementToCheck = elementToCheck.parentElement);
|
|
38
|
+
return false;
|
|
110
39
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// zone.
|
|
122
|
-
// Note: This is not entirely reliable in Safari due to this bug
|
|
123
|
-
// https://bugs.webkit.org/show_bug.cgi?id=66547
|
|
124
|
-
|
|
125
|
-
if (isElementInZone(event.relatedTarget)) {
|
|
126
|
-
return;
|
|
40
|
+
function maybeDragStart(event) {
|
|
41
|
+
if (isDragging) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
isDragging = true;
|
|
45
|
+
ownerDocument.addEventListener("dragend", maybeDragEnd);
|
|
46
|
+
ownerDocument.addEventListener("mousemove", maybeDragEnd);
|
|
47
|
+
if (_onDragStart) {
|
|
48
|
+
onDragStartEvent(event);
|
|
49
|
+
}
|
|
127
50
|
}
|
|
128
|
-
|
|
129
|
-
|
|
51
|
+
function onDragEnter(event) {
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
if (element.contains(
|
|
54
|
+
/** @type {Node} */
|
|
55
|
+
event.relatedTarget
|
|
56
|
+
)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (_onDragEnter) {
|
|
60
|
+
onDragEnterEvent(event);
|
|
61
|
+
}
|
|
130
62
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
63
|
+
function onDragOver(event) {
|
|
64
|
+
if (!event.defaultPrevented && _onDragOver) {
|
|
65
|
+
onDragOverEvent(event);
|
|
66
|
+
}
|
|
67
|
+
event.preventDefault();
|
|
136
68
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
// not recognized.
|
|
145
|
-
// eslint-disable-next-line no-unused-expressions
|
|
146
|
-
event.dataTransfer && event.dataTransfer.files.length;
|
|
147
|
-
if (_onDrop) {
|
|
148
|
-
onDropEvent(event);
|
|
69
|
+
function onDragLeave(event) {
|
|
70
|
+
if (isElementInZone(event.relatedTarget)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (_onDragLeave) {
|
|
74
|
+
onDragLeaveEvent(event);
|
|
75
|
+
}
|
|
149
76
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
77
|
+
function onDrop(event) {
|
|
78
|
+
if (event.defaultPrevented) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
event.dataTransfer && event.dataTransfer.files.length;
|
|
83
|
+
if (_onDrop) {
|
|
84
|
+
onDropEvent(event);
|
|
85
|
+
}
|
|
86
|
+
maybeDragEnd(event);
|
|
155
87
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
88
|
+
function maybeDragEnd(event) {
|
|
89
|
+
if (!isDragging) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
isDragging = false;
|
|
93
|
+
ownerDocument.removeEventListener("dragend", maybeDragEnd);
|
|
94
|
+
ownerDocument.removeEventListener("mousemove", maybeDragEnd);
|
|
95
|
+
if (_onDragEnd) {
|
|
96
|
+
onDragEndEvent(event);
|
|
97
|
+
}
|
|
161
98
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
99
|
+
element.setAttribute("data-is-drop-zone", "true");
|
|
100
|
+
element.addEventListener("drop", onDrop);
|
|
101
|
+
element.addEventListener("dragenter", onDragEnter);
|
|
102
|
+
element.addEventListener("dragover", onDragOver);
|
|
103
|
+
element.addEventListener("dragleave", onDragLeave);
|
|
104
|
+
ownerDocument.addEventListener("dragenter", maybeDragStart);
|
|
105
|
+
return () => {
|
|
106
|
+
element.removeAttribute("data-is-drop-zone");
|
|
107
|
+
element.removeEventListener("drop", onDrop);
|
|
108
|
+
element.removeEventListener("dragenter", onDragEnter);
|
|
109
|
+
element.removeEventListener("dragover", onDragOver);
|
|
110
|
+
element.removeEventListener("dragleave", onDragLeave);
|
|
111
|
+
ownerDocument.removeEventListener("dragend", maybeDragEnd);
|
|
112
|
+
ownerDocument.removeEventListener("mousemove", maybeDragEnd);
|
|
113
|
+
ownerDocument.removeEventListener(
|
|
114
|
+
"dragenter",
|
|
115
|
+
maybeDragStart
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
[isDisabled, dropZoneElement]
|
|
120
|
+
// Refresh when the passed in dropZoneElement changes.
|
|
182
121
|
);
|
|
183
122
|
}
|
|
184
|
-
|
|
123
|
+
export {
|
|
124
|
+
useDropZone as default
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["useRefEffect","useEvent","useDropZone","dropZoneElement","isDisabled","onDrop","_onDrop","onDragStart","_onDragStart","onDragEnter","_onDragEnter","onDragLeave","_onDragLeave","onDragEnd","_onDragEnd","onDragOver","_onDragOver","onDropEvent","onDragStartEvent","onDragEnterEvent","onDragLeaveEvent","onDragEndEvent","onDragOverEvent","elem","element","isDragging","ownerDocument","isElementInZone","targetToCheck","defaultView","HTMLElement","contains","elementToCheck","dataset","isDropZone","parentElement","maybeDragStart","event","addEventListener","maybeDragEnd","preventDefault","relatedTarget","defaultPrevented","dataTransfer","files","length","removeEventListener","setAttribute","removeAttribute"],"sources":["@wordpress/compose/src/hooks/use-drop-zone/index.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\nimport useEvent from '../use-event';\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.\n * @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.\n * @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.\n * @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.\n * @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.\n * @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.\n * @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tdropZoneElement,\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropEvent = useEvent( _onDrop );\n\tconst onDragStartEvent = useEvent( _onDragStart );\n\tconst onDragEnterEvent = useEvent( _onDragEnter );\n\tconst onDragLeaveEvent = useEvent( _onDragLeave );\n\tconst onDragEndEvent = useEvent( _onDragEnd );\n\tconst onDragOverEvent = useEvent( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( elem ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// If a custom dropZoneRef is passed, use that instead of the element.\n\t\t\t// This allows the dropzone to cover an expanded area, rather than\n\t\t\t// be restricted to the area of the ref returned by this hook.\n\t\t\tconst element = dropZoneElement ?? elem;\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {EventTarget|null} targetToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( targetToCheck ) {\n\t\t\t\tconst { defaultView } = ownerDocument;\n\n\t\t\t\tif (\n\t\t\t\t\t! targetToCheck ||\n\t\t\t\t\t! defaultView ||\n\t\t\t\t\t! ( targetToCheck instanceof defaultView.HTMLElement ) ||\n\t\t\t\t\t! element.contains( targetToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t/** @type {HTMLElement|null} */\n\t\t\t\tlet elementToCheck = targetToCheck;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( _onDragStart ) {\n\t\t\t\t\tonDragStartEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( _onDragEnter ) {\n\t\t\t\t\tonDragEnterEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && _onDragOver ) {\n\t\t\t\t\tonDragOverEvent( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\t// Note: This is not entirely reliable in Safari due to this bug\n\t\t\t\t// https://bugs.webkit.org/show_bug.cgi?id=66547\n\n\t\t\t\tif ( isElementInZone( event.relatedTarget ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( _onDragLeave ) {\n\t\t\t\t\tonDragLeaveEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( _onDrop ) {\n\t\t\t\t\tonDropEvent( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( _onDragEnd ) {\n\t\t\t\t\tonDragEndEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.setAttribute( 'data-is-drop-zone', 'true' );\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\telement.removeAttribute( 'data-is-drop-zone' );\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\t\t\t};\n\t\t},\n\t\t[ isDisabled, dropZoneElement ] // Refresh when the passed in dropZoneElement changes.\n\t);\n}\n"],"mappings":"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","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/hooks/use-drop-zone/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\nimport useEvent from '../use-event';\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.\n * @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.\n * @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.\n * @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.\n * @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.\n * @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.\n * @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tdropZoneElement,\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropEvent = useEvent( _onDrop );\n\tconst onDragStartEvent = useEvent( _onDragStart );\n\tconst onDragEnterEvent = useEvent( _onDragEnter );\n\tconst onDragLeaveEvent = useEvent( _onDragLeave );\n\tconst onDragEndEvent = useEvent( _onDragEnd );\n\tconst onDragOverEvent = useEvent( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( elem ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// If a custom dropZoneRef is passed, use that instead of the element.\n\t\t\t// This allows the dropzone to cover an expanded area, rather than\n\t\t\t// be restricted to the area of the ref returned by this hook.\n\t\t\tconst element = dropZoneElement ?? elem;\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {EventTarget|null} targetToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( targetToCheck ) {\n\t\t\t\tconst { defaultView } = ownerDocument;\n\n\t\t\t\tif (\n\t\t\t\t\t! targetToCheck ||\n\t\t\t\t\t! defaultView ||\n\t\t\t\t\t! ( targetToCheck instanceof defaultView.HTMLElement ) ||\n\t\t\t\t\t! element.contains( targetToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t/** @type {HTMLElement|null} */\n\t\t\t\tlet elementToCheck = targetToCheck;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( _onDragStart ) {\n\t\t\t\t\tonDragStartEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( _onDragEnter ) {\n\t\t\t\t\tonDragEnterEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && _onDragOver ) {\n\t\t\t\t\tonDragOverEvent( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\t// Note: This is not entirely reliable in Safari due to this bug\n\t\t\t\t// https://bugs.webkit.org/show_bug.cgi?id=66547\n\n\t\t\t\tif ( isElementInZone( event.relatedTarget ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( _onDragLeave ) {\n\t\t\t\t\tonDragLeaveEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( _onDrop ) {\n\t\t\t\t\tonDropEvent( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( _onDragEnd ) {\n\t\t\t\t\tonDragEndEvent( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.setAttribute( 'data-is-drop-zone', 'true' );\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\telement.removeAttribute( 'data-is-drop-zone' );\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\t\t\t};\n\t\t},\n\t\t[ isDisabled, dropZoneElement ] // Refresh when the passed in dropZoneElement changes.\n\t);\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,45 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
import { useRef, useInsertionEffect, useCallback } from '@wordpress/element';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Any function.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Creates a stable callback function that has access to the latest state and
|
|
12
|
-
* can be used within event handlers and effect callbacks. Throws when used in
|
|
13
|
-
* the render phase.
|
|
14
|
-
*
|
|
15
|
-
* @param callback The callback function to wrap.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
*
|
|
19
|
-
* ```tsx
|
|
20
|
-
* function Component( props ) {
|
|
21
|
-
* const onClick = useEvent( props.onClick );
|
|
22
|
-
* useEffect( () => {
|
|
23
|
-
* onClick();
|
|
24
|
-
* // Won't trigger the effect again when props.onClick is updated.
|
|
25
|
-
* }, [ onClick ] );
|
|
26
|
-
* // Won't re-render Button when props.onClick is updated (if `Button` is
|
|
27
|
-
* // wrapped in `React.memo`).
|
|
28
|
-
* return <Button onClick={ onClick } />;
|
|
29
|
-
* }
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export default function useEvent(
|
|
33
|
-
/**
|
|
34
|
-
* The callback function to wrap.
|
|
35
|
-
*/
|
|
36
|
-
callback) {
|
|
1
|
+
import { useRef, useInsertionEffect, useCallback } from "@wordpress/element";
|
|
2
|
+
function useEvent(callback) {
|
|
37
3
|
const ref = useRef(() => {
|
|
38
|
-
throw new Error(
|
|
4
|
+
throw new Error(
|
|
5
|
+
"Callbacks created with `useEvent` cannot be called during rendering."
|
|
6
|
+
);
|
|
39
7
|
});
|
|
40
8
|
useInsertionEffect(() => {
|
|
41
9
|
ref.current = callback;
|
|
42
10
|
});
|
|
43
|
-
return useCallback(
|
|
11
|
+
return useCallback(
|
|
12
|
+
(...args) => ref.current?.(...args),
|
|
13
|
+
[]
|
|
14
|
+
);
|
|
44
15
|
}
|
|
45
|
-
|
|
16
|
+
export {
|
|
17
|
+
useEvent as default
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/hooks/use-event/index.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRef, useInsertionEffect, useCallback } from '@wordpress/element';\n\n/**\n * Any function.\n */\nexport type AnyFunction = ( ...args: any ) => any;\n\n/**\n * Creates a stable callback function that has access to the latest state and\n * can be used within event handlers and effect callbacks. Throws when used in\n * the render phase.\n *\n * @param callback The callback function to wrap.\n *\n * @example\n *\n * ```tsx\n * function Component( props ) {\n * const onClick = useEvent( props.onClick );\n * useEffect( () => {\n * onClick();\n * // Won't trigger the effect again when props.onClick is updated.\n * }, [ onClick ] );\n * // Won't re-render Button when props.onClick is updated (if `Button` is\n * // wrapped in `React.memo`).\n * return <Button onClick={ onClick } />;\n * }\n * ```\n */\nexport default function useEvent< T extends AnyFunction >(\n\t/**\n\t * The callback function to wrap.\n\t */\n\tcallback?: T\n) {\n\tconst ref = useRef< AnyFunction | undefined >( () => {\n\t\tthrow new Error(\n\t\t\t'Callbacks created with `useEvent` cannot be called during rendering.'\n\t\t);\n\t} );\n\tuseInsertionEffect( () => {\n\t\tref.current = callback;\n\t} );\n\treturn useCallback< AnyFunction >(\n\t\t( ...args ) => ref.current?.( ...args ),\n\t\t[]\n\t) as T;\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,QAAQ,oBAAoB,mBAAmB;AA6BzC,SAAR,SAIN,UACC;AACD,QAAM,MAAM,OAAmC,MAAM;AACpD,UAAM,IAAI;AAAA,MACT;AAAA,IACD;AAAA,EACD,CAAE;AACF,qBAAoB,MAAM;AACzB,QAAI,UAAU;AAAA,EACf,CAAE;AACF,SAAO;AAAA,IACN,IAAK,SAAU,IAAI,UAAW,GAAG,IAAK;AAAA,IACtC,CAAC;AAAA,EACF;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,51 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import { getScrollContainer } from '@wordpress/dom';
|
|
6
|
-
import { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Internal dependencies
|
|
10
|
-
*/
|
|
11
|
-
import { debounce } from '../../utils/debounce';
|
|
1
|
+
import { useState, useLayoutEffect } from "@wordpress/element";
|
|
2
|
+
import { getScrollContainer } from "@wordpress/dom";
|
|
3
|
+
import { PAGEUP, PAGEDOWN, HOME, END } from "@wordpress/keycodes";
|
|
4
|
+
import { debounce } from "../../utils/debounce";
|
|
12
5
|
const DEFAULT_INIT_WINDOW_SIZE = 30;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
* @property {number} visibleItems Items visible in the current viewport
|
|
18
|
-
* @property {number} start Start index of the window
|
|
19
|
-
* @property {number} end End index of the window
|
|
20
|
-
* @property {(index:number)=>boolean} itemInView Returns true if item is in the window
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @typedef {Object} WPFixedWindowListOptions
|
|
25
|
-
*
|
|
26
|
-
* @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.
|
|
27
|
-
* @property {boolean} [useWindowing] When false avoids calculating the window size
|
|
28
|
-
* @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.
|
|
29
|
-
* @property {any} [expandedState] Used to recalculate the window size when the expanded state of a list changes.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
*
|
|
34
|
-
* @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.
|
|
35
|
-
* @param { number } itemHeight Fixed item height in pixels
|
|
36
|
-
* @param { number } totalItems Total items in list
|
|
37
|
-
* @param { WPFixedWindowListOptions } [options] Options object
|
|
38
|
-
* @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter
|
|
39
|
-
*/
|
|
40
|
-
export default function useFixedWindowList(elementRef, itemHeight, totalItems, options) {
|
|
41
|
-
var _options$initWindowSi, _options$useWindowing;
|
|
42
|
-
const initWindowSize = (_options$initWindowSi = options?.initWindowSize) !== null && _options$initWindowSi !== void 0 ? _options$initWindowSi : DEFAULT_INIT_WINDOW_SIZE;
|
|
43
|
-
const useWindowing = (_options$useWindowing = options?.useWindowing) !== null && _options$useWindowing !== void 0 ? _options$useWindowing : true;
|
|
6
|
+
function useFixedWindowList(elementRef, itemHeight, totalItems, options) {
|
|
7
|
+
const initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE;
|
|
8
|
+
const useWindowing = options?.useWindowing ?? true;
|
|
44
9
|
const [fixedListWindow, setFixedListWindow] = useState({
|
|
45
10
|
visibleItems: initWindowSize,
|
|
46
11
|
start: 0,
|
|
47
12
|
end: initWindowSize,
|
|
48
|
-
itemInView: (
|
|
13
|
+
itemInView: (index) => {
|
|
49
14
|
return index >= 0 && index <= initWindowSize;
|
|
50
15
|
}
|
|
51
16
|
});
|
|
@@ -54,23 +19,28 @@ export default function useFixedWindowList(elementRef, itemHeight, totalItems, o
|
|
|
54
19
|
return;
|
|
55
20
|
}
|
|
56
21
|
const scrollContainer = getScrollContainer(elementRef.current);
|
|
57
|
-
const measureWindow = (
|
|
58
|
-
var _options$windowOversc;
|
|
22
|
+
const measureWindow = (initRender) => {
|
|
59
23
|
if (!scrollContainer) {
|
|
60
24
|
return;
|
|
61
25
|
}
|
|
62
|
-
const visibleItems = Math.ceil(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
26
|
+
const visibleItems = Math.ceil(
|
|
27
|
+
scrollContainer.clientHeight / itemHeight
|
|
28
|
+
);
|
|
29
|
+
const windowOverscan = initRender ? visibleItems : options?.windowOverscan ?? visibleItems;
|
|
30
|
+
const firstViewableIndex = Math.floor(
|
|
31
|
+
scrollContainer.scrollTop / itemHeight
|
|
32
|
+
);
|
|
66
33
|
const start = Math.max(0, firstViewableIndex - windowOverscan);
|
|
67
|
-
const end = Math.min(
|
|
68
|
-
|
|
34
|
+
const end = Math.min(
|
|
35
|
+
totalItems - 1,
|
|
36
|
+
firstViewableIndex + visibleItems + windowOverscan
|
|
37
|
+
);
|
|
38
|
+
setFixedListWindow((lastWindow) => {
|
|
69
39
|
const nextWindow = {
|
|
70
40
|
visibleItems,
|
|
71
41
|
start,
|
|
72
42
|
end,
|
|
73
|
-
itemInView: (
|
|
43
|
+
itemInView: (index) => {
|
|
74
44
|
return start <= index && index <= end;
|
|
75
45
|
}
|
|
76
46
|
};
|
|
@@ -84,52 +54,81 @@ export default function useFixedWindowList(elementRef, itemHeight, totalItems, o
|
|
|
84
54
|
const debounceMeasureList = debounce(() => {
|
|
85
55
|
measureWindow();
|
|
86
56
|
}, 16);
|
|
87
|
-
scrollContainer?.addEventListener(
|
|
88
|
-
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
89
|
-
|
|
57
|
+
scrollContainer?.addEventListener("scroll", debounceMeasureList);
|
|
58
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
59
|
+
"resize",
|
|
60
|
+
debounceMeasureList
|
|
61
|
+
);
|
|
62
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
63
|
+
"resize",
|
|
64
|
+
debounceMeasureList
|
|
65
|
+
);
|
|
90
66
|
return () => {
|
|
91
|
-
scrollContainer?.removeEventListener(
|
|
92
|
-
|
|
67
|
+
scrollContainer?.removeEventListener(
|
|
68
|
+
"scroll",
|
|
69
|
+
debounceMeasureList
|
|
70
|
+
);
|
|
71
|
+
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
72
|
+
"resize",
|
|
73
|
+
debounceMeasureList
|
|
74
|
+
);
|
|
93
75
|
};
|
|
94
|
-
}, [
|
|
76
|
+
}, [
|
|
77
|
+
itemHeight,
|
|
78
|
+
elementRef,
|
|
79
|
+
totalItems,
|
|
80
|
+
options?.expandedState,
|
|
81
|
+
options?.windowOverscan,
|
|
82
|
+
useWindowing
|
|
83
|
+
]);
|
|
95
84
|
useLayoutEffect(() => {
|
|
96
85
|
if (!useWindowing) {
|
|
97
86
|
return;
|
|
98
87
|
}
|
|
99
88
|
const scrollContainer = getScrollContainer(elementRef.current);
|
|
100
|
-
const handleKeyDown = (
|
|
89
|
+
const handleKeyDown = (event) => {
|
|
101
90
|
switch (event.keyCode) {
|
|
102
|
-
case HOME:
|
|
103
|
-
{
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
case PAGEDOWN:
|
|
121
|
-
{
|
|
122
|
-
return scrollContainer?.scrollTo({
|
|
123
|
-
top: scrollContainer.scrollTop + fixedListWindow.visibleItems * itemHeight
|
|
124
|
-
});
|
|
125
|
-
}
|
|
91
|
+
case HOME: {
|
|
92
|
+
return scrollContainer?.scrollTo({ top: 0 });
|
|
93
|
+
}
|
|
94
|
+
case END: {
|
|
95
|
+
return scrollContainer?.scrollTo({
|
|
96
|
+
top: totalItems * itemHeight
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
case PAGEUP: {
|
|
100
|
+
return scrollContainer?.scrollTo({
|
|
101
|
+
top: scrollContainer.scrollTop - fixedListWindow.visibleItems * itemHeight
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
case PAGEDOWN: {
|
|
105
|
+
return scrollContainer?.scrollTo({
|
|
106
|
+
top: scrollContainer.scrollTop + fixedListWindow.visibleItems * itemHeight
|
|
107
|
+
});
|
|
108
|
+
}
|
|
126
109
|
}
|
|
127
110
|
};
|
|
128
|
-
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
111
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
112
|
+
"keydown",
|
|
113
|
+
handleKeyDown
|
|
114
|
+
);
|
|
129
115
|
return () => {
|
|
130
|
-
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
116
|
+
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
117
|
+
"keydown",
|
|
118
|
+
handleKeyDown
|
|
119
|
+
);
|
|
131
120
|
};
|
|
132
|
-
}, [
|
|
121
|
+
}, [
|
|
122
|
+
totalItems,
|
|
123
|
+
itemHeight,
|
|
124
|
+
elementRef,
|
|
125
|
+
fixedListWindow.visibleItems,
|
|
126
|
+
useWindowing,
|
|
127
|
+
options?.expandedState
|
|
128
|
+
]);
|
|
133
129
|
return [fixedListWindow, setFixedListWindow];
|
|
134
130
|
}
|
|
135
|
-
|
|
131
|
+
export {
|
|
132
|
+
useFixedWindowList as default
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=index.js.map
|