@wordpress/compose 7.5.0 → 7.7.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 +4 -0
- package/README.md +1 -1
- package/build/hooks/use-copy-on-click/index.js +5 -5
- package/build/hooks/use-copy-on-click/index.js.map +1 -1
- package/build/hooks/use-focus-on-mount/index.js +4 -4
- package/build/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build/hooks/use-focus-outside/index.js +11 -11
- package/build/hooks/use-focus-outside/index.js.map +1 -1
- package/build/hooks/use-keyboard-shortcut/index.js +3 -3
- package/build/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build/hooks/use-merge-refs/index.js +12 -12
- package/build/hooks/use-merge-refs/index.js.map +1 -1
- package/build/hooks/use-ref-effect/index.js +4 -4
- package/build/hooks/use-ref-effect/index.js.map +1 -1
- package/build/hooks/use-resize-observer/index.js +79 -184
- package/build/hooks/use-resize-observer/index.js.map +1 -1
- package/build-module/hooks/use-copy-on-click/index.js +5 -5
- package/build-module/hooks/use-copy-on-click/index.js.map +1 -1
- package/build-module/hooks/use-focus-on-mount/index.js +4 -4
- package/build-module/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build-module/hooks/use-focus-outside/index.js +11 -11
- package/build-module/hooks/use-focus-outside/index.js.map +1 -1
- package/build-module/hooks/use-keyboard-shortcut/index.js +3 -3
- package/build-module/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build-module/hooks/use-merge-refs/index.js +12 -12
- package/build-module/hooks/use-merge-refs/index.js.map +1 -1
- package/build-module/hooks/use-ref-effect/index.js +4 -4
- package/build-module/hooks/use-ref-effect/index.js.map +1 -1
- package/build-module/hooks/use-resize-observer/index.js +79 -184
- package/build-module/hooks/use-resize-observer/index.js.map +1 -1
- package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
- package/build-types/hooks/use-resize-observer/index.d.ts +8 -14
- package/build-types/hooks/use-resize-observer/index.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/hooks/use-copy-on-click/index.js +5 -5
- package/src/hooks/use-focus-on-mount/index.js +4 -4
- package/src/hooks/use-focus-outside/index.ts +11 -11
- package/src/hooks/use-keyboard-shortcut/index.js +3 -3
- package/src/hooks/use-merge-refs/index.js +15 -13
- package/src/hooks/use-ref-effect/index.ts +4 -4
- package/src/hooks/use-resize-observer/index.tsx +85 -288
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Mousetrap","useEffect","useRef","isAppleOS","useKeyboardShortcut","shortcuts","callback","bindGlobal","eventName","isDisabled","target","
|
|
1
|
+
{"version":3,"names":["Mousetrap","useEffect","useRef","isAppleOS","useKeyboardShortcut","shortcuts","callback","bindGlobal","eventName","isDisabled","target","currentCallbackRef","current","mousetrap","document","shortcutsArray","Array","isArray","forEach","shortcut","keys","split","modifiers","Set","filter","value","length","hasAlt","has","hasShift","size","Error","bindFn","args","reset"],"sources":["@wordpress/compose/src/hooks/use-keyboard-shortcut/index.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport Mousetrap from 'mousetrap';\nimport 'mousetrap/plugins/global-bind/mousetrap-global-bind';\n\n/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\nimport { isAppleOS } from '@wordpress/keycodes';\n\n/**\n * A block selection object.\n *\n * @typedef {Object} WPKeyboardShortcutConfig\n *\n * @property {boolean} [bindGlobal] Handle keyboard events anywhere including inside textarea/input fields.\n * @property {string} [eventName] Event name used to trigger the handler, defaults to keydown.\n * @property {boolean} [isDisabled] Disables the keyboard handler if the value is true.\n * @property {import('react').RefObject<HTMLElement>} [target] React reference to the DOM element used to catch the keyboard event.\n */\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * Attach a keyboard shortcut handler.\n *\n * @see https://craig.is/killing/mice#api.bind for information about the `callback` parameter.\n *\n * @param {string[]|string} shortcuts Keyboard Shortcuts.\n * @param {(e: import('mousetrap').ExtendedKeyboardEvent, combo: string) => void} callback Shortcut callback.\n * @param {WPKeyboardShortcutConfig} options Shortcut options.\n */\nfunction useKeyboardShortcut(\n\t/* eslint-enable jsdoc/valid-types */\n\tshortcuts,\n\tcallback,\n\t{\n\t\tbindGlobal = false,\n\t\teventName = 'keydown',\n\t\tisDisabled = false, // This is important for performance considerations.\n\t\ttarget,\n\t} = {}\n) {\n\tconst currentCallbackRef = useRef( callback );\n\tuseEffect( () => {\n\t\tcurrentCallbackRef.current = callback;\n\t}, [ callback ] );\n\n\tuseEffect( () => {\n\t\tif ( isDisabled ) {\n\t\t\treturn;\n\t\t}\n\t\tconst mousetrap = new Mousetrap(\n\t\t\ttarget && target.current\n\t\t\t\t? target.current\n\t\t\t\t: // We were passing `document` here previously, so to successfully cast it to Element we must cast it first to `unknown`.\n\t\t\t\t // Not sure if this is a mistake but it was the behavior previous to the addition of types so we're just doing what's\n\t\t\t\t // necessary to maintain the existing behavior.\n\t\t\t\t /** @type {Element} */ ( /** @type {unknown} */ ( document ) )\n\t\t);\n\t\tconst shortcutsArray = Array.isArray( shortcuts )\n\t\t\t? shortcuts\n\t\t\t: [ shortcuts ];\n\t\tshortcutsArray.forEach( ( shortcut ) => {\n\t\t\tconst keys = shortcut.split( '+' );\n\t\t\t// Determines whether a key is a modifier by the length of the string.\n\t\t\t// E.g. if I add a pass a shortcut Shift+Cmd+M, it'll determine that\n\t\t\t// the modifiers are Shift and Cmd because they're not a single character.\n\t\t\tconst modifiers = new Set(\n\t\t\t\tkeys.filter( ( value ) => value.length > 1 )\n\t\t\t);\n\t\t\tconst hasAlt = modifiers.has( 'alt' );\n\t\t\tconst hasShift = modifiers.has( 'shift' );\n\n\t\t\t// This should be better moved to the shortcut registration instead.\n\t\t\tif (\n\t\t\t\tisAppleOS() &&\n\t\t\t\t( ( modifiers.size === 1 && hasAlt ) ||\n\t\t\t\t\t( modifiers.size === 2 && hasAlt && hasShift ) )\n\t\t\t) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Cannot bind ${ shortcut }. Alt and Shift+Alt modifiers are reserved for character input.`\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst bindFn = bindGlobal ? 'bindGlobal' : 'bind';\n\t\t\t// @ts-ignore `bindGlobal` is an undocumented property\n\t\t\tmousetrap[ bindFn ](\n\t\t\t\tshortcut,\n\t\t\t\t(\n\t\t\t\t\t/* eslint-disable jsdoc/valid-types */\n\t\t\t\t\t/** @type {[e: import('mousetrap').ExtendedKeyboardEvent, combo: string]} */ ...args\n\t\t\t\t) =>\n\t\t\t\t\t/* eslint-enable jsdoc/valid-types */\n\t\t\t\t\tcurrentCallbackRef.current( ...args ),\n\t\t\t\teventName\n\t\t\t);\n\t\t} );\n\n\t\treturn () => {\n\t\t\tmousetrap.reset();\n\t\t};\n\t}, [ shortcuts, bindGlobal, eventName, target, isDisabled ] );\n}\n\nexport default useKeyboardShortcut;\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,SAAS,MAAM,WAAW;AACjC,OAAO,qDAAqD;;AAE5D;AACA;AACA;AACA,SAASC,SAAS,EAAEC,MAAM,QAAQ,oBAAoB;AACtD,SAASC,SAAS,QAAQ,qBAAqB;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,mBAAmBA,CAAA,CAC3B;AACAC,SAAS,EACTC,QAAQ,EACR;EACCC,UAAU,GAAG,KAAK;EAClBC,SAAS,GAAG,SAAS;EACrBC,UAAU,GAAG,KAAK;EAAE;EACpBC;AACD,CAAC,GAAG,CAAC,CAAC,EACL;EACD,MAAMC,kBAAkB,GAAGT,MAAM,CAAEI,QAAS,CAAC;EAC7CL,SAAS,CAAE,MAAM;IAChBU,kBAAkB,CAACC,OAAO,GAAGN,QAAQ;EACtC,CAAC,EAAE,CAAEA,QAAQ,CAAG,CAAC;EAEjBL,SAAS,CAAE,MAAM;IAChB,IAAKQ,UAAU,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U,EAAEC,SAAS,EAAEE,MAAM,EAAED,UAAU,CAAG,CAAC;AAC9D;AAEA,eAAeL,mBAAmB","ignoreList":[]}
|
|
@@ -70,38 +70,38 @@ function assignRef(ref, value) {
|
|
|
70
70
|
*/
|
|
71
71
|
export default function useMergeRefs(refs) {
|
|
72
72
|
const element = useRef();
|
|
73
|
-
const
|
|
74
|
-
const
|
|
73
|
+
const isAttachedRef = useRef(false);
|
|
74
|
+
const didElementChangeRef = useRef(false);
|
|
75
75
|
/* eslint-disable jsdoc/no-undefined-types */
|
|
76
76
|
/** @type {import('react').MutableRefObject<TRef[]>} */
|
|
77
77
|
/* eslint-enable jsdoc/no-undefined-types */
|
|
78
|
-
const
|
|
79
|
-
const
|
|
78
|
+
const previousRefsRef = useRef([]);
|
|
79
|
+
const currentRefsRef = useRef(refs);
|
|
80
80
|
|
|
81
81
|
// Update on render before the ref callback is called, so the ref callback
|
|
82
82
|
// always has access to the current refs.
|
|
83
|
-
|
|
83
|
+
currentRefsRef.current = refs;
|
|
84
84
|
|
|
85
85
|
// If any of the refs change, call the previous ref with `null` and the new
|
|
86
86
|
// ref with the node, except when the element changes in the same cycle, in
|
|
87
87
|
// which case the ref callbacks will already have been called.
|
|
88
88
|
useLayoutEffect(() => {
|
|
89
|
-
if (
|
|
89
|
+
if (didElementChangeRef.current === false && isAttachedRef.current === true) {
|
|
90
90
|
refs.forEach((ref, index) => {
|
|
91
|
-
const previousRef =
|
|
91
|
+
const previousRef = previousRefsRef.current[index];
|
|
92
92
|
if (ref !== previousRef) {
|
|
93
93
|
assignRef(previousRef, null);
|
|
94
94
|
assignRef(ref, element.current);
|
|
95
95
|
}
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
previousRefsRef.current = refs;
|
|
99
99
|
}, refs);
|
|
100
100
|
|
|
101
101
|
// No dependencies, must be reset after every render so ref callbacks are
|
|
102
102
|
// correctly called after a ref change.
|
|
103
103
|
useLayoutEffect(() => {
|
|
104
|
-
|
|
104
|
+
didElementChangeRef.current = false;
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
// There should be no dependencies so that `callback` is only called when
|
|
@@ -110,12 +110,12 @@ export default function useMergeRefs(refs) {
|
|
|
110
110
|
// Update the element so it can be used when calling ref callbacks on a
|
|
111
111
|
// dependency change.
|
|
112
112
|
assignRef(element, value);
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
didElementChangeRef.current = true;
|
|
114
|
+
isAttachedRef.current = value !== null;
|
|
115
115
|
|
|
116
116
|
// When an element changes, the current ref callback should be called
|
|
117
117
|
// with the new element and the previous one with `null`.
|
|
118
|
-
const refsToAssign = value ?
|
|
118
|
+
const refsToAssign = value ? currentRefsRef.current : previousRefsRef.current;
|
|
119
119
|
|
|
120
120
|
// Update the latest refs.
|
|
121
121
|
for (const ref of refsToAssign) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useRef","useCallback","useLayoutEffect","assignRef","ref","value","hasOwnProperty","current","useMergeRefs","refs","element","
|
|
1
|
+
{"version":3,"names":["useRef","useCallback","useLayoutEffect","assignRef","ref","value","hasOwnProperty","current","useMergeRefs","refs","element","isAttachedRef","didElementChangeRef","previousRefsRef","currentRefsRef","forEach","index","previousRef","refsToAssign"],"sources":["@wordpress/compose/src/hooks/use-merge-refs/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useCallback, useLayoutEffect } from '@wordpress/element';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * @template T\n * @typedef {T extends import('react').Ref<infer R> ? R : never} TypeFromRef\n */\n/* eslint-enable jsdoc/valid-types */\n\n/**\n * @template T\n * @param {import('react').Ref<T>} ref\n * @param {T} value\n */\nfunction assignRef( ref, value ) {\n\tif ( typeof ref === 'function' ) {\n\t\tref( value );\n\t} else if ( ref && ref.hasOwnProperty( 'current' ) ) {\n\t\t/* eslint-disable jsdoc/no-undefined-types */\n\t\t/** @type {import('react').MutableRefObject<T>} */ ( ref ).current =\n\t\t\tvalue;\n\t\t/* eslint-enable jsdoc/no-undefined-types */\n\t}\n}\n\n/**\n * Merges refs into one ref callback.\n *\n * It also ensures that the merged ref callbacks are only called when they\n * change (as a result of a `useCallback` dependency update) OR when the ref\n * value changes, just as React does when passing a single ref callback to the\n * component.\n *\n * As expected, if you pass a new function on every render, the ref callback\n * will be called after every render.\n *\n * If you don't wish a ref callback to be called after every render, wrap it\n * with `useCallback( callback, dependencies )`. When a dependency changes, the\n * old ref callback will be called with `null` and the new ref callback will be\n * called with the same value.\n *\n * To make ref callbacks easier to use, you can also pass the result of\n * `useRefEffect`, which makes cleanup easier by allowing you to return a\n * cleanup function instead of handling `null`.\n *\n * It's also possible to _disable_ a ref (and its behaviour) by simply not\n * passing the ref.\n *\n * ```jsx\n * const ref = useRefEffect( ( node ) => {\n * node.addEventListener( ... );\n * return () => {\n * node.removeEventListener( ... );\n * };\n * }, [ ...dependencies ] );\n * const otherRef = useRef();\n * const mergedRefs useMergeRefs( [\n * enabled && ref,\n * otherRef,\n * ] );\n * return <div ref={ mergedRefs } />;\n * ```\n *\n * @template {import('react').Ref<any>} TRef\n * @param {Array<TRef>} refs The refs to be merged.\n *\n * @return {import('react').RefCallback<TypeFromRef<TRef>>} The merged ref callback.\n */\nexport default function useMergeRefs( refs ) {\n\tconst element = useRef();\n\tconst isAttachedRef = useRef( false );\n\tconst didElementChangeRef = useRef( false );\n\t/* eslint-disable jsdoc/no-undefined-types */\n\t/** @type {import('react').MutableRefObject<TRef[]>} */\n\t/* eslint-enable jsdoc/no-undefined-types */\n\tconst previousRefsRef = useRef( [] );\n\tconst currentRefsRef = useRef( refs );\n\n\t// Update on render before the ref callback is called, so the ref callback\n\t// always has access to the current refs.\n\tcurrentRefsRef.current = refs;\n\n\t// If any of the refs change, call the previous ref with `null` and the new\n\t// ref with the node, except when the element changes in the same cycle, in\n\t// which case the ref callbacks will already have been called.\n\tuseLayoutEffect( () => {\n\t\tif (\n\t\t\tdidElementChangeRef.current === false &&\n\t\t\tisAttachedRef.current === true\n\t\t) {\n\t\t\trefs.forEach( ( ref, index ) => {\n\t\t\t\tconst previousRef = previousRefsRef.current[ index ];\n\t\t\t\tif ( ref !== previousRef ) {\n\t\t\t\t\tassignRef( previousRef, null );\n\t\t\t\t\tassignRef( ref, element.current );\n\t\t\t\t}\n\t\t\t} );\n\t\t}\n\n\t\tpreviousRefsRef.current = refs;\n\t}, refs );\n\n\t// No dependencies, must be reset after every render so ref callbacks are\n\t// correctly called after a ref change.\n\tuseLayoutEffect( () => {\n\t\tdidElementChangeRef.current = false;\n\t} );\n\n\t// There should be no dependencies so that `callback` is only called when\n\t// the node changes.\n\treturn useCallback( ( value ) => {\n\t\t// Update the element so it can be used when calling ref callbacks on a\n\t\t// dependency change.\n\t\tassignRef( element, value );\n\n\t\tdidElementChangeRef.current = true;\n\t\tisAttachedRef.current = value !== null;\n\n\t\t// When an element changes, the current ref callback should be called\n\t\t// with the new element and the previous one with `null`.\n\t\tconst refsToAssign = value\n\t\t\t? currentRefsRef.current\n\t\t\t: previousRefsRef.current;\n\n\t\t// Update the latest refs.\n\t\tfor ( const ref of refsToAssign ) {\n\t\t\tassignRef( ref, value );\n\t\t}\n\t}, [] );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,EAAEC,WAAW,EAAEC,eAAe,QAAQ,oBAAoB;;AAEzE;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASC,SAASA,CAAEC,GAAG,EAAEC,KAAK,EAAG;EAChC,IAAK,OAAOD,GAAG,KAAK,UAAU,EAAG;IAChCA,GAAG,CAAEC,KAAM,CAAC;EACb,CAAC,MAAM,IAAKD,GAAG,IAAIA,GAAG,CAACE,cAAc,CAAE,SAAU,CAAC,EAAG;IACpD;IACA,kDAAqDF,GAAG,CAAGG,OAAO,GACjEF,KAAK;IACN;EACD;AACD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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,GAAG,IAAI;IAClCI,aAAa,CAACJ,OAAO,GAAGF,KAAK,KAAK,IAAI;;IAEtC;IACA;IACA,MAAMa,YAAY,GAAGb,KAAK,GACvBS,cAAc,CAACP,OAAO,GACtBM,eAAe,CAACN,OAAO;;IAE1B;IACA,KAAM,MAAMH,GAAG,IAAIc,YAAY,EAAG;MACjCf,SAAS,CAAEC,GAAG,EAAEC,KAAM,CAAC;IACxB;EACD,CAAC,EAAE,EAAG,CAAC;AACR","ignoreList":[]}
|
|
@@ -27,12 +27,12 @@ import { useCallback, useRef } from '@wordpress/element';
|
|
|
27
27
|
* @return Ref callback.
|
|
28
28
|
*/
|
|
29
29
|
export default function useRefEffect(callback, dependencies) {
|
|
30
|
-
const
|
|
30
|
+
const cleanupRef = useRef();
|
|
31
31
|
return useCallback(node => {
|
|
32
32
|
if (node) {
|
|
33
|
-
|
|
34
|
-
} else if (
|
|
35
|
-
|
|
33
|
+
cleanupRef.current = callback(node);
|
|
34
|
+
} else if (cleanupRef.current) {
|
|
35
|
+
cleanupRef.current();
|
|
36
36
|
}
|
|
37
37
|
}, dependencies);
|
|
38
38
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useCallback","useRef","useRefEffect","callback","dependencies","
|
|
1
|
+
{"version":3,"names":["useCallback","useRef","useRefEffect","callback","dependencies","cleanupRef","node","current"],"sources":["@wordpress/compose/src/hooks/use-ref-effect/index.ts"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { DependencyList, RefCallback } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useRef } from '@wordpress/element';\n\n/**\n * Effect-like ref callback. Just like with `useEffect`, this allows you to\n * return a cleanup function to be run if the ref changes or one of the\n * dependencies changes. The ref is provided as an argument to the callback\n * functions. The main difference between this and `useEffect` is that\n * the `useEffect` callback is not called when the ref changes, but this is.\n * Pass the returned ref callback as the component's ref and merge multiple refs\n * with `useMergeRefs`.\n *\n * It's worth noting that if the dependencies array is empty, there's not\n * strictly a need to clean up event handlers for example, because the node is\n * to be removed. It *is* necessary if you add dependencies because the ref\n * callback will be called multiple times for the same node.\n *\n * @param callback Callback with ref as argument.\n * @param dependencies Dependencies of the callback.\n *\n * @return Ref callback.\n */\nexport default function useRefEffect< TElement = Node >(\n\tcallback: ( node: TElement ) => ( () => void ) | void,\n\tdependencies: DependencyList\n): RefCallback< TElement | null > {\n\tconst cleanupRef = useRef< ( () => void ) | void >();\n\treturn useCallback( ( node: TElement | null ) => {\n\t\tif ( node ) {\n\t\t\tcleanupRef.current = callback( node );\n\t\t} else if ( cleanupRef.current ) {\n\t\t\tcleanupRef.current();\n\t\t}\n\t}, dependencies );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA,SAASA,WAAW,EAAEC,MAAM,QAAQ,oBAAoB;;AAExD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,YAAYA,CACnCC,QAAqD,EACrDC,YAA4B,EACK;EACjC,MAAMC,UAAU,GAAGJ,MAAM,CAA0B,CAAC;EACpD,OAAOD,WAAW,CAAIM,IAAqB,IAAM;IAChD,IAAKA,IAAI,EAAG;MACXD,UAAU,CAACE,OAAO,GAAGJ,QAAQ,CAAEG,IAAK,CAAC;IACtC,CAAC,MAAM,IAAKD,UAAU,CAACE,OAAO,EAAG;MAChCF,UAAU,CAACE,OAAO,CAAC,CAAC;IACrB;EACD,CAAC,EAAEH,YAAa,CAAC;AAClB","ignoreList":[]}
|
|
@@ -5,64 +5,8 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* WordPress dependencies
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { useCallback, useLayoutEffect, useRef, useState } from '@wordpress/element';
|
|
9
9
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
-
// This of course could've been more streamlined with internal state instead of
|
|
11
|
-
// refs, but then host hooks / components could not opt out of renders.
|
|
12
|
-
// This could've been exported to its own module, but the current build doesn't
|
|
13
|
-
// seem to work with module imports and I had no more time to spend on this...
|
|
14
|
-
function useResolvedElement(subscriber, refOrElement) {
|
|
15
|
-
const callbackRefElement = useRef(null);
|
|
16
|
-
const lastReportRef = useRef(null);
|
|
17
|
-
const cleanupRef = useRef();
|
|
18
|
-
const callSubscriber = useCallback(() => {
|
|
19
|
-
let element = null;
|
|
20
|
-
if (callbackRefElement.current) {
|
|
21
|
-
element = callbackRefElement.current;
|
|
22
|
-
} else if (refOrElement) {
|
|
23
|
-
if (refOrElement instanceof HTMLElement) {
|
|
24
|
-
element = refOrElement;
|
|
25
|
-
} else {
|
|
26
|
-
element = refOrElement.current;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
if (lastReportRef.current && lastReportRef.current.element === element && lastReportRef.current.reporter === callSubscriber) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
if (cleanupRef.current) {
|
|
33
|
-
cleanupRef.current();
|
|
34
|
-
// Making sure the cleanup is not called accidentally multiple times.
|
|
35
|
-
cleanupRef.current = null;
|
|
36
|
-
}
|
|
37
|
-
lastReportRef.current = {
|
|
38
|
-
reporter: callSubscriber,
|
|
39
|
-
element
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Only calling the subscriber, if there's an actual element to report.
|
|
43
|
-
if (element) {
|
|
44
|
-
cleanupRef.current = subscriber(element);
|
|
45
|
-
}
|
|
46
|
-
}, [refOrElement, subscriber]);
|
|
47
|
-
|
|
48
|
-
// On each render, we check whether a ref changed, or if we got a new raw
|
|
49
|
-
// element.
|
|
50
|
-
useEffect(() => {
|
|
51
|
-
// With this we're *technically* supporting cases where ref objects' current value changes, but only if there's a
|
|
52
|
-
// render accompanying that change as well.
|
|
53
|
-
// To guarantee we always have the right element, one must use the ref callback provided instead, but we support
|
|
54
|
-
// RefObjects to make the hook API more convenient in certain cases.
|
|
55
|
-
callSubscriber();
|
|
56
|
-
}, [callSubscriber]);
|
|
57
|
-
return useCallback(element => {
|
|
58
|
-
callbackRefElement.current = element;
|
|
59
|
-
callSubscriber();
|
|
60
|
-
}, [callSubscriber]);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Declaring my own type here instead of using the one provided by TS (available since 4.2.2), because this way I'm not
|
|
64
|
-
// forcing consumers to use a specific TS version.
|
|
65
|
-
|
|
66
10
|
// We're only using the first element of the size sequences, until future versions of the spec solidify on how
|
|
67
11
|
// exactly it'll be used for fragments in multi-column scenarios:
|
|
68
12
|
// From the spec:
|
|
@@ -88,115 +32,79 @@ function useResolvedElement(subscriber, refOrElement) {
|
|
|
88
32
|
// even though it seems we have access to results for all box types.
|
|
89
33
|
// This also means that we get to keep the current api, being able to return a simple { width, height } pair,
|
|
90
34
|
// regardless of box option.
|
|
91
|
-
const extractSize =
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
35
|
+
const extractSize = entry => {
|
|
36
|
+
let entrySize;
|
|
37
|
+
if (!entry.contentBoxSize) {
|
|
38
|
+
// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.
|
|
39
|
+
// See the 6th step in the description for the RO algorithm:
|
|
40
|
+
// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h
|
|
41
|
+
// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".
|
|
42
|
+
// In real browser implementations of course these objects differ, but the width/height values should be equivalent.
|
|
43
|
+
entrySize = [entry.contentRect.width, entry.contentRect.height];
|
|
44
|
+
} else if (entry.contentBoxSize[0]) {
|
|
45
|
+
const contentBoxSize = entry.contentBoxSize[0];
|
|
46
|
+
entrySize = [contentBoxSize.inlineSize, contentBoxSize.blockSize];
|
|
47
|
+
} else {
|
|
48
|
+
// TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's buggy
|
|
49
|
+
// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.
|
|
50
|
+
const contentBoxSize = entry.contentBoxSize;
|
|
51
|
+
entrySize = [contentBoxSize.inlineSize, contentBoxSize.blockSize];
|
|
102
52
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// @ts-ignore
|
|
109
|
-
entry[boxProp][sizeType];
|
|
53
|
+
const [width, height] = entrySize.map(d => Math.round(d));
|
|
54
|
+
return {
|
|
55
|
+
width,
|
|
56
|
+
height
|
|
57
|
+
};
|
|
110
58
|
};
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
59
|
+
const RESIZE_ELEMENT_STYLES = {
|
|
60
|
+
position: 'absolute',
|
|
61
|
+
top: 0,
|
|
62
|
+
left: 0,
|
|
63
|
+
right: 0,
|
|
64
|
+
bottom: 0,
|
|
65
|
+
pointerEvents: 'none',
|
|
66
|
+
opacity: 0,
|
|
67
|
+
overflow: 'hidden',
|
|
68
|
+
zIndex: -1
|
|
69
|
+
};
|
|
70
|
+
function ResizeElement({
|
|
71
|
+
onResize
|
|
72
|
+
}) {
|
|
73
|
+
const resizeElementRef = useRef(null);
|
|
74
|
+
const resizeCallbackRef = useRef(onResize);
|
|
75
|
+
useLayoutEffect(() => {
|
|
76
|
+
resizeCallbackRef.current = onResize;
|
|
77
|
+
}, [onResize]);
|
|
78
|
+
useLayoutEffect(() => {
|
|
79
|
+
const resizeElement = resizeElementRef.current;
|
|
80
|
+
const resizeObserver = new ResizeObserver(entries => {
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
const newSize = extractSize(entry);
|
|
83
|
+
resizeCallbackRef.current(newSize);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
resizeObserver.observe(resizeElement);
|
|
132
87
|
return () => {
|
|
133
|
-
|
|
88
|
+
resizeObserver.unobserve(resizeElement);
|
|
134
89
|
};
|
|
135
90
|
}, []);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
height: undefined
|
|
91
|
+
return /*#__PURE__*/_jsx("div", {
|
|
92
|
+
ref: resizeElementRef,
|
|
93
|
+
style: RESIZE_ELEMENT_STYLES,
|
|
94
|
+
"aria-hidden": "true"
|
|
141
95
|
});
|
|
142
|
-
|
|
143
|
-
// This block is kinda like a useEffect, only it's called whenever a new
|
|
144
|
-
// element could be resolved based on the ref option. It also has a cleanup
|
|
145
|
-
// function.
|
|
146
|
-
const refCallback = useResolvedElement(useCallback(element => {
|
|
147
|
-
// We only use a single Resize Observer instance, and we're instantiating it on demand, only once there's something to observe.
|
|
148
|
-
// This instance is also recreated when the `box` option changes, so that a new observation is fired if there was a previously observed element with a different box option.
|
|
149
|
-
if (!resizeObserverRef.current || resizeObserverRef.current.box !== opts.box || resizeObserverRef.current.round !== round) {
|
|
150
|
-
resizeObserverRef.current = {
|
|
151
|
-
box: opts.box,
|
|
152
|
-
round,
|
|
153
|
-
instance: new ResizeObserver(entries => {
|
|
154
|
-
const entry = entries[0];
|
|
155
|
-
let boxProp = 'borderBoxSize';
|
|
156
|
-
if (opts.box === 'border-box') {
|
|
157
|
-
boxProp = 'borderBoxSize';
|
|
158
|
-
} else {
|
|
159
|
-
boxProp = opts.box === 'device-pixel-content-box' ? 'devicePixelContentBoxSize' : 'contentBoxSize';
|
|
160
|
-
}
|
|
161
|
-
const reportedWidth = extractSize(entry, boxProp, 'inlineSize');
|
|
162
|
-
const reportedHeight = extractSize(entry, boxProp, 'blockSize');
|
|
163
|
-
const newWidth = reportedWidth ? round(reportedWidth) : undefined;
|
|
164
|
-
const newHeight = reportedHeight ? round(reportedHeight) : undefined;
|
|
165
|
-
if (previous.current.width !== newWidth || previous.current.height !== newHeight) {
|
|
166
|
-
const newSize = {
|
|
167
|
-
width: newWidth,
|
|
168
|
-
height: newHeight
|
|
169
|
-
};
|
|
170
|
-
previous.current.width = newWidth;
|
|
171
|
-
previous.current.height = newHeight;
|
|
172
|
-
if (onResizeRef.current) {
|
|
173
|
-
onResizeRef.current(newSize);
|
|
174
|
-
} else if (!didUnmount.current) {
|
|
175
|
-
setSize(newSize);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
resizeObserverRef.current.instance.observe(element, {
|
|
182
|
-
box: opts.box
|
|
183
|
-
});
|
|
184
|
-
return () => {
|
|
185
|
-
if (resizeObserverRef.current) {
|
|
186
|
-
resizeObserverRef.current.instance.unobserve(element);
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
}, [opts.box, round]), opts.ref);
|
|
190
|
-
return useMemo(() => ({
|
|
191
|
-
ref: refCallback,
|
|
192
|
-
width: size.width,
|
|
193
|
-
height: size.height
|
|
194
|
-
}), [refCallback, size ? size.width : null, size ? size.height : null]);
|
|
195
96
|
}
|
|
97
|
+
function sizeEquals(a, b) {
|
|
98
|
+
return a.width === b.width && a.height === b.height;
|
|
99
|
+
}
|
|
100
|
+
const NULL_SIZE = {
|
|
101
|
+
width: null,
|
|
102
|
+
height: null
|
|
103
|
+
};
|
|
196
104
|
|
|
197
105
|
/**
|
|
198
|
-
* Hook which allows to listen the resize event of any target element when it changes
|
|
199
|
-
* _Note: `useResizeObserver` will report `null` until after first render.
|
|
106
|
+
* Hook which allows to listen to the resize event of any target element when it changes size.
|
|
107
|
+
* _Note: `useResizeObserver` will report `null` sizes until after first render.
|
|
200
108
|
*
|
|
201
109
|
* @example
|
|
202
110
|
*
|
|
@@ -213,33 +121,20 @@ function useResizeObserver(opts = {}) {
|
|
|
213
121
|
* };
|
|
214
122
|
* ```
|
|
215
123
|
*/
|
|
216
|
-
export default function
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
style: {
|
|
230
|
-
position: 'absolute',
|
|
231
|
-
top: 0,
|
|
232
|
-
left: 0,
|
|
233
|
-
right: 0,
|
|
234
|
-
bottom: 0,
|
|
235
|
-
pointerEvents: 'none',
|
|
236
|
-
opacity: 0,
|
|
237
|
-
overflow: 'hidden',
|
|
238
|
-
zIndex: -1
|
|
239
|
-
},
|
|
240
|
-
"aria-hidden": "true",
|
|
241
|
-
ref: ref
|
|
124
|
+
export default function useResizeObserver() {
|
|
125
|
+
const [size, setSize] = useState(NULL_SIZE);
|
|
126
|
+
|
|
127
|
+
// Using a ref to track the previous width / height to avoid unnecessary renders.
|
|
128
|
+
const previousSizeRef = useRef(NULL_SIZE);
|
|
129
|
+
const handleResize = useCallback(newSize => {
|
|
130
|
+
if (!sizeEquals(previousSizeRef.current, newSize)) {
|
|
131
|
+
previousSizeRef.current = newSize;
|
|
132
|
+
setSize(newSize);
|
|
133
|
+
}
|
|
134
|
+
}, []);
|
|
135
|
+
const resizeElement = /*#__PURE__*/_jsx(ResizeElement, {
|
|
136
|
+
onResize: handleResize
|
|
242
137
|
});
|
|
243
|
-
return [
|
|
138
|
+
return [resizeElement, size];
|
|
244
139
|
}
|
|
245
140
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useMemo","useRef","useCallback","useEffect","useState","jsx","_jsx","useResolvedElement","subscriber","refOrElement","callbackRefElement","lastReportRef","cleanupRef","callSubscriber","element","current","HTMLElement","reporter","extractSize","entry","boxProp","sizeType","contentRect","undefined","useResizeObserver","opts","onResize","onResizeRef","round","Math","resizeObserverRef","size","setSize","width","height","didUnmount","previous","refCallback","box","instance","ResizeObserver","entries","reportedWidth","reportedHeight","newWidth","newHeight","newSize","observe","unobserve","ref","useResizeAware","sizes","resizeListener","style","position","top","left","right","bottom","pointerEvents","opacity","overflow","zIndex"],"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.tsx"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { ReactElement, RefCallback, RefObject } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tuseMemo,\n\tuseRef,\n\tuseCallback,\n\tuseEffect,\n\tuseState,\n} from '@wordpress/element';\n\ntype SubscriberCleanup = () => void;\ntype SubscriberResponse = SubscriberCleanup | void;\n\n// This of course could've been more streamlined with internal state instead of\n// refs, but then host hooks / components could not opt out of renders.\n// This could've been exported to its own module, but the current build doesn't\n// seem to work with module imports and I had no more time to spend on this...\nfunction useResolvedElement< T extends HTMLElement >(\n\tsubscriber: ( element: T ) => SubscriberResponse,\n\trefOrElement?: T | RefObject< T > | null\n): RefCallback< T > {\n\tconst callbackRefElement = useRef< T | null >( null );\n\tconst lastReportRef = useRef< {\n\t\treporter: () => void;\n\t\telement: T | null;\n\t} | null >( null );\n\tconst cleanupRef = useRef< SubscriberResponse | null >();\n\n\tconst callSubscriber = useCallback( () => {\n\t\tlet element = null;\n\t\tif ( callbackRefElement.current ) {\n\t\t\telement = callbackRefElement.current;\n\t\t} else if ( refOrElement ) {\n\t\t\tif ( refOrElement instanceof HTMLElement ) {\n\t\t\t\telement = refOrElement;\n\t\t\t} else {\n\t\t\t\telement = refOrElement.current;\n\t\t\t}\n\t\t}\n\n\t\tif (\n\t\t\tlastReportRef.current &&\n\t\t\tlastReportRef.current.element === element &&\n\t\t\tlastReportRef.current.reporter === callSubscriber\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( cleanupRef.current ) {\n\t\t\tcleanupRef.current();\n\t\t\t// Making sure the cleanup is not called accidentally multiple times.\n\t\t\tcleanupRef.current = null;\n\t\t}\n\t\tlastReportRef.current = {\n\t\t\treporter: callSubscriber,\n\t\t\telement,\n\t\t};\n\n\t\t// Only calling the subscriber, if there's an actual element to report.\n\t\tif ( element ) {\n\t\t\tcleanupRef.current = subscriber( element );\n\t\t}\n\t}, [ refOrElement, subscriber ] );\n\n\t// On each render, we check whether a ref changed, or if we got a new raw\n\t// element.\n\tuseEffect( () => {\n\t\t// With this we're *technically* supporting cases where ref objects' current value changes, but only if there's a\n\t\t// render accompanying that change as well.\n\t\t// To guarantee we always have the right element, one must use the ref callback provided instead, but we support\n\t\t// RefObjects to make the hook API more convenient in certain cases.\n\t\tcallSubscriber();\n\t}, [ callSubscriber ] );\n\n\treturn useCallback< RefCallback< T > >(\n\t\t( element ) => {\n\t\t\tcallbackRefElement.current = element;\n\t\t\tcallSubscriber();\n\t\t},\n\t\t[ callSubscriber ]\n\t);\n}\n\ntype ObservedSize = {\n\twidth: number | undefined;\n\theight: number | undefined;\n};\n\ntype ResizeHandler = ( size: ObservedSize ) => void;\n\ntype HookResponse< T extends HTMLElement > = {\n\tref: RefCallback< T >;\n} & ObservedSize;\n\n// Declaring my own type here instead of using the one provided by TS (available since 4.2.2), because this way I'm not\n// forcing consumers to use a specific TS version.\ntype ResizeObserverBoxOptions =\n\t| 'border-box'\n\t| 'content-box'\n\t| 'device-pixel-content-box';\n\ndeclare global {\n\tinterface ResizeObserverEntry {\n\t\treadonly devicePixelContentBoxSize: ReadonlyArray< ResizeObserverSize >;\n\t}\n}\n\n// We're only using the first element of the size sequences, until future versions of the spec solidify on how\n// exactly it'll be used for fragments in multi-column scenarios:\n// From the spec:\n// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,\n// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not\n// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single\n// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.\n// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)\n//\n// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,\n// regardless of the \"box\" option.\n// The spec states the following on this:\n// > This does not have any impact on which box dimensions are returned to the defined callback when the event\n// > is fired, it solely defines which box the author wishes to observe layout changes on.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// I'm not exactly clear on what this means, especially when you consider a later section stating the following:\n// > This section is non-normative. An author may desire to observe more than one CSS box.\n// > In this case, author will need to use multiple ResizeObservers.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.\n// For this reason I decided to only return the requested size,\n// even though it seems we have access to results for all box types.\n// This also means that we get to keep the current api, being able to return a simple { width, height } pair,\n// regardless of box option.\nconst extractSize = (\n\tentry: ResizeObserverEntry,\n\tboxProp: 'borderBoxSize' | 'contentBoxSize' | 'devicePixelContentBoxSize',\n\tsizeType: keyof ResizeObserverSize\n): number | undefined => {\n\tif ( ! entry[ boxProp ] ) {\n\t\tif ( boxProp === 'contentBoxSize' ) {\n\t\t\t// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.\n\t\t\t// See the 6th step in the description for the RO algorithm:\n\t\t\t// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h\n\t\t\t// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of \"content-box\".\n\t\t\t// In real browser implementations of course these objects differ, but the width/height values should be equivalent.\n\t\t\treturn entry.contentRect[\n\t\t\t\tsizeType === 'inlineSize' ? 'width' : 'height'\n\t\t\t];\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t// A couple bytes smaller than calling Array.isArray() and just as effective here.\n\treturn entry[ boxProp ][ 0 ]\n\t\t? entry[ boxProp ][ 0 ][ sizeType ]\n\t\t: // TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's current\n\t\t // behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.\n\t\t // @ts-ignore\n\t\t entry[ boxProp ][ sizeType ];\n};\n\ntype RoundingFunction = ( n: number ) => number;\n\nfunction useResizeObserver< T extends HTMLElement >(\n\topts: {\n\t\tref?: RefObject< T > | T | null | undefined;\n\t\tonResize?: ResizeHandler;\n\t\tbox?: ResizeObserverBoxOptions;\n\t\tround?: RoundingFunction;\n\t} = {}\n): HookResponse< T > {\n\t// Saving the callback as a ref. With this, I don't need to put onResize in the\n\t// effect dep array, and just passing in an anonymous function without memoising\n\t// will not reinstantiate the hook's ResizeObserver.\n\tconst onResize = opts.onResize;\n\tconst onResizeRef = useRef< ResizeHandler | undefined >( undefined );\n\tonResizeRef.current = onResize;\n\tconst round = opts.round || Math.round;\n\n\t// Using a single instance throughout the hook's lifetime\n\tconst resizeObserverRef = useRef< {\n\t\tbox?: ResizeObserverBoxOptions;\n\t\tround?: RoundingFunction;\n\t\tinstance: ResizeObserver;\n\t} >();\n\n\tconst [ size, setSize ] = useState< {\n\t\twidth?: number;\n\t\theight?: number;\n\t} >( {\n\t\twidth: undefined,\n\t\theight: undefined,\n\t} );\n\n\t// In certain edge cases the RO might want to report a size change just after\n\t// the component unmounted.\n\tconst didUnmount = useRef( false );\n\tuseEffect( () => {\n\t\tdidUnmount.current = false;\n\t\treturn () => {\n\t\t\tdidUnmount.current = true;\n\t\t};\n\t}, [] );\n\n\t// Using a ref to track the previous width / height to avoid unnecessary renders.\n\tconst previous: {\n\t\tcurrent: {\n\t\t\twidth?: number;\n\t\t\theight?: number;\n\t\t};\n\t} = useRef( {\n\t\twidth: undefined,\n\t\theight: undefined,\n\t} );\n\n\t// This block is kinda like a useEffect, only it's called whenever a new\n\t// element could be resolved based on the ref option. It also has a cleanup\n\t// function.\n\tconst refCallback = useResolvedElement< T >(\n\t\tuseCallback(\n\t\t\t( element ) => {\n\t\t\t\t// We only use a single Resize Observer instance, and we're instantiating it on demand, only once there's something to observe.\n\t\t\t\t// This instance is also recreated when the `box` option changes, so that a new observation is fired if there was a previously observed element with a different box option.\n\t\t\t\tif (\n\t\t\t\t\t! resizeObserverRef.current ||\n\t\t\t\t\tresizeObserverRef.current.box !== opts.box ||\n\t\t\t\t\tresizeObserverRef.current.round !== round\n\t\t\t\t) {\n\t\t\t\t\tresizeObserverRef.current = {\n\t\t\t\t\t\tbox: opts.box,\n\t\t\t\t\t\tround,\n\t\t\t\t\t\tinstance: new ResizeObserver( ( entries ) => {\n\t\t\t\t\t\t\tconst entry = entries[ 0 ];\n\n\t\t\t\t\t\t\tlet boxProp:\n\t\t\t\t\t\t\t\t| 'borderBoxSize'\n\t\t\t\t\t\t\t\t| 'contentBoxSize'\n\t\t\t\t\t\t\t\t| 'devicePixelContentBoxSize' = 'borderBoxSize';\n\t\t\t\t\t\t\tif ( opts.box === 'border-box' ) {\n\t\t\t\t\t\t\t\tboxProp = 'borderBoxSize';\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tboxProp =\n\t\t\t\t\t\t\t\t\topts.box === 'device-pixel-content-box'\n\t\t\t\t\t\t\t\t\t\t? 'devicePixelContentBoxSize'\n\t\t\t\t\t\t\t\t\t\t: 'contentBoxSize';\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst reportedWidth = extractSize(\n\t\t\t\t\t\t\t\tentry,\n\t\t\t\t\t\t\t\tboxProp,\n\t\t\t\t\t\t\t\t'inlineSize'\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tconst reportedHeight = extractSize(\n\t\t\t\t\t\t\t\tentry,\n\t\t\t\t\t\t\t\tboxProp,\n\t\t\t\t\t\t\t\t'blockSize'\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tconst newWidth = reportedWidth\n\t\t\t\t\t\t\t\t? round( reportedWidth )\n\t\t\t\t\t\t\t\t: undefined;\n\t\t\t\t\t\t\tconst newHeight = reportedHeight\n\t\t\t\t\t\t\t\t? round( reportedHeight )\n\t\t\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tprevious.current.width !== newWidth ||\n\t\t\t\t\t\t\t\tprevious.current.height !== newHeight\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tconst newSize = {\n\t\t\t\t\t\t\t\t\twidth: newWidth,\n\t\t\t\t\t\t\t\t\theight: newHeight,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\tprevious.current.width = newWidth;\n\t\t\t\t\t\t\t\tprevious.current.height = newHeight;\n\t\t\t\t\t\t\t\tif ( onResizeRef.current ) {\n\t\t\t\t\t\t\t\t\tonResizeRef.current( newSize );\n\t\t\t\t\t\t\t\t} else if ( ! didUnmount.current ) {\n\t\t\t\t\t\t\t\t\tsetSize( newSize );\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} ),\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tresizeObserverRef.current.instance.observe( element, {\n\t\t\t\t\tbox: opts.box,\n\t\t\t\t} );\n\n\t\t\t\treturn () => {\n\t\t\t\t\tif ( resizeObserverRef.current ) {\n\t\t\t\t\t\tresizeObserverRef.current.instance.unobserve( element );\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t},\n\t\t\t[ opts.box, round ]\n\t\t),\n\t\topts.ref\n\t);\n\n\treturn useMemo(\n\t\t() => ( {\n\t\t\tref: refCallback,\n\t\t\twidth: size.width,\n\t\t\theight: size.height,\n\t\t} ),\n\t\t[ refCallback, size ? size.width : null, size ? size.height : null ]\n\t);\n}\n\n/**\n * Hook which allows to listen the resize event of any target element when it changes sizes.\n * _Note: `useResizeObserver` will report `null` until after first render.\n *\n * @example\n *\n * ```js\n * const App = () => {\n * \tconst [ resizeListener, sizes ] = useResizeObserver();\n *\n * \treturn (\n * \t\t<div>\n * \t\t\t{ resizeListener }\n * \t\t\tYour content here\n * \t\t</div>\n * \t);\n * };\n * ```\n */\nexport default function useResizeAware(): [\n\tReactElement,\n\t{ width: number | null; height: number | null },\n] {\n\tconst { ref, width, height } = useResizeObserver();\n\tconst sizes = useMemo( () => {\n\t\treturn { width: width ?? null, height: height ?? null };\n\t}, [ width, height ] );\n\tconst resizeListener = (\n\t\t<div\n\t\t\tstyle={ {\n\t\t\t\tposition: 'absolute',\n\t\t\t\ttop: 0,\n\t\t\t\tleft: 0,\n\t\t\t\tright: 0,\n\t\t\t\tbottom: 0,\n\t\t\t\tpointerEvents: 'none',\n\t\t\t\topacity: 0,\n\t\t\t\toverflow: 'hidden',\n\t\t\t\tzIndex: -1,\n\t\t\t} }\n\t\t\taria-hidden=\"true\"\n\t\t\tref={ ref }\n\t\t/>\n\t);\n\treturn [ resizeListener, sizes ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA,SACCA,OAAO,EACPC,MAAM,EACNC,WAAW,EACXC,SAAS,EACTC,QAAQ,QACF,oBAAoB;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAK5B;AACA;AACA;AACA;AACA,SAASC,kBAAkBA,CAC1BC,UAAgD,EAChDC,YAAwC,EACrB;EACnB,MAAMC,kBAAkB,GAAGT,MAAM,CAAc,IAAK,CAAC;EACrD,MAAMU,aAAa,GAAGV,MAAM,CAGhB,IAAK,CAAC;EAClB,MAAMW,UAAU,GAAGX,MAAM,CAA8B,CAAC;EAExD,MAAMY,cAAc,GAAGX,WAAW,CAAE,MAAM;IACzC,IAAIY,OAAO,GAAG,IAAI;IAClB,IAAKJ,kBAAkB,CAACK,OAAO,EAAG;MACjCD,OAAO,GAAGJ,kBAAkB,CAACK,OAAO;IACrC,CAAC,MAAM,IAAKN,YAAY,EAAG;MAC1B,IAAKA,YAAY,YAAYO,WAAW,EAAG;QAC1CF,OAAO,GAAGL,YAAY;MACvB,CAAC,MAAM;QACNK,OAAO,GAAGL,YAAY,CAACM,OAAO;MAC/B;IACD;IAEA,IACCJ,aAAa,CAACI,OAAO,IACrBJ,aAAa,CAACI,OAAO,CAACD,OAAO,KAAKA,OAAO,IACzCH,aAAa,CAACI,OAAO,CAACE,QAAQ,KAAKJ,cAAc,EAChD;MACD;IACD;IAEA,IAAKD,UAAU,CAACG,OAAO,EAAG;MACzBH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AAE,QAAQ;MAClBC,MAAM,EAAE,CAAC;IACV,CAAG;IACH,eAAY,MAAM;IAClBb,GAAG,EAAGA;EAAK,CACX,CACD;EACD,OAAO,CAAEG,cAAc,EAAED,KAAK,CAAE;AACjC","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["useCallback","useLayoutEffect","useRef","useState","jsx","_jsx","extractSize","entry","entrySize","contentBoxSize","contentRect","width","height","inlineSize","blockSize","map","d","Math","round","RESIZE_ELEMENT_STYLES","position","top","left","right","bottom","pointerEvents","opacity","overflow","zIndex","ResizeElement","onResize","resizeElementRef","resizeCallbackRef","current","resizeElement","resizeObserver","ResizeObserver","entries","newSize","observe","unobserve","ref","style","sizeEquals","a","b","NULL_SIZE","useResizeObserver","size","setSize","previousSizeRef","handleResize"],"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.tsx"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { ReactElement } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport {\n\tuseCallback,\n\tuseLayoutEffect,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\n\ntype ObservedSize = {\n\twidth: number | null;\n\theight: number | null;\n};\n\n// We're only using the first element of the size sequences, until future versions of the spec solidify on how\n// exactly it'll be used for fragments in multi-column scenarios:\n// From the spec:\n// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,\n// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not\n// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single\n// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.\n// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)\n//\n// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,\n// regardless of the \"box\" option.\n// The spec states the following on this:\n// > This does not have any impact on which box dimensions are returned to the defined callback when the event\n// > is fired, it solely defines which box the author wishes to observe layout changes on.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// I'm not exactly clear on what this means, especially when you consider a later section stating the following:\n// > This section is non-normative. An author may desire to observe more than one CSS box.\n// > In this case, author will need to use multiple ResizeObservers.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.\n// For this reason I decided to only return the requested size,\n// even though it seems we have access to results for all box types.\n// This also means that we get to keep the current api, being able to return a simple { width, height } pair,\n// regardless of box option.\nconst extractSize = ( entry: ResizeObserverEntry ): ObservedSize => {\n\tlet entrySize;\n\tif ( ! entry.contentBoxSize ) {\n\t\t// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.\n\t\t// See the 6th step in the description for the RO algorithm:\n\t\t// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h\n\t\t// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of \"content-box\".\n\t\t// In real browser implementations of course these objects differ, but the width/height values should be equivalent.\n\t\tentrySize = [ entry.contentRect.width, entry.contentRect.height ];\n\t} else if ( entry.contentBoxSize[ 0 ] ) {\n\t\tconst contentBoxSize = entry.contentBoxSize[ 0 ];\n\t\tentrySize = [ contentBoxSize.inlineSize, contentBoxSize.blockSize ];\n\t} else {\n\t\t// TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's buggy\n\t\t// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.\n\t\tconst contentBoxSize =\n\t\t\tentry.contentBoxSize as unknown as ResizeObserverSize;\n\t\tentrySize = [ contentBoxSize.inlineSize, contentBoxSize.blockSize ];\n\t}\n\n\tconst [ width, height ] = entrySize.map( ( d ) => Math.round( d ) );\n\treturn { width, height };\n};\n\nconst RESIZE_ELEMENT_STYLES = {\n\tposition: 'absolute',\n\ttop: 0,\n\tleft: 0,\n\tright: 0,\n\tbottom: 0,\n\tpointerEvents: 'none',\n\topacity: 0,\n\toverflow: 'hidden',\n\tzIndex: -1,\n} as const;\n\ntype ResizeElementProps = {\n\tonResize: ( s: ObservedSize ) => void;\n};\n\nfunction ResizeElement( { onResize }: ResizeElementProps ) {\n\tconst resizeElementRef = useRef< HTMLDivElement >( null );\n\tconst resizeCallbackRef = useRef( onResize );\n\n\tuseLayoutEffect( () => {\n\t\tresizeCallbackRef.current = onResize;\n\t}, [ onResize ] );\n\n\tuseLayoutEffect( () => {\n\t\tconst resizeElement = resizeElementRef.current as HTMLDivElement;\n\t\tconst resizeObserver = new ResizeObserver( ( entries ) => {\n\t\t\tfor ( const entry of entries ) {\n\t\t\t\tconst newSize = extractSize( entry );\n\t\t\t\tresizeCallbackRef.current( newSize );\n\t\t\t}\n\t\t} );\n\n\t\tresizeObserver.observe( resizeElement );\n\n\t\treturn () => {\n\t\t\tresizeObserver.unobserve( resizeElement );\n\t\t};\n\t}, [] );\n\n\treturn (\n\t\t<div\n\t\t\tref={ resizeElementRef }\n\t\t\tstyle={ RESIZE_ELEMENT_STYLES }\n\t\t\taria-hidden=\"true\"\n\t\t/>\n\t);\n}\n\nfunction sizeEquals( a: ObservedSize, b: ObservedSize ) {\n\treturn a.width === b.width && a.height === b.height;\n}\n\nconst NULL_SIZE: ObservedSize = { width: null, height: null };\n\n/**\n * Hook which allows to listen to the resize event of any target element when it changes size.\n * _Note: `useResizeObserver` will report `null` sizes until after first render.\n *\n * @example\n *\n * ```js\n * const App = () => {\n * \tconst [ resizeListener, sizes ] = useResizeObserver();\n *\n * \treturn (\n * \t\t<div>\n * \t\t\t{ resizeListener }\n * \t\t\tYour content here\n * \t\t</div>\n * \t);\n * };\n * ```\n */\nexport default function useResizeObserver(): [ ReactElement, ObservedSize ] {\n\tconst [ size, setSize ] = useState( NULL_SIZE );\n\n\t// Using a ref to track the previous width / height to avoid unnecessary renders.\n\tconst previousSizeRef = useRef( NULL_SIZE );\n\n\tconst handleResize = useCallback( ( newSize: ObservedSize ) => {\n\t\tif ( ! sizeEquals( previousSizeRef.current, newSize ) ) {\n\t\t\tpreviousSizeRef.current = newSize;\n\t\t\tsetSize( newSize );\n\t\t}\n\t}, [] );\n\n\tconst resizeElement = <ResizeElement onResize={ handleResize } />;\n\treturn [ resizeElement, size ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA,SACCA,WAAW,EACXC,eAAe,EACfC,MAAM,EACNC,QAAQ,QACF,oBAAoB;AAAC,SAAAC,GAAA,IAAAC,IAAA;AAO5B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,WAAW,GAAKC,KAA0B,IAAoB;EACnE,IAAIC,SAAS;EACb,IAAK,CAAED,KAAK,CAACE,cAAc,EAAG;IAC7B;IACA;IACA;IACA;IACA;IACAD,SAAS,GAAG,CAAED,KAAK,CAACG,WAAW,CAACC,KAAK,EAAEJ,KAAK,CAACG,WAAW,CAACE,MAAM,CAAE;EAClE,CAAC,MAAM,IAAKL,KAAK,CAACE,cAAc,CAAE,CAAC,CAAE,EAAG;IACvC,MAAMA,cAAc,GAAGF,KAAK,CAACE,cAAc,CAAE,CAAC,CAAE;IAChDD,SAAS,GAAG,CAAEC,cAAc,CAACI,UAAU,EAAEJ,cAAc,CAACK,SAAS,CAAE;EACpE,CAAC,MAAM;IACN;IACA;IACA,MAAML,cAAc,GACnBF,KAAK,CAACE,cAA+C;IACtDD,SAAS,GAAG,CAAEC,cAAc,CAACI,UAAU,EAAEJ,cAAc,CAACK,SAAS,CAAE;EACpE;EAEA,MAAM,CAAEH,KAAK,EAAEC,MAAM,CAAE,GAAGJ,SAAS,CAACO,GAAG,CAAIC,CAAC,IAAMC,IAAI,CAACC,KAAK,CAAEF,CAAE,CAAE,CAAC;EACnE,OAAO;IAAEL,KAAK;IAAEC;EAAO,CAAC;AACzB,CAAC;AAED,MAAMO,qBAAqB,GAAG;EAC7BC,QAAQ,EAAE,UAAU;EACpBC,GAAG,EAAE,CAAC;EACNC,IAAI,EAAE,CAAC;EACPC,KAAK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;EAAEC,MAAM,EAAE;AAAK,CAAC;;AAE7D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASmC,iBAAiBA,CAAA,EAAmC;EAC3E,MAAM,CAAEC,IAAI,EAAEC,OAAO,CAAE,GAAG9C,QAAQ,CAAE2C,SAAU,CAAC;;EAE/C;EACA,MAAMI,eAAe,GAAGhD,MAAM,CAAE4C,SAAU,CAAC;EAE3C,MAAMK,YAAY,GAAGnD,WAAW,CAAIsC,OAAqB,IAAM;IAC9D,IAAK,CAAEK,UAAU,CAAEO,eAAe,CAACjB,OAAO,EAAEK,OAAQ,CAAC,EAAG;MACvDY,eAAe,CAACjB,OAAO,GAAGK,OAAO;MACjCW,OAAO,CAAEX,OAAQ,CAAC;IACnB;EACD,CAAC,EAAE,EAAG,CAAC;EAEP,MAAMJ,aAAa,gBAAG7B,IAAA,CAACwB,aAAa;IAACC,QAAQ,EAAGqB;EAAc,CAAE,CAAC;EACjE,OAAO,CAAEjB,aAAa,EAAEc,IAAI,CAAE;AAC/B","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-merge-refs/index.js"],"names":[],"mappings":"AA4BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,qCALwC,IAAI,SAA/B,OAAQ,OAAO,EAAE,GAAG,CAAC,GAAG,CAAE,QAC5B,KAAK,CAAC,IAAI,CAAC,GAEV,OAAO,OAAO,EAAE,WAAW,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-merge-refs/index.js"],"names":[],"mappings":"AA4BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,qCALwC,IAAI,SAA/B,OAAQ,OAAO,EAAE,GAAG,CAAC,GAAG,CAAE,QAC5B,KAAK,CAAC,IAAI,CAAC,GAEV,OAAO,OAAO,EAAE,WAAW,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CA+DzD;wBA7HY,CAAC,IACD,CAAC,SAAS,OAAO,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK"}
|
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import type { ReactElement } from 'react';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
5
|
+
type ObservedSize = {
|
|
6
|
+
width: number | null;
|
|
7
|
+
height: number | null;
|
|
8
|
+
};
|
|
10
9
|
/**
|
|
11
|
-
* Hook which allows to listen the resize event of any target element when it changes
|
|
12
|
-
* _Note: `useResizeObserver` will report `null` until after first render.
|
|
10
|
+
* Hook which allows to listen to the resize event of any target element when it changes size.
|
|
11
|
+
* _Note: `useResizeObserver` will report `null` sizes until after first render.
|
|
13
12
|
*
|
|
14
13
|
* @example
|
|
15
14
|
*
|
|
@@ -26,11 +25,6 @@ declare global {
|
|
|
26
25
|
* };
|
|
27
26
|
* ```
|
|
28
27
|
*/
|
|
29
|
-
export default function
|
|
30
|
-
|
|
31
|
-
{
|
|
32
|
-
width: number | null;
|
|
33
|
-
height: number | null;
|
|
34
|
-
}
|
|
35
|
-
];
|
|
28
|
+
export default function useResizeObserver(): [ReactElement, ObservedSize];
|
|
29
|
+
export {};
|
|
36
30
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-resize-observer/index.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-resize-observer/index.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAY1C,KAAK,YAAY,GAAG;IACnB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AA0GF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,IAAI,CAAE,YAAY,EAAE,YAAY,CAAE,CAe1E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/compose",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.7.0",
|
|
4
4
|
"description": "WordPress higher-order components (HOCs).",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@babel/runtime": "^7.16.0",
|
|
34
34
|
"@types/mousetrap": "^1.6.8",
|
|
35
|
-
"@wordpress/deprecated": "^4.
|
|
36
|
-
"@wordpress/dom": "^4.
|
|
37
|
-
"@wordpress/element": "^6.
|
|
38
|
-
"@wordpress/is-shallow-equal": "^5.
|
|
39
|
-
"@wordpress/keycodes": "^4.
|
|
40
|
-
"@wordpress/priority-queue": "^3.
|
|
41
|
-
"@wordpress/undo-manager": "^1.
|
|
35
|
+
"@wordpress/deprecated": "^4.7.0",
|
|
36
|
+
"@wordpress/dom": "^4.7.0",
|
|
37
|
+
"@wordpress/element": "^6.7.0",
|
|
38
|
+
"@wordpress/is-shallow-equal": "^5.7.0",
|
|
39
|
+
"@wordpress/keycodes": "^4.7.0",
|
|
40
|
+
"@wordpress/priority-queue": "^3.7.0",
|
|
41
|
+
"@wordpress/undo-manager": "^1.7.0",
|
|
42
42
|
"change-case": "^4.1.2",
|
|
43
43
|
"clipboard": "^2.0.11",
|
|
44
44
|
"mousetrap": "^1.6.5",
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "c90d920de07c53dff82c5914635b56fafa503b7f"
|
|
54
54
|
}
|