@wordpress/compose 7.7.0 → 7.8.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 +9 -2
- package/README.md +41 -11
- package/build/hooks/use-drop-zone/index.js +1 -0
- package/build/hooks/use-drop-zone/index.js.map +1 -1
- package/build/hooks/use-event/index.js +51 -0
- package/build/hooks/use-event/index.js.map +1 -0
- package/build/hooks/use-focus-on-mount/index.js +8 -8
- package/build/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build/hooks/use-resize-observer/_legacy/index.js +139 -0
- package/build/hooks/use-resize-observer/_legacy/index.js.map +1 -0
- package/build/hooks/use-resize-observer/_legacy/index.native.js.map +1 -0
- package/build/hooks/use-resize-observer/index.js +71 -108
- package/build/hooks/use-resize-observer/index.js.map +1 -1
- package/build/index.js +8 -0
- package/build/index.js.map +1 -1
- package/build-module/hooks/use-drop-zone/index.js +1 -0
- package/build-module/hooks/use-drop-zone/index.js.map +1 -1
- package/build-module/hooks/use-event/index.js +45 -0
- package/build-module/hooks/use-event/index.js.map +1 -0
- package/build-module/hooks/use-focus-on-mount/index.js +8 -8
- package/build-module/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build-module/hooks/use-resize-observer/_legacy/index.js +130 -0
- package/build-module/hooks/use-resize-observer/_legacy/index.js.map +1 -0
- package/build-module/hooks/use-resize-observer/_legacy/index.native.js.map +1 -0
- package/build-module/hooks/use-resize-observer/index.js +71 -110
- package/build-module/hooks/use-resize-observer/index.js.map +1 -1
- package/build-module/index.js +1 -0
- package/build-module/index.js.map +1 -1
- package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
- package/build-types/hooks/use-event/index.d.ts +32 -0
- package/build-types/hooks/use-event/index.d.ts.map +1 -0
- package/build-types/hooks/use-focus-on-mount/index.d.ts.map +1 -1
- package/build-types/hooks/use-resize-observer/_legacy/index.d.ts +32 -0
- package/build-types/hooks/use-resize-observer/_legacy/index.d.ts.map +1 -0
- package/build-types/hooks/use-resize-observer/index.d.ts +39 -5
- package/build-types/hooks/use-resize-observer/index.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -0
- package/package.json +9 -9
- package/src/hooks/use-drop-zone/index.js +2 -0
- package/src/hooks/use-event/index.ts +51 -0
- package/src/hooks/use-focus-on-mount/index.js +8 -10
- package/src/hooks/use-resize-observer/{index.tsx → _legacy/index.tsx} +14 -30
- package/src/hooks/use-resize-observer/{test → _legacy/test}/index.native.js +1 -1
- package/src/hooks/use-resize-observer/index.ts +119 -0
- package/src/index.js +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/hooks/use-resize-observer/index.native.js.map +0 -1
- package/build-module/hooks/use-resize-observer/index.native.js.map +0 -1
- /package/build/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
- /package/build-module/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
- /package/src/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 7.8.0 (2024-09-19)
|
|
6
|
+
|
|
7
|
+
### New Features
|
|
8
|
+
|
|
9
|
+
- `useEvent`: a new utility that creates a stable callback function that has access to the latest state and can be used within event handlers and effect callbacks ([#64943](https://github.com/WordPress/gutenberg/pull/64943)).
|
|
10
|
+
- `useResizeObserver`: new and improved version of the utility (legacy API is still supported) ([#64943](https://github.com/WordPress/gutenberg/pull/64943)).
|
|
11
|
+
|
|
5
12
|
## 7.7.0 (2024-09-05)
|
|
6
13
|
|
|
7
14
|
## 7.6.0 (2024-08-21)
|
|
@@ -205,8 +212,8 @@
|
|
|
205
212
|
|
|
206
213
|
### Breaking Changes
|
|
207
214
|
|
|
208
|
-
- Drop support for Internet Explorer 11 ([#31110](https://github.com/WordPress/gutenberg/pull/31110)). Learn more at https://make.wordpress.org/core/2021/04/22/ie-11-support-phase-out-plan
|
|
209
|
-
- Increase the minimum Node.js version to v12 matching Long Term Support releases ([#31270](https://github.com/WordPress/gutenberg/pull/31270)). Learn more at https://nodejs.org/en/about/releases
|
|
215
|
+
- Drop support for Internet Explorer 11 ([#31110](https://github.com/WordPress/gutenberg/pull/31110)). Learn more at <https://make.wordpress.org/core/2021/04/22/ie-11-support-phase-out-plan/>.
|
|
216
|
+
- Increase the minimum Node.js version to v12 matching Long Term Support releases ([#31270](https://github.com/WordPress/gutenberg/pull/31270)). Learn more at <https://nodejs.org/en/about/releases/>.
|
|
210
217
|
|
|
211
218
|
## 3.25.0 (2021-03-17)
|
|
212
219
|
|
package/README.md
CHANGED
|
@@ -305,6 +305,29 @@ _Returns_
|
|
|
305
305
|
|
|
306
306
|
- `import('react').RefCallback<HTMLElement>`: Element Ref.
|
|
307
307
|
|
|
308
|
+
### useEvent
|
|
309
|
+
|
|
310
|
+
Creates a stable callback function that has access to the latest state and can be used within event handlers and effect callbacks. Throws when used in the render phase.
|
|
311
|
+
|
|
312
|
+
_Usage_
|
|
313
|
+
|
|
314
|
+
```tsx
|
|
315
|
+
function Component( props ) {
|
|
316
|
+
const onClick = useEvent( props.onClick );
|
|
317
|
+
useEffect( () => {
|
|
318
|
+
onClick();
|
|
319
|
+
// Won't trigger the effect again when props.onClick is updated.
|
|
320
|
+
}, [ onClick ] );
|
|
321
|
+
// Won't re-render Button when props.onClick is updated (if `Button` is
|
|
322
|
+
// wrapped in `React.memo`).
|
|
323
|
+
return <Button onClick={ onClick } />;
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
_Parameters_
|
|
328
|
+
|
|
329
|
+
- _callback_ `T`: The callback function to wrap.
|
|
330
|
+
|
|
308
331
|
### useFocusableIframe
|
|
309
332
|
|
|
310
333
|
Dispatches a bubbling focus event when the iframe receives focus. Use `onFocus` as usual on the iframe or a parent element.
|
|
@@ -500,23 +523,30 @@ _Returns_
|
|
|
500
523
|
|
|
501
524
|
### useResizeObserver
|
|
502
525
|
|
|
503
|
-
|
|
526
|
+
Sets up a [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/Resize_Observer_API) for an HTML or SVG element.
|
|
527
|
+
|
|
528
|
+
Pass the returned setter as a callback ref to the React element you want to observe, or use it in layout effects for advanced use cases.
|
|
504
529
|
|
|
505
530
|
_Usage_
|
|
506
531
|
|
|
507
|
-
```
|
|
508
|
-
const
|
|
509
|
-
|
|
532
|
+
```tsx
|
|
533
|
+
const setElement = useResizeObserver(
|
|
534
|
+
( resizeObserverEntries ) => console.log( resizeObserverEntries ),
|
|
535
|
+
{ box: 'border-box' }
|
|
536
|
+
);
|
|
537
|
+
<div ref={ setElement } />;
|
|
510
538
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
</div>
|
|
516
|
-
);
|
|
517
|
-
};
|
|
539
|
+
// The setter can be used in other ways, for example:
|
|
540
|
+
useLayoutEffect( () => {
|
|
541
|
+
setElement( document.querySelector( `data-element-id="${ elementId }"` ) );
|
|
542
|
+
}, [ elementId ] );
|
|
518
543
|
```
|
|
519
544
|
|
|
545
|
+
_Parameters_
|
|
546
|
+
|
|
547
|
+
- _callback_ `ResizeObserverCallback`: The `ResizeObserver` callback - [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver#callback).
|
|
548
|
+
- _options_ `ResizeObserverOptions`: Options passed to `ResizeObserver.observe` when called - [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/observe#options). Changes will be ignored.
|
|
549
|
+
|
|
520
550
|
### useStateWithHistory
|
|
521
551
|
|
|
522
552
|
useState with undo/redo history.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_element","require","_useRefEffect","_interopRequireDefault","useFreshRef","value","ref","useRef","current","useDropZone","dropZoneElement","isDisabled","onDrop","_onDrop","onDragStart","_onDragStart","onDragEnter","_onDragEnter","onDragLeave","_onDragLeave","onDragEnd","_onDragEnd","onDragOver","_onDragOver","onDropRef","onDragStartRef","onDragEnterRef","onDragLeaveRef","onDragEndRef","onDragOverRef","useRefEffect","elem","element","isDragging","ownerDocument","isElementInZone","targetToCheck","defaultView","HTMLElement","contains","elementToCheck","dataset","isDropZone","parentElement","maybeDragStart","event","addEventListener","maybeDragEnd","preventDefault","relatedTarget","defaultPrevented","dataTransfer","files","length","removeEventListener"],"sources":["@wordpress/compose/src/hooks/use-drop-zone/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * @template T\n * @param {T} value\n * @return {import('react').MutableRefObject<T|null>} A ref with the value.\n */\nfunction useFreshRef( value ) {\n\t/* eslint-enable jsdoc/valid-types */\n\t/* eslint-disable jsdoc/no-undefined-types */\n\t/** @type {import('react').MutableRefObject<T>} */\n\t/* eslint-enable jsdoc/no-undefined-types */\n\t// Disable reason: We're doing something pretty JavaScript-y here where the\n\t// ref will always have a current value that is not null or undefined but it\n\t// needs to start as undefined. We don't want to change the return type so\n\t// it's easier to just ts-ignore this specific line that's complaining about\n\t// undefined not being part of T.\n\t// @ts-ignore\n\tconst ref = useRef();\n\tref.current = value;\n\treturn ref;\n}\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.\n * @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.\n * @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.\n * @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.\n * @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.\n * @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.\n * @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tdropZoneElement,\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropRef = useFreshRef( _onDrop );\n\tconst onDragStartRef = useFreshRef( _onDragStart );\n\tconst onDragEnterRef = useFreshRef( _onDragEnter );\n\tconst onDragLeaveRef = useFreshRef( _onDragLeave );\n\tconst onDragEndRef = useFreshRef( _onDragEnd );\n\tconst onDragOverRef = useFreshRef( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( elem ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// If a custom dropZoneRef is passed, use that instead of the element.\n\t\t\t// This allows the dropzone to cover an expanded area, rather than\n\t\t\t// be restricted to the area of the ref returned by this hook.\n\t\t\tconst element = dropZoneElement ?? elem;\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {EventTarget|null} targetToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( targetToCheck ) {\n\t\t\t\tconst { defaultView } = ownerDocument;\n\t\t\t\tif (\n\t\t\t\t\t! targetToCheck ||\n\t\t\t\t\t! defaultView ||\n\t\t\t\t\t! ( targetToCheck instanceof defaultView.HTMLElement ) ||\n\t\t\t\t\t! element.contains( targetToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t/** @type {HTMLElement|null} */\n\t\t\t\tlet elementToCheck = targetToCheck;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragStartRef.current ) {\n\t\t\t\t\tonDragStartRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragEnterRef.current ) {\n\t\t\t\t\tonDragEnterRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && onDragOverRef.current ) {\n\t\t\t\t\tonDragOverRef.current( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\t// Note: This is not entirely reliable in Safari due to this bug\n\t\t\t\t// https://bugs.webkit.org/show_bug.cgi?id=66547\n\t\t\t\tif ( isElementInZone( event.relatedTarget ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragLeaveRef.current ) {\n\t\t\t\t\tonDragLeaveRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( onDropRef.current ) {\n\t\t\t\t\tonDropRef.current( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragEndRef.current ) {\n\t\t\t\t\tonDragEndRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.dataset.isDropZone = 'true';\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\tdelete element.dataset.isDropZone;\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\t\t\t};\n\t\t},\n\t\t[ isDisabled, dropZoneElement ] // Refresh when the passed in dropZoneElement changes.\n\t);\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,aAAA,GAAAC,sBAAA,CAAAF,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,SAASG,WAAWA,CAAEC,KAAK,EAAG;EAC7B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,GAAG,GAAG,IAAAC,eAAM,EAAC,CAAC;EACpBD,GAAG,CAACE,OAAO,GAAGH,KAAK;EACnB,OAAOC,GAAG;AACX;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASG,WAAWA,CAAE;EACpCC,eAAe;EACfC,UA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,OAAO,CAACsB,mBAAmB,CAAE,UAAU,EAAEhC,UAAW,CAAC;MACrDU,OAAO,CAACsB,mBAAmB,CAAE,WAAW,EAAEpC,WAAY,CAAC;MACvDgB,aAAa,CAACoB,mBAAmB,CAAE,SAAS,EAAEP,YAAa,CAAC;MAC5Db,aAAa,CAACoB,mBAAmB,CAAE,WAAW,EAAEP,YAAa,CAAC;MAC9Db,aAAa,CAACoB,mBAAmB,CAChC,WAAW,EACXV,cACD,CAAC;IACF,CAAC;EACF,CAAC,EACD,CAAEjC,UAAU,EAAED,eAAe,CAAE,CAAC;EACjC,CAAC;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_element","require","_useRefEffect","_interopRequireDefault","useFreshRef","value","ref","useRef","current","useDropZone","dropZoneElement","isDisabled","onDrop","_onDrop","onDragStart","_onDragStart","onDragEnter","_onDragEnter","onDragLeave","_onDragLeave","onDragEnd","_onDragEnd","onDragOver","_onDragOver","onDropRef","onDragStartRef","onDragEnterRef","onDragLeaveRef","onDragEndRef","onDragOverRef","useRefEffect","elem","element","isDragging","ownerDocument","isElementInZone","targetToCheck","defaultView","HTMLElement","contains","elementToCheck","dataset","isDropZone","parentElement","maybeDragStart","event","addEventListener","maybeDragEnd","preventDefault","relatedTarget","defaultPrevented","dataTransfer","files","length","removeEventListener"],"sources":["@wordpress/compose/src/hooks/use-drop-zone/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\n\n/* eslint-disable jsdoc/valid-types */\n/**\n * @template T\n * @param {T} value\n * @return {import('react').MutableRefObject<T|null>} A ref with the value.\n */\nfunction useFreshRef( value ) {\n\t/* eslint-enable jsdoc/valid-types */\n\t/* eslint-disable jsdoc/no-undefined-types */\n\t/** @type {import('react').MutableRefObject<T>} */\n\t/* eslint-enable jsdoc/no-undefined-types */\n\t// Disable reason: We're doing something pretty JavaScript-y here where the\n\t// ref will always have a current value that is not null or undefined but it\n\t// needs to start as undefined. We don't want to change the return type so\n\t// it's easier to just ts-ignore this specific line that's complaining about\n\t// undefined not being part of T.\n\t// @ts-ignore\n\tconst ref = useRef();\n\tref.current = value;\n\treturn ref;\n}\n\n/**\n * A hook to facilitate drag and drop handling.\n *\n * @param {Object} props Named parameters.\n * @param {?HTMLElement} [props.dropZoneElement] Optional element to be used as the drop zone.\n * @param {boolean} [props.isDisabled] Whether or not to disable the drop zone.\n * @param {(e: DragEvent) => void} [props.onDragStart] Called when dragging has started.\n * @param {(e: DragEvent) => void} [props.onDragEnter] Called when the zone is entered.\n * @param {(e: DragEvent) => void} [props.onDragOver] Called when the zone is moved within.\n * @param {(e: DragEvent) => void} [props.onDragLeave] Called when the zone is left.\n * @param {(e: MouseEvent) => void} [props.onDragEnd] Called when dragging has ended.\n * @param {(e: DragEvent) => void} [props.onDrop] Called when dropping in the zone.\n *\n * @return {import('react').RefCallback<HTMLElement>} Ref callback to be passed to the drop zone element.\n */\nexport default function useDropZone( {\n\tdropZoneElement,\n\tisDisabled,\n\tonDrop: _onDrop,\n\tonDragStart: _onDragStart,\n\tonDragEnter: _onDragEnter,\n\tonDragLeave: _onDragLeave,\n\tonDragEnd: _onDragEnd,\n\tonDragOver: _onDragOver,\n} ) {\n\tconst onDropRef = useFreshRef( _onDrop );\n\tconst onDragStartRef = useFreshRef( _onDragStart );\n\tconst onDragEnterRef = useFreshRef( _onDragEnter );\n\tconst onDragLeaveRef = useFreshRef( _onDragLeave );\n\tconst onDragEndRef = useFreshRef( _onDragEnd );\n\tconst onDragOverRef = useFreshRef( _onDragOver );\n\n\treturn useRefEffect(\n\t\t( elem ) => {\n\t\t\tif ( isDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// If a custom dropZoneRef is passed, use that instead of the element.\n\t\t\t// This allows the dropzone to cover an expanded area, rather than\n\t\t\t// be restricted to the area of the ref returned by this hook.\n\t\t\tconst element = dropZoneElement ?? elem;\n\n\t\t\tlet isDragging = false;\n\n\t\t\tconst { ownerDocument } = element;\n\n\t\t\t/**\n\t\t\t * Checks if an element is in the drop zone.\n\t\t\t *\n\t\t\t * @param {EventTarget|null} targetToCheck\n\t\t\t *\n\t\t\t * @return {boolean} True if in drop zone, false if not.\n\t\t\t */\n\t\t\tfunction isElementInZone( targetToCheck ) {\n\t\t\t\tconst { defaultView } = ownerDocument;\n\n\t\t\t\tif (\n\t\t\t\t\t! targetToCheck ||\n\t\t\t\t\t! defaultView ||\n\t\t\t\t\t! ( targetToCheck instanceof defaultView.HTMLElement ) ||\n\t\t\t\t\t! element.contains( targetToCheck )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t/** @type {HTMLElement|null} */\n\t\t\t\tlet elementToCheck = targetToCheck;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ( elementToCheck.dataset.isDropZone ) {\n\t\t\t\t\t\treturn elementToCheck === element;\n\t\t\t\t\t}\n\t\t\t\t} while ( ( elementToCheck = elementToCheck.parentElement ) );\n\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tfunction maybeDragStart( /** @type {DragEvent} */ event ) {\n\t\t\t\tif ( isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = true;\n\n\t\t\t\t// Note that `dragend` doesn't fire consistently for file and\n\t\t\t\t// HTML drag events where the drag origin is outside the browser\n\t\t\t\t// window. In Firefox it may also not fire if the originating\n\t\t\t\t// node is removed.\n\t\t\t\townerDocument.addEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.addEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragStartRef.current ) {\n\t\t\t\t\tonDragStartRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragEnter( /** @type {DragEvent} */ event ) {\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// The `dragenter` event will also fire when entering child\n\t\t\t\t// elements, but we only want to call `onDragEnter` when\n\t\t\t\t// entering the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been left) should be outside the drop zone.\n\t\t\t\tif (\n\t\t\t\t\telement.contains(\n\t\t\t\t\t\t/** @type {Node} */ ( event.relatedTarget )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragEnterRef.current ) {\n\t\t\t\t\tonDragEnterRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDragOver( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Only call onDragOver for the innermost hovered drop zones.\n\t\t\t\tif ( ! event.defaultPrevented && onDragOverRef.current ) {\n\t\t\t\t\tonDragOverRef.current( event );\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDragOver` is already handled.\n\t\t\t\tevent.preventDefault();\n\t\t\t}\n\n\t\t\tfunction onDragLeave( /** @type {DragEvent} */ event ) {\n\t\t\t\t// The `dragleave` event will also fire when leaving child\n\t\t\t\t// elements, but we only want to call `onDragLeave` when\n\t\t\t\t// leaving the drop zone, which means the `relatedTarget`\n\t\t\t\t// (element that has been entered) should be outside the drop\n\t\t\t\t// zone.\n\t\t\t\t// Note: This is not entirely reliable in Safari due to this bug\n\t\t\t\t// https://bugs.webkit.org/show_bug.cgi?id=66547\n\n\t\t\t\tif ( isElementInZone( event.relatedTarget ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( onDragLeaveRef.current ) {\n\t\t\t\t\tonDragLeaveRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction onDrop( /** @type {DragEvent} */ event ) {\n\t\t\t\t// Don't handle drop if an inner drop zone already handled it.\n\t\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// Prevent the browser default while also signalling to parent\n\t\t\t\t// drop zones that `onDrop` is already handled.\n\t\t\t\tevent.preventDefault();\n\n\t\t\t\t// This seemingly useless line has been shown to resolve a\n\t\t\t\t// Safari issue where files dragged directly from the dock are\n\t\t\t\t// not recognized.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tevent.dataTransfer && event.dataTransfer.files.length;\n\n\t\t\t\tif ( onDropRef.current ) {\n\t\t\t\t\tonDropRef.current( event );\n\t\t\t\t}\n\n\t\t\t\tmaybeDragEnd( event );\n\t\t\t}\n\n\t\t\tfunction maybeDragEnd( /** @type {MouseEvent} */ event ) {\n\t\t\t\tif ( ! isDragging ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tisDragging = false;\n\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\n\t\t\t\tif ( onDragEndRef.current ) {\n\t\t\t\t\tonDragEndRef.current( event );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\telement.dataset.isDropZone = 'true';\n\t\t\telement.addEventListener( 'drop', onDrop );\n\t\t\telement.addEventListener( 'dragenter', onDragEnter );\n\t\t\telement.addEventListener( 'dragover', onDragOver );\n\t\t\telement.addEventListener( 'dragleave', onDragLeave );\n\t\t\t// The `dragstart` event doesn't fire if the drag started outside\n\t\t\t// the document.\n\t\t\townerDocument.addEventListener( 'dragenter', maybeDragStart );\n\n\t\t\treturn () => {\n\t\t\t\tdelete element.dataset.isDropZone;\n\t\t\t\telement.removeEventListener( 'drop', onDrop );\n\t\t\t\telement.removeEventListener( 'dragenter', onDragEnter );\n\t\t\t\telement.removeEventListener( 'dragover', onDragOver );\n\t\t\t\telement.removeEventListener( 'dragleave', onDragLeave );\n\t\t\t\townerDocument.removeEventListener( 'dragend', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener( 'mousemove', maybeDragEnd );\n\t\t\t\townerDocument.removeEventListener(\n\t\t\t\t\t'dragenter',\n\t\t\t\t\tmaybeDragStart\n\t\t\t\t);\n\t\t\t};\n\t\t},\n\t\t[ isDisabled, dropZoneElement ] // Refresh when the passed in dropZoneElement changes.\n\t);\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAKA,IAAAC,aAAA,GAAAC,sBAAA,CAAAF,OAAA;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,SAASG,WAAWA,CAAEC,KAAK,EAAG;EAC7B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,GAAG,GAAG,IAAAC,eAAM,EAAC,CAAC;EACpBD,GAAG,CAACE,OAAO,GAAGH,KAAK;EACnB,OAAOC,GAAG;AACX;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASG,WAAWA,CAAE;EACpCC,eAAe;EACfC,UAAU;EACVC,MAAM,EAAEC,OAAO;EACfC,WAAW,EAAEC,YAAY;EACzBC,WAAW,EAAEC,YAAY;EACzBC,WAAW,EAAEC,YAAY;EACzBC,SAAS,EAAEC,UAAU;EACrBC,UAAU,EAAEC;AACb,CAAC,EAAG;EACH,MAAMC,SAAS,GAAGpB,WAAW,CAAES,OAAQ,CAAC;EACxC,MAAMY,cAAc,GAAGrB,WAAW,CAAEW,YAAa,CAAC;EAClD,MAAMW,cAAc,GAAGtB,WAAW,CAAEa,YAAa,CAAC;EAClD,MAAMU,cAAc,GAAGvB,WAAW,CAAEe,YAAa,CAAC;EAClD,MAAMS,YAAY,GAAGxB,WAAW,CAAEiB,UAAW,CAAC;EAC9C,MAAMQ,aAAa,GAAGzB,WAAW,CAAEmB,WAAY,CAAC;EAEhD,OAAO,IAAAO,qBAAY,EAChBC,IAAI,IAAM;IACX,IAAKpB,UAAU,EAAG;MACjB;IACD;;IAEA;IACA;IACA;IACA,MAAMqB,OAAO,GAAGtB,eAAe,aAAfA,eAAe,cAAfA,eAAe,GAAIqB,IAAI;IAE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B;IACD;IAEAb,OAAO,CAACS,OAAO,CAACC,UAAU,GAAG,MAAM;IACnCV,OAAO,CAACc,gBAAgB,CAAE,MAAM,EAAElC,MAAO,CAAC;IAC1CoB,OAAO,CAACc,gBAAgB,CAAE,WAAW,EAAE9B,WAAY,CAAC;IACpDgB,OAAO,CAACc,gBAAgB,CAAE,UAAU,EAAExB,UAAW,CAAC;IAClDU,OAAO,CAACc,gBAAgB,CAAE,WAAW,EAAE5B,WAAY,CAAC;IACpD;IACA;IACAgB,aAAa,CAACY,gBAAgB,CAAE,WAAW,EAAEF,cAAe,CAAC;IAE7D,OAAO,MAAM;MACZ,OAAOZ,OAAO,CAACS,OAAO,CAACC,UAAU;MACjCV,OAAO,CAACsB,mBAAmB,CAAE,MAAM,EAAE1C,MAAO,CAAC;MAC7CoB,OAAO,CAACsB,mBAAmB,CAAE,WAAW,EAAEtC,WAAY,CAAC;MACvDgB,OAAO,CAACsB,mBAAmB,CAAE,UAAU,EAAEhC,UAAW,CAAC;MACrDU,OAAO,CAACsB,mBAAmB,CAAE,WAAW,EAAEpC,WAAY,CAAC;MACvDgB,aAAa,CAACoB,mBAAmB,CAAE,SAAS,EAAEP,YAAa,CAAC;MAC5Db,aAAa,CAACoB,mBAAmB,CAAE,WAAW,EAAEP,YAAa,CAAC;MAC9Db,aAAa,CAACoB,mBAAmB,CAChC,WAAW,EACXV,cACD,CAAC;IACF,CAAC;EACF,CAAC,EACD,CAAEjC,UAAU,EAAED,eAAe,CAAE,CAAC;EACjC,CAAC;AACF","ignoreList":[]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useEvent;
|
|
7
|
+
var _element = require("@wordpress/element");
|
|
8
|
+
/**
|
|
9
|
+
* WordPress dependencies
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Any function.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a stable callback function that has access to the latest state and
|
|
18
|
+
* can be used within event handlers and effect callbacks. Throws when used in
|
|
19
|
+
* the render phase.
|
|
20
|
+
*
|
|
21
|
+
* @param callback The callback function to wrap.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* ```tsx
|
|
26
|
+
* function Component( props ) {
|
|
27
|
+
* const onClick = useEvent( props.onClick );
|
|
28
|
+
* useEffect( () => {
|
|
29
|
+
* onClick();
|
|
30
|
+
* // Won't trigger the effect again when props.onClick is updated.
|
|
31
|
+
* }, [ onClick ] );
|
|
32
|
+
* // Won't re-render Button when props.onClick is updated (if `Button` is
|
|
33
|
+
* // wrapped in `React.memo`).
|
|
34
|
+
* return <Button onClick={ onClick } />;
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function useEvent(
|
|
39
|
+
/**
|
|
40
|
+
* The callback function to wrap.
|
|
41
|
+
*/
|
|
42
|
+
callback) {
|
|
43
|
+
const ref = (0, _element.useRef)(() => {
|
|
44
|
+
throw new Error('Callbacks created with `useEvent` cannot be called during rendering.');
|
|
45
|
+
});
|
|
46
|
+
(0, _element.useInsertionEffect)(() => {
|
|
47
|
+
ref.current = callback;
|
|
48
|
+
});
|
|
49
|
+
return (0, _element.useCallback)((...args) => ref.current?.(...args), []);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","useEvent","callback","ref","useRef","Error","useInsertionEffect","current","useCallback","args"],"sources":["@wordpress/compose/src/hooks/use-event/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useInsertionEffect, useCallback } from '@wordpress/element';\n\n/**\n * Any function.\n */\nexport type AnyFunction = ( ...args: any ) => any;\n\n/**\n * Creates a stable callback function that has access to the latest state and\n * can be used within event handlers and effect callbacks. Throws when used in\n * the render phase.\n *\n * @param callback The callback function to wrap.\n *\n * @example\n *\n * ```tsx\n * function Component( props ) {\n * const onClick = useEvent( props.onClick );\n * useEffect( () => {\n * onClick();\n * // Won't trigger the effect again when props.onClick is updated.\n * }, [ onClick ] );\n * // Won't re-render Button when props.onClick is updated (if `Button` is\n * // wrapped in `React.memo`).\n * return <Button onClick={ onClick } />;\n * }\n * ```\n */\nexport default function useEvent< T extends AnyFunction >(\n\t/**\n\t * The callback function to wrap.\n\t */\n\tcallback?: T\n) {\n\tconst ref = useRef< AnyFunction | undefined >( () => {\n\t\tthrow new Error(\n\t\t\t'Callbacks created with `useEvent` cannot be called during rendering.'\n\t\t);\n\t} );\n\tuseInsertionEffect( () => {\n\t\tref.current = callback;\n\t} );\n\treturn useCallback< AnyFunction >(\n\t\t( ...args ) => ref.current?.( ...args ),\n\t\t[]\n\t) as T;\n}\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAHA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASC,QAAQA;AAC/B;AACD;AACA;AACCC,QAAY,EACX;EACD,MAAMC,GAAG,GAAG,IAAAC,eAAM,EAA6B,MAAM;IACpD,MAAM,IAAIC,KAAK,CACd,sEACD,CAAC;EACF,CAAE,CAAC;EACH,IAAAC,2BAAkB,EAAE,MAAM;IACzBH,GAAG,CAACI,OAAO,GAAGL,QAAQ;EACvB,CAAE,CAAC;EACH,OAAO,IAAAM,oBAAW,EACjB,CAAE,GAAGC,IAAI,KAAMN,GAAG,CAACI,OAAO,GAAI,GAAGE,IAAK,CAAC,EACvC,EACD,CAAC;AACF","ignoreList":[]}
|
|
@@ -68,16 +68,16 @@ function useFocusOnMount(focusOnMount = 'firstElement') {
|
|
|
68
68
|
if (node.contains((_node$ownerDocument$a = node.ownerDocument?.activeElement) !== null && _node$ownerDocument$a !== void 0 ? _node$ownerDocument$a : null)) {
|
|
69
69
|
return;
|
|
70
70
|
}
|
|
71
|
-
if (focusOnMountRef.current
|
|
72
|
-
|
|
73
|
-
const firstTabbable = _dom.focus.tabbable.find(node)[0];
|
|
74
|
-
if (firstTabbable) {
|
|
75
|
-
setFocus(firstTabbable);
|
|
76
|
-
}
|
|
77
|
-
}, 0);
|
|
71
|
+
if (focusOnMountRef.current !== 'firstElement') {
|
|
72
|
+
setFocus(node);
|
|
78
73
|
return;
|
|
79
74
|
}
|
|
80
|
-
|
|
75
|
+
timerIdRef.current = setTimeout(() => {
|
|
76
|
+
const firstTabbable = _dom.focus.tabbable.find(node)[0];
|
|
77
|
+
if (firstTabbable) {
|
|
78
|
+
setFocus(firstTabbable);
|
|
79
|
+
}
|
|
80
|
+
}, 0);
|
|
81
81
|
return () => {
|
|
82
82
|
if (timerIdRef.current) {
|
|
83
83
|
clearTimeout(timerIdRef.current);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_element","require","_dom","_useRefEffect","_interopRequireDefault","useFocusOnMount","focusOnMount","focusOnMountRef","useRef","setFocus","target","focus","preventScroll","timerIdRef","useEffect","current","useRefEffect","node","_node$ownerDocument$a","contains","ownerDocument","activeElement","setTimeout","firstTabbable","tabbable","find","clearTimeout"],"sources":["@wordpress/compose/src/hooks/use-focus-on-mount/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useEffect } from '@wordpress/element';\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\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\n\t/**\n\t * Sets focus on a DOM element.\n\t *\n\t * @param {HTMLElement} target The DOM element to set focus to.\n\t * @return {void}\n\t */\n\tconst setFocus = ( target ) => {\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\t/** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */\n\tconst timerIdRef = useRef();\n\n\tuseEffect( () => {\n\t\tfocusOnMountRef.current = focusOnMount;\n\t}, [ focusOnMount ] );\n\n\treturn useRefEffect( ( 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\tif ( focusOnMountRef.current
|
|
1
|
+
{"version":3,"names":["_element","require","_dom","_useRefEffect","_interopRequireDefault","useFocusOnMount","focusOnMount","focusOnMountRef","useRef","setFocus","target","focus","preventScroll","timerIdRef","useEffect","current","useRefEffect","node","_node$ownerDocument$a","contains","ownerDocument","activeElement","setTimeout","firstTabbable","tabbable","find","clearTimeout"],"sources":["@wordpress/compose/src/hooks/use-focus-on-mount/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useRef, useEffect } from '@wordpress/element';\nimport { focus } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport useRefEffect from '../use-ref-effect';\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\n\t/**\n\t * Sets focus on a DOM element.\n\t *\n\t * @param {HTMLElement} target The DOM element to set focus to.\n\t * @return {void}\n\t */\n\tconst setFocus = ( target ) => {\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\t/** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */\n\tconst timerIdRef = useRef();\n\n\tuseEffect( () => {\n\t\tfocusOnMountRef.current = focusOnMount;\n\t}, [ focusOnMount ] );\n\n\treturn useRefEffect( ( 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\tif ( focusOnMountRef.current !== 'firstElement' ) {\n\t\t\tsetFocus( node );\n\t\t\treturn;\n\t\t}\n\n\t\ttimerIdRef.current = setTimeout( () => {\n\t\t\tconst firstTabbable = focus.tabbable.find( node )[ 0 ];\n\t\t\tif ( firstTabbable ) {\n\t\t\t\tsetFocus( firstTabbable );\n\t\t\t}\n\t\t}, 0 );\n\n\t\treturn () => {\n\t\t\tif ( timerIdRef.current ) {\n\t\t\t\tclearTimeout( timerIdRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,IAAA,GAAAD,OAAA;AAKA,IAAAE,aAAA,GAAAC,sBAAA,CAAAH,OAAA;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA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YAAY,CAAEb,UAAU,CAACE,OAAQ,CAAC;MACnC;IACD,CAAC;EACF,CAAC,EAAE,EAAG,CAAC;AACR","ignoreList":[]}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = useLegacyResizeObserver;
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
9
|
+
var _index = _interopRequireDefault(require("../index"));
|
|
10
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
11
|
+
/**
|
|
12
|
+
* External dependencies
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* WordPress dependencies
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Internal dependencies
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// We're only using the first element of the size sequences, until future versions of the spec solidify on how
|
|
24
|
+
// exactly it'll be used for fragments in multi-column scenarios:
|
|
25
|
+
// From the spec:
|
|
26
|
+
// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,
|
|
27
|
+
// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not
|
|
28
|
+
// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single
|
|
29
|
+
// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.
|
|
30
|
+
// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.
|
|
31
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)
|
|
32
|
+
//
|
|
33
|
+
// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,
|
|
34
|
+
// regardless of the "box" option.
|
|
35
|
+
// The spec states the following on this:
|
|
36
|
+
// > This does not have any impact on which box dimensions are returned to the defined callback when the event
|
|
37
|
+
// > is fired, it solely defines which box the author wishes to observe layout changes on.
|
|
38
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
39
|
+
// I'm not exactly clear on what this means, especially when you consider a later section stating the following:
|
|
40
|
+
// > This section is non-normative. An author may desire to observe more than one CSS box.
|
|
41
|
+
// > In this case, author will need to use multiple ResizeObservers.
|
|
42
|
+
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
43
|
+
// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.
|
|
44
|
+
// For this reason I decided to only return the requested size,
|
|
45
|
+
// even though it seems we have access to results for all box types.
|
|
46
|
+
// This also means that we get to keep the current api, being able to return a simple { width, height } pair,
|
|
47
|
+
// regardless of box option.
|
|
48
|
+
const extractSize = entry => {
|
|
49
|
+
let entrySize;
|
|
50
|
+
if (!entry.contentBoxSize) {
|
|
51
|
+
// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.
|
|
52
|
+
// See the 6th step in the description for the RO algorithm:
|
|
53
|
+
// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h
|
|
54
|
+
// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".
|
|
55
|
+
// In real browser implementations of course these objects differ, but the width/height values should be equivalent.
|
|
56
|
+
entrySize = [entry.contentRect.width, entry.contentRect.height];
|
|
57
|
+
} else if (entry.contentBoxSize[0]) {
|
|
58
|
+
const contentBoxSize = entry.contentBoxSize[0];
|
|
59
|
+
entrySize = [contentBoxSize.inlineSize, contentBoxSize.blockSize];
|
|
60
|
+
} else {
|
|
61
|
+
// TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's buggy
|
|
62
|
+
// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.
|
|
63
|
+
const contentBoxSize = entry.contentBoxSize;
|
|
64
|
+
entrySize = [contentBoxSize.inlineSize, contentBoxSize.blockSize];
|
|
65
|
+
}
|
|
66
|
+
const [width, height] = entrySize.map(d => Math.round(d));
|
|
67
|
+
return {
|
|
68
|
+
width,
|
|
69
|
+
height
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
const RESIZE_ELEMENT_STYLES = {
|
|
73
|
+
position: 'absolute',
|
|
74
|
+
top: 0,
|
|
75
|
+
left: 0,
|
|
76
|
+
right: 0,
|
|
77
|
+
bottom: 0,
|
|
78
|
+
pointerEvents: 'none',
|
|
79
|
+
opacity: 0,
|
|
80
|
+
overflow: 'hidden',
|
|
81
|
+
zIndex: -1
|
|
82
|
+
};
|
|
83
|
+
function ResizeElement({
|
|
84
|
+
onResize
|
|
85
|
+
}) {
|
|
86
|
+
const resizeElementRef = (0, _index.default)(entries => {
|
|
87
|
+
const newSize = extractSize(entries.at(-1)); // Entries are never empty.
|
|
88
|
+
onResize(newSize);
|
|
89
|
+
});
|
|
90
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
91
|
+
ref: resizeElementRef,
|
|
92
|
+
style: RESIZE_ELEMENT_STYLES,
|
|
93
|
+
"aria-hidden": "true"
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function sizeEquals(a, b) {
|
|
97
|
+
return a.width === b.width && a.height === b.height;
|
|
98
|
+
}
|
|
99
|
+
const NULL_SIZE = {
|
|
100
|
+
width: null,
|
|
101
|
+
height: null
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Hook which allows to listen to the resize event of any target element when it changes size.
|
|
106
|
+
* _Note: `useResizeObserver` will report `null` sizes until after first render.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
*
|
|
110
|
+
* ```js
|
|
111
|
+
* const App = () => {
|
|
112
|
+
* const [ resizeListener, sizes ] = useResizeObserver();
|
|
113
|
+
*
|
|
114
|
+
* return (
|
|
115
|
+
* <div>
|
|
116
|
+
* { resizeListener }
|
|
117
|
+
* Your content here
|
|
118
|
+
* </div>
|
|
119
|
+
* );
|
|
120
|
+
* };
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
function useLegacyResizeObserver() {
|
|
124
|
+
const [size, setSize] = (0, _element.useState)(NULL_SIZE);
|
|
125
|
+
|
|
126
|
+
// Using a ref to track the previous width / height to avoid unnecessary renders.
|
|
127
|
+
const previousSizeRef = (0, _element.useRef)(NULL_SIZE);
|
|
128
|
+
const handleResize = (0, _element.useCallback)(newSize => {
|
|
129
|
+
if (!sizeEquals(previousSizeRef.current, newSize)) {
|
|
130
|
+
previousSizeRef.current = newSize;
|
|
131
|
+
setSize(newSize);
|
|
132
|
+
}
|
|
133
|
+
}, []);
|
|
134
|
+
const resizeElement = /*#__PURE__*/(0, _jsxRuntime.jsx)(ResizeElement, {
|
|
135
|
+
onResize: handleResize
|
|
136
|
+
});
|
|
137
|
+
return [resizeElement, size];
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_element","require","_index","_interopRequireDefault","_jsxRuntime","extractSize","entry","entrySize","contentBoxSize","contentRect","width","height","inlineSize","blockSize","map","d","Math","round","RESIZE_ELEMENT_STYLES","position","top","left","right","bottom","pointerEvents","opacity","overflow","zIndex","ResizeElement","onResize","resizeElementRef","useResizeObserver","entries","newSize","at","jsx","ref","style","sizeEquals","a","b","NULL_SIZE","useLegacyResizeObserver","size","setSize","useState","previousSizeRef","useRef","handleResize","useCallback","current","resizeElement"],"sources":["@wordpress/compose/src/hooks/use-resize-observer/_legacy/index.tsx"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { ReactElement } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback, useRef, useState } from '@wordpress/element';\n/**\n * Internal dependencies\n */\nimport useResizeObserver from '../index';\n\nexport type ObservedSize = {\n\twidth: number | null;\n\theight: number | null;\n};\n\n// We're only using the first element of the size sequences, until future versions of the spec solidify on how\n// exactly it'll be used for fragments in multi-column scenarios:\n// From the spec:\n// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,\n// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not\n// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single\n// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.\n// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)\n//\n// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,\n// regardless of the \"box\" option.\n// The spec states the following on this:\n// > This does not have any impact on which box dimensions are returned to the defined callback when the event\n// > is fired, it solely defines which box the author wishes to observe layout changes on.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// I'm not exactly clear on what this means, especially when you consider a later section stating the following:\n// > This section is non-normative. An author may desire to observe more than one CSS box.\n// > In this case, author will need to use multiple ResizeObservers.\n// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)\n// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.\n// For this reason I decided to only return the requested size,\n// even though it seems we have access to results for all box types.\n// This also means that we get to keep the current api, being able to return a simple { width, height } pair,\n// regardless of box option.\nconst extractSize = ( entry: ResizeObserverEntry ): ObservedSize => {\n\tlet entrySize;\n\tif ( ! entry.contentBoxSize ) {\n\t\t// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.\n\t\t// See the 6th step in the description for the RO algorithm:\n\t\t// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h\n\t\t// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of \"content-box\".\n\t\t// In real browser implementations of course these objects differ, but the width/height values should be equivalent.\n\t\tentrySize = [ entry.contentRect.width, entry.contentRect.height ];\n\t} else if ( entry.contentBoxSize[ 0 ] ) {\n\t\tconst contentBoxSize = entry.contentBoxSize[ 0 ];\n\t\tentrySize = [ contentBoxSize.inlineSize, contentBoxSize.blockSize ];\n\t} else {\n\t\t// TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's buggy\n\t\t// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.\n\t\tconst contentBoxSize =\n\t\t\tentry.contentBoxSize as unknown as ResizeObserverSize;\n\t\tentrySize = [ contentBoxSize.inlineSize, contentBoxSize.blockSize ];\n\t}\n\n\tconst [ width, height ] = entrySize.map( ( d ) => Math.round( d ) );\n\treturn { width, height };\n};\n\nconst RESIZE_ELEMENT_STYLES = {\n\tposition: 'absolute',\n\ttop: 0,\n\tleft: 0,\n\tright: 0,\n\tbottom: 0,\n\tpointerEvents: 'none',\n\topacity: 0,\n\toverflow: 'hidden',\n\tzIndex: -1,\n} as const;\n\ntype ResizeElementProps = {\n\tonResize: ( s: ObservedSize ) => void;\n};\n\nfunction ResizeElement( { onResize }: ResizeElementProps ) {\n\tconst resizeElementRef = useResizeObserver( ( entries ) => {\n\t\tconst newSize = extractSize( entries.at( -1 )! ); // Entries are never empty.\n\t\tonResize( newSize );\n\t} );\n\n\treturn (\n\t\t<div\n\t\t\tref={ resizeElementRef }\n\t\t\tstyle={ RESIZE_ELEMENT_STYLES }\n\t\t\taria-hidden=\"true\"\n\t\t/>\n\t);\n}\n\nfunction sizeEquals( a: ObservedSize, b: ObservedSize ) {\n\treturn a.width === b.width && a.height === b.height;\n}\n\nconst NULL_SIZE: ObservedSize = { width: null, height: null };\n\n/**\n * Hook which allows to listen to the resize event of any target element when it changes size.\n * _Note: `useResizeObserver` will report `null` sizes until after first render.\n *\n * @example\n *\n * ```js\n * const App = () => {\n * \tconst [ resizeListener, sizes ] = useResizeObserver();\n *\n * \treturn (\n * \t\t<div>\n * \t\t\t{ resizeListener }\n * \t\t\tYour content here\n * \t\t</div>\n * \t);\n * };\n * ```\n */\nexport default function useLegacyResizeObserver(): [\n\tReactElement,\n\tObservedSize,\n] {\n\tconst [ size, setSize ] = useState( NULL_SIZE );\n\n\t// Using a ref to track the previous width / height to avoid unnecessary renders.\n\tconst previousSizeRef = useRef( NULL_SIZE );\n\n\tconst handleResize = useCallback( ( newSize: ObservedSize ) => {\n\t\tif ( ! sizeEquals( previousSizeRef.current, newSize ) ) {\n\t\t\tpreviousSizeRef.current = newSize;\n\t\t\tsetSize( newSize );\n\t\t}\n\t}, [] );\n\n\tconst resizeElement = <ResizeElement onResize={ handleResize } />;\n\treturn [ resizeElement, size ];\n}\n"],"mappings":";;;;;;;AAQA,IAAAA,QAAA,GAAAC,OAAA;AAIA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AAAyC,IAAAG,WAAA,GAAAH,OAAA;AAZzC;AACA;AACA;;AAGA;AACA;AACA;;AAEA;AACA;AACA;;AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMI,WAAW,GAAKC,KAA0B,IAAoB;EACnE,IAAIC,SAAS;EACb,IAAK,CAAED,KAAK,CAACE,cAAc,EAAG;IAC7B;IACA;IACA;IACA;IACA;IACAD,SAAS,GAAG,CAAED,KAAK,CAACG,WAAW,CAACC,KAAK,EAAEJ,KAAK,CAACG,WAAW,CAACE,MAAM,CAAE;EAClE,CAAC,MAAM,IAAKL,KAAK,CAACE,cAAc,CAAE,CAAC,CAAE,EAAG;IACvC,MAAMA,cAAc,GAAGF,KAAK,CAACE,cAAc,CAAE,CAAC,CAAE;IAChDD,SAAS,GAAG,CAAEC,cAAc,CAACI,UAAU,EAAEJ,cAAc,CAACK,SAAS,CAAE;EACpE,CAAC,MAAM;IACN;IACA;IACA,MAAML,cAAc,GACnBF,KAAK,CAACE,cAA+C;IACtDD,SAAS,GAAG,CAAEC,cAAc,CAACI,UAAU,EAAEJ,cAAc,CAACK,SAAS,CAAE;EACpE;EAEA,MAAM,CAAEH,KAAK,EAAEC,MAAM,CAAE,GAAGJ,SAAS,CAACO,GAAG,CAAIC,CAAC,IAAMC,IAAI,CAACC,KAAK,CAAEF,CAAE,CAAE,CAAC;EACnE,OAAO;IAAEL,KAAK;IAAEC;EAAO,CAAC;AACzB,CAAC;AA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AY,GAAG,IAAAC,oBAAW,EAAIhB,OAAqB,IAAM;IAC9D,IAAK,CAAEK,UAAU,CAAEQ,eAAe,CAACI,OAAO,EAAEjB,OAAQ,CAAC,EAAG;MACvDa,eAAe,CAACI,OAAO,GAAGjB,OAAO;MACjCW,OAAO,CAAEX,OAAQ,CAAC;IACnB;EACD,CAAC,EAAE,EAAG,CAAC;EAEP,MAAMkB,aAAa,gBAAG,IAAA/C,WAAA,CAAA+B,GAAA,EAACP,aAAa;IAACC,QAAQ,EAAGmB;EAAc,CAAE,CAAC;EACjE,OAAO,CAAEG,aAAa,EAAER,IAAI,CAAE;AAC/B","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_element","_jsxRuntime","useResizeObserver","measurements","setMeasurements","useState","onLayout","useCallback","nativeEvent","width","height","layout","prevState","Math","floor","observer","jsx","View","testID","style","StyleSheet","absoluteFill","_default","exports","default"],"sources":["@wordpress/compose/src/hooks/use-resize-observer/_legacy/index.native.js"],"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 */\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\n\t\t\ttestID=\"resize-observer\"\n\t\t\tstyle={ StyleSheet.absoluteFill }\n\t\t\tonLayout={ onLayout }\n\t\t/>\n\t);\n\n\treturn [ observer, measurements ];\n};\n\nexport default useResizeObserver;\n"],"mappings":";;;;;;AAGA,IAAAA,YAAA,GAAAC,OAAA;AAIA,IAAAC,QAAA,GAAAD,OAAA;AAA2D,IAAAE,WAAA,GAAAF,OAAA;AAP3D;AACA;AACA;;AAEA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GACA,MAAMG,iBAAiB,GAAGA,CAAA,KAAM;EAC/B,MAAM,CAAEC,YAAY,EAAEC,eAAe,CAAE,GAAG,IAAAC,iBAAQ,EAAE,IAAK,CAAC;EAE1D,MAAMC,QAAQ,GAAG,IAAAC,oBAAW,EAAE,CAAE;IAAEC;EAAY,CAAC,KAAM;IACpD,MAAM;MAAEC,KAAK;MAAEC;IAAO,CAAC,GAAGF,WAAW,CAACG,MAAM;IAC5CP,eAAe,CAAIQ,SAAS,IAAM;MACjC,IACC,CAAEA,SAAS,IACXA,SAAS,CAACH,KAAK,KAAKA,KAAK,IACzBG,SAAS,CAACF,MAAM,KAAKA,MAAM,EAC1B;QACD,OAAO;UACND,KAAK,EAAEI,IAAI,CAACC,KAAK,CAAEL,KAAM,CAAC;UAC1BC,MAAM,EAAEG,IAAI,CAACC,KAAK,CAAEJ,MAAO;QAC5B,CAAC;MACF;MACA,OAAOE,SAAS;IACjB,CAAE,CAAC;EACJ,CAAC,EAAE,EAAG,CAAC;EAEP,MAAMG,QAAQ,gBACb,IAAAd,WAAA,CAAAe,GAAA,EAAClB,YAAA,CAAAmB,IAAI;IACJC,MAAM,EAAC,iBAAiB;IACxBC,KAAK,EAAGC,uBAAU,CAACC,YAAc;IACjCf,QAAQ,EAAGA;EAAU,CACrB,CACD;EAED,OAAO,CAAES,QAAQ,EAAEZ,YAAY,CAAE;AAClC,CAAC;AAAC,IAAAmB,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEatB,iBAAiB","ignoreList":[]}
|