@wordpress/compose 7.32.0 → 7.32.1-next.47f435fc9.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,83 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var use_fixed_window_list_exports = {};
|
|
20
|
+
__export(use_fixed_window_list_exports, {
|
|
21
|
+
default: () => useFixedWindowList
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
/**
|
|
12
|
-
* WordPress dependencies
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Internal dependencies
|
|
17
|
-
*/
|
|
18
|
-
|
|
23
|
+
module.exports = __toCommonJS(use_fixed_window_list_exports);
|
|
24
|
+
var import_element = require("@wordpress/element");
|
|
25
|
+
var import_dom = require("@wordpress/dom");
|
|
26
|
+
var import_keycodes = require("@wordpress/keycodes");
|
|
27
|
+
var import_debounce = require("../../utils/debounce");
|
|
19
28
|
const DEFAULT_INIT_WINDOW_SIZE = 30;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @typedef {Object} WPFixedWindowList
|
|
23
|
-
*
|
|
24
|
-
* @property {number} visibleItems Items visible in the current viewport
|
|
25
|
-
* @property {number} start Start index of the window
|
|
26
|
-
* @property {number} end End index of the window
|
|
27
|
-
* @property {(index:number)=>boolean} itemInView Returns true if item is in the window
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @typedef {Object} WPFixedWindowListOptions
|
|
32
|
-
*
|
|
33
|
-
* @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.
|
|
34
|
-
* @property {boolean} [useWindowing] When false avoids calculating the window size
|
|
35
|
-
* @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.
|
|
36
|
-
* @property {any} [expandedState] Used to recalculate the window size when the expanded state of a list changes.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
* @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.
|
|
42
|
-
* @param { number } itemHeight Fixed item height in pixels
|
|
43
|
-
* @param { number } totalItems Total items in list
|
|
44
|
-
* @param { WPFixedWindowListOptions } [options] Options object
|
|
45
|
-
* @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter
|
|
46
|
-
*/
|
|
47
29
|
function useFixedWindowList(elementRef, itemHeight, totalItems, options) {
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const [fixedListWindow, setFixedListWindow] = (0, _element.useState)({
|
|
30
|
+
const initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE;
|
|
31
|
+
const useWindowing = options?.useWindowing ?? true;
|
|
32
|
+
const [fixedListWindow, setFixedListWindow] = (0, import_element.useState)({
|
|
52
33
|
visibleItems: initWindowSize,
|
|
53
34
|
start: 0,
|
|
54
35
|
end: initWindowSize,
|
|
55
|
-
itemInView: (
|
|
36
|
+
itemInView: (index) => {
|
|
56
37
|
return index >= 0 && index <= initWindowSize;
|
|
57
38
|
}
|
|
58
39
|
});
|
|
59
|
-
(0,
|
|
40
|
+
(0, import_element.useLayoutEffect)(() => {
|
|
60
41
|
if (!useWindowing) {
|
|
61
42
|
return;
|
|
62
43
|
}
|
|
63
|
-
const scrollContainer = (0,
|
|
64
|
-
const measureWindow = (
|
|
65
|
-
var _options$windowOversc;
|
|
44
|
+
const scrollContainer = (0, import_dom.getScrollContainer)(elementRef.current);
|
|
45
|
+
const measureWindow = (initRender) => {
|
|
66
46
|
if (!scrollContainer) {
|
|
67
47
|
return;
|
|
68
48
|
}
|
|
69
|
-
const visibleItems = Math.ceil(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
49
|
+
const visibleItems = Math.ceil(
|
|
50
|
+
scrollContainer.clientHeight / itemHeight
|
|
51
|
+
);
|
|
52
|
+
const windowOverscan = initRender ? visibleItems : options?.windowOverscan ?? visibleItems;
|
|
53
|
+
const firstViewableIndex = Math.floor(
|
|
54
|
+
scrollContainer.scrollTop / itemHeight
|
|
55
|
+
);
|
|
73
56
|
const start = Math.max(0, firstViewableIndex - windowOverscan);
|
|
74
|
-
const end = Math.min(
|
|
75
|
-
|
|
57
|
+
const end = Math.min(
|
|
58
|
+
totalItems - 1,
|
|
59
|
+
firstViewableIndex + visibleItems + windowOverscan
|
|
60
|
+
);
|
|
61
|
+
setFixedListWindow((lastWindow) => {
|
|
76
62
|
const nextWindow = {
|
|
77
63
|
visibleItems,
|
|
78
64
|
start,
|
|
79
65
|
end,
|
|
80
|
-
itemInView: (
|
|
66
|
+
itemInView: (index) => {
|
|
81
67
|
return start <= index && index <= end;
|
|
82
68
|
}
|
|
83
69
|
};
|
|
@@ -88,55 +74,81 @@ function useFixedWindowList(elementRef, itemHeight, totalItems, options) {
|
|
|
88
74
|
});
|
|
89
75
|
};
|
|
90
76
|
measureWindow(true);
|
|
91
|
-
const debounceMeasureList = (0,
|
|
77
|
+
const debounceMeasureList = (0, import_debounce.debounce)(() => {
|
|
92
78
|
measureWindow();
|
|
93
79
|
}, 16);
|
|
94
|
-
scrollContainer?.addEventListener(
|
|
95
|
-
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
96
|
-
|
|
80
|
+
scrollContainer?.addEventListener("scroll", debounceMeasureList);
|
|
81
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
82
|
+
"resize",
|
|
83
|
+
debounceMeasureList
|
|
84
|
+
);
|
|
85
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
86
|
+
"resize",
|
|
87
|
+
debounceMeasureList
|
|
88
|
+
);
|
|
97
89
|
return () => {
|
|
98
|
-
scrollContainer?.removeEventListener(
|
|
99
|
-
|
|
90
|
+
scrollContainer?.removeEventListener(
|
|
91
|
+
"scroll",
|
|
92
|
+
debounceMeasureList
|
|
93
|
+
);
|
|
94
|
+
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
95
|
+
"resize",
|
|
96
|
+
debounceMeasureList
|
|
97
|
+
);
|
|
100
98
|
};
|
|
101
|
-
}, [
|
|
102
|
-
|
|
99
|
+
}, [
|
|
100
|
+
itemHeight,
|
|
101
|
+
elementRef,
|
|
102
|
+
totalItems,
|
|
103
|
+
options?.expandedState,
|
|
104
|
+
options?.windowOverscan,
|
|
105
|
+
useWindowing
|
|
106
|
+
]);
|
|
107
|
+
(0, import_element.useLayoutEffect)(() => {
|
|
103
108
|
if (!useWindowing) {
|
|
104
109
|
return;
|
|
105
110
|
}
|
|
106
|
-
const scrollContainer = (0,
|
|
107
|
-
const handleKeyDown = (
|
|
111
|
+
const scrollContainer = (0, import_dom.getScrollContainer)(elementRef.current);
|
|
112
|
+
const handleKeyDown = (event) => {
|
|
108
113
|
switch (event.keyCode) {
|
|
109
|
-
case
|
|
110
|
-
{
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
case _keycodes.PAGEDOWN:
|
|
128
|
-
{
|
|
129
|
-
return scrollContainer?.scrollTo({
|
|
130
|
-
top: scrollContainer.scrollTop + fixedListWindow.visibleItems * itemHeight
|
|
131
|
-
});
|
|
132
|
-
}
|
|
114
|
+
case import_keycodes.HOME: {
|
|
115
|
+
return scrollContainer?.scrollTo({ top: 0 });
|
|
116
|
+
}
|
|
117
|
+
case import_keycodes.END: {
|
|
118
|
+
return scrollContainer?.scrollTo({
|
|
119
|
+
top: totalItems * itemHeight
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
case import_keycodes.PAGEUP: {
|
|
123
|
+
return scrollContainer?.scrollTo({
|
|
124
|
+
top: scrollContainer.scrollTop - fixedListWindow.visibleItems * itemHeight
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
case import_keycodes.PAGEDOWN: {
|
|
128
|
+
return scrollContainer?.scrollTo({
|
|
129
|
+
top: scrollContainer.scrollTop + fixedListWindow.visibleItems * itemHeight
|
|
130
|
+
});
|
|
131
|
+
}
|
|
133
132
|
}
|
|
134
133
|
};
|
|
135
|
-
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
134
|
+
scrollContainer?.ownerDocument?.defaultView?.addEventListener(
|
|
135
|
+
"keydown",
|
|
136
|
+
handleKeyDown
|
|
137
|
+
);
|
|
136
138
|
return () => {
|
|
137
|
-
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
139
|
+
scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
|
|
140
|
+
"keydown",
|
|
141
|
+
handleKeyDown
|
|
142
|
+
);
|
|
138
143
|
};
|
|
139
|
-
}, [
|
|
144
|
+
}, [
|
|
145
|
+
totalItems,
|
|
146
|
+
itemHeight,
|
|
147
|
+
elementRef,
|
|
148
|
+
fixedListWindow.visibleItems,
|
|
149
|
+
useWindowing,
|
|
150
|
+
options?.expandedState
|
|
151
|
+
]);
|
|
140
152
|
return [fixedListWindow, setFixedListWindow];
|
|
141
153
|
}
|
|
142
|
-
//# sourceMappingURL=index.js.map
|
|
154
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["_element","require","_dom","_keycodes","_debounce","DEFAULT_INIT_WINDOW_SIZE","useFixedWindowList","elementRef","itemHeight","totalItems","options","_options$initWindowSi","_options$useWindowing","initWindowSize","useWindowing","fixedListWindow","setFixedListWindow","useState","visibleItems","start","end","itemInView","index","useLayoutEffect","scrollContainer","getScrollContainer","current","measureWindow","initRender","_options$windowOversc","Math","ceil","clientHeight","windowOverscan","firstViewableIndex","floor","scrollTop","max","min","lastWindow","nextWindow","debounceMeasureList","debounce","addEventListener","ownerDocument","defaultView","removeEventListener","expandedState","handleKeyDown","event","keyCode","HOME","scrollTo","top","END","PAGEUP","PAGEDOWN"],"sources":["@wordpress/compose/src/hooks/use-fixed-window-list/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useLayoutEffect } from '@wordpress/element';\nimport { getScrollContainer } from '@wordpress/dom';\nimport { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { debounce } from '../../utils/debounce';\n\nconst DEFAULT_INIT_WINDOW_SIZE = 30;\n\n/**\n * @typedef {Object} WPFixedWindowList\n *\n * @property {number} visibleItems Items visible in the current viewport\n * @property {number} start Start index of the window\n * @property {number} end End index of the window\n * @property {(index:number)=>boolean} itemInView Returns true if item is in the window\n */\n\n/**\n * @typedef {Object} WPFixedWindowListOptions\n *\n * @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.\n * @property {boolean} [useWindowing] When false avoids calculating the window size\n * @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.\n * @property {any} [expandedState] Used to recalculate the window size when the expanded state of a list changes.\n */\n\n/**\n *\n * @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.\n * @param { number } itemHeight Fixed item height in pixels\n * @param { number } totalItems Total items in list\n * @param { WPFixedWindowListOptions } [options] Options object\n * @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter\n */\nexport default function useFixedWindowList(\n\telementRef,\n\titemHeight,\n\ttotalItems,\n\toptions\n) {\n\tconst initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE;\n\tconst useWindowing = options?.useWindowing ?? true;\n\n\tconst [ fixedListWindow, setFixedListWindow ] = useState( {\n\t\tvisibleItems: initWindowSize,\n\t\tstart: 0,\n\t\tend: initWindowSize,\n\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\treturn index >= 0 && index <= initWindowSize;\n\t\t},\n\t} );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst measureWindow = (\n\t\t\t/** @type {boolean | undefined} */ initRender\n\t\t) => {\n\t\t\tif ( ! scrollContainer ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst visibleItems = Math.ceil(\n\t\t\t\tscrollContainer.clientHeight / itemHeight\n\t\t\t);\n\t\t\t// Aim to keep opening list view fast, afterward we can optimize for scrolling.\n\t\t\tconst windowOverscan = initRender\n\t\t\t\t? visibleItems\n\t\t\t\t: options?.windowOverscan ?? visibleItems;\n\t\t\tconst firstViewableIndex = Math.floor(\n\t\t\t\tscrollContainer.scrollTop / itemHeight\n\t\t\t);\n\t\t\tconst start = Math.max( 0, firstViewableIndex - windowOverscan );\n\t\t\tconst end = Math.min(\n\t\t\t\ttotalItems - 1,\n\t\t\t\tfirstViewableIndex + visibleItems + windowOverscan\n\t\t\t);\n\t\t\tsetFixedListWindow( ( lastWindow ) => {\n\t\t\t\tconst nextWindow = {\n\t\t\t\t\tvisibleItems,\n\t\t\t\t\tstart,\n\t\t\t\t\tend,\n\t\t\t\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\t\t\t\treturn start <= index && index <= end;\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t\tif (\n\t\t\t\t\tlastWindow.start !== nextWindow.start ||\n\t\t\t\t\tlastWindow.end !== nextWindow.end ||\n\t\t\t\t\tlastWindow.visibleItems !== nextWindow.visibleItems\n\t\t\t\t) {\n\t\t\t\t\treturn nextWindow;\n\t\t\t\t}\n\t\t\t\treturn lastWindow;\n\t\t\t} );\n\t\t};\n\n\t\tmeasureWindow( true );\n\t\tconst debounceMeasureList = debounce( () => {\n\t\t\tmeasureWindow();\n\t\t}, 16 );\n\t\tscrollContainer?.addEventListener( 'scroll', debounceMeasureList );\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\n\t\treturn () => {\n\t\t\tscrollContainer?.removeEventListener(\n\t\t\t\t'scroll',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'resize',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t};\n\t}, [\n\t\titemHeight,\n\t\telementRef,\n\t\ttotalItems,\n\t\toptions?.expandedState,\n\t\toptions?.windowOverscan,\n\t\tuseWindowing,\n\t] );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst handleKeyDown = ( /** @type {KeyboardEvent} */ event ) => {\n\t\t\tswitch ( event.keyCode ) {\n\t\t\t\tcase HOME: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( { top: 0 } );\n\t\t\t\t}\n\t\t\t\tcase END: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop: totalItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEUP: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop -\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEDOWN: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop +\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'keydown',\n\t\t\thandleKeyDown\n\t\t);\n\t\treturn () => {\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'keydown',\n\t\t\t\thandleKeyDown\n\t\t\t);\n\t\t};\n\t}, [\n\t\ttotalItems,\n\t\titemHeight,\n\t\telementRef,\n\t\tfixedListWindow.visibleItems,\n\t\tuseWindowing,\n\t\toptions?.expandedState,\n\t] );\n\n\treturn [ fixedListWindow, setFixedListWindow ];\n}\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,IAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AAKA,IAAAG,SAAA,GAAAH,OAAA;AAVA;AACA;AACA;;AAKA;AACA;AACA;;AAGA,MAAMI,wBAAwB,GAAG,EAAE;;AAEnC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASC,kBAAkBA,CACzCC,UAAU,EACVC,UAAU,EACVC,UAAU,EACVC,OAAO,EACN;EAAA,IAAAC,qBAAA,EAAAC,qBAAA;EACD,MAAMC,cAAc,IAAAF,qBAAA,GAAGD,OAAO,EAAEG,cAAc,cAAAF,qBAAA,cAAAA,qBAAA,GAAIN,wBAAwB;EAC1E,MAAMS,YAAY,IAAAF,qBAAA,GAAGF,OAAO,EAAEI,YAAY,cAAAF,qBAAA,cAAAA,qBAAA,GAAI,IAAI;EAElD,MAAM,CAAEG,eAAe,EAAEC,kBAAkB,CAAE,GAAG,IAAAC,iBAAQ,EAAE;IACzDC,YAAY,EAAEL,cAAc;IAC5BM,KAAK,EAAE,CAAC;IACRC,GAAG,EAAEP,cAAc;IACnBQ,UAAU,EAAEA,CAAE,qBAAsBC,KAAK,KAAM;MAC9C,OAAOA,KAAK,IAAI,CAAC,IAA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D;YACnB,CAAE,CAAC;UACJ;QACA,KAAK+C,gBAAM;UAAE;YACZ,OAAO/B,eAAe,EAAE4B,QAAQ,CAAE;cACjCC,GAAG,EACF7B,eAAe,CAACY,SAAS,GACzBrB,eAAe,CAACG,YAAY,GAAGV;YACjC,CAAE,CAAC;UACJ;QACA,KAAKgD,kBAAQ;UAAE;YACd,OAAOhC,eAAe,EAAE4B,QAAQ,CAAE;cACjCC,GAAG,EACF7B,eAAe,CAACY,SAAS,GACzBrB,eAAe,CAACG,YAAY,GAAGV;YACjC,CAAE,CAAC;UACJ;MACD;IACD,CAAC;IACDgB,eAAe,EAAEoB,aAAa,EAAEC,WAAW,EAAEF,gBAAgB,CAC5D,SAAS,EACTK,aACD,CAAC;IACD,OAAO,MAAM;MACZxB,eAAe,EAAEoB,aAAa,EAAEC,WAAW,EAAEC,mBAAmB,CAC/D,SAAS,EACTE,aACD,CAAC;IACF,CAAC;EACF,CAAC,EAAE,CACFvC,UAAU,EACVD,UAAU,EACVD,UAAU,EACVQ,eAAe,CAACG,YAAY,EAC5BJ,YAAY,EACZJ,OAAO,EAAEqC,aAAa,CACrB,CAAC;EAEH,OAAO,CAAEhC,eAAe,EAAEC,kBAAkB,CAAE;AAC/C","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/hooks/use-fixed-window-list/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useState, useLayoutEffect } from '@wordpress/element';\nimport { getScrollContainer } from '@wordpress/dom';\nimport { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { debounce } from '../../utils/debounce';\n\nconst DEFAULT_INIT_WINDOW_SIZE = 30;\n\n/**\n * @typedef {Object} WPFixedWindowList\n *\n * @property {number} visibleItems Items visible in the current viewport\n * @property {number} start Start index of the window\n * @property {number} end End index of the window\n * @property {(index:number)=>boolean} itemInView Returns true if item is in the window\n */\n\n/**\n * @typedef {Object} WPFixedWindowListOptions\n *\n * @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.\n * @property {boolean} [useWindowing] When false avoids calculating the window size\n * @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.\n * @property {any} [expandedState] Used to recalculate the window size when the expanded state of a list changes.\n */\n\n/**\n *\n * @param {import('react').RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.\n * @param { number } itemHeight Fixed item height in pixels\n * @param { number } totalItems Total items in list\n * @param { WPFixedWindowListOptions } [options] Options object\n * @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter\n */\nexport default function useFixedWindowList(\n\telementRef,\n\titemHeight,\n\ttotalItems,\n\toptions\n) {\n\tconst initWindowSize = options?.initWindowSize ?? DEFAULT_INIT_WINDOW_SIZE;\n\tconst useWindowing = options?.useWindowing ?? true;\n\n\tconst [ fixedListWindow, setFixedListWindow ] = useState( {\n\t\tvisibleItems: initWindowSize,\n\t\tstart: 0,\n\t\tend: initWindowSize,\n\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\treturn index >= 0 && index <= initWindowSize;\n\t\t},\n\t} );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst measureWindow = (\n\t\t\t/** @type {boolean | undefined} */ initRender\n\t\t) => {\n\t\t\tif ( ! scrollContainer ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst visibleItems = Math.ceil(\n\t\t\t\tscrollContainer.clientHeight / itemHeight\n\t\t\t);\n\t\t\t// Aim to keep opening list view fast, afterward we can optimize for scrolling.\n\t\t\tconst windowOverscan = initRender\n\t\t\t\t? visibleItems\n\t\t\t\t: options?.windowOverscan ?? visibleItems;\n\t\t\tconst firstViewableIndex = Math.floor(\n\t\t\t\tscrollContainer.scrollTop / itemHeight\n\t\t\t);\n\t\t\tconst start = Math.max( 0, firstViewableIndex - windowOverscan );\n\t\t\tconst end = Math.min(\n\t\t\t\ttotalItems - 1,\n\t\t\t\tfirstViewableIndex + visibleItems + windowOverscan\n\t\t\t);\n\t\t\tsetFixedListWindow( ( lastWindow ) => {\n\t\t\t\tconst nextWindow = {\n\t\t\t\t\tvisibleItems,\n\t\t\t\t\tstart,\n\t\t\t\t\tend,\n\t\t\t\t\titemInView: ( /** @type {number} */ index ) => {\n\t\t\t\t\t\treturn start <= index && index <= end;\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t\tif (\n\t\t\t\t\tlastWindow.start !== nextWindow.start ||\n\t\t\t\t\tlastWindow.end !== nextWindow.end ||\n\t\t\t\t\tlastWindow.visibleItems !== nextWindow.visibleItems\n\t\t\t\t) {\n\t\t\t\t\treturn nextWindow;\n\t\t\t\t}\n\t\t\t\treturn lastWindow;\n\t\t\t} );\n\t\t};\n\n\t\tmeasureWindow( true );\n\t\tconst debounceMeasureList = debounce( () => {\n\t\t\tmeasureWindow();\n\t\t}, 16 );\n\t\tscrollContainer?.addEventListener( 'scroll', debounceMeasureList );\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'resize',\n\t\t\tdebounceMeasureList\n\t\t);\n\n\t\treturn () => {\n\t\t\tscrollContainer?.removeEventListener(\n\t\t\t\t'scroll',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'resize',\n\t\t\t\tdebounceMeasureList\n\t\t\t);\n\t\t};\n\t}, [\n\t\titemHeight,\n\t\telementRef,\n\t\ttotalItems,\n\t\toptions?.expandedState,\n\t\toptions?.windowOverscan,\n\t\tuseWindowing,\n\t] );\n\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tconst handleKeyDown = ( /** @type {KeyboardEvent} */ event ) => {\n\t\t\tswitch ( event.keyCode ) {\n\t\t\t\tcase HOME: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( { top: 0 } );\n\t\t\t\t}\n\t\t\t\tcase END: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop: totalItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEUP: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop -\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEDOWN: {\n\t\t\t\t\treturn scrollContainer?.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop +\n\t\t\t\t\t\t\tfixedListWindow.visibleItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tscrollContainer?.ownerDocument?.defaultView?.addEventListener(\n\t\t\t'keydown',\n\t\t\thandleKeyDown\n\t\t);\n\t\treturn () => {\n\t\t\tscrollContainer?.ownerDocument?.defaultView?.removeEventListener(\n\t\t\t\t'keydown',\n\t\t\t\thandleKeyDown\n\t\t\t);\n\t\t};\n\t}, [\n\t\ttotalItems,\n\t\titemHeight,\n\t\telementRef,\n\t\tfixedListWindow.visibleItems,\n\t\tuseWindowing,\n\t\toptions?.expandedState,\n\t] );\n\n\treturn [ fixedListWindow, setFixedListWindow ];\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA0C;AAC1C,iBAAmC;AACnC,sBAA4C;AAK5C,sBAAyB;AAEzB,MAAM,2BAA2B;AA4B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AAA,EACV,CAAE;AAEF,SAAO,CAAE,iBAAiB,kBAAmB;AAC9C;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,52 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
Object.
|
|
5
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var use_focus_on_mount_exports = {};
|
|
30
|
+
__export(use_focus_on_mount_exports, {
|
|
31
|
+
default: () => useFocusOnMount
|
|
6
32
|
});
|
|
7
|
-
exports
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Internal dependencies
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Hook used to focus the first tabbable element on mount.
|
|
21
|
-
*
|
|
22
|
-
* @param {boolean | 'firstElement'} focusOnMount Focus on mount mode.
|
|
23
|
-
* @return {import('react').RefCallback<HTMLElement>} Ref callback.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```js
|
|
27
|
-
* import { useFocusOnMount } from '@wordpress/compose';
|
|
28
|
-
*
|
|
29
|
-
* const WithFocusOnMount = () => {
|
|
30
|
-
* const ref = useFocusOnMount()
|
|
31
|
-
* return (
|
|
32
|
-
* <div ref={ ref }>
|
|
33
|
-
* <Button />
|
|
34
|
-
* <Button />
|
|
35
|
-
* </div>
|
|
36
|
-
* );
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
function useFocusOnMount(focusOnMount = 'firstElement') {
|
|
41
|
-
const focusOnMountRef = (0, _element.useRef)(focusOnMount);
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Sets focus on a DOM element.
|
|
45
|
-
*
|
|
46
|
-
* @param {HTMLElement} target The DOM element to set focus to.
|
|
47
|
-
* @return {void}
|
|
48
|
-
*/
|
|
49
|
-
const setFocus = target => {
|
|
33
|
+
module.exports = __toCommonJS(use_focus_on_mount_exports);
|
|
34
|
+
var import_element = require("@wordpress/element");
|
|
35
|
+
var import_dom = require("@wordpress/dom");
|
|
36
|
+
var import_use_ref_effect = __toESM(require("../use-ref-effect"));
|
|
37
|
+
function useFocusOnMount(focusOnMount = "firstElement") {
|
|
38
|
+
const focusOnMountRef = (0, import_element.useRef)(focusOnMount);
|
|
39
|
+
const setFocus = (target) => {
|
|
50
40
|
target.focus({
|
|
51
41
|
// When focusing newly mounted dialogs,
|
|
52
42
|
// the position of the popover is often not right on the first render
|
|
@@ -54,26 +44,23 @@ function useFocusOnMount(focusOnMount = 'firstElement') {
|
|
|
54
44
|
preventScroll: true
|
|
55
45
|
});
|
|
56
46
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const timerIdRef = (0, _element.useRef)();
|
|
60
|
-
(0, _element.useEffect)(() => {
|
|
47
|
+
const timerIdRef = (0, import_element.useRef)();
|
|
48
|
+
(0, import_element.useEffect)(() => {
|
|
61
49
|
focusOnMountRef.current = focusOnMount;
|
|
62
50
|
}, [focusOnMount]);
|
|
63
|
-
return (0,
|
|
64
|
-
var _node$ownerDocument$a;
|
|
51
|
+
return (0, import_use_ref_effect.default)((node) => {
|
|
65
52
|
if (!node || focusOnMountRef.current === false) {
|
|
66
53
|
return;
|
|
67
54
|
}
|
|
68
|
-
if (node.contains(
|
|
55
|
+
if (node.contains(node.ownerDocument?.activeElement ?? null)) {
|
|
69
56
|
return;
|
|
70
57
|
}
|
|
71
|
-
if (focusOnMountRef.current !==
|
|
58
|
+
if (focusOnMountRef.current !== "firstElement") {
|
|
72
59
|
setFocus(node);
|
|
73
60
|
return;
|
|
74
61
|
}
|
|
75
62
|
timerIdRef.current = setTimeout(() => {
|
|
76
|
-
const firstTabbable =
|
|
63
|
+
const firstTabbable = import_dom.focus.tabbable.find(node)[0];
|
|
77
64
|
if (firstTabbable) {
|
|
78
65
|
setFocus(firstTabbable);
|
|
79
66
|
}
|
|
@@ -85,4 +72,4 @@ function useFocusOnMount(focusOnMount = 'firstElement') {
|
|
|
85
72
|
};
|
|
86
73
|
}, []);
|
|
87
74
|
}
|
|
88
|
-
//# sourceMappingURL=index.js.map
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/hooks/use-focus-on-mount/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRef, useEffect } from '@wordpress/element';\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/**\n * Hook used to focus the first tabbable element on mount.\n *\n * @param {boolean | 'firstElement'} focusOnMount Focus on mount mode.\n * @return {import('react').RefCallback<HTMLElement>} Ref callback.\n *\n * @example\n * ```js\n * import { useFocusOnMount } from '@wordpress/compose';\n *\n * const WithFocusOnMount = () => {\n * const ref = useFocusOnMount()\n * return (\n * <div ref={ ref }>\n * <Button />\n * <Button />\n * </div>\n * );\n * }\n * ```\n */\nexport default function useFocusOnMount( focusOnMount = 'firstElement' ) {\n\tconst focusOnMountRef = useRef( focusOnMount );\n\n\t/**\n\t * Sets focus on a DOM element.\n\t *\n\t * @param {HTMLElement} target The DOM element to set focus to.\n\t * @return {void}\n\t */\n\tconst setFocus = ( target ) => {\n\t\ttarget.focus( {\n\t\t\t// When focusing newly mounted dialogs,\n\t\t\t// the position of the popover is often not right on the first render\n\t\t\t// This prevents the layout shifts when focusing the dialogs.\n\t\t\tpreventScroll: true,\n\t\t} );\n\t};\n\n\t/** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */\n\tconst timerIdRef = useRef();\n\n\tuseEffect( () => {\n\t\tfocusOnMountRef.current = focusOnMount;\n\t}, [ focusOnMount ] );\n\n\treturn useRefEffect( ( node ) => {\n\t\tif ( ! node || focusOnMountRef.current === false ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( node.contains( node.ownerDocument?.activeElement ?? null ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( focusOnMountRef.current !== 'firstElement' ) {\n\t\t\tsetFocus( node );\n\t\t\treturn;\n\t\t}\n\n\t\ttimerIdRef.current = setTimeout( () => {\n\t\t\tconst firstTabbable = focus.tabbable.find( node )[ 0 ];\n\t\t\tif ( firstTabbable ) {\n\t\t\t\tsetFocus( firstTabbable );\n\t\t\t}\n\t\t}, 0 );\n\n\t\treturn () => {\n\t\t\tif ( timerIdRef.current ) {\n\t\t\t\tclearTimeout( timerIdRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAkC;AAClC,iBAAsB;AAKtB,4BAAyB;AAuBV,SAAR,gBAAkC,eAAe,gBAAiB;AACxE,QAAM,sBAAkB,uBAAQ,YAAa;AAQ7C,QAAM,WAAW,CAAE,WAAY;AAC9B,WAAO,MAAO;AAAA;AAAA;AAAA;AAAA,MAIb,eAAe;AAAA,IAChB,CAAE;AAAA,EACH;AAGA,QAAM,iBAAa,uBAAO;AAE1B,gCAAW,MAAM;AAChB,oBAAgB,UAAU;AAAA,EAC3B,GAAG,CAAE,YAAa,CAAE;AAEpB,aAAO,sBAAAA,SAAc,CAAE,SAAU;AAChC,QAAK,CAAE,QAAQ,gBAAgB,YAAY,OAAQ;AAClD;AAAA,IACD;AAEA,QAAK,KAAK,SAAU,KAAK,eAAe,iBAAiB,IAAK,GAAI;AACjE;AAAA,IACD;AAEA,QAAK,gBAAgB,YAAY,gBAAiB;AACjD,eAAU,IAAK;AACf;AAAA,IACD;AAEA,eAAW,UAAU,WAAY,MAAM;AACtC,YAAM,gBAAgB,iBAAM,SAAS,KAAM,IAAK,EAAG,CAAE;AACrD,UAAK,eAAgB;AACpB,iBAAU,aAAc;AAAA,MACzB;AAAA,IACD,GAAG,CAAE;AAEL,WAAO,MAAM;AACZ,UAAK,WAAW,SAAU;AACzB,qBAAc,WAAW,OAAQ;AAAA,MAClC;AAAA,IACD;AAAA,EACD,GAAG,CAAC,CAAE;AACP;",
|
|
6
|
+
"names": ["useRefEffect"]
|
|
7
|
+
}
|
|
@@ -1,142 +1,84 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var use_focus_outside_exports = {};
|
|
20
|
+
__export(use_focus_outside_exports, {
|
|
21
|
+
default: () => useFocusOutside
|
|
5
22
|
});
|
|
6
|
-
exports
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
* WordPress dependencies
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Input types which are classified as button types, for use in considering
|
|
14
|
-
* whether element is a (focus-normalized) button.
|
|
15
|
-
*/
|
|
16
|
-
const INPUT_BUTTON_TYPES = ['button', 'submit'];
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* List of HTML button elements subject to focus normalization
|
|
20
|
-
*
|
|
21
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Returns true if the given element is a button element subject to focus
|
|
26
|
-
* normalization, or false otherwise.
|
|
27
|
-
*
|
|
28
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
|
|
29
|
-
*
|
|
30
|
-
* @param eventTarget The target from a mouse or touch event.
|
|
31
|
-
*
|
|
32
|
-
* @return Whether the element is a button element subject to focus normalization.
|
|
33
|
-
*/
|
|
23
|
+
module.exports = __toCommonJS(use_focus_outside_exports);
|
|
24
|
+
var import_element = require("@wordpress/element");
|
|
25
|
+
const INPUT_BUTTON_TYPES = ["button", "submit"];
|
|
34
26
|
function isFocusNormalizedButton(eventTarget) {
|
|
35
27
|
if (!(eventTarget instanceof window.HTMLElement)) {
|
|
36
28
|
return false;
|
|
37
29
|
}
|
|
38
30
|
switch (eventTarget.nodeName) {
|
|
39
|
-
case
|
|
40
|
-
case
|
|
31
|
+
case "A":
|
|
32
|
+
case "BUTTON":
|
|
41
33
|
return true;
|
|
42
|
-
case
|
|
43
|
-
return INPUT_BUTTON_TYPES.includes(
|
|
34
|
+
case "INPUT":
|
|
35
|
+
return INPUT_BUTTON_TYPES.includes(
|
|
36
|
+
eventTarget.type
|
|
37
|
+
);
|
|
44
38
|
}
|
|
45
39
|
return false;
|
|
46
40
|
}
|
|
47
|
-
/**
|
|
48
|
-
* A react hook that can be used to check whether focus has moved outside the
|
|
49
|
-
* element the event handlers are bound to.
|
|
50
|
-
*
|
|
51
|
-
* @param onFocusOutside A callback triggered when focus moves outside
|
|
52
|
-
* the element the event handlers are bound to.
|
|
53
|
-
*
|
|
54
|
-
* @return An object containing event handlers. Bind the event handlers to a
|
|
55
|
-
* wrapping element element to capture when focus moves outside that element.
|
|
56
|
-
*/
|
|
57
41
|
function useFocusOutside(onFocusOutside) {
|
|
58
|
-
const currentOnFocusOutsideRef = (0,
|
|
59
|
-
(0,
|
|
42
|
+
const currentOnFocusOutsideRef = (0, import_element.useRef)(onFocusOutside);
|
|
43
|
+
(0, import_element.useEffect)(() => {
|
|
60
44
|
currentOnFocusOutsideRef.current = onFocusOutside;
|
|
61
45
|
}, [onFocusOutside]);
|
|
62
|
-
const preventBlurCheckRef = (0,
|
|
63
|
-
const blurCheckTimeoutIdRef = (0,
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Cancel a blur check timeout.
|
|
67
|
-
*/
|
|
68
|
-
const cancelBlurCheck = (0, _element.useCallback)(() => {
|
|
46
|
+
const preventBlurCheckRef = (0, import_element.useRef)(false);
|
|
47
|
+
const blurCheckTimeoutIdRef = (0, import_element.useRef)();
|
|
48
|
+
const cancelBlurCheck = (0, import_element.useCallback)(() => {
|
|
69
49
|
clearTimeout(blurCheckTimeoutIdRef.current);
|
|
70
50
|
}, []);
|
|
71
|
-
|
|
72
|
-
// Cancel a blur check if the callback or ref is no longer provided.
|
|
73
|
-
(0, _element.useEffect)(() => {
|
|
51
|
+
(0, import_element.useEffect)(() => {
|
|
74
52
|
if (!onFocusOutside) {
|
|
75
53
|
cancelBlurCheck();
|
|
76
54
|
}
|
|
77
55
|
}, [onFocusOutside, cancelBlurCheck]);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* unassign a flag for preventing blur check on button elements. Some
|
|
82
|
-
* browsers, namely Firefox and Safari, do not emit a focus event on
|
|
83
|
-
* button elements when clicked, while others do. The logic here
|
|
84
|
-
* intends to normalize this as treating click on buttons as focus.
|
|
85
|
-
*
|
|
86
|
-
* @param event
|
|
87
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
|
|
88
|
-
*/
|
|
89
|
-
const normalizeButtonFocus = (0, _element.useCallback)(event => {
|
|
90
|
-
const {
|
|
91
|
-
type,
|
|
92
|
-
target
|
|
93
|
-
} = event;
|
|
94
|
-
const isInteractionEnd = ['mouseup', 'touchend'].includes(type);
|
|
56
|
+
const normalizeButtonFocus = (0, import_element.useCallback)((event) => {
|
|
57
|
+
const { type, target } = event;
|
|
58
|
+
const isInteractionEnd = ["mouseup", "touchend"].includes(type);
|
|
95
59
|
if (isInteractionEnd) {
|
|
96
60
|
preventBlurCheckRef.current = false;
|
|
97
61
|
} else if (isFocusNormalizedButton(target)) {
|
|
98
62
|
preventBlurCheckRef.current = true;
|
|
99
63
|
}
|
|
100
64
|
}, []);
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* A callback triggered when a blur event occurs on the element the handler
|
|
104
|
-
* is bound to.
|
|
105
|
-
*
|
|
106
|
-
* Calls the `onFocusOutside` callback in an immediate timeout if focus has
|
|
107
|
-
* move outside the bound element and is still within the document.
|
|
108
|
-
*/
|
|
109
|
-
const queueBlurCheck = (0, _element.useCallback)(event => {
|
|
110
|
-
// React does not allow using an event reference asynchronously
|
|
111
|
-
// due to recycling behavior, except when explicitly persisted.
|
|
65
|
+
const queueBlurCheck = (0, import_element.useCallback)((event) => {
|
|
112
66
|
event.persist();
|
|
113
|
-
|
|
114
|
-
// Skip blur check if clicking button. See `normalizeButtonFocus`.
|
|
115
67
|
if (preventBlurCheckRef.current) {
|
|
116
68
|
return;
|
|
117
69
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// therefore don't exist in the React tree. An example is opening
|
|
122
|
-
// the Media Library modal from another dialog.
|
|
123
|
-
// This attribute should contain a selector of the related target
|
|
124
|
-
// we want to ignore, because we still need to trigger the blur event
|
|
125
|
-
// on all other cases.
|
|
126
|
-
const ignoreForRelatedTarget = event.target.getAttribute('data-unstable-ignore-focus-outside-for-relatedtarget');
|
|
70
|
+
const ignoreForRelatedTarget = event.target.getAttribute(
|
|
71
|
+
"data-unstable-ignore-focus-outside-for-relatedtarget"
|
|
72
|
+
);
|
|
127
73
|
if (ignoreForRelatedTarget && event.relatedTarget?.closest(ignoreForRelatedTarget)) {
|
|
128
74
|
return;
|
|
129
75
|
}
|
|
130
76
|
blurCheckTimeoutIdRef.current = setTimeout(() => {
|
|
131
|
-
// If document is not focused then focus should remain
|
|
132
|
-
// inside the wrapped component and therefore we cancel
|
|
133
|
-
// this blur event thereby leaving focus in place.
|
|
134
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus.
|
|
135
77
|
if (!document.hasFocus()) {
|
|
136
78
|
event.preventDefault();
|
|
137
79
|
return;
|
|
138
80
|
}
|
|
139
|
-
if (
|
|
81
|
+
if ("function" === typeof currentOnFocusOutsideRef.current) {
|
|
140
82
|
currentOnFocusOutsideRef.current(event);
|
|
141
83
|
}
|
|
142
84
|
}, 0);
|
|
@@ -150,4 +92,4 @@ function useFocusOutside(onFocusOutside) {
|
|
|
150
92
|
onBlur: queueBlurCheck
|
|
151
93
|
};
|
|
152
94
|
}
|
|
153
|
-
//# sourceMappingURL=index.js.map
|
|
95
|
+
//# sourceMappingURL=index.js.map
|