@wordpress/compose 5.6.0 → 5.9.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 +18 -0
- package/README.md +37 -11
- package/build/higher-order/if-condition/index.js +14 -10
- package/build/higher-order/if-condition/index.js.map +1 -1
- package/build/higher-order/pure/index.js +9 -5
- package/build/higher-order/pure/index.js.map +1 -1
- package/build/higher-order/with-global-events/index.js +2 -2
- package/build/higher-order/with-global-events/index.js.map +1 -1
- package/build/higher-order/with-instance-id/index.js +3 -3
- package/build/higher-order/with-instance-id/index.js.map +1 -1
- package/build/higher-order/with-preferred-color-scheme/index.native.js +2 -2
- package/build/higher-order/with-preferred-color-scheme/index.native.js.map +1 -1
- package/build/higher-order/with-safe-timeout/index.js +10 -7
- package/build/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build/higher-order/with-state/index.js +2 -2
- package/build/higher-order/with-state/index.js.map +1 -1
- package/build/hooks/use-copy-to-clipboard/index.js +1 -1
- package/build/hooks/use-copy-to-clipboard/index.js.map +1 -1
- package/build/hooks/use-debounce/index.js.map +1 -1
- package/build/hooks/use-dialog/index.js.map +1 -1
- package/build/hooks/use-disabled/index.js +77 -7
- package/build/hooks/use-disabled/index.js.map +1 -1
- package/build/hooks/use-focus-on-mount/index.js +6 -1
- package/build/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build/hooks/use-focus-return/index.js.map +1 -1
- package/build/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build/hooks/use-merge-refs/index.js.map +1 -1
- package/build/hooks/use-ref-effect/index.js.map +1 -1
- package/build/hooks/use-resize-observer/index.js +224 -13
- package/build/hooks/use-resize-observer/index.js.map +1 -1
- package/build/hooks/use-resize-observer/index.native.js +0 -2
- package/build/hooks/use-resize-observer/index.native.js.map +1 -1
- package/build/hooks/use-throttle/index.js.map +1 -1
- package/build/index.js +54 -13
- package/build/index.js.map +1 -1
- package/build/index.native.js +48 -7
- package/build/index.native.js.map +1 -1
- package/build/utils/create-higher-order-component/index.js +19 -5
- package/build/utils/create-higher-order-component/index.js.map +1 -1
- package/build-module/higher-order/if-condition/index.js +13 -7
- package/build-module/higher-order/if-condition/index.js.map +1 -1
- package/build-module/higher-order/pure/index.js +10 -9
- package/build-module/higher-order/pure/index.js.map +1 -1
- package/build-module/higher-order/with-global-events/index.js +1 -1
- package/build-module/higher-order/with-global-events/index.js.map +1 -1
- package/build-module/higher-order/with-instance-id/index.js +3 -3
- package/build-module/higher-order/with-instance-id/index.js.map +1 -1
- package/build-module/higher-order/with-preferred-color-scheme/index.native.js +1 -1
- package/build-module/higher-order/with-preferred-color-scheme/index.native.js.map +1 -1
- package/build-module/higher-order/with-safe-timeout/index.js +9 -7
- package/build-module/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build-module/higher-order/with-state/index.js +1 -1
- package/build-module/higher-order/with-state/index.js.map +1 -1
- package/build-module/hooks/use-copy-to-clipboard/index.js +1 -1
- package/build-module/hooks/use-copy-to-clipboard/index.js.map +1 -1
- package/build-module/hooks/use-debounce/index.js.map +1 -1
- package/build-module/hooks/use-dialog/index.js.map +1 -1
- package/build-module/hooks/use-disabled/index.js +78 -7
- package/build-module/hooks/use-disabled/index.js.map +1 -1
- package/build-module/hooks/use-focus-on-mount/index.js +6 -1
- package/build-module/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build-module/hooks/use-focus-return/index.js.map +1 -1
- package/build-module/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build-module/hooks/use-merge-refs/index.js.map +1 -1
- package/build-module/hooks/use-ref-effect/index.js.map +1 -1
- package/build-module/hooks/use-resize-observer/index.js +226 -9
- package/build-module/hooks/use-resize-observer/index.js.map +1 -1
- package/build-module/hooks/use-resize-observer/index.native.js +0 -2
- package/build-module/hooks/use-resize-observer/index.native.js.map +1 -1
- package/build-module/hooks/use-throttle/index.js.map +1 -1
- package/build-module/index.js +3 -3
- package/build-module/index.js.map +1 -1
- package/build-module/index.native.js +3 -2
- package/build-module/index.native.js.map +1 -1
- package/build-module/utils/create-higher-order-component/index.js +18 -4
- package/build-module/utils/create-higher-order-component/index.js.map +1 -1
- package/build-types/higher-order/if-condition/index.d.ts +5 -2
- package/build-types/higher-order/if-condition/index.d.ts.map +1 -1
- package/build-types/higher-order/pure/index.d.ts +1 -1
- package/build-types/higher-order/pure/index.d.ts.map +1 -1
- package/build-types/higher-order/with-global-events/index.d.ts.map +1 -1
- package/build-types/higher-order/with-instance-id/index.d.ts +8 -4
- package/build-types/higher-order/with-instance-id/index.d.ts.map +1 -1
- package/build-types/higher-order/with-safe-timeout/index.d.ts +37 -4
- package/build-types/higher-order/with-safe-timeout/index.d.ts.map +1 -1
- package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
- package/build-types/hooks/use-dialog/index.d.ts.map +1 -1
- package/build-types/hooks/use-disabled/index.d.ts +6 -2
- package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
- package/build-types/hooks/use-focus-on-mount/index.d.ts.map +1 -1
- package/build-types/hooks/use-focus-return/index.d.ts.map +1 -1
- package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
- package/build-types/hooks/use-ref-effect/index.d.ts +1 -1
- package/build-types/hooks/use-ref-effect/index.d.ts.map +1 -1
- package/build-types/hooks/use-resize-observer/index.d.ts +32 -2
- package/build-types/hooks/use-resize-observer/index.d.ts.map +1 -1
- package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
- package/build-types/index.d.ts +2 -2
- package/build-types/utils/create-higher-order-component/index.d.ts +5 -12
- package/build-types/utils/create-higher-order-component/index.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/higher-order/if-condition/index.tsx +11 -7
- package/src/higher-order/pure/index.tsx +29 -31
- package/src/higher-order/with-global-events/index.js +4 -2
- package/src/higher-order/with-global-events/test/index.js +2 -2
- package/src/higher-order/with-instance-id/index.tsx +19 -10
- package/src/higher-order/with-preferred-color-scheme/index.native.js +1 -1
- package/src/higher-order/with-safe-timeout/index.tsx +22 -19
- package/src/higher-order/with-state/index.js +1 -1
- package/src/hooks/use-copy-to-clipboard/index.js +1 -1
- package/src/hooks/use-debounce/index.js +4 -5
- package/src/hooks/use-dialog/index.js +13 -12
- package/src/hooks/use-disabled/index.js +139 -50
- package/src/hooks/use-focus-on-mount/index.js +6 -1
- package/src/hooks/use-focus-return/index.js +3 -1
- package/src/hooks/use-keyboard-shortcut/index.js +1 -1
- package/src/hooks/use-merge-refs/index.js +2 -1
- package/src/hooks/use-ref-effect/index.ts +2 -2
- package/src/hooks/use-resize-observer/index.native.js +0 -2
- package/src/hooks/use-resize-observer/index.tsx +362 -0
- package/src/hooks/use-throttle/index.js +4 -5
- package/src/index.js +3 -3
- package/src/index.native.js +3 -2
- package/src/utils/create-higher-order-component/index.ts +30 -27
- package/src/utils/create-higher-order-component/test/index.js +1 -1
- package/tsconfig.json +2 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/src/hooks/use-resize-observer/index.js +0 -31
|
@@ -40,11 +40,13 @@ const DISABLED_ELIGIBLE_NODE_NAMES = ['BUTTON', 'FIELDSET', 'INPUT', 'OPTGROUP',
|
|
|
40
40
|
* (input fields, links, buttons, etc.) need to be disabled. This hook adds the
|
|
41
41
|
* behavior to disable nested DOM elements to the returned ref.
|
|
42
42
|
*
|
|
43
|
+
* @param {Object} config Configuration object.
|
|
44
|
+
* @param {boolean=} config.isDisabled Whether the element should be disabled.
|
|
43
45
|
* @return {import('react').RefCallback<HTMLElement>} Element Ref.
|
|
44
46
|
*
|
|
45
47
|
* @example
|
|
46
48
|
* ```js
|
|
47
|
-
* import {
|
|
49
|
+
* import { useDisabled } from '@wordpress/compose';
|
|
48
50
|
* const DisabledExample = () => {
|
|
49
51
|
* const disabledRef = useDisabled();
|
|
50
52
|
* return (
|
|
@@ -58,34 +60,101 @@ const DISABLED_ELIGIBLE_NODE_NAMES = ['BUTTON', 'FIELDSET', 'INPUT', 'OPTGROUP',
|
|
|
58
60
|
*/
|
|
59
61
|
|
|
60
62
|
function useDisabled() {
|
|
63
|
+
let {
|
|
64
|
+
isDisabled: isDisabledProp = false
|
|
65
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
61
66
|
return (0, _useRefEffect.default)(node => {
|
|
67
|
+
if (isDisabledProp) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
/** A variable keeping track of the previous updates in order to restore them. */
|
|
71
|
+
|
|
72
|
+
/** @type {Function[]} */
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
const updates = [];
|
|
76
|
+
|
|
62
77
|
const disable = () => {
|
|
63
|
-
node.style.
|
|
64
|
-
|
|
78
|
+
if (node.style.getPropertyValue('user-select') !== 'none') {
|
|
79
|
+
const previousValue = node.style.getPropertyValue('user-select');
|
|
80
|
+
node.style.setProperty('user-select', 'none');
|
|
81
|
+
node.style.setProperty('-webkit-user-select', 'none');
|
|
82
|
+
updates.push(() => {
|
|
83
|
+
if (!node.isConnected) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
node.style.setProperty('user-select', previousValue);
|
|
88
|
+
node.style.setProperty('-webkit-user-select', previousValue);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
65
91
|
|
|
66
92
|
_dom.focus.focusable.find(node).forEach(focusable => {
|
|
67
93
|
var _node$ownerDocument$d;
|
|
68
94
|
|
|
69
|
-
if ((0, _lodash.includes)(DISABLED_ELIGIBLE_NODE_NAMES, focusable.nodeName)
|
|
95
|
+
if ((0, _lodash.includes)(DISABLED_ELIGIBLE_NODE_NAMES, focusable.nodeName) && // @ts-ignore
|
|
96
|
+
!focusable.disabled) {
|
|
70
97
|
focusable.setAttribute('disabled', '');
|
|
98
|
+
updates.push(() => {
|
|
99
|
+
if (!focusable.isConnected) {
|
|
100
|
+
return;
|
|
101
|
+
} // @ts-ignore
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
focusable.disabled = false;
|
|
105
|
+
});
|
|
71
106
|
}
|
|
72
107
|
|
|
73
|
-
if (focusable.nodeName === 'A') {
|
|
108
|
+
if (focusable.nodeName === 'A' && focusable.getAttribute('tabindex') !== '-1') {
|
|
109
|
+
const previousValue = focusable.getAttribute('tabindex');
|
|
74
110
|
focusable.setAttribute('tabindex', '-1');
|
|
111
|
+
updates.push(() => {
|
|
112
|
+
if (!focusable.isConnected) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!previousValue) {
|
|
117
|
+
focusable.removeAttribute('tabindex');
|
|
118
|
+
} else {
|
|
119
|
+
focusable.setAttribute('tabindex', previousValue);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
75
122
|
}
|
|
76
123
|
|
|
77
124
|
const tabIndex = focusable.getAttribute('tabindex');
|
|
78
125
|
|
|
79
126
|
if (tabIndex !== null && tabIndex !== '-1') {
|
|
80
127
|
focusable.removeAttribute('tabindex');
|
|
128
|
+
updates.push(() => {
|
|
129
|
+
if (!focusable.isConnected) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
focusable.setAttribute('tabindex', tabIndex);
|
|
134
|
+
});
|
|
81
135
|
}
|
|
82
136
|
|
|
83
|
-
if (focusable.hasAttribute('contenteditable')) {
|
|
137
|
+
if (focusable.hasAttribute('contenteditable') && focusable.getAttribute('contenteditable') !== 'false') {
|
|
84
138
|
focusable.setAttribute('contenteditable', 'false');
|
|
139
|
+
updates.push(() => {
|
|
140
|
+
if (!focusable.isConnected) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
focusable.setAttribute('contenteditable', 'true');
|
|
145
|
+
});
|
|
85
146
|
}
|
|
86
147
|
|
|
87
148
|
if ((_node$ownerDocument$d = node.ownerDocument.defaultView) !== null && _node$ownerDocument$d !== void 0 && _node$ownerDocument$d.HTMLElement && focusable instanceof node.ownerDocument.defaultView.HTMLElement) {
|
|
149
|
+
const previousValue = focusable.style.getPropertyValue('pointer-events');
|
|
88
150
|
focusable.style.setProperty('pointer-events', 'none');
|
|
151
|
+
updates.push(() => {
|
|
152
|
+
if (!focusable.isConnected) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
focusable.style.setProperty('pointer-events', previousValue);
|
|
157
|
+
});
|
|
89
158
|
}
|
|
90
159
|
});
|
|
91
160
|
}; // Debounce re-disable since disabling process itself will incur
|
|
@@ -110,7 +179,8 @@ function useDisabled() {
|
|
|
110
179
|
}
|
|
111
180
|
|
|
112
181
|
debouncedDisable.cancel();
|
|
182
|
+
updates.forEach(update => update());
|
|
113
183
|
};
|
|
114
|
-
}, []);
|
|
184
|
+
}, [isDisabledProp]);
|
|
115
185
|
}
|
|
116
186
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-disabled/index.js"],"names":["DISABLED_ELIGIBLE_NODE_NAMES","useDisabled","node","disable","style","setProperty","focus","focusable","find","forEach","nodeName","setAttribute","tabIndex","getAttribute","removeAttribute","hasAttribute","ownerDocument","defaultView","HTMLElement","debouncedDisable","undefined","leading","observer","window","MutationObserver","observe","childList","attributes","subtree","disconnect","cancel"],"mappings":";;;;;;;;;AAGA;;AAKA;;AAKA;;AAbA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,4BAA4B,GAAG,CACpC,QADoC,EAEpC,UAFoC,EAGpC,OAHoC,EAIpC,UAJoC,EAKpC,QALoC,EAMpC,QANoC,EAOpC,UAPoC,CAArC;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,SAASC,WAAT,GAAuB;AACrC,SAAO,2BAAgBC,IAAF,IAAY;AAChC,UAAMC,OAAO,GAAG,MAAM;AACrBD,MAAAA,IAAI,CAACE,KAAL,C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,CAACC,gBAAX,CAA6BL,gBAA7B,CAAjB;AACAG,IAAAA,QAAQ,CAACG,OAAT,CAAkBvB,IAAlB,EAAwB;AACvBwB,MAAAA,SAAS,EAAE,IADY;AAEvBC,MAAAA,UAAU,EAAE,IAFW;AAGvBC,MAAAA,OAAO,EAAE;AAHc,KAAxB;AAMA,WAAO,MAAM;AACZ,UAAKN,QAAL,EAAgB;AACfA,QAAAA,QAAQ,CAACO,UAAT;AACA;;AACDV,MAAAA,gBAAgB,CAACW,MAAjB;AACA,KALD;AAMA,GAvDM,EAuDJ,EAvDI,CAAP;AAwDA","sourcesContent":["/**\n * External dependencies\n */\nimport { includes, debounce } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/**\n * Names of control nodes which qualify for disabled behavior.\n *\n * See WHATWG HTML Standard: 4.10.18.5: \"Enabling and disabling form controls: the disabled attribute\".\n *\n * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls:-the-disabled-attribute\n *\n * @type {string[]}\n */\nconst DISABLED_ELIGIBLE_NODE_NAMES = [\n\t'BUTTON',\n\t'FIELDSET',\n\t'INPUT',\n\t'OPTGROUP',\n\t'OPTION',\n\t'SELECT',\n\t'TEXTAREA',\n];\n\n/**\n * In some circumstances, such as block previews, all focusable DOM elements\n * (input fields, links, buttons, etc.) need to be disabled. This hook adds the\n * behavior to disable nested DOM elements to the returned ref.\n *\n * @return {import('react').RefCallback<HTMLElement>} Element Ref.\n *\n * @example\n * ```js\n * import { __experimentalUseDisabled as useDisabled } from '@wordpress/compose';\n * const DisabledExample = () => {\n * \tconst disabledRef = useDisabled();\n *\treturn (\n *\t\t<div ref={ disabledRef }>\n *\t\t\t<a href=\"#\">This link will have tabindex set to -1</a>\n *\t\t\t<input placeholder=\"This input will have the disabled attribute added to it.\" type=\"text\" />\n *\t\t</div>\n *\t);\n * };\n * ```\n */\nexport default function useDisabled() {\n\treturn useRefEffect( ( node ) => {\n\t\tconst disable = () => {\n\t\t\tnode.style.setProperty( 'user-select', 'none' );\n\t\t\tnode.style.setProperty( '-webkit-user-select', 'none' );\n\t\t\tfocus.focusable.find( node ).forEach( ( focusable ) => {\n\t\t\t\tif (\n\t\t\t\t\tincludes( DISABLED_ELIGIBLE_NODE_NAMES, focusable.nodeName )\n\t\t\t\t) {\n\t\t\t\t\tfocusable.setAttribute( 'disabled', '' );\n\t\t\t\t}\n\n\t\t\t\tif ( focusable.nodeName === 'A' ) {\n\t\t\t\t\tfocusable.setAttribute( 'tabindex', '-1' );\n\t\t\t\t}\n\n\t\t\t\tconst tabIndex = focusable.getAttribute( 'tabindex' );\n\t\t\t\tif ( tabIndex !== null && tabIndex !== '-1' ) {\n\t\t\t\t\tfocusable.removeAttribute( 'tabindex' );\n\t\t\t\t}\n\n\t\t\t\tif ( focusable.hasAttribute( 'contenteditable' ) ) {\n\t\t\t\t\tfocusable.setAttribute( 'contenteditable', 'false' );\n\t\t\t\t}\n\n\t\t\t\tif (\n\t\t\t\t\tnode.ownerDocument.defaultView?.HTMLElement &&\n\t\t\t\t\tfocusable instanceof\n\t\t\t\t\t\tnode.ownerDocument.defaultView.HTMLElement\n\t\t\t\t) {\n\t\t\t\t\tfocusable.style.setProperty( 'pointer-events', 'none' );\n\t\t\t\t}\n\t\t\t} );\n\t\t};\n\n\t\t// Debounce re-disable since disabling process itself will incur\n\t\t// additional mutations which should be ignored.\n\t\tconst debouncedDisable = debounce( disable, undefined, {\n\t\t\tleading: true,\n\t\t} );\n\t\tdisable();\n\n\t\t/** @type {MutationObserver | undefined} */\n\t\tconst observer = new window.MutationObserver( debouncedDisable );\n\t\tobserver.observe( node, {\n\t\t\tchildList: true,\n\t\t\tattributes: true,\n\t\t\tsubtree: true,\n\t\t} );\n\n\t\treturn () => {\n\t\t\tif ( observer ) {\n\t\t\t\tobserver.disconnect();\n\t\t\t}\n\t\t\tdebouncedDisable.cancel();\n\t\t};\n\t}, [] );\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-disabled/index.js"],"names":["DISABLED_ELIGIBLE_NODE_NAMES","useDisabled","isDisabled","isDisabledProp","node","updates","disable","style","getPropertyValue","previousValue","setProperty","push","isConnected","focus","focusable","find","forEach","nodeName","disabled","setAttribute","getAttribute","removeAttribute","tabIndex","hasAttribute","ownerDocument","defaultView","HTMLElement","debouncedDisable","undefined","leading","observer","window","MutationObserver","observe","childList","attributes","subtree","disconnect","cancel","update"],"mappings":";;;;;;;;;AAGA;;AAKA;;AAKA;;AAbA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,4BAA4B,GAAG,CACpC,QADoC,EAEpC,UAFoC,EAGpC,OAHoC,EAIpC,UAJoC,EAKpC,QALoC,EAMpC,QANoC,EAOpC,UAPoC,CAArC;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACe,SAASC,WAAT,GAEN;AAAA,MAF4B;AACpCC,IAAAA,UAAU,EAAEC,cAAc,GAAG;AADO,GAE5B,uEAAL,EAAK;AACR,SAAO,2BACJC,IAAF,IAAY;AACX,QAAKD,cAAL,EAAsB;AACrB;AACA;AAED;;AACA;;;AACA,UAAME,OAAO,GAAG,EAAhB;;AAEA,UAAMC,OAAO,GAAG,MAAM;AACrB,UAAKF,IAAI,CAACG,KAAL,CAAWC,gBAAX,CAA6B,aAA7B,MAAiD,MAAtD,EAA+D;AAC9D,cAAMC,aAAa,GAClBL,IAAI,CAACG,KAAL,CAAWC,gBAAX,CAA6B,aAA7B,CADD;AAEAJ,QAAAA,IAAI,CAACG,KAAL,CAAWG,WAAX,CAAwB,aAAxB,EAAuC,MAAvC;AACAN,QAAAA,IAAI,CAACG,KAAL,C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ATW,CAmHX;AACA;;;AACA,UAAMkB,gBAAgB,GAAG,sBAAUrB,OAAV,EAAmBsB,SAAnB,EAA8B;AACtDC,MAAAA,OAAO,EAAE;AAD6C,KAA9B,CAAzB;AAGAvB,IAAAA,OAAO;AAEP;;AACA,UAAMwB,QAAQ,GAAG,IAAIC,MAAM,CAACC,gBAAX,CAA6BL,gBAA7B,CAAjB;AACAG,IAAAA,QAAQ,CAACG,OAAT,CAAkB7B,IAAlB,EAAwB;AACvB8B,MAAAA,SAAS,EAAE,IADY;AAEvBC,MAAAA,UAAU,EAAE,IAFW;AAGvBC,MAAAA,OAAO,EAAE;AAHc,KAAxB;AAMA,WAAO,MAAM;AACZ,UAAKN,QAAL,EAAgB;AACfA,QAAAA,QAAQ,CAACO,UAAT;AACA;;AACDV,MAAAA,gBAAgB,CAACW,MAAjB;AACAjC,MAAAA,OAAO,CAACW,OAAR,CAAmBuB,MAAF,IAAcA,MAAM,EAArC;AACA,KAND;AAOA,GA1IK,EA2IN,CAAEpC,cAAF,CA3IM,CAAP;AA6IA","sourcesContent":["/**\n * External dependencies\n */\nimport { includes, debounce } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/**\n * Names of control nodes which qualify for disabled behavior.\n *\n * See WHATWG HTML Standard: 4.10.18.5: \"Enabling and disabling form controls: the disabled attribute\".\n *\n * @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#enabling-and-disabling-form-controls:-the-disabled-attribute\n *\n * @type {string[]}\n */\nconst DISABLED_ELIGIBLE_NODE_NAMES = [\n\t'BUTTON',\n\t'FIELDSET',\n\t'INPUT',\n\t'OPTGROUP',\n\t'OPTION',\n\t'SELECT',\n\t'TEXTAREA',\n];\n\n/**\n * In some circumstances, such as block previews, all focusable DOM elements\n * (input fields, links, buttons, etc.) need to be disabled. This hook adds the\n * behavior to disable nested DOM elements to the returned ref.\n *\n * @param {Object} config Configuration object.\n * @param {boolean=} config.isDisabled Whether the element should be disabled.\n * @return {import('react').RefCallback<HTMLElement>} Element Ref.\n *\n * @example\n * ```js\n * import { useDisabled } from '@wordpress/compose';\n * const DisabledExample = () => {\n * \tconst disabledRef = useDisabled();\n *\treturn (\n *\t\t<div ref={ disabledRef }>\n *\t\t\t<a href=\"#\">This link will have tabindex set to -1</a>\n *\t\t\t<input placeholder=\"This input will have the disabled attribute added to it.\" type=\"text\" />\n *\t\t</div>\n *\t);\n * };\n * ```\n */\nexport default function useDisabled( {\n\tisDisabled: isDisabledProp = false,\n} = {} ) {\n\treturn useRefEffect(\n\t\t( node ) => {\n\t\t\tif ( isDisabledProp ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t/** A variable keeping track of the previous updates in order to restore them. */\n\t\t\t/** @type {Function[]} */\n\t\t\tconst updates = [];\n\n\t\t\tconst disable = () => {\n\t\t\t\tif ( node.style.getPropertyValue( 'user-select' ) !== 'none' ) {\n\t\t\t\t\tconst previousValue =\n\t\t\t\t\t\tnode.style.getPropertyValue( 'user-select' );\n\t\t\t\t\tnode.style.setProperty( 'user-select', 'none' );\n\t\t\t\t\tnode.style.setProperty( '-webkit-user-select', 'none' );\n\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\tif ( ! node.isConnected ) {\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnode.style.setProperty( 'user-select', previousValue );\n\t\t\t\t\t\tnode.style.setProperty(\n\t\t\t\t\t\t\t'-webkit-user-select',\n\t\t\t\t\t\t\tpreviousValue\n\t\t\t\t\t\t);\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\tfocus.focusable.find( node ).forEach( ( focusable ) => {\n\t\t\t\t\tif (\n\t\t\t\t\t\tincludes(\n\t\t\t\t\t\t\tDISABLED_ELIGIBLE_NODE_NAMES,\n\t\t\t\t\t\t\tfocusable.nodeName\n\t\t\t\t\t\t) &&\n\t\t\t\t\t\t// @ts-ignore\n\t\t\t\t\t\t! focusable.disabled\n\t\t\t\t\t) {\n\t\t\t\t\t\tfocusable.setAttribute( 'disabled', '' );\n\t\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\t\tif ( ! focusable.isConnected ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t// @ts-ignore\n\t\t\t\t\t\t\tfocusable.disabled = false;\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tfocusable.nodeName === 'A' &&\n\t\t\t\t\t\tfocusable.getAttribute( 'tabindex' ) !== '-1'\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst previousValue =\n\t\t\t\t\t\t\tfocusable.getAttribute( 'tabindex' );\n\t\t\t\t\t\tfocusable.setAttribute( 'tabindex', '-1' );\n\t\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\t\tif ( ! focusable.isConnected ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif ( ! previousValue ) {\n\t\t\t\t\t\t\t\tfocusable.removeAttribute( 'tabindex' );\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tfocusable.setAttribute(\n\t\t\t\t\t\t\t\t\t'tabindex',\n\t\t\t\t\t\t\t\t\tpreviousValue\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\n\t\t\t\t\tconst tabIndex = focusable.getAttribute( 'tabindex' );\n\t\t\t\t\tif ( tabIndex !== null && tabIndex !== '-1' ) {\n\t\t\t\t\t\tfocusable.removeAttribute( 'tabindex' );\n\t\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\t\tif ( ! focusable.isConnected ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tfocusable.setAttribute( 'tabindex', tabIndex );\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tfocusable.hasAttribute( 'contenteditable' ) &&\n\t\t\t\t\t\tfocusable.getAttribute( 'contenteditable' ) !== 'false'\n\t\t\t\t\t) {\n\t\t\t\t\t\tfocusable.setAttribute( 'contenteditable', 'false' );\n\t\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\t\tif ( ! focusable.isConnected ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tfocusable.setAttribute( 'contenteditable', 'true' );\n\t\t\t\t\t\t} );\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\tnode.ownerDocument.defaultView?.HTMLElement &&\n\t\t\t\t\t\tfocusable instanceof\n\t\t\t\t\t\t\tnode.ownerDocument.defaultView.HTMLElement\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst previousValue =\n\t\t\t\t\t\t\tfocusable.style.getPropertyValue(\n\t\t\t\t\t\t\t\t'pointer-events'\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\tfocusable.style.setProperty( 'pointer-events', 'none' );\n\t\t\t\t\t\tupdates.push( () => {\n\t\t\t\t\t\t\tif ( ! focusable.isConnected ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tfocusable.style.setProperty(\n\t\t\t\t\t\t\t\t'pointer-events',\n\t\t\t\t\t\t\t\tpreviousValue\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\t\t\t};\n\n\t\t\t// Debounce re-disable since disabling process itself will incur\n\t\t\t// additional mutations which should be ignored.\n\t\t\tconst debouncedDisable = debounce( disable, undefined, {\n\t\t\t\tleading: true,\n\t\t\t} );\n\t\t\tdisable();\n\n\t\t\t/** @type {MutationObserver | undefined} */\n\t\t\tconst observer = new window.MutationObserver( debouncedDisable );\n\t\t\tobserver.observe( node, {\n\t\t\t\tchildList: true,\n\t\t\t\tattributes: true,\n\t\t\t\tsubtree: true,\n\t\t\t} );\n\n\t\t\treturn () => {\n\t\t\t\tif ( observer ) {\n\t\t\t\t\tobserver.disconnect();\n\t\t\t\t}\n\t\t\t\tdebouncedDisable.cancel();\n\t\t\t\tupdates.forEach( ( update ) => update() );\n\t\t\t};\n\t\t},\n\t\t[ isDisabledProp ]\n\t);\n}\n"]}
|
|
@@ -63,7 +63,12 @@ function useFocusOnMount() {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
target.focus(
|
|
66
|
+
target.focus({
|
|
67
|
+
// When focusing newly mounted dialogs,
|
|
68
|
+
// the position of the popover is often not right on the first render
|
|
69
|
+
// This prevents the layout shifts when focusing the dialogs.
|
|
70
|
+
preventScroll: true
|
|
71
|
+
});
|
|
67
72
|
}, []);
|
|
68
73
|
}
|
|
69
74
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-focus-on-mount/index.js"],"names":["useFocusOnMount","focusOnMount","focusOnMountRef","current","node","contains","ownerDocument","activeElement","target","firstTabbable","focus","tabbable","find"],"mappings":";;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,eAAT,GAA0D;AAAA,MAAhCC,YAAgC,uEAAjB,cAAiB;AACxE,QAAMC,eAAe,GAAG,qBAAQD,YAAR,CAAxB;AACA,0BAAW,MAAM;AAChBC,IAAAA,eAAe,CAACC,OAAhB,GAA0BF,YAA1B;AACA,GAFD,EAEG,CAAEA,YAAF,CAFH;AAIA,SAAO,0BAAeG,IAAF,IAAY;AAAA;;AAC/B,QAAK,CAAEA,IAAF,IAAUF,eAAe,CAACC,OAAhB,KAA4B,KAA3C,EAAmD;AAClD;AACA;;AAED,QAAKC,IAAI,CAACC,QAAL,iDAAeD,IAAI,CAACE,aAApB,wDAAe,oBAAoBC,aAAnC,yEAAoD,IAApD,CAAL,EAAkE;AACjE;AACA;;AAED,QAAIC,MAAM,GAAGJ,IAAb;;AAEA,QAAKF,eAAe,CAACC,OAAhB,KAA4B,cAAjC,EAAkD;AACjD,YAAMM,aAAa,GAAGC,WAAMC,QAAN,CAAeC,IAAf,CAAqBR,IAArB,EAA6B,CAA7B,CAAtB;;AAEA,UAAKK,aAAL,EAAqB;AACpBD,QAAAA,MAAM;AAAG;AAA6BC,QAAAA,aAAtC;AACA;AACD;;AAEDD,IAAAA,MAAM,CAACE,KAAP;AACA,
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-focus-on-mount/index.js"],"names":["useFocusOnMount","focusOnMount","focusOnMountRef","current","node","contains","ownerDocument","activeElement","target","firstTabbable","focus","tabbable","find","preventScroll"],"mappings":";;;;;;;AAGA;;AACA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,eAAT,GAA0D;AAAA,MAAhCC,YAAgC,uEAAjB,cAAiB;AACxE,QAAMC,eAAe,GAAG,qBAAQD,YAAR,CAAxB;AACA,0BAAW,MAAM;AAChBC,IAAAA,eAAe,CAACC,OAAhB,GAA0BF,YAA1B;AACA,GAFD,EAEG,CAAEA,YAAF,CAFH;AAIA,SAAO,0BAAeG,IAAF,IAAY;AAAA;;AAC/B,QAAK,CAAEA,IAAF,IAAUF,eAAe,CAACC,OAAhB,KAA4B,KAA3C,EAAmD;AAClD;AACA;;AAED,QAAKC,IAAI,CAACC,QAAL,iDAAeD,IAAI,CAACE,aAApB,wDAAe,oBAAoBC,aAAnC,yEAAoD,IAApD,CAAL,EAAkE;AACjE;AACA;;AAED,QAAIC,MAAM,GAAGJ,IAAb;;AAEA,QAAKF,eAAe,CAACC,OAAhB,KAA4B,cAAjC,EAAkD;AACjD,YAAMM,aAAa,GAAGC,WAAMC,QAAN,CAAeC,IAAf,CAAqBR,IAArB,EAA6B,CAA7B,CAAtB;;AAEA,UAAKK,aAAL,EAAqB;AACpBD,QAAAA,MAAM;AAAG;AAA6BC,QAAAA,aAAtC;AACA;AACD;;AAEDD,IAAAA,MAAM,CAACE,KAAP,CAAc;AACb;AACA;AACA;AACAG,MAAAA,aAAa,EAAE;AAJF,KAAd;AAMA,GAzBM,EAyBJ,EAzBI,CAAP;AA0BA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useEffect, useCallback } from '@wordpress/element';\nimport { focus } from '@wordpress/dom';\n\n/**\n * Hook used to focus the first tabbable element on mount.\n *\n * @param {boolean | 'firstElement'} focusOnMount Focus on mount mode.\n * @return {import('react').RefCallback<HTMLElement>} Ref callback.\n *\n * @example\n * ```js\n * import { useFocusOnMount } from '@wordpress/compose';\n *\n * const WithFocusOnMount = () => {\n * const ref = useFocusOnMount()\n * return (\n * <div ref={ ref }>\n * <Button />\n * <Button />\n * </div>\n * );\n * }\n * ```\n */\nexport default function useFocusOnMount( focusOnMount = 'firstElement' ) {\n\tconst focusOnMountRef = useRef( focusOnMount );\n\tuseEffect( () => {\n\t\tfocusOnMountRef.current = focusOnMount;\n\t}, [ focusOnMount ] );\n\n\treturn useCallback( ( node ) => {\n\t\tif ( ! node || focusOnMountRef.current === false ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( node.contains( node.ownerDocument?.activeElement ?? null ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet target = node;\n\n\t\tif ( focusOnMountRef.current === 'firstElement' ) {\n\t\t\tconst firstTabbable = focus.tabbable.find( node )[ 0 ];\n\n\t\t\tif ( firstTabbable ) {\n\t\t\t\ttarget = /** @type {HTMLElement} */ ( firstTabbable );\n\t\t\t}\n\t\t}\n\n\t\ttarget.focus( {\n\t\t\t// When focusing newly mounted dialogs,\n\t\t\t// the position of the popover is often not right on the first render\n\t\t\t// This prevents the layout shifts when focusing the dialogs.\n\t\t\tpreventScroll: true,\n\t\t} );\n\t}, [] );\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-focus-return/index.js"],"names":["useFocusReturn","onFocusReturn","ref","focusedBeforeMount","onFocusReturnRef","current","node","ownerDocument","activeElement","isFocused","contains","isConnected","focus"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,cAAT,CAAyBC,aAAzB,EAAyC;AACxC;AACA,QAAMC,GAAG,GAAG,qBAAQ,IAAR,CAAZ;AACA;;AACA,QAAMC,kBAAkB,GAAG,qBAAQ,IAAR,CAA3B;AACA,QAAMC,gBAAgB,GAAG,qBAAQH,aAAR,CAAzB;AACA,0BAAW,MAAM;AAChBG,IAAAA,gBAAgB,CAACC,OAAjB,GAA2BJ,aAA3B;AACA,GAFD,EAEG,CAAEA,aAAF,CAFH;AAIA,SAAO,0BAAeK,IAAF,IAAY;AAC/B,QAAKA,IAAL,EAAY;AACX;AACAJ,MAAAA,GAAG,CAACG,OAAJ,GAAcC,IAAd,CAFW,CAIX;;AACA,UAAKH,kBAAkB,CAACE,OAAxB,EAAkC;AACjC;AACA;;AAEDF,MAAAA,kBAAkB,CAACE,OAAnB,GAA6BC,IAAI,CAACC,aAAL,CAAmBC,aAAhD;AACA,KAVD,MAUO,IAAKL,kBAAkB,CAACE,OAAxB,EAAkC;AAAA;;AACxC,YAAMI,SAAS,mBAAGP,GAAG,CAACG,OAAP,iDAAG,aAAaK,QAAb,kBACjBR,GAAG,CAACG,OADa,kDACjB,cAAaE,aAAb,CAA2BC,aADV,CAAlB;;AAIA,UAAK,iBAAAN,GAAG,CAACG,OAAJ,wDAAaM,WAAb,IAA4B,CAAEF,SAAnC,EAA+C;AAC9C;AACA,OAPuC,CASxC;AACA;AACA;AACA;;;AACA,UAAKL,gBAAgB,CAACC,OAAtB,EAAgC;AAC/BD,QAAAA,gBAAgB,CAACC,OAAjB;AACA,OAFD,MAEO;AAAA;;AACN;AAAkC,
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-focus-return/index.js"],"names":["useFocusReturn","onFocusReturn","ref","focusedBeforeMount","onFocusReturnRef","current","node","ownerDocument","activeElement","isFocused","contains","isConnected","focus"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,cAAT,CAAyBC,aAAzB,EAAyC;AACxC;AACA,QAAMC,GAAG,GAAG,qBAAQ,IAAR,CAAZ;AACA;;AACA,QAAMC,kBAAkB,GAAG,qBAAQ,IAAR,CAA3B;AACA,QAAMC,gBAAgB,GAAG,qBAAQH,aAAR,CAAzB;AACA,0BAAW,MAAM;AAChBG,IAAAA,gBAAgB,CAACC,OAAjB,GAA2BJ,aAA3B;AACA,GAFD,EAEG,CAAEA,aAAF,CAFH;AAIA,SAAO,0BAAeK,IAAF,IAAY;AAC/B,QAAKA,IAAL,EAAY;AACX;AACAJ,MAAAA,GAAG,CAACG,OAAJ,GAAcC,IAAd,CAFW,CAIX;;AACA,UAAKH,kBAAkB,CAACE,OAAxB,EAAkC;AACjC;AACA;;AAEDF,MAAAA,kBAAkB,CAACE,OAAnB,GAA6BC,IAAI,CAACC,aAAL,CAAmBC,aAAhD;AACA,KAVD,MAUO,IAAKL,kBAAkB,CAACE,OAAxB,EAAkC;AAAA;;AACxC,YAAMI,SAAS,mBAAGP,GAAG,CAACG,OAAP,iDAAG,aAAaK,QAAb,kBACjBR,GAAG,CAACG,OADa,kDACjB,cAAaE,aAAb,CAA2BC,aADV,CAAlB;;AAIA,UAAK,iBAAAN,GAAG,CAACG,OAAJ,wDAAaM,WAAb,IAA4B,CAAEF,SAAnC,EAA+C;AAC9C;AACA,OAPuC,CASxC;AACA;AACA;AACA;;;AACA,UAAKL,gBAAgB,CAACC,OAAtB,EAAgC;AAC/BD,QAAAA,gBAAgB,CAACC,OAAjB;AACA,OAFD,MAEO;AAAA;;AACN;AAAkC,iCACjCF,kBAAkB,CAACE,OADc,gFAE/BO,KAF+B;AAGlC;AACD;AACD,GAhCM,EAgCJ,EAhCI,CAAP;AAiCA;;eAEcZ,c","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useEffect, useCallback } from '@wordpress/element';\n\n/**\n * When opening modals/sidebars/dialogs, the focus\n * must move to the opened area and return to the\n * previously focused element when closed.\n * The current hook implements the returning behavior.\n *\n * @param {() => void} [onFocusReturn] Overrides the default return behavior.\n * @return {import('react').RefCallback<HTMLElement>} Element Ref.\n *\n * @example\n * ```js\n * import { useFocusReturn } from '@wordpress/compose';\n *\n * const WithFocusReturn = () => {\n * const ref = useFocusReturn()\n * return (\n * <div ref={ ref }>\n * <Button />\n * <Button />\n * </div>\n * );\n * }\n * ```\n */\nfunction useFocusReturn( onFocusReturn ) {\n\t/** @type {import('react').MutableRefObject<null | HTMLElement>} */\n\tconst ref = useRef( null );\n\t/** @type {import('react').MutableRefObject<null | Element>} */\n\tconst focusedBeforeMount = useRef( null );\n\tconst onFocusReturnRef = useRef( onFocusReturn );\n\tuseEffect( () => {\n\t\tonFocusReturnRef.current = onFocusReturn;\n\t}, [ onFocusReturn ] );\n\n\treturn useCallback( ( node ) => {\n\t\tif ( node ) {\n\t\t\t// Set ref to be used when unmounting.\n\t\t\tref.current = node;\n\n\t\t\t// Only set when the node mounts.\n\t\t\tif ( focusedBeforeMount.current ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tfocusedBeforeMount.current = node.ownerDocument.activeElement;\n\t\t} else if ( focusedBeforeMount.current ) {\n\t\t\tconst isFocused = ref.current?.contains(\n\t\t\t\tref.current?.ownerDocument.activeElement\n\t\t\t);\n\n\t\t\tif ( ref.current?.isConnected && ! isFocused ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Defer to the component's own explicit focus return behavior, if\n\t\t\t// specified. This allows for support that the `onFocusReturn`\n\t\t\t// decides to allow the default behavior to occur under some\n\t\t\t// conditions.\n\t\t\tif ( onFocusReturnRef.current ) {\n\t\t\t\tonFocusReturnRef.current();\n\t\t\t} else {\n\t\t\t\t/** @type {null | HTMLElement} */ (\n\t\t\t\t\tfocusedBeforeMount.current\n\t\t\t\t)?.focus();\n\t\t\t}\n\t\t}\n\t}, [] );\n}\n\nexport default useFocusReturn;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-keyboard-shortcut/index.js"],"names":["isAppleOS","_window","window","platform","navigator","indexOf","useKeyboardShortcut","shortcuts","callback","bindGlobal","eventName","isDisabled","target","currentCallback","current","mousetrap","Mousetrap","document","forEach","shortcut","keys","split","modifiers","Set","filter","value","length","hasAlt","has","hasShift","size","Error","bindFn","reset"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AAKA;;AAVA;AACA;AACA;;AAKA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,SAAT,GAAuC;AAAA,MAAnBC,OAAmB,uEAATC,MAAS;;AACtC,QAAM;AAAEC,IAAAA;AAAF,MAAeF,OAAO,CAACG,SAA7B;AAEA,SACCD,QAAQ,CAACE,OAAT,CAAkB,KAAlB,MAA8B,CAAC,CAA/B,IACA,sBAAU,CAAE,MAAF,EAAU,QAAV,CAAV,EAAgCF,QAAhC,CAFD;AAIA;AAED;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASG,mBAAT;AACC;AACAC,SAFD,EAGCC,QAHD,EAUE;AAAA,MAND;AACCC,IAAAA,UAAU,GAAG,KADd;AAECC,IAAAA,SAAS,GAAG,SAFb;AAGCC,IAAAA,UAAU,GAAG,KAHd;AAGqB;AACpBC,IAAAA;AAJD,GAMC,uEADG,EACH;AACD,QAAMC,eAAe,GAAG,qBAAQL,QAAR,CAAxB;AACA,0BAAW,MAAM;AAChBK,IAAAA,eAAe,CAACC,OAAhB,GAA0BN,QAA1B;AACA,GAFD,EAEG,CAAEA,QAAF,CAFH;AAIA,0BAAW,MAAM;AAChB,QAAKG,UAAL,EAAkB;AACjB;AACA;;AACD,UAAMI,SAAS,GAAG,IAAIC,kBAAJ,CACjBJ,MAAM,IAAIA,MAAM,CAACE,OAAjB,GACGF,MAAM,CAACE,OADV,GAEG;AACA;AACA;;AACA;;
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-keyboard-shortcut/index.js"],"names":["isAppleOS","_window","window","platform","navigator","indexOf","useKeyboardShortcut","shortcuts","callback","bindGlobal","eventName","isDisabled","target","currentCallback","current","mousetrap","Mousetrap","document","forEach","shortcut","keys","split","modifiers","Set","filter","value","length","hasAlt","has","hasShift","size","Error","bindFn","reset"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AAKA;;AAVA;AACA;AACA;;AAKA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,SAAT,GAAuC;AAAA,MAAnBC,OAAmB,uEAATC,MAAS;;AACtC,QAAM;AAAEC,IAAAA;AAAF,MAAeF,OAAO,CAACG,SAA7B;AAEA,SACCD,QAAQ,CAACE,OAAT,CAAkB,KAAlB,MAA8B,CAAC,CAA/B,IACA,sBAAU,CAAE,MAAF,EAAU,QAAV,CAAV,EAAgCF,QAAhC,CAFD;AAIA;AAED;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASG,mBAAT;AACC;AACAC,SAFD,EAGCC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C,CAtB+C,CAuB/C;;AACAM,MAAAA,SAAS,CAAEiB,MAAF,CAAT,CACCb,QADD,EAEC;AAAA;AAIC;AACAN,UAAAA,eAAe,CAACC,OAAhB,CAAyB,YAAzB;AALD;AAAA,OAFD,EAQCJ,SARD;AAUA,KAlCD;AAoCA,WAAO,MAAM;AACZK,MAAAA,SAAS,CAACkB,KAAV;AACA,KAFD;AAGA,GAnDD,EAmDG,CAAE1B,SAAF,EAAaE,UAAb,EAAyBC,SAAzB,EAAoCE,MAApC,EAA4CD,UAA5C,CAnDH;AAoDA;;eAEcL,mB","sourcesContent":["/**\n * External dependencies\n */\nimport Mousetrap from 'mousetrap';\nimport 'mousetrap/plugins/global-bind/mousetrap-global-bind';\nimport { includes, castArray } from 'lodash';\n\n/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\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/**\n * Return true if platform is MacOS.\n *\n * @param {Window} [_window] window object by default; used for DI testing.\n *\n * @return {boolean} True if MacOS; false otherwise.\n */\nfunction isAppleOS( _window = window ) {\n\tconst { platform } = _window.navigator;\n\n\treturn (\n\t\tplatform.indexOf( 'Mac' ) !== -1 ||\n\t\tincludes( [ 'iPad', 'iPhone' ], platform )\n\t);\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 currentCallback = useRef( callback );\n\tuseEffect( () => {\n\t\tcurrentCallback.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\tcastArray( shortcuts ).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\tcurrentCallback.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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-merge-refs/index.js"],"names":["assignRef","ref","value","hasOwnProperty","current","useMergeRefs","refs","element","didElementChange","previousRefs","currentRefs","forEach","index","previousRef","refsToAssign"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASA,SAAT,CAAoBC,GAApB,EAAyBC,KAAzB,EAAiC;AAChC,MAAK,OAAOD,GAAP,KAAe,UAApB,EAAiC;AAChCA,IAAAA,GAAG,CAAEC,KAAF,CAAH;AACA,GAFD,MAEO,IAAKD,GAAG,IAAIA,GAAG,CAACE,cAAJ,CAAoB,SAApB,CAAZ,EAA8C;AACpD;;AACA;AAAqDF,IAAAA,GAAF,CAAQG,OAAR,
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-merge-refs/index.js"],"names":["assignRef","ref","value","hasOwnProperty","current","useMergeRefs","refs","element","didElementChange","previousRefs","currentRefs","forEach","index","previousRef","refsToAssign"],"mappings":";;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;;AACA;AACA;AACA;AACA;;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASA,SAAT,CAAoBC,GAApB,EA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AAuBE,IAAvB;AACA,GAZD,EAYGA,IAZH,EAhB4C,CA8B5C;AACA;;AACA,gCAAiB,MAAM;AACtBE,IAAAA,gBAAgB,CAACJ,OAAjB,GAA2B,KAA3B;AACA,GAFD,EAhC4C,CAoC5C;AACA;;AACA,SAAO,0BAAeF,KAAF,IAAa;AAChC;AACA;AACAF,IAAAA,SAAS,CAAEO,OAAF,EAAWL,KAAX,CAAT;AAEAM,IAAAA,gBAAgB,CAACJ,OAAjB,GAA2B,IAA3B,CALgC,CAOhC;AACA;;AACA,UAAMU,YAAY,GAAGZ,KAAK,GAAGQ,WAAW,CAACN,OAAf,GAAyBK,YAAY,CAACL,OAAhE,CATgC,CAWhC;;AACA,SAAM,MAAMH,GAAZ,IAAmBa,YAAnB,EAAkC;AACjCd,MAAAA,SAAS,CAAEC,GAAF,EAAOC,KAAP,CAAT;AACA;AACD,GAfM,EAeJ,EAfI,CAAP;AAgBA","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 didElementChange = 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 previousRefs = useRef( [] );\n\tconst currentRefs = 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\tcurrentRefs.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 ( didElementChange.current === false ) {\n\t\t\trefs.forEach( ( ref, index ) => {\n\t\t\t\tconst previousRef = previousRefs.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\tpreviousRefs.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\tdidElementChange.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\tdidElementChange.current = true;\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 ? currentRefs.current : previousRefs.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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-ref-effect/index.ts"],"names":["useRefEffect","callback","dependencies","cleanup","node","current"],"mappings":";;;;;;;AAQA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,YAAT,CACdC,QADc,EAEdC,YAFc,EAGmB;AACjC,QAAMC,OAAO,GAAG,sBAAhB;AACA,SAAO,0BAAeC,IAAF,IAA6B;AAChD,QAAKA,IAAL,EAAY;AACXD,MAAAA,OAAO,CAACE,OAAR,GAAkBJ,QAAQ,CAAEG,IAAF,CAA1B;AACA,KAFD,MAEO,IAAKD,OAAO,CAACE,OAAb,EAAuB;AAC7BF,MAAAA,OAAO,CAACE,OAAR;AACA;AACD,GANM,EAMJH,YANI,CAAP;AAOA","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 ) |
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-ref-effect/index.ts"],"names":["useRefEffect","callback","dependencies","cleanup","node","current"],"mappings":";;;;;;;AAQA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,YAAT,CACdC,QADc,EAEdC,YAFc,EAGmB;AACjC,QAAMC,OAAO,GAAG,sBAAhB;AACA,SAAO,0BAAeC,IAAF,IAA6B;AAChD,QAAKA,IAAL,EAAY;AACXD,MAAAA,OAAO,CAACE,OAAR,GAAkBJ,QAAQ,CAAEG,IAAF,CAA1B;AACA,KAFD,MAEO,IAAKD,OAAO,CAACE,OAAb,EAAuB;AAC7BF,MAAAA,OAAO,CAACE,OAAR;AACA;AACD,GANM,EAMJH,YANI,CAAP;AAOA","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 cleanup = useRef< ( () => void ) | void >();\n\treturn useCallback( ( node: TElement | null ) => {\n\t\tif ( node ) {\n\t\t\tcleanup.current = callback( node );\n\t\t} else if ( cleanup.current ) {\n\t\t\tcleanup.current();\n\t\t}\n\t}, dependencies );\n}\n"]}
|
|
@@ -1,26 +1,209 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
8
|
-
exports.default =
|
|
6
|
+
exports.default = useResizeAware;
|
|
9
7
|
|
|
10
|
-
var
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* External dependencies
|
|
14
12
|
*/
|
|
15
13
|
|
|
14
|
+
/**
|
|
15
|
+
* WordPress dependencies
|
|
16
|
+
*/
|
|
17
|
+
// This of course could've been more streamlined with internal state instead of
|
|
18
|
+
// refs, but then host hooks / components could not opt out of renders.
|
|
19
|
+
// This could've been exported to its own module, but the current build doesn't
|
|
20
|
+
// seem to work with module imports and I had no more time to spend on this...
|
|
21
|
+
function useResolvedElement(subscriber, refOrElement) {
|
|
22
|
+
const callbackRefElement = (0, _element.useRef)(null);
|
|
23
|
+
const lastReportRef = (0, _element.useRef)(null);
|
|
24
|
+
const cleanupRef = (0, _element.useRef)();
|
|
25
|
+
const callSubscriber = (0, _element.useCallback)(() => {
|
|
26
|
+
let element = null;
|
|
27
|
+
|
|
28
|
+
if (callbackRefElement.current) {
|
|
29
|
+
element = callbackRefElement.current;
|
|
30
|
+
} else if (refOrElement) {
|
|
31
|
+
if (refOrElement instanceof HTMLElement) {
|
|
32
|
+
element = refOrElement;
|
|
33
|
+
} else {
|
|
34
|
+
element = refOrElement.current;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (lastReportRef.current && lastReportRef.current.element === element && lastReportRef.current.reporter === callSubscriber) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (cleanupRef.current) {
|
|
43
|
+
cleanupRef.current(); // Making sure the cleanup is not called accidentally multiple times.
|
|
44
|
+
|
|
45
|
+
cleanupRef.current = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
lastReportRef.current = {
|
|
49
|
+
reporter: callSubscriber,
|
|
50
|
+
element
|
|
51
|
+
}; // Only calling the subscriber, if there's an actual element to report.
|
|
52
|
+
|
|
53
|
+
if (element) {
|
|
54
|
+
cleanupRef.current = subscriber(element);
|
|
55
|
+
}
|
|
56
|
+
}, [refOrElement, subscriber]); // On each render, we check whether a ref changed, or if we got a new raw
|
|
57
|
+
// element.
|
|
58
|
+
|
|
59
|
+
(0, _element.useEffect)(() => {
|
|
60
|
+
// With this we're *technically* supporting cases where ref objects' current value changes, but only if there's a
|
|
61
|
+
// render accompanying that change as well.
|
|
62
|
+
// To guarantee we always have the right element, one must use the ref callback provided instead, but we support
|
|
63
|
+
// RefObjects to make the hook API more convenient in certain cases.
|
|
64
|
+
callSubscriber();
|
|
65
|
+
}, [callSubscriber]);
|
|
66
|
+
return (0, _element.useCallback)(element => {
|
|
67
|
+
callbackRefElement.current = element;
|
|
68
|
+
callSubscriber();
|
|
69
|
+
}, [callSubscriber]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// We're only using the first element of the size sequences, until future versions of the spec solidify on how
|
|
73
|
+
// exactly it'll be used for fragments in multi-column scenarios:
|
|
74
|
+
// From the spec:
|
|
75
|
+
// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,
|
|
76
|
+
// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not
|
|
77
|
+
// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single
|
|
78
|
+
// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.
|
|
79
|
+
// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.
|
|
80
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)
|
|
81
|
+
//
|
|
82
|
+
// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,
|
|
83
|
+
// regardless of the "box" option.
|
|
84
|
+
// The spec states the following on this:
|
|
85
|
+
// > This does not have any impact on which box dimensions are returned to the defined callback when the event
|
|
86
|
+
// > is fired, it solely defines which box the author wishes to observe layout changes on.
|
|
87
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
88
|
+
// I'm not exactly clear on what this means, especially when you consider a later section stating the following:
|
|
89
|
+
// > This section is non-normative. An author may desire to observe more than one CSS box.
|
|
90
|
+
// > In this case, author will need to use multiple ResizeObservers.
|
|
91
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
92
|
+
// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.
|
|
93
|
+
// For this reason I decided to only return the requested size,
|
|
94
|
+
// even though it seems we have access to results for all box types.
|
|
95
|
+
// This also means that we get to keep the current api, being able to return a simple { width, height } pair,
|
|
96
|
+
// regardless of box option.
|
|
97
|
+
const extractSize = (entry, boxProp, sizeType) => {
|
|
98
|
+
if (!entry[boxProp]) {
|
|
99
|
+
if (boxProp === 'contentBoxSize') {
|
|
100
|
+
// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.
|
|
101
|
+
// See the 6th step in the description for the RO algorithm:
|
|
102
|
+
// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h
|
|
103
|
+
// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".
|
|
104
|
+
// In real browser implementations of course these objects differ, but the width/height values should be equivalent.
|
|
105
|
+
return entry.contentRect[sizeType === 'inlineSize' ? 'width' : 'height'];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return undefined;
|
|
109
|
+
} // A couple bytes smaller than calling Array.isArray() and just as effective here.
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
return entry[boxProp][0] ? entry[boxProp][0][sizeType] : // TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's current
|
|
113
|
+
// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
entry[boxProp][sizeType];
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
function useResizeObserver() {
|
|
119
|
+
let opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
120
|
+
// Saving the callback as a ref. With this, I don't need to put onResize in the
|
|
121
|
+
// effect dep array, and just passing in an anonymous function without memoising
|
|
122
|
+
// will not reinstantiate the hook's ResizeObserver.
|
|
123
|
+
const onResize = opts.onResize;
|
|
124
|
+
const onResizeRef = (0, _element.useRef)(undefined);
|
|
125
|
+
onResizeRef.current = onResize;
|
|
126
|
+
const round = opts.round || Math.round; // Using a single instance throughout the hook's lifetime
|
|
127
|
+
|
|
128
|
+
const resizeObserverRef = (0, _element.useRef)();
|
|
129
|
+
const [size, setSize] = (0, _element.useState)({
|
|
130
|
+
width: undefined,
|
|
131
|
+
height: undefined
|
|
132
|
+
}); // In certain edge cases the RO might want to report a size change just after
|
|
133
|
+
// the component unmounted.
|
|
134
|
+
|
|
135
|
+
const didUnmount = (0, _element.useRef)(false);
|
|
136
|
+
(0, _element.useEffect)(() => {
|
|
137
|
+
return () => {
|
|
138
|
+
didUnmount.current = true;
|
|
139
|
+
};
|
|
140
|
+
}, []); // Using a ref to track the previous width / height to avoid unnecessary renders.
|
|
141
|
+
|
|
142
|
+
const previous = (0, _element.useRef)({
|
|
143
|
+
width: undefined,
|
|
144
|
+
height: undefined
|
|
145
|
+
}); // This block is kinda like a useEffect, only it's called whenever a new
|
|
146
|
+
// element could be resolved based on the ref option. It also has a cleanup
|
|
147
|
+
// function.
|
|
148
|
+
|
|
149
|
+
const refCallback = useResolvedElement((0, _element.useCallback)(element => {
|
|
150
|
+
// We only use a single Resize Observer instance, and we're instantiating it on demand, only once there's something to observe.
|
|
151
|
+
// 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.
|
|
152
|
+
if (!resizeObserverRef.current || resizeObserverRef.current.box !== opts.box || resizeObserverRef.current.round !== round) {
|
|
153
|
+
resizeObserverRef.current = {
|
|
154
|
+
box: opts.box,
|
|
155
|
+
round,
|
|
156
|
+
instance: new ResizeObserver(entries => {
|
|
157
|
+
const entry = entries[0];
|
|
158
|
+
let boxProp = 'borderBoxSize';
|
|
159
|
+
|
|
160
|
+
if (opts.box === 'border-box') {
|
|
161
|
+
boxProp = 'borderBoxSize';
|
|
162
|
+
} else {
|
|
163
|
+
boxProp = opts.box === 'device-pixel-content-box' ? 'devicePixelContentBoxSize' : 'contentBoxSize';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const reportedWidth = extractSize(entry, boxProp, 'inlineSize');
|
|
167
|
+
const reportedHeight = extractSize(entry, boxProp, 'blockSize');
|
|
168
|
+
const newWidth = reportedWidth ? round(reportedWidth) : undefined;
|
|
169
|
+
const newHeight = reportedHeight ? round(reportedHeight) : undefined;
|
|
170
|
+
|
|
171
|
+
if (previous.current.width !== newWidth || previous.current.height !== newHeight) {
|
|
172
|
+
const newSize = {
|
|
173
|
+
width: newWidth,
|
|
174
|
+
height: newHeight
|
|
175
|
+
};
|
|
176
|
+
previous.current.width = newWidth;
|
|
177
|
+
previous.current.height = newHeight;
|
|
178
|
+
|
|
179
|
+
if (onResizeRef.current) {
|
|
180
|
+
onResizeRef.current(newSize);
|
|
181
|
+
} else if (!didUnmount.current) {
|
|
182
|
+
setSize(newSize);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
resizeObserverRef.current.instance.observe(element, {
|
|
190
|
+
box: opts.box
|
|
191
|
+
});
|
|
192
|
+
return () => {
|
|
193
|
+
if (resizeObserverRef.current) {
|
|
194
|
+
resizeObserverRef.current.instance.unobserve(element);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}, [opts.box, round]), opts.ref);
|
|
198
|
+
return (0, _element.useMemo)(() => ({
|
|
199
|
+
ref: refCallback,
|
|
200
|
+
width: size.width,
|
|
201
|
+
height: size.height
|
|
202
|
+
}), [refCallback, size ? size.width : null, size ? size.height : null]);
|
|
203
|
+
}
|
|
16
204
|
/**
|
|
17
205
|
* Hook which allows to listen the resize event of any target element when it changes sizes.
|
|
18
|
-
* _Note: `useResizeObserver` will report `null` until after first
|
|
19
|
-
*
|
|
20
|
-
* Simply a re-export of `react-resize-aware` so refer to its documentation <https://github.com/FezVrasta/react-resize-aware>
|
|
21
|
-
* for more details.
|
|
22
|
-
*
|
|
23
|
-
* @see https://github.com/FezVrasta/react-resize-aware
|
|
206
|
+
* _Note: `useResizeObserver` will report `null` until after first render.
|
|
24
207
|
*
|
|
25
208
|
* @example
|
|
26
209
|
*
|
|
@@ -36,8 +219,36 @@ var _reactResizeAware = _interopRequireDefault(require("react-resize-aware"));
|
|
|
36
219
|
* );
|
|
37
220
|
* };
|
|
38
221
|
* ```
|
|
39
|
-
*
|
|
40
222
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
function useResizeAware() {
|
|
226
|
+
const {
|
|
227
|
+
ref,
|
|
228
|
+
width,
|
|
229
|
+
height
|
|
230
|
+
} = useResizeObserver();
|
|
231
|
+
const sizes = (0, _element.useMemo)(() => {
|
|
232
|
+
return {
|
|
233
|
+
width: width !== null && width !== void 0 ? width : null,
|
|
234
|
+
height: height !== null && height !== void 0 ? height : null
|
|
235
|
+
};
|
|
236
|
+
}, [width, height]);
|
|
237
|
+
const resizeListener = (0, _element.createElement)("div", {
|
|
238
|
+
style: {
|
|
239
|
+
position: 'absolute',
|
|
240
|
+
top: 0,
|
|
241
|
+
left: 0,
|
|
242
|
+
right: 0,
|
|
243
|
+
bottom: 0,
|
|
244
|
+
pointerEvents: 'none',
|
|
245
|
+
opacity: 0,
|
|
246
|
+
overflow: 'hidden',
|
|
247
|
+
zIndex: -1
|
|
248
|
+
},
|
|
249
|
+
"aria-hidden": "true",
|
|
250
|
+
ref: ref
|
|
251
|
+
});
|
|
252
|
+
return [resizeListener, sizes];
|
|
253
|
+
}
|
|
43
254
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.js"],"names":["useResizeAware"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eACeA,yB","sourcesContent":["/**\n * External dependencies\n */\nimport useResizeAware from 'react-resize-aware';\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 * Simply a re-export of `react-resize-aware` so refer to its documentation <https://github.com/FezVrasta/react-resize-aware>\n * for more details.\n *\n * @see https://github.com/FezVrasta/react-resize-aware\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 *\n */\nexport default useResizeAware;\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.tsx"],"names":["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","position","top","left","right","bottom","pointerEvents","opacity","overflow","zIndex"],"mappings":";;;;;;;AAQA;;AARA;AACA;AACA;;AAGA;AACA;AACA;AAaA;AACA;AACA;AACA;AACA,SAASA,kBAAT,CACCC,UADD,EAECC,YAFD,EAGoB;AACnB,QAAMC,kBAAkB,GAAG,qBAAoB,IAApB,CAA3B;AACA,QAAMC,aAAa,GAAG,qBAGV,IAHU,CAAtB;AAIA,QAAMC,UAAU,GAAG,sBAAnB;AAEA,QAAMC,cAAc,GAAG,0BAAa,MAAM;AACzC,QAAIC,OAAO,GAAG,IAAd;;AACA,QAAKJ,kBAAkB,CAACK,OAAxB,EAAkC;AACjCD,MAAAA,OAAO,GAAGJ,kBAAkB,CAACK,OAA7B;AACA,KAFD,MAEO,IAAKN,YAAL,EAAoB;AAC1B,UAAKA,YAAY,YAAYO,WAA7B,EAA2C;AAC1CF,QAAAA,OAAO,GAAGL,YAAV;AACA,OAFD,MAEO;AACNK,QAAAA,OAAO,GAAGL,YAAY,CAACM,OAAvB;AACA;AACD;;AAED,QACCJ,aAAa,CAACI,OAAd,IACAJ,aAAa,CAACI,OAAd,CAAsBD,OAAtB,KAAkCA,OADlC,IAEAH,aAAa,CAACI,OAAd,CAAsBE,QAAtB,KAAmCJ,cAHpC,EAIE;AACD;AACA;;AAED,QAAKD,UAAU,CAACG,OAAhB,EAA0B;AACzBH,MAAAA,UAAU,CAACG,OAAX,GADyB,CAEzB;;AACAH,MAAAA,UAAU,CAACG,OAAX,GAAqB,IAArB;AACA;;AACDJ,IAAAA,aAAa,CAACI,OAAd,GAAwB;AACvBE,MAAAA,QAAQ,EAAEJ,cADa;AAEvBC,MAAAA;AAFuB,KAAxB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K,EAAE,CAJA;AAKPC,MAAAA,MAAM,EAAE,CALD;AAMPC,MAAAA,aAAa,EAAE,MANR;AAOPC,MAAAA,OAAO,EAAE,CAPF;AAQPC,MAAAA,QAAQ,EAAE,QARH;AASPC,MAAAA,MAAM,EAAE,CAAC;AATF,KADT;AAYC,mBAAY,MAZb;AAaC,IAAA,GAAG,EAAGZ;AAbP,IADD;AAiBA,SAAO,CAAEG,cAAF,EAAkBD,KAAlB,CAAP;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport type { 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';\nimport type { WPElement } 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\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\tWPElement,\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"]}
|
|
@@ -20,8 +20,6 @@ var _reactNative = require("react-native");
|
|
|
20
20
|
/**
|
|
21
21
|
* Hook which allows to listen the resize event of any target element when it changes sizes.
|
|
22
22
|
*
|
|
23
|
-
* @return {[JSX.Element, { width: number, height: number } | null]} An array of {Element} `resizeListener` and {?Object} `sizes` with properties `width` and `height`
|
|
24
|
-
*
|
|
25
23
|
* @example
|
|
26
24
|
*
|
|
27
25
|
* ```js
|