From 6a7d4b723d04294de450df9c647bd82f7f2cce25 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 21 Apr 2022 11:21:14 +0100 Subject: [PATCH 1/9] Update react-resize-aware with use-resize-observer --- package-lock.json | 24 +++++--- packages/components/package.json | 2 +- packages/components/src/flyout/utils.js | 12 ++-- .../src/resizable-box/resize-tooltip/utils.ts | 4 +- .../toggle-group-control/component.tsx | 10 +++- packages/compose/README.md | 9 +-- packages/compose/package.json | 4 +- .../src/hooks/use-resize-observer/index.js | 31 ---------- .../hooks/use-resize-observer/index.native.js | 2 - .../src/hooks/use-resize-observer/index.tsx | 56 +++++++++++++++++++ 10 files changed, 90 insertions(+), 64 deletions(-) delete mode 100644 packages/compose/src/hooks/use-resize-observer/index.js create mode 100644 packages/compose/src/hooks/use-resize-observer/index.tsx diff --git a/package-lock.json b/package-lock.json index acd5b5df134116..d1e118093a0c39 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4056,6 +4056,11 @@ "chalk": "^4.0.0" } }, + "@juggle/resize-observer": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/@juggle/resize-observer/-/resize-observer-3.3.1.tgz", + "integrity": "sha512-zMM9Ds+SawiUkakS7y94Ymqx+S0ORzpG3frZirN3l+UlXUmSUR7hF4wxCVqW+ei94JzV5kt0uXBcoOEAuiydrw==" + }, "@kwsites/file-exists": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@kwsites/file-exists/-/file-exists-1.1.1.tgz", @@ -17282,8 +17287,8 @@ "re-resizable": "^6.4.0", "react-colorful": "^5.3.1", "react-dates": "^21.8.0", - "react-resize-aware": "^3.1.0", "reakit": "^1.3.8", + "use-resize-observer": "^8.0.0", "uuid": "^8.3.0" }, "dependencies": { @@ -17369,8 +17374,8 @@ "clipboard": "^2.0.8", "lodash": "^4.17.21", "mousetrap": "^1.6.5", - "react-resize-aware": "^3.1.0", - "use-memo-one": "^1.1.1" + "use-memo-one": "^1.1.1", + "use-resize-observer": "^8.0.0" } }, "@wordpress/core-data": { @@ -51126,11 +51131,6 @@ "integrity": "sha512-PgidR3wST3dDYKr6b4pJoqQFpPGNKDSCDx4cZoshjXipw3LzO7mG1My2pwEzz2JVkF+inx3xRpDeQLFQGH/hsQ==", "dev": true }, - "react-resize-aware": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/react-resize-aware/-/react-resize-aware-3.1.0.tgz", - "integrity": "sha512-bIhHlxVTX7xKUz14ksXMEHjzCZPTpQZKZISY3nbTD273pDKPABGFNFBP6Tr42KECxzC5YQiKpMchjTVJCqaxpA==" - }, "react-router": { "version": "6.1.1", "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.1.1.tgz", @@ -57982,6 +57982,14 @@ "resolved": "https://registry.npmjs.org/use-memo-one/-/use-memo-one-1.1.1.tgz", "integrity": "sha512-oFfsyun+bP7RX8X2AskHNTxu+R3QdE/RC5IefMbqptmACAA/gfol1KDD5KRzPsGMa62sWxGZw+Ui43u6x4ddoQ==" }, + "use-resize-observer": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/use-resize-observer/-/use-resize-observer-8.0.0.tgz", + "integrity": "sha512-n0iKSeiQpJCyaFh5JA0qsVLBIovsF4EIIR1G6XiBwKJN66ZrD4Oj62bjcuTAATPKiSp6an/2UZZxCf/67fk3sQ==", + "requires": { + "@juggle/resize-observer": "^3.3.1" + } + }, "use-subscription": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/use-subscription/-/use-subscription-1.5.0.tgz", diff --git a/packages/components/package.json b/packages/components/package.json index d587d1889e085b..294af1a7485b76 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -65,8 +65,8 @@ "re-resizable": "^6.4.0", "react-colorful": "^5.3.1", "react-dates": "^21.8.0", - "react-resize-aware": "^3.1.0", "reakit": "^1.3.8", + "use-resize-observer": "^8.0.0", "uuid": "^8.3.0" }, "peerDependencies": { diff --git a/packages/components/src/flyout/utils.js b/packages/components/src/flyout/utils.js index 25a3f1c07fc791..7258e8d72d8dc4 100644 --- a/packages/components/src/flyout/utils.js +++ b/packages/components/src/flyout/utils.js @@ -1,19 +1,17 @@ -/** - * External dependencies - */ -import useResizeAware from 'react-resize-aware'; - /** * WordPress dependencies */ -import { useIsomorphicLayoutEffect } from '@wordpress/compose'; +import { + useIsomorphicLayoutEffect, + useResizeObserver, +} from '@wordpress/compose'; /** * * @param { { onResize?: () => any } } onResize */ export function useFlyoutResizeUpdater( { onResize } ) { - const [ resizeListener, sizes ] = useResizeAware(); + const [ resizeListener, sizes ] = useResizeObserver(); useIsomorphicLayoutEffect( () => { onResize?.(); diff --git a/packages/components/src/resizable-box/resize-tooltip/utils.ts b/packages/components/src/resizable-box/resize-tooltip/utils.ts index b4ec4b19f19fcb..3ac2d6babc4298 100644 --- a/packages/components/src/resizable-box/resize-tooltip/utils.ts +++ b/packages/components/src/resizable-box/resize-tooltip/utils.ts @@ -2,12 +2,12 @@ * External dependencies */ import { noop } from 'lodash'; -import useResizeAware from 'react-resize-aware'; /** * WordPress dependencies */ import { useEffect, useRef, useState } from '@wordpress/element'; +import { useResizeObserver } from '@wordpress/compose'; const { clearTimeout, setTimeout } = window; @@ -60,7 +60,7 @@ export function useResizeLabel( { * This custom hook uses injects an iFrame into the element, allowing it * to tap into the onResize (window) callback events. */ - const [ resizeListener, sizes ] = useResizeAware(); + const [ resizeListener, sizes ] = useResizeObserver(); /* * Indicates if the x/y axis is preferred. diff --git a/packages/components/src/toggle-group-control/toggle-group-control/component.tsx b/packages/components/src/toggle-group-control/toggle-group-control/component.tsx index 0ee7b7af82c4b4..a7727ccaf06670 100644 --- a/packages/components/src/toggle-group-control/toggle-group-control/component.tsx +++ b/packages/components/src/toggle-group-control/toggle-group-control/component.tsx @@ -4,14 +4,18 @@ import type { ForwardedRef } from 'react'; // eslint-disable-next-line no-restricted-imports import { RadioGroup, useRadioState } from 'reakit'; -import useResizeAware from 'react-resize-aware'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { useRef, useMemo } from '@wordpress/element'; -import { useMergeRefs, useInstanceId, usePrevious } from '@wordpress/compose'; +import { + useMergeRefs, + useInstanceId, + usePrevious, + useResizeObserver, +} from '@wordpress/compose'; /** * Internal dependencies @@ -49,7 +53,7 @@ function ToggleGroupControl( } = useContextSystem( props, 'ToggleGroupControl' ); const cx = useCx(); const containerRef = useRef(); - const [ resizeListener, sizes ] = useResizeAware(); + const [ resizeListener, sizes ] = useResizeObserver(); const baseId = useInstanceId( ToggleGroupControl, 'toggle-group-control' diff --git a/packages/compose/README.md b/packages/compose/README.md index 19e12b4470ac24..bd123baf2d31aa 100644 --- a/packages/compose/README.md +++ b/packages/compose/README.md @@ -447,14 +447,7 @@ _Returns_ ### useResizeObserver Hook which allows to listen the resize event of any target element when it changes sizes. -_Note: `useResizeObserver` will report `null` until after first render_ - -Simply a re-export of `react-resize-aware` so refer to its documentation -for more details. - -_Related_ - -- +\_Note: `useResizeObserver` will report `null` until after first render. _Usage_ diff --git a/packages/compose/package.json b/packages/compose/package.json index 0bc48ac1f3f4c5..c1704bce5f7219 100644 --- a/packages/compose/package.json +++ b/packages/compose/package.json @@ -41,8 +41,8 @@ "clipboard": "^2.0.8", "lodash": "^4.17.21", "mousetrap": "^1.6.5", - "react-resize-aware": "^3.1.0", - "use-memo-one": "^1.1.1" + "use-memo-one": "^1.1.1", + "use-resize-observer": "^8.0.0" }, "peerDependencies": { "react": "^17.0.0" diff --git a/packages/compose/src/hooks/use-resize-observer/index.js b/packages/compose/src/hooks/use-resize-observer/index.js deleted file mode 100644 index e992b57fa6b834..00000000000000 --- a/packages/compose/src/hooks/use-resize-observer/index.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * External dependencies - */ -import useResizeAware from 'react-resize-aware'; - -/** - * Hook which allows to listen the resize event of any target element when it changes sizes. - * _Note: `useResizeObserver` will report `null` until after first render_ - * - * Simply a re-export of `react-resize-aware` so refer to its documentation - * for more details. - * - * @see https://github.com/FezVrasta/react-resize-aware - * - * @example - * - * ```js - * const App = () => { - * const [ resizeListener, sizes ] = useResizeObserver(); - * - * return ( - *
- * { resizeListener } - * Your content here - *
- * ); - * }; - * ``` - * - */ -export default useResizeAware; diff --git a/packages/compose/src/hooks/use-resize-observer/index.native.js b/packages/compose/src/hooks/use-resize-observer/index.native.js index 981412b4f86e96..101213cf90ceaf 100644 --- a/packages/compose/src/hooks/use-resize-observer/index.native.js +++ b/packages/compose/src/hooks/use-resize-observer/index.native.js @@ -10,8 +10,6 @@ import { useState, useCallback } from '@wordpress/element'; /** * Hook which allows to listen the resize event of any target element when it changes sizes. * - * @return {[JSX.Element, { width: number, height: number } | null]} An array of {Element} `resizeListener` and {?Object} `sizes` with properties `width` and `height` - * * @example * * ```js diff --git a/packages/compose/src/hooks/use-resize-observer/index.tsx b/packages/compose/src/hooks/use-resize-observer/index.tsx new file mode 100644 index 00000000000000..d996d8a5058aa2 --- /dev/null +++ b/packages/compose/src/hooks/use-resize-observer/index.tsx @@ -0,0 +1,56 @@ +/** + * External dependencies + */ +import useResizeObserver from 'use-resize-observer'; + +/** + * WordPress dependencies + */ +import { useMemo } from '@wordpress/element'; +import type { WPElement } from '@wordpress/element'; + +/** + * Hook which allows to listen the resize event of any target element when it changes sizes. + * _Note: `useResizeObserver` will report `null` until after first render. + * + * @example + * + * ```js + * const App = () => { + * const [ resizeListener, sizes ] = useResizeObserver(); + * + * return ( + *
+ * { resizeListener } + * Your content here + *
+ * ); + * }; + * ``` + */ +export default function useResizeAware(): [ + WPElement, + { width: number | null; height: number | null } +] { + const { ref, width, height } = useResizeObserver(); + const sizes = useMemo( () => { + return { width: width ?? null, height: height ?? null }; + }, [ width, height ] ); + const resizeListener = ( +
+ ); + return [ resizeListener, sizes ]; +} From 95f7c538aa30186ffdb44512afc48ce9167f33a2 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 21 Apr 2022 12:30:33 +0100 Subject: [PATCH 2/9] Fix unit tests --- .../flyout/test/__snapshots__/index.js.snap | 24 ++++++++----------- .../test/__snapshots__/index.js.snap | 14 ----------- .../src/hooks/use-resize-observer/index.tsx | 1 + 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/packages/components/src/flyout/test/__snapshots__/index.js.snap b/packages/components/src/flyout/test/__snapshots__/index.js.snap index 66893021274f94..edd119e242d78b 100644 --- a/packages/components/src/flyout/test/__snapshots__/index.js.snap +++ b/packages/components/src/flyout/test/__snapshots__/index.js.snap @@ -93,13 +93,6 @@ exports[`props should render correctly 1`] = `
-