@wordpress/compose 5.6.0 → 5.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 +14 -0
- package/README.md +35 -9
- 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-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/index.js +6 -6
- package/build/index.js.map +1 -1
- package/build/index.native.js +8 -0
- package/build/index.native.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-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/index.js +1 -1
- package/build-module/index.js.map +1 -1
- package/build-module/index.native.js +1 -0
- package/build-module/index.native.js.map +1 -1
- package/build-types/higher-order/if-condition/index.d.ts +0 -1
- package/build-types/higher-order/if-condition/index.d.ts.map +1 -1
- package/build-types/higher-order/with-instance-id/index.d.ts +0 -1
- package/build-types/higher-order/with-instance-id/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-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/index.d.ts +1 -1
- package/package.json +8 -9
- package/src/hooks/use-disabled/index.js +140 -50
- package/src/hooks/use-focus-on-mount/index.js +6 -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/index.js +1 -1
- package/src/index.native.js +1 -0
- package/tsconfig.json +2 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/src/hooks/use-resize-observer/index.js +0 -31
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 5.7.0 (2022-05-18)
|
|
6
|
+
|
|
7
|
+
### Bug Fix
|
|
8
|
+
|
|
9
|
+
- `useRefEffect`: Allow `void` as a valid callback return type ([#40798](https://github.com/WordPress/gutenberg/pull/40798)).
|
|
10
|
+
|
|
11
|
+
### New Features
|
|
12
|
+
|
|
13
|
+
- Add `useDisabled` hook.
|
|
14
|
+
|
|
15
|
+
### Internal
|
|
16
|
+
|
|
17
|
+
- Update the implementation of useResizeObserver to rely on the ResizableObserver API.
|
|
18
|
+
|
|
5
19
|
## 5.6.0 (2022-05-04)
|
|
6
20
|
|
|
7
21
|
## 5.5.0 (2022-04-21)
|
package/README.md
CHANGED
|
@@ -205,6 +205,39 @@ _Returns_
|
|
|
205
205
|
|
|
206
206
|
- `import('lodash').DebouncedFunc<TFunc>`: Debounced function.
|
|
207
207
|
|
|
208
|
+
### useDisabled
|
|
209
|
+
|
|
210
|
+
In some circumstances, such as block previews, all focusable DOM elements
|
|
211
|
+
(input fields, links, buttons, etc.) need to be disabled. This hook adds the
|
|
212
|
+
behavior to disable nested DOM elements to the returned ref.
|
|
213
|
+
|
|
214
|
+
_Usage_
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
import { useDisabled } from '@wordpress/compose';
|
|
218
|
+
const DisabledExample = () => {
|
|
219
|
+
const disabledRef = useDisabled();
|
|
220
|
+
return (
|
|
221
|
+
<div ref={ disabledRef }>
|
|
222
|
+
<a href="#">This link will have tabindex set to -1</a>
|
|
223
|
+
<input
|
|
224
|
+
placeholder="This input will have the disabled attribute added to it."
|
|
225
|
+
type="text"
|
|
226
|
+
/>
|
|
227
|
+
</div>
|
|
228
|
+
);
|
|
229
|
+
};
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
_Parameters_
|
|
233
|
+
|
|
234
|
+
- _config_ `Object`: Configuration object.
|
|
235
|
+
- _config.isDisabled_ `boolean=`: Whether the element should be disabled.
|
|
236
|
+
|
|
237
|
+
_Returns_
|
|
238
|
+
|
|
239
|
+
- `import('react').RefCallback<HTMLElement>`: Element Ref.
|
|
240
|
+
|
|
208
241
|
### useFocusableIframe
|
|
209
242
|
|
|
210
243
|
Dispatches a bubbling focus event when the iframe receives focus. Use
|
|
@@ -404,7 +437,7 @@ callback will be called multiple times for the same node.
|
|
|
404
437
|
|
|
405
438
|
_Parameters_
|
|
406
439
|
|
|
407
|
-
- _callback_ `( node: TElement ) => ( () => void ) |
|
|
440
|
+
- _callback_ `( node: TElement ) => ( () => void ) | void`: Callback with ref as argument.
|
|
408
441
|
- _dependencies_ `DependencyList`: Dependencies of the callback.
|
|
409
442
|
|
|
410
443
|
_Returns_
|
|
@@ -414,14 +447,7 @@ _Returns_
|
|
|
414
447
|
### useResizeObserver
|
|
415
448
|
|
|
416
449
|
Hook which allows to listen the resize event of any target element when it changes sizes.
|
|
417
|
-
_Note: `useResizeObserver` will report `null` until after first
|
|
418
|
-
|
|
419
|
-
Simply a re-export of `react-resize-aware` so refer to its documentation <https://github.com/FezVrasta/react-resize-aware>
|
|
420
|
-
for more details.
|
|
421
|
-
|
|
422
|
-
_Related_
|
|
423
|
-
|
|
424
|
-
- <https://github.com/FezVrasta/react-resize-aware>
|
|
450
|
+
\_Note: `useResizeObserver` will report `null` until after first render.
|
|
425
451
|
|
|
426
452
|
_Usage_
|
|
427
453
|
|
|
@@ -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,CAAWC,WAAX,CAAwB,aAAxB,EAAuC,MAAvC;AACAH,MAAAA,IAAI,CAACE,KAAL,CAAWC,WAAX,CAAwB,qBAAxB,EAA+C,MAA/C;;AACAC,iBAAMC,SAAN,CAAgBC,IAAhB,CAAsBN,IAAtB,EAA6BO,OAA7B,CAAwCF,SAAF,IAAiB;AAAA;;AACtD,YACC,sBAAUP,4BAAV,EAAwCO,SAAS,CAACG,QAAlD,CADD,EAEE;AACDH,UAAAA,SAAS,CAACI,YAAV,CAAwB,UAAxB,EAAoC,EAApC;AACA;;AAED,YAAKJ,SAAS,CAACG,QAAV,KAAuB,GAA5B,EAAkC;AACjCH,UAAAA,SAAS,CAACI,YAAV,CAAwB,UAAxB,EAAoC,IAApC;AACA;;AAED,cAAMC,QAAQ,GAAGL,SAAS,CAACM,YAAV,CAAwB,UAAxB,CAAjB;;AACA,YAAKD,QAAQ,KAAK,IAAb,IAAqBA,QAAQ,KAAK,IAAvC,EAA8C;AAC7CL,UAAAA,SAAS,CAACO,eAAV,CAA2B,UAA3B;AACA;;AAED,YAAKP,SAAS,CAACQ,YAAV,CAAwB,iBAAxB,CAAL,EAAmD;AAClDR,UAAAA,SAAS,CAACI,YAAV,CAAwB,iBAAxB,EAA2C,OAA3C;AACA;;AAED,YACC,yBAAAT,IAAI,CAACc,aAAL,CAAmBC,WAAnB,wEAAgCC,WAAhC,IACAX,SAAS,YACRL,IAAI,CAACc,aAAL,CAAmBC,WAAnB,CAA+BC,WAHjC,EAIE;AACDX,UAAAA,SAAS,CAACH,KAAV,CAAgBC,WAAhB,CAA6B,gBAA7B,EAA+C,MAA/C;AACA;AACD,OA3BD;AA4BA,KA/BD,CADgC,CAkChC;AACA;;;AACA,UAAMc,gBAAgB,GAAG,sBAAUhB,OAAV,EAAmBiB,SAAnB,EAA8B;AACtDC,MAAAA,OAAO,EAAE;AAD6C,KAA9B,CAAzB;AAGAlB,IAAAA,OAAO;AAEP;;AACA,UAAMmB,QAAQ,GAAG,IAAIC,MAAM,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,4BA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DV,MAAAA,gBAAgB,CAACW,MAAjB;AACAjC,MAAAA,OAAO,CAACW,OAAR,CAAmBuB,MAAF,IAAcA,MAAM,EAArC;AACA,KAND;AAOA,GA3IK,EA4IN,CAAEpC,cAAF,CA5IM,CAAP;AA8IA","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 = node.style.getPropertyValue(\n\t\t\t\t\t\t'user-select'\n\t\t\t\t\t);\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 = focusable.getAttribute(\n\t\t\t\t\t\t\t'tabindex'\n\t\t\t\t\t\t);\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 = focusable.style.getPropertyValue(\n\t\t\t\t\t\t\t'pointer-events'\n\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-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,Y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AAA,KAAP;AAAcC,IAAAA;AAAd,MAAyBV,iBAAiB,EAAhD;AACA,QAAM2B,KAAK,GAAG,sBAAS,MAAM;AAC5B,WAAO;AAAElB,MAAAA,KAAK,EAAEA,KAAF,aAAEA,KAAF,cAAEA,KAAF,GAAW,IAAlB;AAAwBC,MAAAA,MAAM,EAAEA,MAAF,aAAEA,MAAF,cAAEA,MAAF,GAAY;AAA1C,KAAP;AACA,GAFa,EAEX,CAAED,KAAF,EAASC,MAAT,CAFW,CAAd;AAGA,QAAMkB,cAAc,GACnB;AACC,IAAA,KAAK,EAAG;AACPC,MAAAA,QAAQ,EAAE,UADH;AAEPC,MAAAA,GAAG,EAAE,CAFE;AAGPC,MAAAA,IAAI,EAAE,CAHC;AAIPC,MAAAA,KAAK,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
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.native.js"],"names":["useResizeObserver","measurements","setMeasurements","onLayout","nativeEvent","width","height","layout","prevState","Math","floor","observer","StyleSheet","absoluteFill"],"mappings":";;;;;;;AAOA;;AAJA;;AAHA;AACA;AACA;;AAEA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA
|
|
1
|
+
{"version":3,"sources":["@wordpress/compose/src/hooks/use-resize-observer/index.native.js"],"names":["useResizeObserver","measurements","setMeasurements","onLayout","nativeEvent","width","height","layout","prevState","Math","floor","observer","StyleSheet","absoluteFill"],"mappings":";;;;;;;AAOA;;AAJA;;AAHA;AACA;AACA;;AAEA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMA,iBAAiB,GAAG,MAAM;AAC/B,QAAM,CAAEC,YAAF,EAAgBC,eAAhB,IAAoC,uBAAU,IAAV,CAA1C;AAEA,QAAMC,QAAQ,GAAG,0BAAa,QAAuB;AAAA,QAArB;AAAEC,MAAAA;AAAF,KAAqB;AACpD,UAAM;AAAEC,MAAAA,KAAF;AAASC,MAAAA;AAAT,QAAoBF,WAAW,CAACG,MAAtC;AACAL,IAAAA,eAAe,CAAIM,SAAF,IAAiB;AACjC,UACC,CAAEA,SAAF,IACAA,SAAS,CAACH,KAAV,KAAoBA,KADpB,IAEAG,SAAS,CAACF,MAAV,KAAqBA,MAHtB,EAIE;AACD,eAAO;AACND,UAAAA,KAAK,EAAEI,IAAI,CAACC,KAAL,CAAYL,KAAZ,CADD;AAENC,UAAAA,MAAM,EAAEG,IAAI,CAACC,KAAL,CAAYJ,MAAZ;AAFF,SAAP;AAIA;;AACD,aAAOE,SAAP;AACA,KAZc,CAAf;AAaA,GAfgB,EAed,EAfc,CAAjB;AAiBA,QAAMG,QAAQ,GACb,4BAAC,iBAAD;AAAM,IAAA,KAAK,EAAGC,wBAAWC,YAAzB;AAAwC,IAAA,QAAQ,EAAGV;AAAnD,IADD;AAIA,SAAO,CAAEQ,QAAF,EAAYV,YAAZ,CAAP;AACA,CAzBD;;eA2BeD,iB","sourcesContent":["/**\n * External dependencies\n */\nimport { View, StyleSheet } from 'react-native';\n/**\n * WordPress dependencies\n */\nimport { useState, useCallback } from '@wordpress/element';\n\n/**\n * Hook which allows to listen the resize event of any target element when it changes sizes.\n *\n * @example\n *\n * ```js\n * const App = () => {\n * \tconst [ resizeListener, sizes ] = useResizeObserver();\n *\n * \treturn (\n * \t\t<View>\n * \t\t\t{ resizeListener }\n * \t\t\tYour content here\n * \t\t</View>\n * \t);\n * };\n * ```\n *\n */\nconst useResizeObserver = () => {\n\tconst [ measurements, setMeasurements ] = useState( null );\n\n\tconst onLayout = useCallback( ( { nativeEvent } ) => {\n\t\tconst { width, height } = nativeEvent.layout;\n\t\tsetMeasurements( ( prevState ) => {\n\t\t\tif (\n\t\t\t\t! prevState ||\n\t\t\t\tprevState.width !== width ||\n\t\t\t\tprevState.height !== height\n\t\t\t) {\n\t\t\t\treturn {\n\t\t\t\t\twidth: Math.floor( width ),\n\t\t\t\t\theight: Math.floor( height ),\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn prevState;\n\t\t} );\n\t}, [] );\n\n\tconst observer = (\n\t\t<View style={ StyleSheet.absoluteFill } onLayout={ onLayout } />\n\t);\n\n\treturn [ observer, measurements ];\n};\n\nexport default useResizeObserver;\n"]}
|