@wordpress/compose 7.32.0 → 7.32.1-next.b8c8708f3.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.
Files changed (235) hide show
  1. package/build/higher-order/compose.js +25 -20
  2. package/build/higher-order/compose.js.map +7 -1
  3. package/build/higher-order/if-condition/index.js +35 -41
  4. package/build/higher-order/if-condition/index.js.map +7 -1
  5. package/build/higher-order/pipe.js +32 -66
  6. package/build/higher-order/pipe.js.map +7 -1
  7. package/build/higher-order/pure/index.js +44 -37
  8. package/build/higher-order/pure/index.js.map +7 -1
  9. package/build/higher-order/with-global-events/index.js +64 -73
  10. package/build/higher-order/with-global-events/index.js.map +7 -1
  11. package/build/higher-order/with-global-events/listener.js +34 -21
  12. package/build/higher-order/with-global-events/listener.js.map +7 -1
  13. package/build/higher-order/with-instance-id/index.js +45 -27
  14. package/build/higher-order/with-instance-id/index.js.map +7 -1
  15. package/build/higher-order/with-safe-timeout/index.js +70 -66
  16. package/build/higher-order/with-safe-timeout/index.js.map +7 -1
  17. package/build/higher-order/with-state/index.js +52 -40
  18. package/build/higher-order/with-state/index.js.map +7 -1
  19. package/build/hooks/use-async-list/index.js +38 -42
  20. package/build/hooks/use-async-list/index.js.map +7 -1
  21. package/build/hooks/use-constrained-tabbing/index.js +54 -75
  22. package/build/hooks/use-constrained-tabbing/index.js.map +7 -1
  23. package/build/hooks/use-copy-on-click/index.js +46 -56
  24. package/build/hooks/use-copy-on-click/index.js.map +7 -1
  25. package/build/hooks/use-copy-to-clipboard/index.js +41 -51
  26. package/build/hooks/use-copy-to-clipboard/index.js.map +7 -1
  27. package/build/hooks/use-debounce/index.js +30 -37
  28. package/build/hooks/use-debounce/index.js.map +7 -1
  29. package/build/hooks/use-debounced-input/index.js +39 -27
  30. package/build/hooks/use-debounced-input/index.js.map +7 -1
  31. package/build/hooks/use-dialog/index.js +63 -55
  32. package/build/hooks/use-dialog/index.js.map +7 -1
  33. package/build/hooks/use-disabled/index.js +74 -82
  34. package/build/hooks/use-disabled/index.js.map +7 -1
  35. package/build/hooks/use-dragging/index.js +51 -48
  36. package/build/hooks/use-dragging/index.js.map +7 -1
  37. package/build/hooks/use-drop-zone/index.js +133 -168
  38. package/build/hooks/use-drop-zone/index.js.map +7 -1
  39. package/build/hooks/use-event/index.js +33 -45
  40. package/build/hooks/use-event/index.js.map +7 -1
  41. package/build/hooks/use-fixed-window-list/index.js +109 -97
  42. package/build/hooks/use-fixed-window-list/index.js.map +7 -1
  43. package/build/hooks/use-focus-on-mount/index.js +44 -57
  44. package/build/hooks/use-focus-on-mount/index.js.map +7 -1
  45. package/build/hooks/use-focus-outside/index.js +44 -102
  46. package/build/hooks/use-focus-outside/index.js.map +7 -1
  47. package/build/hooks/use-focus-return/index.js +35 -56
  48. package/build/hooks/use-focus-return/index.js.map +7 -1
  49. package/build/hooks/use-focusable-iframe/index.js +38 -35
  50. package/build/hooks/use-focusable-iframe/index.js.map +7 -1
  51. package/build/hooks/use-instance-id/index.js +26 -42
  52. package/build/hooks/use-instance-id/index.js.map +7 -1
  53. package/build/hooks/use-isomorphic-layout-effect/index.js +25 -17
  54. package/build/hooks/use-isomorphic-layout-effect/index.js.map +7 -1
  55. package/build/hooks/use-keyboard-shortcut/index.js +72 -69
  56. package/build/hooks/use-keyboard-shortcut/index.js.map +7 -1
  57. package/build/hooks/use-media-query/index.js +39 -35
  58. package/build/hooks/use-media-query/index.js.map +7 -1
  59. package/build/hooks/use-merge-refs/index.js +34 -102
  60. package/build/hooks/use-merge-refs/index.js.map +7 -1
  61. package/build/hooks/use-observable-value/index.js +31 -28
  62. package/build/hooks/use-observable-value/index.js.map +7 -1
  63. package/build/hooks/use-preferred-color-scheme/index.android.js +31 -23
  64. package/build/hooks/use-preferred-color-scheme/index.android.js.map +7 -1
  65. package/build/hooks/use-preferred-color-scheme/index.ios.js +25 -17
  66. package/build/hooks/use-preferred-color-scheme/index.ios.js.map +7 -1
  67. package/build/hooks/use-previous/index.js +26 -25
  68. package/build/hooks/use-previous/index.js.map +7 -1
  69. package/build/hooks/use-reduced-motion/index.js +35 -18
  70. package/build/hooks/use-reduced-motion/index.js.map +7 -1
  71. package/build/hooks/use-ref-effect/index.js +25 -35
  72. package/build/hooks/use-ref-effect/index.js.map +7 -1
  73. package/build/hooks/use-resize-observer/index.js +35 -67
  74. package/build/hooks/use-resize-observer/index.js.map +7 -1
  75. package/build/hooks/use-resize-observer/legacy/index.js +47 -103
  76. package/build/hooks/use-resize-observer/legacy/index.js.map +7 -1
  77. package/build/hooks/use-resize-observer/use-resize-observer.js +44 -36
  78. package/build/hooks/use-resize-observer/use-resize-observer.js.map +7 -1
  79. package/build/hooks/use-state-with-history/index.js +68 -63
  80. package/build/hooks/use-state-with-history/index.js.map +7 -1
  81. package/build/hooks/use-throttle/index.js +30 -37
  82. package/build/hooks/use-throttle/index.js.map +7 -1
  83. package/build/hooks/use-viewport-match/index.js +51 -70
  84. package/build/hooks/use-viewport-match/index.js.map +7 -1
  85. package/build/hooks/use-warn-on-change/index.js +50 -40
  86. package/build/hooks/use-warn-on-change/index.js.map +7 -1
  87. package/build/index.js +156 -358
  88. package/build/index.js.map +7 -1
  89. package/build/utils/create-higher-order-component/index.js +30 -34
  90. package/build/utils/create-higher-order-component/index.js.map +7 -1
  91. package/build/utils/debounce/index.js +38 -99
  92. package/build/utils/debounce/index.js.map +7 -1
  93. package/build/utils/observable-map/index.js +29 -19
  94. package/build/utils/observable-map/index.js.map +7 -1
  95. package/build/utils/throttle/index.js +30 -81
  96. package/build/utils/throttle/index.js.map +7 -1
  97. package/build-module/higher-order/compose.js +6 -15
  98. package/build-module/higher-order/compose.js.map +7 -1
  99. package/build-module/higher-order/if-condition/index.js +16 -37
  100. package/build-module/higher-order/if-condition/index.js.map +7 -1
  101. package/build-module/higher-order/pipe.js +10 -62
  102. package/build-module/higher-order/pipe.js.map +7 -1
  103. package/build-module/higher-order/pure/index.js +12 -29
  104. package/build-module/higher-order/pure/index.js.map +7 -1
  105. package/build-module/higher-order/with-global-events/index.js +34 -67
  106. package/build-module/higher-order/with-global-events/index.js.map +7 -1
  107. package/build-module/higher-order/with-global-events/listener.js +16 -17
  108. package/build-module/higher-order/with-global-events/listener.js.map +7 -1
  109. package/build-module/higher-order/with-instance-id/index.js +17 -22
  110. package/build-module/higher-order/with-instance-id/index.js.map +7 -1
  111. package/build-module/higher-order/with-safe-timeout/index.js +52 -62
  112. package/build-module/higher-order/with-safe-timeout/index.js.map +7 -1
  113. package/build-module/higher-order/with-state/index.js +23 -35
  114. package/build-module/higher-order/with-state/index.js.map +7 -1
  115. package/build-module/hooks/use-async-list/index.js +16 -33
  116. package/build-module/hooks/use-async-list/index.js.map +7 -1
  117. package/build-module/hooks/use-constrained-tabbing/index.js +26 -70
  118. package/build-module/hooks/use-constrained-tabbing/index.js.map +7 -1
  119. package/build-module/hooks/use-copy-on-click/index.js +14 -47
  120. package/build-module/hooks/use-copy-on-click/index.js.map +7 -1
  121. package/build-module/hooks/use-copy-to-clipboard/index.js +11 -44
  122. package/build-module/hooks/use-copy-to-clipboard/index.js.map +7 -1
  123. package/build-module/hooks/use-debounce/index.js +12 -33
  124. package/build-module/hooks/use-debounce/index.js.map +7 -1
  125. package/build-module/hooks/use-debounced-input/index.js +7 -18
  126. package/build-module/hooks/use-debounced-input/index.js.map +7 -1
  127. package/build-module/hooks/use-dialog/index.js +29 -43
  128. package/build-module/hooks/use-dialog/index.js.map +7 -1
  129. package/build-module/hooks/use-disabled/index.js +47 -78
  130. package/build-module/hooks/use-disabled/index.js.map +7 -1
  131. package/build-module/hooks/use-dragging/index.js +19 -39
  132. package/build-module/hooks/use-dragging/index.js.map +7 -1
  133. package/build-module/hooks/use-drop-zone/index.js +100 -158
  134. package/build-module/hooks/use-drop-zone/index.js.map +7 -1
  135. package/build-module/hooks/use-event/index.js +13 -39
  136. package/build-module/hooks/use-event/index.js.map +7 -1
  137. package/build-module/hooks/use-fixed-window-list/index.js +86 -87
  138. package/build-module/hooks/use-fixed-window-list/index.js.map +7 -1
  139. package/build-module/hooks/use-focus-on-mount/index.js +12 -48
  140. package/build-module/hooks/use-focus-on-mount/index.js.map +7 -1
  141. package/build-module/hooks/use-focus-outside/index.js +21 -93
  142. package/build-module/hooks/use-focus-outside/index.js.map +7 -1
  143. package/build-module/hooks/use-focus-return/index.js +13 -48
  144. package/build-module/hooks/use-focus-return/index.js.map +7 -1
  145. package/build-module/hooks/use-focusable-iframe/index.js +11 -31
  146. package/build-module/hooks/use-focusable-iframe/index.js.map +7 -1
  147. package/build-module/hooks/use-instance-id/index.js +7 -36
  148. package/build-module/hooks/use-instance-id/index.js.map +7 -1
  149. package/build-module/hooks/use-isomorphic-layout-effect/index.js +7 -13
  150. package/build-module/hooks/use-isomorphic-layout-effect/index.js.map +7 -1
  151. package/build-module/hooks/use-keyboard-shortcut/index.js +40 -60
  152. package/build-module/hooks/use-keyboard-shortcut/index.js.map +7 -1
  153. package/build-module/hooks/use-media-query/index.js +21 -30
  154. package/build-module/hooks/use-media-query/index.js.map +7 -1
  155. package/build-module/hooks/use-merge-refs/index.js +10 -92
  156. package/build-module/hooks/use-merge-refs/index.js.map +7 -1
  157. package/build-module/hooks/use-observable-value/index.js +13 -24
  158. package/build-module/hooks/use-observable-value/index.js.map +7 -1
  159. package/build-module/hooks/use-preferred-color-scheme/index.android.js +15 -18
  160. package/build-module/hooks/use-preferred-color-scheme/index.android.js.map +7 -1
  161. package/build-module/hooks/use-preferred-color-scheme/index.ios.js +6 -12
  162. package/build-module/hooks/use-preferred-color-scheme/index.ios.js.map +7 -1
  163. package/build-module/hooks/use-previous/index.js +7 -20
  164. package/build-module/hooks/use-previous/index.js.map +7 -1
  165. package/build-module/hooks/use-reduced-motion/index.js +7 -13
  166. package/build-module/hooks/use-reduced-motion/index.js.map +7 -1
  167. package/build-module/hooks/use-ref-effect/index.js +7 -31
  168. package/build-module/hooks/use-ref-effect/index.js.map +7 -1
  169. package/build-module/hooks/use-resize-observer/index.js +7 -61
  170. package/build-module/hooks/use-resize-observer/index.js.map +7 -1
  171. package/build-module/hooks/use-resize-observer/legacy/index.js +28 -96
  172. package/build-module/hooks/use-resize-observer/legacy/index.js.map +7 -1
  173. package/build-module/hooks/use-resize-observer/use-resize-observer.js +10 -28
  174. package/build-module/hooks/use-resize-observer/use-resize-observer.js.map +7 -1
  175. package/build-module/hooks/use-state-with-history/index.js +47 -55
  176. package/build-module/hooks/use-state-with-history/index.js.map +7 -1
  177. package/build-module/hooks/use-throttle/index.js +12 -33
  178. package/build-module/hooks/use-throttle/index.js.map +7 -1
  179. package/build-module/hooks/use-viewport-match/index.js +22 -64
  180. package/build-module/hooks/use-viewport-match/index.js.map +7 -1
  181. package/build-module/hooks/use-warn-on-change/index.js +21 -34
  182. package/build-module/hooks/use-warn-on-change/index.js.map +7 -1
  183. package/build-module/index.js +83 -53
  184. package/build-module/index.js.map +7 -1
  185. package/build-module/utils/create-higher-order-component/index.js +9 -30
  186. package/build-module/utils/create-higher-order-component/index.js.map +7 -1
  187. package/build-module/utils/debounce/index.js +17 -95
  188. package/build-module/utils/debounce/index.js.map +7 -1
  189. package/build-module/utils/observable-map/index.js +8 -16
  190. package/build-module/utils/observable-map/index.js.map +7 -1
  191. package/build-module/utils/throttle/index.js +8 -75
  192. package/build-module/utils/throttle/index.js.map +7 -1
  193. package/package.json +17 -10
  194. package/src/hooks/use-debounce/index.js +1 -1
  195. package/tsconfig.tsbuildinfo +1 -1
  196. package/build/higher-order/with-network-connectivity/index.native.js +0 -27
  197. package/build/higher-order/with-network-connectivity/index.native.js.map +0 -1
  198. package/build/higher-order/with-preferred-color-scheme/index.native.js +0 -37
  199. package/build/higher-order/with-preferred-color-scheme/index.native.js.map +0 -1
  200. package/build/hooks/use-constrained-tabbing/index.native.js +0 -20
  201. package/build/hooks/use-constrained-tabbing/index.native.js.map +0 -1
  202. package/build/hooks/use-focus-outside/index.native.js +0 -175
  203. package/build/hooks/use-focus-outside/index.native.js.map +0 -1
  204. package/build/hooks/use-keyboard-shortcut/index.native.js +0 -9
  205. package/build/hooks/use-keyboard-shortcut/index.native.js.map +0 -1
  206. package/build/hooks/use-network-connectivity/index.native.js +0 -58
  207. package/build/hooks/use-network-connectivity/index.native.js.map +0 -1
  208. package/build/hooks/use-preferred-color-scheme-style/index.native.js +0 -42
  209. package/build/hooks/use-preferred-color-scheme-style/index.native.js.map +0 -1
  210. package/build/hooks/use-resize-observer/index.native.js +0 -14
  211. package/build/hooks/use-resize-observer/index.native.js.map +0 -1
  212. package/build/hooks/use-resize-observer/legacy/index.native.js +0 -62
  213. package/build/hooks/use-resize-observer/legacy/index.native.js.map +0 -1
  214. package/build/index.native.js +0 -297
  215. package/build/index.native.js.map +0 -1
  216. package/build-module/higher-order/with-network-connectivity/index.native.js +0 -19
  217. package/build-module/higher-order/with-network-connectivity/index.native.js.map +0 -1
  218. package/build-module/higher-order/with-preferred-color-scheme/index.native.js +0 -29
  219. package/build-module/higher-order/with-preferred-color-scheme/index.native.js.map +0 -1
  220. package/build-module/hooks/use-constrained-tabbing/index.native.js +0 -13
  221. package/build-module/hooks/use-constrained-tabbing/index.native.js.map +0 -1
  222. package/build-module/hooks/use-focus-outside/index.native.js +0 -169
  223. package/build-module/hooks/use-focus-outside/index.native.js.map +0 -1
  224. package/build-module/hooks/use-keyboard-shortcut/index.native.js +0 -3
  225. package/build-module/hooks/use-keyboard-shortcut/index.native.js.map +0 -1
  226. package/build-module/hooks/use-network-connectivity/index.native.js +0 -52
  227. package/build-module/hooks/use-network-connectivity/index.native.js.map +0 -1
  228. package/build-module/hooks/use-preferred-color-scheme-style/index.native.js +0 -35
  229. package/build-module/hooks/use-preferred-color-scheme-style/index.native.js.map +0 -1
  230. package/build-module/hooks/use-resize-observer/index.native.js +0 -2
  231. package/build-module/hooks/use-resize-observer/index.native.js.map +0 -1
  232. package/build-module/hooks/use-resize-observer/legacy/index.native.js +0 -56
  233. package/build-module/hooks/use-resize-observer/legacy/index.native.js.map +0 -1
  234. package/build-module/index.native.js +0 -45
  235. package/build-module/index.native.js.map +0 -1
@@ -1,83 +1,69 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
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.default = useFixedWindowList;
7
- var _element = require("@wordpress/element");
8
- var _dom = require("@wordpress/dom");
9
- var _keycodes = require("@wordpress/keycodes");
10
- var _debounce = require("../../utils/debounce");
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
- var _options$initWindowSi, _options$useWindowing;
49
- const initWindowSize = (_options$initWindowSi = options?.initWindowSize) !== null && _options$initWindowSi !== void 0 ? _options$initWindowSi : DEFAULT_INIT_WINDOW_SIZE;
50
- const useWindowing = (_options$useWindowing = options?.useWindowing) !== null && _options$useWindowing !== void 0 ? _options$useWindowing : true;
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: (/** @type {number} */index) => {
36
+ itemInView: (index) => {
56
37
  return index >= 0 && index <= initWindowSize;
57
38
  }
58
39
  });
59
- (0, _element.useLayoutEffect)(() => {
40
+ (0, import_element.useLayoutEffect)(() => {
60
41
  if (!useWindowing) {
61
42
  return;
62
43
  }
63
- const scrollContainer = (0, _dom.getScrollContainer)(elementRef.current);
64
- const measureWindow = (/** @type {boolean | undefined} */initRender) => {
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(scrollContainer.clientHeight / itemHeight);
70
- // Aim to keep opening list view fast, afterward we can optimize for scrolling.
71
- const windowOverscan = initRender ? visibleItems : (_options$windowOversc = options?.windowOverscan) !== null && _options$windowOversc !== void 0 ? _options$windowOversc : visibleItems;
72
- const firstViewableIndex = Math.floor(scrollContainer.scrollTop / itemHeight);
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(totalItems - 1, firstViewableIndex + visibleItems + windowOverscan);
75
- setFixedListWindow(lastWindow => {
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: (/** @type {number} */index) => {
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, _debounce.debounce)(() => {
77
+ const debounceMeasureList = (0, import_debounce.debounce)(() => {
92
78
  measureWindow();
93
79
  }, 16);
94
- scrollContainer?.addEventListener('scroll', debounceMeasureList);
95
- scrollContainer?.ownerDocument?.defaultView?.addEventListener('resize', debounceMeasureList);
96
- scrollContainer?.ownerDocument?.defaultView?.addEventListener('resize', debounceMeasureList);
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('scroll', debounceMeasureList);
99
- scrollContainer?.ownerDocument?.defaultView?.removeEventListener('resize', debounceMeasureList);
90
+ scrollContainer?.removeEventListener(
91
+ "scroll",
92
+ debounceMeasureList
93
+ );
94
+ scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
95
+ "resize",
96
+ debounceMeasureList
97
+ );
100
98
  };
101
- }, [itemHeight, elementRef, totalItems, options?.expandedState, options?.windowOverscan, useWindowing]);
102
- (0, _element.useLayoutEffect)(() => {
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, _dom.getScrollContainer)(elementRef.current);
107
- const handleKeyDown = (/** @type {KeyboardEvent} */event) => {
111
+ const scrollContainer = (0, import_dom.getScrollContainer)(elementRef.current);
112
+ const handleKeyDown = (event) => {
108
113
  switch (event.keyCode) {
109
- case _keycodes.HOME:
110
- {
111
- return scrollContainer?.scrollTo({
112
- top: 0
113
- });
114
- }
115
- case _keycodes.END:
116
- {
117
- return scrollContainer?.scrollTo({
118
- top: totalItems * itemHeight
119
- });
120
- }
121
- case _keycodes.PAGEUP:
122
- {
123
- return scrollContainer?.scrollTo({
124
- top: scrollContainer.scrollTop - fixedListWindow.visibleItems * itemHeight
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('keydown', handleKeyDown);
134
+ scrollContainer?.ownerDocument?.defaultView?.addEventListener(
135
+ "keydown",
136
+ handleKeyDown
137
+ );
136
138
  return () => {
137
- scrollContainer?.ownerDocument?.defaultView?.removeEventListener('keydown', handleKeyDown);
139
+ scrollContainer?.ownerDocument?.defaultView?.removeEventListener(
140
+ "keydown",
141
+ handleKeyDown
142
+ );
138
143
  };
139
- }, [totalItems, itemHeight, elementRef, fixedListWindow.visibleItems, useWindowing, options?.expandedState]);
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,IAAIA,KAAK,IAAIT,cAAc;IAC7C;EACD,CAAE,CAAC;EAEH,IAAAU,wBAAe,EAAE,MAAM;IACtB,IAAK,CAAET,YAAY,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AAG,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;AA4BlB,SAAR,mBACN,YACA,YACA,YACA,SACC;AACD,QAAM,iBAAiB,SAAS,kBAAkB;AAClD,QAAM,eAAe,SAAS,gBAAgB;AAE9C,QAAM,CAAE,iBAAiB,kBAAmB,QA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runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
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.default = useFocusOnMount;
8
- var _element = require("@wordpress/element");
9
- var _dom = require("@wordpress/dom");
10
- var _useRefEffect = _interopRequireDefault(require("../use-ref-effect"));
11
- /**
12
- * WordPress dependencies
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
- /** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */
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, _useRefEffect.default)(node => {
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((_node$ownerDocument$a = node.ownerDocument?.activeElement) !== null && _node$ownerDocument$a !== void 0 ? _node$ownerDocument$a : null)) {
55
+ if (node.contains(node.ownerDocument?.activeElement ?? null)) {
69
56
  return;
70
57
  }
71
- if (focusOnMountRef.current !== 'firstElement') {
58
+ if (focusOnMountRef.current !== "firstElement") {
72
59
  setFocus(node);
73
60
  return;
74
61
  }
75
62
  timerIdRef.current = setTimeout(() => {
76
- const firstTabbable = _dom.focus.tabbable.find(node)[0];
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
- {"version":3,"names":["_element","require","_dom","_useRefEffect","_interopRequireDefault","useFocusOnMount","focusOnMount","focusOnMountRef","useRef","setFocus","target","focus","preventScroll","timerIdRef","useEffect","current","useRefEffect","node","_node$ownerDocument$a","contains","ownerDocument","activeElement","setTimeout","firstTabbable","tabbable","find","clearTimeout"],"sources":["@wordpress/compose/src/hooks/use-focus-on-mount/index.js"],"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"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,IAAA,GAAAD,OAAA;AAKA,IAAAE,aAAA,GAAAC,sBAAA,CAAAH,OAAA;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASI,eAAeA,CAAEC,YAAY,GAAG,cAAc,EAAG;EACxE,MAAMC,eAAe,GAAG,IAAAC,eAAM,EAAEF,YAAa,CAAC;;EAE9C;AACD;AACA;AACA;AACA;AACA;EACC,MAAMG,QAAQ,GAAKC,MAAM,IAAM;IAC9BA,MAAM,CAACC,KAAK,CAAE;MACb;MACA;MACA;MACAC,aAAa,EAAE;IAChB,CAAE,CAAC;EACJ,CAAC;;EAED;EACA,MAAMC,UAAU,GAAG,IAAAL,eAAM,EAAC,CAAC;EAE3B,IAAAM,kBAAS,EAAE,MAAM;IAChBP,eAAe,CAACQ,OAAO,GAAGT,YAAY;EACvC,CAAC,EAAE,CAAEA,YAAY,CAAG,CAAC;EAErB,OAAO,IAAAU,qBAAY,EAAIC,IAAI,IAAM;IAAA,IAAAC,qBAAA;IAChC,IAAK,CAAED,IAAI,IAAIV,eAAe,CAACQ,OAAO,KAAK,KAAK,EAAG;MAClD;IACD;IAEA,IAAKE,IAAI,CAACE,QAAQ,EAAAD,qBAAA,GAAED,IAAI,CAACG,aAAa,EAAEC,aAAa,cAAAH,qBAAA,cAAAA,qBAAA,GAAI,IAAK,CAAC,EAAG;MACjE;IACD;IAEA,IAAKX,eAAe,CAACQ,OAAO,KAAK,cAAc,EAAG;MACjDN,QAAQ,CAAEQ,IAAK,CAAC;MAChB;IACD;IAEAJ,UAAU,CAACE,OAAO,GAAGO,UAAU,CAAE,MAAM;MACtC,MAAMC,aAAa,GAAGZ,UAAK,CAACa,QAAQ,CAACC,IAAI,CAAER,IAAK,CAAC,CAAE,CAAC,CAAE;MACtD,IAAKM,aAAa,EAAG;QACpBd,QAAQ,CAAEc,aAAc,CAAC;MAC1B;IACD,CAAC,EAAE,CAAE,CAAC;IAEN,OAAO,MAAM;MACZ,IAAKV,UAAU,CAACE,OAAO,EAAG;QACzBW,YAAY,CAAEb,UAAU,CAACE,OAAQ,CAAC;MACnC;IACD,CAAC;EACF,CAAC,EAAE,EAAG,CAAC;AACR","ignoreList":[]}
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.defineProperty(exports, "__esModule", {
4
- value: true
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.default = useFocusOutside;
7
- var _element = require("@wordpress/element");
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 'A':
40
- case 'BUTTON':
31
+ case "A":
32
+ case "BUTTON":
41
33
  return true;
42
- case 'INPUT':
43
- return INPUT_BUTTON_TYPES.includes(eventTarget.type);
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, _element.useRef)(onFocusOutside);
59
- (0, _element.useEffect)(() => {
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, _element.useRef)(false);
63
- const blurCheckTimeoutIdRef = (0, _element.useRef)();
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
- * Handles a mousedown or mouseup event to respectively assign and
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
- // The usage of this attribute should be avoided. The only use case
120
- // would be when we load modals that are not React components and
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 ('function' === typeof currentOnFocusOutsideRef.current) {
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