@wordpress/compose 8.1.1 → 8.2.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 (109) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +42 -38
  3. package/build/hooks/use-constrained-tabbing/index.cjs +3 -9
  4. package/build/hooks/use-constrained-tabbing/index.cjs.map +3 -3
  5. package/build/hooks/use-debounce/index.cjs +1 -1
  6. package/build/hooks/use-debounce/index.cjs.map +3 -3
  7. package/build/hooks/use-disabled/index.cjs.map +2 -2
  8. package/build/hooks/use-dragging/index.cjs +27 -22
  9. package/build/hooks/use-dragging/index.cjs.map +4 -4
  10. package/build/hooks/use-dragging/types.cjs +19 -0
  11. package/build/hooks/use-dragging/types.cjs.map +7 -0
  12. package/build/hooks/use-drop-zone/index.cjs +2 -5
  13. package/build/hooks/use-drop-zone/index.cjs.map +3 -3
  14. package/build/hooks/use-drop-zone/types.cjs +19 -0
  15. package/build/hooks/use-drop-zone/types.cjs.map +7 -0
  16. package/build/hooks/use-fixed-window-list/index.cjs +10 -8
  17. package/build/hooks/use-fixed-window-list/index.cjs.map +3 -3
  18. package/build/hooks/use-focus-return/index.cjs +12 -9
  19. package/build/hooks/use-focus-return/index.cjs.map +3 -3
  20. package/build/hooks/use-isomorphic-layout-effect/index.cjs +1 -1
  21. package/build/hooks/use-isomorphic-layout-effect/index.cjs.map +1 -1
  22. package/build/hooks/use-keyboard-shortcut/index.cjs +1 -4
  23. package/build/hooks/use-keyboard-shortcut/index.cjs.map +3 -3
  24. package/build/hooks/use-reduced-motion/index.cjs +1 -1
  25. package/build/hooks/use-reduced-motion/index.cjs.map +2 -2
  26. package/build/hooks/use-throttle/index.cjs +1 -1
  27. package/build/hooks/use-throttle/index.cjs.map +3 -3
  28. package/build/hooks/use-viewport-match/index.cjs +2 -5
  29. package/build/hooks/use-viewport-match/index.cjs.map +3 -3
  30. package/build/hooks/use-warn-on-change/index.cjs +3 -10
  31. package/build/hooks/use-warn-on-change/index.cjs.map +3 -3
  32. package/build/utils/subscribe-delegated-listener/index.cjs.map +1 -1
  33. package/build-module/hooks/use-constrained-tabbing/index.mjs +3 -9
  34. package/build-module/hooks/use-constrained-tabbing/index.mjs.map +3 -3
  35. package/build-module/hooks/use-debounce/index.mjs +1 -1
  36. package/build-module/hooks/use-debounce/index.mjs.map +3 -3
  37. package/build-module/hooks/use-disabled/index.mjs.map +2 -2
  38. package/build-module/hooks/use-dragging/index.mjs +27 -22
  39. package/build-module/hooks/use-dragging/index.mjs.map +4 -4
  40. package/build-module/hooks/use-dragging/types.mjs +1 -0
  41. package/build-module/hooks/use-dragging/types.mjs.map +7 -0
  42. package/build-module/hooks/use-drop-zone/index.mjs +2 -5
  43. package/build-module/hooks/use-drop-zone/index.mjs.map +3 -3
  44. package/build-module/hooks/use-drop-zone/types.mjs +1 -0
  45. package/build-module/hooks/use-drop-zone/types.mjs.map +7 -0
  46. package/build-module/hooks/use-fixed-window-list/index.mjs +10 -8
  47. package/build-module/hooks/use-fixed-window-list/index.mjs.map +3 -3
  48. package/build-module/hooks/use-focus-return/index.mjs +12 -9
  49. package/build-module/hooks/use-focus-return/index.mjs.map +3 -3
  50. package/build-module/hooks/use-isomorphic-layout-effect/index.mjs +1 -1
  51. package/build-module/hooks/use-isomorphic-layout-effect/index.mjs.map +1 -1
  52. package/build-module/hooks/use-keyboard-shortcut/index.mjs +1 -4
  53. package/build-module/hooks/use-keyboard-shortcut/index.mjs.map +3 -3
  54. package/build-module/hooks/use-reduced-motion/index.mjs +1 -1
  55. package/build-module/hooks/use-reduced-motion/index.mjs.map +2 -2
  56. package/build-module/hooks/use-throttle/index.mjs +1 -1
  57. package/build-module/hooks/use-throttle/index.mjs.map +3 -3
  58. package/build-module/hooks/use-viewport-match/index.mjs +2 -5
  59. package/build-module/hooks/use-viewport-match/index.mjs.map +3 -3
  60. package/build-module/hooks/use-warn-on-change/index.mjs +3 -10
  61. package/build-module/hooks/use-warn-on-change/index.mjs.map +3 -3
  62. package/build-module/utils/subscribe-delegated-listener/index.mjs.map +1 -1
  63. package/build-types/hooks/use-constrained-tabbing/index.d.ts +2 -2
  64. package/build-types/hooks/use-constrained-tabbing/index.d.ts.map +1 -1
  65. package/build-types/hooks/use-debounce/index.d.ts +7 -6
  66. package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
  67. package/build-types/hooks/use-disabled/index.d.ts +1 -1
  68. package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
  69. package/build-types/hooks/use-dragging/index.d.ts +9 -13
  70. package/build-types/hooks/use-dragging/index.d.ts.map +1 -1
  71. package/build-types/hooks/use-dragging/types.d.ts +11 -0
  72. package/build-types/hooks/use-dragging/types.d.ts.map +1 -0
  73. package/build-types/hooks/use-drop-zone/index.d.ts +4 -21
  74. package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
  75. package/build-types/hooks/use-drop-zone/types.d.ts +35 -0
  76. package/build-types/hooks/use-drop-zone/types.d.ts.map +1 -0
  77. package/build-types/hooks/use-fixed-window-list/index.d.ts +22 -26
  78. package/build-types/hooks/use-fixed-window-list/index.d.ts.map +1 -1
  79. package/build-types/hooks/use-focus-return/index.d.ts +3 -3
  80. package/build-types/hooks/use-focus-return/index.d.ts.map +1 -1
  81. package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
  82. package/build-types/hooks/use-keyboard-shortcut/index.d.ts +29 -23
  83. package/build-types/hooks/use-keyboard-shortcut/index.d.ts.map +1 -1
  84. package/build-types/hooks/use-reduced-motion/index.d.ts +1 -1
  85. package/build-types/hooks/use-reduced-motion/index.d.ts.map +1 -1
  86. package/build-types/hooks/use-throttle/index.d.ts +8 -6
  87. package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
  88. package/build-types/hooks/use-viewport-match/index.d.ts +3 -3
  89. package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
  90. package/build-types/hooks/use-warn-on-change/index.d.ts +7 -7
  91. package/build-types/hooks/use-warn-on-change/index.d.ts.map +1 -1
  92. package/package.json +16 -11
  93. package/src/hooks/use-constrained-tabbing/{index.js → index.ts} +7 -11
  94. package/src/hooks/use-debounce/{index.js → index.ts} +11 -6
  95. package/src/hooks/use-disabled/index.ts +3 -1
  96. package/src/hooks/use-dragging/index.ts +78 -0
  97. package/src/hooks/use-dragging/types.ts +11 -0
  98. package/src/hooks/use-drop-zone/{index.js → index.ts} +17 -31
  99. package/src/hooks/use-drop-zone/types.ts +34 -0
  100. package/src/hooks/use-fixed-window-list/{index.js → index.ts} +46 -37
  101. package/src/hooks/use-focus-return/{index.js → index.ts} +26 -21
  102. package/src/hooks/use-keyboard-shortcut/{index.js → index.ts} +43 -28
  103. package/src/hooks/use-reduced-motion/{index.js → index.ts} +2 -2
  104. package/src/hooks/use-throttle/{index.js → index.ts} +12 -6
  105. package/src/hooks/use-viewport-match/{index.js → index.ts} +30 -29
  106. package/src/hooks/use-warn-on-change/index.ts +42 -0
  107. package/src/hooks/use-dragging/index.js +0 -74
  108. package/src/hooks/use-warn-on-change/index.js +0 -43
  109. /package/src/hooks/use-isomorphic-layout-effect/{index.js → index.ts} +0 -0
@@ -0,0 +1,11 @@
1
+ export interface DraggingProps {
2
+ onDragStart?: (e: React.MouseEvent) => void;
3
+ onDragMove?: (e: MouseEvent) => void;
4
+ onDragEnd?: (e?: MouseEvent) => void;
5
+ }
6
+ export interface DraggingReturn {
7
+ startDrag: (e: React.MouseEvent) => void;
8
+ endDrag: (e?: MouseEvent) => void;
9
+ isDragging: boolean;
10
+ }
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-dragging/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC7B,WAAW,CAAC,EAAE,CAAE,CAAC,EAAE,KAAK,CAAC,UAAU,KAAM,IAAI,CAAC;IAC9C,UAAU,CAAC,EAAE,CAAE,CAAC,EAAE,UAAU,KAAM,IAAI,CAAC;IACvC,SAAS,CAAC,EAAE,CAAE,CAAC,CAAC,EAAE,UAAU,KAAM,IAAI,CAAC;CACvC;AAED,MAAM,WAAW,cAAc;IAC9B,SAAS,EAAE,CAAE,CAAC,EAAE,KAAK,CAAC,UAAU,KAAM,IAAI,CAAC;IAC3C,OAAO,EAAE,CAAE,CAAC,CAAC,EAAE,UAAU,KAAM,IAAI,CAAC;IACpC,UAAU,EAAE,OAAO,CAAC;CACpB"}
@@ -1,26 +1,9 @@
1
+ import type { UseDropZoneProps } from './types';
1
2
  /**
2
3
  * A hook to facilitate drag and drop handling.
3
4
  *
4
- * @param {Object} props Named parameters.
5
- * @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.
6
- * @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.
7
- * @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.
8
- * @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.
9
- * @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.
10
- * @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.
11
- * @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.
12
- * @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.
13
- *
14
- * @return {React.RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.
5
+ * @param {UseDropZoneProps} props Hook options
6
+ * @return Ref callback to be passed to the drop zone element.
15
7
  */
16
- export default function useDropZone({ dropZoneElement, isDisabled, onDrop: _onDrop, onDragStart: _onDragStart, onDragEnter: _onDragEnter, onDragLeave: _onDragLeave, onDragEnd: _onDragEnd, onDragOver: _onDragOver }: {
17
- dropZoneElement?: HTMLElement | null;
18
- isDisabled?: boolean;
19
- onDragStart?: (e: DragEvent) => void;
20
- onDragEnter?: (e: DragEvent) => void;
21
- onDragOver?: (e: DragEvent) => void;
22
- onDragLeave?: (e: DragEvent) => void;
23
- onDragEnd?: (e: MouseEvent) => void;
24
- onDrop?: (e: DragEvent) => void;
25
- }): React.RefCallback<HTMLElement>;
8
+ export default function useDropZone({ dropZoneElement, isDisabled, onDrop: _onDrop, onDragStart: _onDragStart, onDragEnter: _onDragEnter, onDragLeave: _onDragLeave, onDragEnd: _onDragEnd, onDragOver: _onDragOver }: UseDropZoneProps): React.RefCallback<HTMLElement>;
26
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-drop-zone/index.js"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,EACpC,eAAe,EACf,UAAU,EACV,MAAM,EAAE,OAAO,EACf,WAAW,EAAE,YAAY,EACzB,WAAW,EAAE,YAAY,EACzB,WAAW,EAAE,YAAY,EACzB,SAAS,EAAE,UAAU,EACrB,UAAU,EAAE,WAAW,EACvB,EApBE;IAAwC,eAAe,AAAvD,CACA,EADS,WAAW,OAAA,CACpB;IAAwC,UAAU,AAAlD,CACA,EADQ,OAAO,CACf;IAAwC,WAAW,AAAnD,CACA,EADQ,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAC9B;IAAwC,WAAW,AAAnD,CACA,EADQ,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAC9B;IAAwC,UAAU,AAAlD,CACA,EADQ,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAC9B;IAAwC,WAAW,AAAnD,CACA,EADQ,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAC9B;IAAwC,SAAS,AAAjD,CACA,EADQ,CAAC,CAAC,EAAE,UAAU,KAAK,IAAI,CAC/B;IAAwC,MAAM,AAA9C,CAEA,EAFQ,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAE9B;CAWF,GAXW,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAoMzC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-drop-zone/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,EACpC,eAAe,EACf,UAAU,EACV,MAAM,EAAE,OAAO,EACf,WAAW,EAAE,YAAY,EACzB,WAAW,EAAE,YAAY,EACzB,WAAW,EAAE,YAAY,EACzB,SAAS,EAAE,UAAU,EACrB,UAAU,EAAE,WAAW,EACvB,EAAE,gBAAgB,GAAI,KAAK,CAAC,WAAW,CAAE,WAAW,CAAE,CAmLtD"}
@@ -0,0 +1,35 @@
1
+ export interface UseDropZoneProps {
2
+ /**
3
+ * Optional element to be used as the drop zone.
4
+ */
5
+ dropZoneElement?: HTMLElement | null;
6
+ /**
7
+ * Whether or not to disable the drop zone.
8
+ */
9
+ isDisabled?: boolean;
10
+ /**
11
+ * Called when dragging has started.
12
+ */
13
+ onDragStart?: (e: DragEvent) => void;
14
+ /**
15
+ * Called when the zone is entered.
16
+ */
17
+ onDragEnter?: (e: DragEvent) => void;
18
+ /**
19
+ * Called when the zone is moved within.
20
+ */
21
+ onDragOver?: (e: DragEvent) => void;
22
+ /**
23
+ * Called when the zone is left.
24
+ */
25
+ onDragLeave?: (e: DragEvent) => void;
26
+ /**
27
+ * Called when dragging has ended.
28
+ */
29
+ onDragEnd?: (e: MouseEvent) => void;
30
+ /**
31
+ * Called when dropping in the zone.
32
+ */
33
+ onDrop?: (e: DragEvent) => void;
34
+ }
35
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-drop-zone/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAChC;;OAEG;IACH,eAAe,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IACrC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;OAEG;IACH,WAAW,CAAC,EAAE,CAAE,CAAC,EAAE,SAAS,KAAM,IAAI,CAAC;IACvC;;OAEG;IACH,WAAW,CAAC,EAAE,CAAE,CAAC,EAAE,SAAS,KAAM,IAAI,CAAC;IACvC;;OAEG;IACH,UAAU,CAAC,EAAE,CAAE,CAAC,EAAE,SAAS,KAAM,IAAI,CAAC;IACtC;;OAEG;IACH,WAAW,CAAC,EAAE,CAAE,CAAC,EAAE,SAAS,KAAM,IAAI,CAAC;IACvC;;OAEG;IACH,SAAS,CAAC,EAAE,CAAE,CAAC,EAAE,UAAU,KAAM,IAAI,CAAC;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAE,CAAC,EAAE,SAAS,KAAM,IAAI,CAAC;CAClC"}
@@ -1,38 +1,34 @@
1
- export type WPFixedWindowList = {
1
+ interface FixedWindowList {
2
+ /** Items visible in the current viewport */
2
3
  visibleItems: number;
4
+ /** Start index of the window */
3
5
  start: number;
6
+ /** End index of the window */
4
7
  end: number;
8
+ /** Returns true if item is in the window */
5
9
  itemInView: (index: number) => boolean;
6
- };
7
- export type WPFixedWindowListOptions = {
10
+ }
11
+ interface FixedWindowListOptions {
12
+ /** Renders windowOverscan number of items before and after the calculated visible window. */
8
13
  windowOverscan?: number;
14
+ /** When false avoids calculating the window size */
9
15
  useWindowing?: boolean;
16
+ /** Initial window size to use on first render before we can calculate the window size. */
10
17
  initWindowSize?: number;
18
+ /** Used to recalculate the window size when the expanded state of a list changes. */
11
19
  expandedState?: any;
12
- };
20
+ }
13
21
  /**
14
- * @typedef {Object} WPFixedWindowList
15
22
  *
16
- * @property {number} visibleItems Items visible in the current viewport
17
- * @property {number} start Start index of the window
18
- * @property {number} end End index of the window
19
- * @property {(index:number)=>boolean} itemInView Returns true if item is in the window
23
+ * @param elementRef Used to find the closest scroll container that contains element.
24
+ * @param itemHeight Fixed item height in pixels
25
+ * @param totalItems Total items in list
26
+ * @param [options] Options object
27
+ * @return Array with the fixed window list and setter
20
28
  */
21
- /**
22
- * @typedef {Object} WPFixedWindowListOptions
23
- *
24
- * @property {number} [windowOverscan] Renders windowOverscan number of items before and after the calculated visible window.
25
- * @property {boolean} [useWindowing] When false avoids calculating the window size
26
- * @property {number} [initWindowSize] Initial window size to use on first render before we can calculate the window size.
27
- * @property {any} [expandedState] Used to recalculate the window size when the expanded state of a list changes.
28
- */
29
- /**
30
- *
31
- * @param {React.RefObject<HTMLElement>} elementRef Used to find the closest scroll container that contains element.
32
- * @param { number } itemHeight Fixed item height in pixels
33
- * @param { number } totalItems Total items in list
34
- * @param { WPFixedWindowListOptions } [options] Options object
35
- * @return {[ WPFixedWindowList, setFixedListWindow:(nextWindow:WPFixedWindowList)=>void]} Array with the fixed window list and setter
36
- */
37
- export default function useFixedWindowList(elementRef: React.RefObject<HTMLElement>, itemHeight: number, totalItems: number, options?: WPFixedWindowListOptions): [WPFixedWindowList, setFixedListWindow: (nextWindow: WPFixedWindowList) => void];
29
+ export default function useFixedWindowList(elementRef: React.RefObject<HTMLElement>, itemHeight: number, totalItems: number, options?: FixedWindowListOptions): [
30
+ FixedWindowList,
31
+ React.Dispatch<React.SetStateAction<FixedWindowList>>
32
+ ];
33
+ export {};
38
34
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-fixed-window-list/index.js"],"names":[],"mappings":"AAeG,YAAkB,iBAAiB,GAEnC;IAAoC,YAAY,EAArC,MAAM,CACjB;IAAoC,KAAK,EAA9B,MAAM,CACjB;IAAoC,GAAG,EAA5B,MAAM,CACjB;IAAoC,UAAU,EAAnC,CAAC,KAAK,EAAC,MAAM,KAAG,OAAO,CACpC;CAAA,CAAA;AAGE,YAAkB,wBAAwB,GAE1C;IAAqB,cAAc,AAAnC,CACA,EADW,MAAM,CACjB;IAAqB,YAAY,AAAjC,CACA,EADW,OAAO,CAClB;IAAqB,cAAc,AAAnC,CACA,EADW,MAAM,CACjB;IAAqB,aAAa,AAAlC,CACF,EADa,GAAG,CAChB;CAAA,CAAA;AAhBD;;;;;;;GAOG;AAEH;;;;;;;GAOG;AAEH;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CACzC,UAAU,EAPA,KAAK,CAAC,SAAS,CAAC,WAAW,CAO3B,EACV,UAAU,EAPC,MAOD,EACV,UAAU,EAPC,MAOD,EACV,OAAO,AAPL,CACA,EADS,wBAOJ,GANI,CAAE,iBAAiB,EAAE,kBAAkB,EAAC,CAAC,UAAU,EAAC,iBAAiB,KAAG,IAAI,CAAC,CA0JxF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-fixed-window-list/index.ts"],"names":[],"mappings":"AAcA,UAAU,eAAe;IACxB,4CAA4C;IAC5C,YAAY,EAAE,MAAM,CAAC;IACrB,gCAAgC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,8BAA8B;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,4CAA4C;IAC5C,UAAU,EAAE,CAAE,KAAK,EAAE,MAAM,KAAM,OAAO,CAAC;CACzC;AAED,UAAU,sBAAsB;IAC/B,6FAA6F;IAC7F,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,oDAAoD;IACpD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,0FAA0F;IAC1F,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qFAAqF;IACrF,aAAa,CAAC,EAAE,GAAG,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CACzC,UAAU,EAAE,KAAK,CAAC,SAAS,CAAE,WAAW,CAAE,EAC1C,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,sBAAsB,GAC9B;IACF,eAAe;IACf,KAAK,CAAC,QAAQ,CAAE,KAAK,CAAC,cAAc,CAAE,eAAe,CAAE,CAAE;CACzD,CAqJA"}
@@ -2,11 +2,11 @@
2
2
  * Adds the unmount behavior of returning focus to the element which had it
3
3
  * previously as is expected for roles like menus or dialogs.
4
4
  *
5
- * @param {() => void} [onFocusReturn] Overrides the default return behavior.
6
- * @return {React.RefCallback<HTMLElement>} Element Ref.
5
+ * @param onFocusReturn Overrides the default return behavior.
6
+ * @return Element Ref.
7
7
  *
8
8
  * @example
9
- * ```js
9
+ * ```ts
10
10
  * import { useFocusReturn } from '@wordpress/compose';
11
11
  *
12
12
  * const WithFocusReturn = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-focus-return/index.js"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,iBAAS,cAAc,CAAE,aAAa,AAlBnC,CACA,EADQ,MAAM,IAkBqB,GAjB1B,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAsEzC;eAEc,cAAc"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-focus-return/index.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,iBAAS,cAAc,CACtB,aAAa,CAAC,EAAE,MAAM,IAAI,GACxB,KAAK,CAAC,WAAW,CAAE,WAAW,CAAE,CAyDlC;eAEc,cAAc"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-isomorphic-layout-effect/index.js"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAa,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEhE;;;;GAIG;AACH,QAAA,MAAM,yBAAyB,wBAC6B,CAAC;eAE9C,yBAAyB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-isomorphic-layout-effect/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAa,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEhE;;;;GAIG;AACH,QAAA,MAAM,yBAAyB,wBAC6B,CAAC;eAE9C,yBAAyB"}
@@ -1,34 +1,40 @@
1
- /**
2
- * External dependencies
3
- */
4
- import Mousetrap from 'mousetrap';
5
1
  import 'mousetrap/plugins/global-bind/mousetrap-global-bind.js';
6
- export type WPKeyboardShortcutConfig = {
7
- bindGlobal?: boolean;
8
- eventName?: string;
9
- isDisabled?: boolean;
10
- target?: React.RefObject<HTMLElement | null>;
11
- };
2
+ import type { ExtendedKeyboardEvent } from 'mousetrap';
12
3
  /**
13
- * A block selection object.
14
- *
15
- * @typedef {Object} WPKeyboardShortcutConfig
16
- *
17
- * @property {boolean} [bindGlobal] Handle keyboard events anywhere including inside textarea/input fields.
18
- * @property {string} [eventName] Event name used to trigger the handler, defaults to keydown.
19
- * @property {boolean} [isDisabled] Disables the keyboard handler if the value is true.
20
- * @property {React.RefObject<HTMLElement | null>} [target] React reference to the DOM element used to catch the keyboard event.
4
+ * WordPress dependencies
21
5
  */
6
+ import type { RefObject } from '@wordpress/element';
7
+ type KeyboardShortcutConfig = {
8
+ /**
9
+ * Handle keyboard events anywhere including inside textarea/input fields.
10
+ */
11
+ bindGlobal: boolean;
12
+ /**
13
+ * Event name used to trigger the handler, defaults to keydown.
14
+ */
15
+ eventName: string;
16
+ /**
17
+ * Disables the keyboard handler if the value is true.
18
+ */
19
+ isDisabled: boolean;
20
+ /**
21
+ * React reference to the DOM element used to catch the keyboard event.
22
+ */
23
+ target: RefObject<HTMLElement>;
24
+ };
22
25
  /**
23
26
  * Attach a keyboard shortcut handler.
24
27
  *
25
28
  * @see https://craig.is/killing/mice#api.bind for information about the `callback` parameter.
26
29
  *
27
- * @param {string[]|string} shortcuts Keyboard Shortcuts.
28
- * @param {(e: Mousetrap.ExtendedKeyboardEvent, combo: string) => void} callback Shortcut callback.
29
- * @param {WPKeyboardShortcutConfig} options Shortcut options.
30
+ * @param shortcuts Keyboard Shortcuts.
31
+ * @param callback Shortcut callback.
32
+ * @param options Shortcut options.
33
+ * @param options.bindGlobal
34
+ * @param options.eventName
35
+ * @param options.isDisabled
36
+ * @param options.target
30
37
  */
31
- declare function useKeyboardShortcut(shortcuts: string[] | string, callback: (e: Mousetrap.ExtendedKeyboardEvent, combo: string) => void, { bindGlobal, eventName, isDisabled, // This is important for performance considerations.
32
- target }?: WPKeyboardShortcutConfig): void;
38
+ declare function useKeyboardShortcut(shortcuts: string[] | string, callback: (e: ExtendedKeyboardEvent, combo: string) => void, { bindGlobal, eventName, isDisabled, target }?: Partial<KeyboardShortcutConfig>): void;
33
39
  export default useKeyboardShortcut;
34
40
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-keyboard-shortcut/index.js"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,wDAAwD,CAAC;AAW7D,YAAkB,wBAAwB,GAE1C;IAAiD,UAAU,AAA3D,CACA,EADW,OAAO,CAClB;IAAiD,SAAS,AAA1D,CACA,EADW,MAAM,CACjB;IAAiD,UAAU,AAA3D,CACA,EADW,OAAO,CAClB;IAAiD,MAAM,AAAvD,CACF,EADa,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAChD;CAAA,CAAA;AATD;;;;;;;;;GASG;AAEH;;;;;;;;GAQG;AACH,iBAAS,mBAAmB,CAC3B,SAAS,EALC,MAAM,EAAE,GAAC,MAKV,EACT,QAAQ,EALE,CAAC,CAAC,EAAE,SAAS,CAAC,qBAAqB,EAAE,KAAK,EAAE,MAAM,KAAK,IAKzD,EACR,EACC,UAAkB,EAClB,SAAqB,EACrB,UAAkB,EAAE,oDAAoD;AACxE,MAAM,EACN,GAVS,wBAUJ,QA2DN;eAEc,mBAAmB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-keyboard-shortcut/index.ts"],"names":[],"mappings":"AAIA,OAAO,wDAAwD,CAAC;AAChE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAEvD;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAIpD,KAAK,sBAAsB,GAAG;IAC7B;;OAEG;IACH,UAAU,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,UAAU,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,MAAM,EAAE,SAAS,CAAE,WAAW,CAAE,CAAC;CACjC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,iBAAS,mBAAmB,CAC3B,SAAS,EAAE,MAAM,EAAE,GAAG,MAAM,EAC5B,QAAQ,EAAE,CAAE,CAAC,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,KAAM,IAAI,EAC7D,EACC,UAAkB,EAClB,SAAqB,EACrB,UAAkB,EAClB,MAAM,EACN,GAAE,OAAO,CAAE,sBAAsB,CAAO,QA4DzC;eAEc,mBAAmB"}
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Hook returning whether the user has a preference for reduced motion.
3
3
  *
4
- * @return {boolean} Reduced motion preference value.
4
+ * @return Reduced motion preference value.
5
5
  */
6
6
  declare const useReducedMotion: () => boolean;
7
7
  export default useReducedMotion;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-reduced-motion/index.js"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,QAAA,MAAM,gBAAgB,QAFV,OAGwC,CAAC;eAEtC,gBAAgB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-reduced-motion/index.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,QAAA,MAAM,gBAAgB,QAAO,OACuB,CAAC;eAEtC,gBAAgB"}
@@ -1,3 +1,5 @@
1
+ import type { ThrottleOptions } from '../../utils/throttle';
2
+ import type { DebouncedFunc } from '../../utils/debounce';
1
3
  /**
2
4
  * Throttles a function similar to Lodash's `throttle`. A new throttled function will
3
5
  * be returned and any scheduled calls cancelled if any of the arguments change,
@@ -6,12 +8,12 @@
6
8
  *
7
9
  * @see https://lodash.com/docs/4#throttle
8
10
  *
9
- * @template {(...args: any[]) => void} TFunc
11
+ * @template TFunc
10
12
  *
11
- * @param {TFunc} fn The function to throttle.
12
- * @param {number} [wait] The number of milliseconds to throttle invocations to.
13
- * @param {import('../../utils/throttle').ThrottleOptions} [options] The options object. See linked documentation for details.
14
- * @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Throttled function.
13
+ * @param fn The function to throttle.
14
+ * @param wait The number of milliseconds to throttle invocations to.
15
+ * @param options The options object.
16
+ * @return Throttled function.
15
17
  */
16
- export default function useThrottle<TFunc extends (...args: any[]) => void>(fn: TFunc, wait?: number, options?: import('../../utils/throttle').ThrottleOptions): import('../../utils/debounce').DebouncedFunc<TFunc>;
18
+ export default function useThrottle<TFunc extends (...args: any[]) => void>(fn: TFunc, wait?: number, options?: ThrottleOptions): DebouncedFunc<TFunc>;
17
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-throttle/index.js"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAPK,KAAK,SAA/B,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAW,EAOR,EAAE,EAL5B,KAK4B,EAAE,IAAI,AAJ1C,CACA,EADQ,MAIkC,EAAE,OAAO,AAHnD,CACA,EADQ,OAAO,sBAAsB,EAAE,eAGY,GAF1C,OAAO,sBAAsB,EAAE,aAAa,CAAC,KAAK,CAAC,CAS9D"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-throttle/index.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,KAAK,SAAS,CAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAM,IAAI,EAC5E,EAAE,EAAE,KAAK,EACT,IAAI,CAAC,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACvB,aAAa,CAAE,KAAK,CAAE,CAOxB"}
@@ -1,6 +1,6 @@
1
- export type WPBreakpoint = "xhuge" | "huge" | "wide" | "xlarge" | "large" | "medium" | "small" | "mobile";
2
- export type WPViewportOperator = ">=" | "<";
3
- declare function useViewportMatch(breakpoint: WPBreakpoint, operator?: WPViewportOperator, view?: Window | undefined): boolean;
1
+ type Breakpoint = 'xhuge' | 'huge' | 'wide' | 'xlarge' | 'large' | 'medium' | 'small' | 'mobile';
2
+ type ViewportOperator = '>=' | '<';
3
+ declare function useViewportMatch(breakpoint: Breakpoint, operator?: ViewportOperator, view?: Window | undefined): boolean;
4
4
  declare namespace useViewportMatch {
5
5
  var __experimentalWidthProvider: import("react").Provider<number | null>;
6
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-viewport-match/index.js"],"names":[],"mappings":"AAWG,YAA0F,YAAY,GAA5F,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAc;AAsBtG,YAAsB,kBAAkB,GAA9B,IAAI,GAAG,GAAG,CAAoB;kCA6C1C,UAAU,EAdA,YAcA,EACV,QAAQ,GAdE,kBAcK,EAGf,IAAI,GAhBM,MAAM,GAAC,SAgBwC,GAP9C,OAAO;;;;eA0BJ,gBAAgB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-viewport-match/index.ts"],"names":[],"mappings":"AAUA,KAAK,UAAU,GACZ,OAAO,GACP,MAAM,GACN,MAAM,GACN,QAAQ,GACR,OAAO,GACP,QAAQ,GACR,OAAO,GACP,QAAQ,CAAC;AACZ,KAAK,gBAAgB,GAAG,IAAI,GAAG,GAAG,CAAC;kCAyDlC,UAAU,EAAE,UAAU,EACtB,QAAQ,GAAE,gBAAuB,EAGjC,IAAI,GAAE,MAAM,GAAG,SAEH,GACV,OAAO;;;;eAmBK,gBAAgB"}
@@ -1,21 +1,21 @@
1
1
  /**
2
- * Hook that performs a shallow comparison between the preview value of an object
2
+ * Hook that performs a shallow comparison between the previous value of an object
3
3
  * and the new one, if there's a difference, it prints it to the console.
4
- * this is useful in performance related work, to check why a component re-renders.
4
+ * This is useful in performance related work, to check why a component re-renders.
5
5
  *
6
6
  * @example
7
7
  *
8
- * ```jsx
9
- * function MyComponent(props) {
8
+ * ```tsx
9
+ * function MyComponent(props: Record<string, any>) {
10
10
  * useWarnOnChange(props);
11
11
  *
12
12
  * return "Something";
13
13
  * }
14
14
  * ```
15
15
  *
16
- * @param {object} object Object which changes to compare.
17
- * @param {string} prefix Just a prefix to show when console logging.
16
+ * @param object Object which changes to compare.
17
+ * @param prefix Just a prefix to show when console logging.
18
18
  */
19
- declare function useWarnOnChange(object: object, prefix?: string): void;
19
+ declare function useWarnOnChange(object: Record<string, any> | any[], prefix?: string): void;
20
20
  export default useWarnOnChange;
21
21
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-warn-on-change/index.js"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAS,eAAe,CAAE,MAAM,EAHrB,MAGqB,EAAE,MAAM,GAF7B,MAEkD,QAc5D;eAEc,eAAe"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-warn-on-change/index.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAS,eAAe,CACvB,MAAM,EAAE,MAAM,CAAE,MAAM,EAAE,GAAG,CAAE,GAAG,GAAG,EAAE,EACrC,MAAM,GAAE,MAA2B,GACjC,IAAI,CAaN;eAEc,eAAe"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/compose",
3
- "version": "8.1.1",
3
+ "version": "8.2.0",
4
4
  "description": "WordPress higher-order components (HOCs).",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -46,15 +46,14 @@
46
46
  "sideEffects": false,
47
47
  "dependencies": {
48
48
  "@types/mousetrap": "^1.6.8",
49
- "@types/react": "^18.3.27",
50
- "@wordpress/deprecated": "^4.48.1",
51
- "@wordpress/dom": "^4.48.1",
52
- "@wordpress/element": "^8.0.1",
53
- "@wordpress/is-shallow-equal": "^5.48.1",
54
- "@wordpress/keycodes": "^4.48.1",
55
- "@wordpress/priority-queue": "^3.48.1",
56
- "@wordpress/private-apis": "^1.48.1",
57
- "@wordpress/undo-manager": "^1.48.1",
49
+ "@wordpress/deprecated": "^4.49.0",
50
+ "@wordpress/dom": "^4.49.0",
51
+ "@wordpress/element": "^8.1.0",
52
+ "@wordpress/is-shallow-equal": "^5.49.0",
53
+ "@wordpress/keycodes": "^4.49.0",
54
+ "@wordpress/priority-queue": "^3.49.0",
55
+ "@wordpress/private-apis": "^1.49.0",
56
+ "@wordpress/undo-manager": "^1.49.0",
58
57
  "change-case": "^4.1.2",
59
58
  "mousetrap": "^1.6.5",
60
59
  "use-memo-one": "^1.1.1"
@@ -67,10 +66,16 @@
67
66
  "react-dom": "^18.3.1"
68
67
  },
69
68
  "peerDependencies": {
69
+ "@types/react": "^18.3.27",
70
70
  "react": "^18.0.0"
71
71
  },
72
+ "peerDependenciesMeta": {
73
+ "@types/react": {
74
+ "optional": true
75
+ }
76
+ },
72
77
  "publishConfig": {
73
78
  "access": "public"
74
79
  },
75
- "gitHead": "99df7432c5c7cb83ba41146fd1f57f3c19004305"
80
+ "gitHead": "0e7112a4f4fde4ea15bd9060489b8f6fe11eb6ca"
76
81
  }
@@ -12,10 +12,10 @@ import useRefEffect from '../use-ref-effect';
12
12
  * In Dialogs/modals, the tabbing must be constrained to the content of
13
13
  * the wrapper element. This hook adds the behavior to the returned ref.
14
14
  *
15
- * @return {React.RefCallback<Element>} Element Ref.
15
+ * @return Element Ref.
16
16
  *
17
17
  * @example
18
- * ```js
18
+ * ```ts
19
19
  * import { useConstrainedTabbing } from '@wordpress/compose';
20
20
  *
21
21
  * const ConstrainedTabbingExample = () => {
@@ -29,9 +29,9 @@ import useRefEffect from '../use-ref-effect';
29
29
  * }
30
30
  * ```
31
31
  */
32
- function useConstrainedTabbing() {
33
- return useRefEffect( ( /** @type {HTMLElement} */ node ) => {
34
- function onKeyDown( /** @type {KeyboardEvent} */ event ) {
32
+ function useConstrainedTabbing(): React.RefCallback< Element > {
33
+ return useRefEffect( ( node: HTMLElement ) => {
34
+ function onKeyDown( event: KeyboardEvent ): void {
35
35
  const { key, shiftKey, target } = event;
36
36
 
37
37
  if ( key !== 'Tab' ) {
@@ -40,9 +40,7 @@ function useConstrainedTabbing() {
40
40
 
41
41
  const action = shiftKey ? 'findPrevious' : 'findNext';
42
42
  const nextElement =
43
- focus.tabbable[ action ](
44
- /** @type {HTMLElement} */ ( target )
45
- ) || null;
43
+ focus.tabbable[ action ]( target as HTMLElement ) || null;
46
44
 
47
45
  // When the target element contains the element that is about to
48
46
  // receive focus, for example when the target is a tabbable
@@ -50,9 +48,7 @@ function useConstrainedTabbing() {
50
48
  // In this case we can't rely on native browsers behavior. We need
51
49
  // to manage focus instead.
52
50
  // See https://github.com/WordPress/gutenberg/issues/46041.
53
- if (
54
- /** @type {HTMLElement} */ ( target ).contains( nextElement )
55
- ) {
51
+ if ( ( target as HTMLElement ).contains( nextElement ) ) {
56
52
  event.preventDefault();
57
53
  nextElement?.focus();
58
54
  return;
@@ -12,6 +12,7 @@ import { useEffect } from '@wordpress/element';
12
12
  * Internal dependencies
13
13
  */
14
14
  import { debounce } from '../../utils/debounce';
15
+ import type { DebounceOptions, DebouncedFunc } from '../../utils/debounce';
15
16
 
16
17
  /**
17
18
  * Debounces a function similar to Lodash's `debounce`. A new debounced function will
@@ -21,14 +22,18 @@ import { debounce } from '../../utils/debounce';
21
22
  *
22
23
  * @see https://lodash.com/docs/4#debounce
23
24
  *
24
- * @template {(...args: any[]) => void} TFunc
25
+ * @template TFunc
25
26
  *
26
- * @param {TFunc} fn The function to debounce.
27
- * @param {number} [wait] The number of milliseconds to delay.
28
- * @param {import('../../utils/debounce').DebounceOptions} [options] The options object.
29
- * @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Debounced function.
27
+ * @param fn The function to debounce.
28
+ * @param wait The number of milliseconds to delay.
29
+ * @param options The options object.
30
+ * @return Debounced function.
30
31
  */
31
- export default function useDebounce( fn, wait, options ) {
32
+ export default function useDebounce< TFunc extends ( ...args: any[] ) => void >(
33
+ fn: TFunc,
34
+ wait?: number,
35
+ options?: DebounceOptions
36
+ ): DebouncedFunc< TFunc > {
32
37
  const debounced = useMemoOne(
33
38
  () => debounce( fn, wait ?? 0, options ),
34
39
  [ fn, wait, options?.leading, options?.trailing, options?.maxWait ]
@@ -32,6 +32,8 @@ import useRefEffect from '../use-ref-effect';
32
32
  */
33
33
  export default function useDisabled( {
34
34
  isDisabled: isDisabledProp = false,
35
+ }: {
36
+ isDisabled?: boolean;
35
37
  } = {} ) {
36
38
  return useRefEffect(
37
39
  ( node ) => {
@@ -45,7 +47,7 @@ export default function useDisabled( {
45
47
  }
46
48
 
47
49
  /** A variable keeping track of the previous updates in order to restore them. */
48
- const updates: Function[] = [];
50
+ const updates: ( () => void )[] = [];
49
51
  const disable = () => {
50
52
  node.childNodes.forEach( ( child ) => {
51
53
  if ( ! ( child instanceof defaultView.HTMLElement ) ) {
@@ -0,0 +1,78 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import useIsomorphicLayoutEffect from '../use-isomorphic-layout-effect';
10
+ import type { DraggingProps, DraggingReturn } from './types';
11
+
12
+ /**
13
+ * Hook to manage drag events with proper event handling
14
+ *
15
+ * @param props Drag handlers
16
+ * @param props.onDragStart
17
+ * @param props.onDragMove
18
+ * @param props.onDragEnd
19
+ * @return Drag state and handlers
20
+ */
21
+ export default function useDragging( {
22
+ onDragStart,
23
+ onDragMove,
24
+ onDragEnd,
25
+ }: DraggingProps ): DraggingReturn {
26
+ const [ isDragging, setIsDragging ] = useState( false );
27
+
28
+ const eventsRef = useRef< DraggingProps >( {
29
+ onDragStart,
30
+ onDragMove,
31
+ onDragEnd,
32
+ } );
33
+ useIsomorphicLayoutEffect( () => {
34
+ eventsRef.current.onDragStart = onDragStart;
35
+ eventsRef.current.onDragMove = onDragMove;
36
+ eventsRef.current.onDragEnd = onDragEnd;
37
+ }, [ onDragStart, onDragMove, onDragEnd ] );
38
+
39
+ const onMouseMove = useCallback( ( event: MouseEvent ): void => {
40
+ eventsRef.current.onDragMove?.( event );
41
+ }, [] );
42
+
43
+ const endDrag = useCallback(
44
+ function endDrag( event?: MouseEvent ): void {
45
+ eventsRef.current.onDragEnd?.( event );
46
+ document.removeEventListener( 'mousemove', onMouseMove );
47
+ document.removeEventListener( 'mouseup', endDrag );
48
+ setIsDragging( false );
49
+ },
50
+ [ onMouseMove ]
51
+ );
52
+
53
+ const startDrag = useCallback(
54
+ ( event: React.MouseEvent ): void => {
55
+ eventsRef.current.onDragStart?.( event );
56
+ document.addEventListener( 'mousemove', onMouseMove );
57
+ document.addEventListener( 'mouseup', endDrag );
58
+ setIsDragging( true );
59
+ },
60
+ [ onMouseMove, endDrag ]
61
+ );
62
+
63
+ // Remove the global events when unmounting if needed.
64
+ useEffect( () => {
65
+ return () => {
66
+ if ( isDragging ) {
67
+ document.removeEventListener( 'mousemove', onMouseMove );
68
+ document.removeEventListener( 'mouseup', endDrag );
69
+ }
70
+ };
71
+ }, [ isDragging, onMouseMove, endDrag ] );
72
+
73
+ return {
74
+ startDrag,
75
+ endDrag,
76
+ isDragging,
77
+ };
78
+ }
@@ -0,0 +1,11 @@
1
+ export interface DraggingProps {
2
+ onDragStart?: ( e: React.MouseEvent ) => void;
3
+ onDragMove?: ( e: MouseEvent ) => void;
4
+ onDragEnd?: ( e?: MouseEvent ) => void;
5
+ }
6
+
7
+ export interface DraggingReturn {
8
+ startDrag: ( e: React.MouseEvent ) => void;
9
+ endDrag: ( e?: MouseEvent ) => void;
10
+ isDragging: boolean;
11
+ }