@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.
- package/CHANGELOG.md +2 -0
- package/README.md +42 -38
- package/build/hooks/use-constrained-tabbing/index.cjs +3 -9
- package/build/hooks/use-constrained-tabbing/index.cjs.map +3 -3
- package/build/hooks/use-debounce/index.cjs +1 -1
- package/build/hooks/use-debounce/index.cjs.map +3 -3
- package/build/hooks/use-disabled/index.cjs.map +2 -2
- package/build/hooks/use-dragging/index.cjs +27 -22
- package/build/hooks/use-dragging/index.cjs.map +4 -4
- package/build/hooks/use-dragging/types.cjs +19 -0
- package/build/hooks/use-dragging/types.cjs.map +7 -0
- package/build/hooks/use-drop-zone/index.cjs +2 -5
- package/build/hooks/use-drop-zone/index.cjs.map +3 -3
- package/build/hooks/use-drop-zone/types.cjs +19 -0
- package/build/hooks/use-drop-zone/types.cjs.map +7 -0
- package/build/hooks/use-fixed-window-list/index.cjs +10 -8
- package/build/hooks/use-fixed-window-list/index.cjs.map +3 -3
- package/build/hooks/use-focus-return/index.cjs +12 -9
- package/build/hooks/use-focus-return/index.cjs.map +3 -3
- package/build/hooks/use-isomorphic-layout-effect/index.cjs +1 -1
- package/build/hooks/use-isomorphic-layout-effect/index.cjs.map +1 -1
- package/build/hooks/use-keyboard-shortcut/index.cjs +1 -4
- package/build/hooks/use-keyboard-shortcut/index.cjs.map +3 -3
- package/build/hooks/use-reduced-motion/index.cjs +1 -1
- package/build/hooks/use-reduced-motion/index.cjs.map +2 -2
- package/build/hooks/use-throttle/index.cjs +1 -1
- package/build/hooks/use-throttle/index.cjs.map +3 -3
- package/build/hooks/use-viewport-match/index.cjs +2 -5
- package/build/hooks/use-viewport-match/index.cjs.map +3 -3
- package/build/hooks/use-warn-on-change/index.cjs +3 -10
- package/build/hooks/use-warn-on-change/index.cjs.map +3 -3
- package/build/utils/subscribe-delegated-listener/index.cjs.map +1 -1
- package/build-module/hooks/use-constrained-tabbing/index.mjs +3 -9
- package/build-module/hooks/use-constrained-tabbing/index.mjs.map +3 -3
- package/build-module/hooks/use-debounce/index.mjs +1 -1
- package/build-module/hooks/use-debounce/index.mjs.map +3 -3
- package/build-module/hooks/use-disabled/index.mjs.map +2 -2
- package/build-module/hooks/use-dragging/index.mjs +27 -22
- package/build-module/hooks/use-dragging/index.mjs.map +4 -4
- package/build-module/hooks/use-dragging/types.mjs +1 -0
- package/build-module/hooks/use-dragging/types.mjs.map +7 -0
- package/build-module/hooks/use-drop-zone/index.mjs +2 -5
- package/build-module/hooks/use-drop-zone/index.mjs.map +3 -3
- package/build-module/hooks/use-drop-zone/types.mjs +1 -0
- package/build-module/hooks/use-drop-zone/types.mjs.map +7 -0
- package/build-module/hooks/use-fixed-window-list/index.mjs +10 -8
- package/build-module/hooks/use-fixed-window-list/index.mjs.map +3 -3
- package/build-module/hooks/use-focus-return/index.mjs +12 -9
- package/build-module/hooks/use-focus-return/index.mjs.map +3 -3
- package/build-module/hooks/use-isomorphic-layout-effect/index.mjs +1 -1
- package/build-module/hooks/use-isomorphic-layout-effect/index.mjs.map +1 -1
- package/build-module/hooks/use-keyboard-shortcut/index.mjs +1 -4
- package/build-module/hooks/use-keyboard-shortcut/index.mjs.map +3 -3
- package/build-module/hooks/use-reduced-motion/index.mjs +1 -1
- package/build-module/hooks/use-reduced-motion/index.mjs.map +2 -2
- package/build-module/hooks/use-throttle/index.mjs +1 -1
- package/build-module/hooks/use-throttle/index.mjs.map +3 -3
- package/build-module/hooks/use-viewport-match/index.mjs +2 -5
- package/build-module/hooks/use-viewport-match/index.mjs.map +3 -3
- package/build-module/hooks/use-warn-on-change/index.mjs +3 -10
- package/build-module/hooks/use-warn-on-change/index.mjs.map +3 -3
- package/build-module/utils/subscribe-delegated-listener/index.mjs.map +1 -1
- package/build-types/hooks/use-constrained-tabbing/index.d.ts +2 -2
- package/build-types/hooks/use-constrained-tabbing/index.d.ts.map +1 -1
- package/build-types/hooks/use-debounce/index.d.ts +7 -6
- package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
- package/build-types/hooks/use-disabled/index.d.ts +1 -1
- package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
- package/build-types/hooks/use-dragging/index.d.ts +9 -13
- package/build-types/hooks/use-dragging/index.d.ts.map +1 -1
- package/build-types/hooks/use-dragging/types.d.ts +11 -0
- package/build-types/hooks/use-dragging/types.d.ts.map +1 -0
- package/build-types/hooks/use-drop-zone/index.d.ts +4 -21
- package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
- package/build-types/hooks/use-drop-zone/types.d.ts +35 -0
- package/build-types/hooks/use-drop-zone/types.d.ts.map +1 -0
- package/build-types/hooks/use-fixed-window-list/index.d.ts +22 -26
- package/build-types/hooks/use-fixed-window-list/index.d.ts.map +1 -1
- package/build-types/hooks/use-focus-return/index.d.ts +3 -3
- package/build-types/hooks/use-focus-return/index.d.ts.map +1 -1
- package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
- package/build-types/hooks/use-keyboard-shortcut/index.d.ts +29 -23
- package/build-types/hooks/use-keyboard-shortcut/index.d.ts.map +1 -1
- package/build-types/hooks/use-reduced-motion/index.d.ts +1 -1
- package/build-types/hooks/use-reduced-motion/index.d.ts.map +1 -1
- package/build-types/hooks/use-throttle/index.d.ts +8 -6
- package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
- package/build-types/hooks/use-viewport-match/index.d.ts +3 -3
- package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
- package/build-types/hooks/use-warn-on-change/index.d.ts +7 -7
- package/build-types/hooks/use-warn-on-change/index.d.ts.map +1 -1
- package/package.json +16 -11
- package/src/hooks/use-constrained-tabbing/{index.js → index.ts} +7 -11
- package/src/hooks/use-debounce/{index.js → index.ts} +11 -6
- package/src/hooks/use-disabled/index.ts +3 -1
- package/src/hooks/use-dragging/index.ts +78 -0
- package/src/hooks/use-dragging/types.ts +11 -0
- package/src/hooks/use-drop-zone/{index.js → index.ts} +17 -31
- package/src/hooks/use-drop-zone/types.ts +34 -0
- package/src/hooks/use-fixed-window-list/{index.js → index.ts} +46 -37
- package/src/hooks/use-focus-return/{index.js → index.ts} +26 -21
- package/src/hooks/use-keyboard-shortcut/{index.js → index.ts} +43 -28
- package/src/hooks/use-reduced-motion/{index.js → index.ts} +2 -2
- package/src/hooks/use-throttle/{index.js → index.ts} +12 -6
- package/src/hooks/use-viewport-match/{index.js → index.ts} +30 -29
- package/src/hooks/use-warn-on-change/index.ts +42 -0
- package/src/hooks/use-dragging/index.js +0 -74
- package/src/hooks/use-warn-on-change/index.js +0 -43
- /package/src/hooks/use-isomorphic-layout-effect/{index.js → index.ts} +0 -0
|
@@ -8,18 +8,23 @@ import { createContext, useContext } from '@wordpress/element';
|
|
|
8
8
|
*/
|
|
9
9
|
import useMediaQuery from '../use-media-query';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
type Breakpoint =
|
|
12
|
+
| 'xhuge'
|
|
13
|
+
| 'huge'
|
|
14
|
+
| 'wide'
|
|
15
|
+
| 'xlarge'
|
|
16
|
+
| 'large'
|
|
17
|
+
| 'medium'
|
|
18
|
+
| 'small'
|
|
19
|
+
| 'mobile';
|
|
20
|
+
type ViewportOperator = '>=' | '<';
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* Hash of breakpoint names with pixel width at which it becomes effective.
|
|
17
24
|
*
|
|
18
25
|
* @see _breakpoints.scss
|
|
19
|
-
*
|
|
20
|
-
* @type {Record<WPBreakpoint, number>}
|
|
21
26
|
*/
|
|
22
|
-
const BREAKPOINTS = {
|
|
27
|
+
const BREAKPOINTS: Record< Breakpoint, number > = {
|
|
23
28
|
xhuge: 1920,
|
|
24
29
|
huge: 1440,
|
|
25
30
|
wide: 1280,
|
|
@@ -30,63 +35,59 @@ const BREAKPOINTS = {
|
|
|
30
35
|
mobile: 480,
|
|
31
36
|
};
|
|
32
37
|
|
|
33
|
-
/**
|
|
34
|
-
* @typedef {">=" | "<"} WPViewportOperator
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
38
|
/**
|
|
38
39
|
* Object mapping media query operators to the condition to be used.
|
|
39
|
-
*
|
|
40
|
-
* @type {Record<WPViewportOperator, string>}
|
|
41
40
|
*/
|
|
42
|
-
const CONDITIONS = {
|
|
41
|
+
const CONDITIONS: Record< ViewportOperator, string > = {
|
|
43
42
|
'>=': 'min-width',
|
|
44
43
|
'<': 'max-width',
|
|
45
44
|
};
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
|
-
* Object mapping media query operators to a function that
|
|
49
|
-
*
|
|
50
|
-
* @type {Record<WPViewportOperator, (breakpointValue: number, width: number) => boolean>}
|
|
47
|
+
* Object mapping media query operators to a function that evaluates if the operator matches.
|
|
51
48
|
*/
|
|
52
|
-
const OPERATOR_EVALUATORS
|
|
49
|
+
const OPERATOR_EVALUATORS: Record<
|
|
50
|
+
ViewportOperator,
|
|
51
|
+
( breakpointValue: number, width: number ) => boolean
|
|
52
|
+
> = {
|
|
53
53
|
'>=': ( breakpointValue, width ) => width >= breakpointValue,
|
|
54
54
|
'<': ( breakpointValue, width ) => width < breakpointValue,
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
const ViewportMatchWidthContext = createContext(
|
|
58
|
-
/** @type {null | number} */ ( null )
|
|
59
|
-
);
|
|
57
|
+
const ViewportMatchWidthContext = createContext< number | null >( null );
|
|
60
58
|
ViewportMatchWidthContext.displayName = 'ViewportMatchWidthContext';
|
|
61
59
|
|
|
62
60
|
/**
|
|
63
61
|
* Returns true if the viewport matches the given query, or false otherwise.
|
|
64
62
|
*
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @param
|
|
63
|
+
* @param breakpoint Breakpoint size name.
|
|
64
|
+
* @param operator Viewport operator.
|
|
65
|
+
* @param view Window instance in which to perform viewport matching.
|
|
68
66
|
*
|
|
69
67
|
* @example
|
|
70
68
|
*
|
|
71
|
-
* ```
|
|
69
|
+
* ```ts
|
|
72
70
|
* useViewportMatch( 'huge', '<' );
|
|
73
71
|
* useViewportMatch( 'medium' );
|
|
74
72
|
* ```
|
|
75
73
|
*
|
|
76
|
-
* @return
|
|
74
|
+
* @return Whether viewport matches query.
|
|
77
75
|
*/
|
|
78
76
|
const useViewportMatch = (
|
|
79
|
-
breakpoint,
|
|
80
|
-
operator = '>=',
|
|
77
|
+
breakpoint: Breakpoint,
|
|
78
|
+
operator: ViewportOperator = '>=',
|
|
81
79
|
// Resolve the default lazily so SSR (where `window` is undeclared) does not
|
|
82
80
|
// throw a ReferenceError when this default expression is evaluated.
|
|
83
|
-
view = typeof window !== 'undefined'
|
|
84
|
-
|
|
81
|
+
view: Window | undefined = typeof window !== 'undefined'
|
|
82
|
+
? window
|
|
83
|
+
: undefined
|
|
84
|
+
): boolean => {
|
|
85
85
|
const simulatedWidth = useContext( ViewportMatchWidthContext );
|
|
86
86
|
const mediaQuery =
|
|
87
87
|
! simulatedWidth &&
|
|
88
88
|
`(${ CONDITIONS[ operator ] }: ${ BREAKPOINTS[ breakpoint ] }px)`;
|
|
89
89
|
const mediaQueryResult = useMediaQuery( mediaQuery || undefined, view );
|
|
90
|
+
|
|
90
91
|
if ( simulatedWidth ) {
|
|
91
92
|
return OPERATOR_EVALUATORS[ operator ](
|
|
92
93
|
BREAKPOINTS[ breakpoint ],
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import usePrevious from '../use-previous';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook that performs a shallow comparison between the previous value of an object
|
|
8
|
+
* and the new one, if there's a difference, it prints it to the console.
|
|
9
|
+
* This is useful in performance related work, to check why a component re-renders.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```tsx
|
|
14
|
+
* function MyComponent(props: Record<string, any>) {
|
|
15
|
+
* useWarnOnChange(props);
|
|
16
|
+
*
|
|
17
|
+
* return "Something";
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @param object Object which changes to compare.
|
|
22
|
+
* @param prefix Just a prefix to show when console logging.
|
|
23
|
+
*/
|
|
24
|
+
function useWarnOnChange(
|
|
25
|
+
object: Record< string, any > | any[],
|
|
26
|
+
prefix: string = 'Change detection'
|
|
27
|
+
): void {
|
|
28
|
+
const previousValues = usePrevious( object );
|
|
29
|
+
|
|
30
|
+
Object.entries( previousValues ?? [] ).forEach( ( [ key, value ] ) => {
|
|
31
|
+
if ( value !== object[ key as keyof typeof object ] ) {
|
|
32
|
+
// eslint-disable-next-line no-console
|
|
33
|
+
console.warn(
|
|
34
|
+
`${ prefix }: ${ key } key changed:`,
|
|
35
|
+
value,
|
|
36
|
+
object[ key as keyof typeof object ]
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
} );
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default useWarnOnChange;
|
|
@@ -1,74 +0,0 @@
|
|
|
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
|
-
|
|
11
|
-
// Event handlers that are triggered from `document` listeners accept a MouseEvent,
|
|
12
|
-
// while those triggered from React listeners accept a React.MouseEvent.
|
|
13
|
-
/**
|
|
14
|
-
* @param {Object} props
|
|
15
|
-
* @param {(e: React.MouseEvent) => void} props.onDragStart
|
|
16
|
-
* @param {(e: MouseEvent) => void} props.onDragMove
|
|
17
|
-
* @param {(e?: MouseEvent) => void} props.onDragEnd
|
|
18
|
-
*/
|
|
19
|
-
export default function useDragging( { onDragStart, onDragMove, onDragEnd } ) {
|
|
20
|
-
const [ isDragging, setIsDragging ] = useState( false );
|
|
21
|
-
|
|
22
|
-
const eventsRef = useRef( {
|
|
23
|
-
onDragStart,
|
|
24
|
-
onDragMove,
|
|
25
|
-
onDragEnd,
|
|
26
|
-
} );
|
|
27
|
-
useIsomorphicLayoutEffect( () => {
|
|
28
|
-
eventsRef.current.onDragStart = onDragStart;
|
|
29
|
-
eventsRef.current.onDragMove = onDragMove;
|
|
30
|
-
eventsRef.current.onDragEnd = onDragEnd;
|
|
31
|
-
}, [ onDragStart, onDragMove, onDragEnd ] );
|
|
32
|
-
|
|
33
|
-
/** @type {(e: MouseEvent) => void} */
|
|
34
|
-
const onMouseMove = useCallback(
|
|
35
|
-
( event ) =>
|
|
36
|
-
eventsRef.current.onDragMove &&
|
|
37
|
-
eventsRef.current.onDragMove( event ),
|
|
38
|
-
[]
|
|
39
|
-
);
|
|
40
|
-
/** @type {(e?: MouseEvent) => void} */
|
|
41
|
-
const endDrag = useCallback( ( event ) => {
|
|
42
|
-
if ( eventsRef.current.onDragEnd ) {
|
|
43
|
-
eventsRef.current.onDragEnd( event );
|
|
44
|
-
}
|
|
45
|
-
document.removeEventListener( 'mousemove', onMouseMove );
|
|
46
|
-
document.removeEventListener( 'mouseup', endDrag );
|
|
47
|
-
setIsDragging( false );
|
|
48
|
-
}, [] );
|
|
49
|
-
/** @type {(e: React.MouseEvent) => void} */
|
|
50
|
-
const startDrag = useCallback( ( event ) => {
|
|
51
|
-
if ( eventsRef.current.onDragStart ) {
|
|
52
|
-
eventsRef.current.onDragStart( event );
|
|
53
|
-
}
|
|
54
|
-
document.addEventListener( 'mousemove', onMouseMove );
|
|
55
|
-
document.addEventListener( 'mouseup', endDrag );
|
|
56
|
-
setIsDragging( true );
|
|
57
|
-
}, [] );
|
|
58
|
-
|
|
59
|
-
// Remove the global events when unmounting if needed.
|
|
60
|
-
useEffect( () => {
|
|
61
|
-
return () => {
|
|
62
|
-
if ( isDragging ) {
|
|
63
|
-
document.removeEventListener( 'mousemove', onMouseMove );
|
|
64
|
-
document.removeEventListener( 'mouseup', endDrag );
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
}, [ isDragging ] );
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
startDrag,
|
|
71
|
-
endDrag,
|
|
72
|
-
isDragging,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import usePrevious from '../use-previous';
|
|
5
|
-
|
|
6
|
-
// Disable reason: Object and object are distinctly different types in TypeScript and we mean the lowercase object in this case
|
|
7
|
-
// but eslint wants to force us to use `Object`. See https://stackoverflow.com/questions/49464634/difference-between-object-and-object-in-typescript
|
|
8
|
-
/* eslint-disable jsdoc/check-types */
|
|
9
|
-
/**
|
|
10
|
-
* Hook that performs a shallow comparison between the preview value of an object
|
|
11
|
-
* and the new one, if there's a difference, it prints it to the console.
|
|
12
|
-
* this is useful in performance related work, to check why a component re-renders.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
*
|
|
16
|
-
* ```jsx
|
|
17
|
-
* function MyComponent(props) {
|
|
18
|
-
* useWarnOnChange(props);
|
|
19
|
-
*
|
|
20
|
-
* return "Something";
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* @param {object} object Object which changes to compare.
|
|
25
|
-
* @param {string} prefix Just a prefix to show when console logging.
|
|
26
|
-
*/
|
|
27
|
-
function useWarnOnChange( object, prefix = 'Change detection' ) {
|
|
28
|
-
const previousValues = usePrevious( object );
|
|
29
|
-
|
|
30
|
-
Object.entries( previousValues ?? [] ).forEach( ( [ key, value ] ) => {
|
|
31
|
-
if ( value !== object[ /** @type {keyof typeof object} */ ( key ) ] ) {
|
|
32
|
-
// eslint-disable-next-line no-console
|
|
33
|
-
console.warn(
|
|
34
|
-
`${ prefix }: ${ key } key changed:`,
|
|
35
|
-
value,
|
|
36
|
-
object[ /** @type {keyof typeof object} */ ( key ) ]
|
|
37
|
-
/* eslint-enable jsdoc/check-types */
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
} );
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export default useWarnOnChange;
|
|
File without changes
|