ISSUE-009 - Alternative useDimensions added.
Turns out... they're not as reliable. In testing I had the menu's height regularly fail to provide its height data.
This commit is contained in:
1 parent
aba5cc20be
commit
7999f15c20
3 files changed
+83
-7
No files matched your search
@@ -15,6 +15,7 @@
|
||||
"@bedrock-layout/split": "^2.3.5",
|
||||
"@bedrock-layout/stack": "^2.3.4",
|
||||
"@bedrock-layout/use-forwarded-ref": "^1.1.11",
|
||||
"@react-hook/resize-observer": "^1.2.5",
|
||||
"@react-spring/web": "^9.4.1",
|
||||
"@reduxjs/toolkit": "^1.7.1",
|
||||
"@testing-library/jest-dom": "^5.14.1",
|
||||
|
||||
+45
-1
@@ -1,4 +1,5 @@
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import useResizeObserver from "@react-hook/resize-observer";
|
||||
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
|
||||
import ResizeObserver from "resize-observer-polyfill";
|
||||
|
||||
export const useDimensions = <T extends HTMLElement>() => {
|
||||
@@ -32,3 +33,46 @@ export const useDimensions = <T extends HTMLElement>() => {
|
||||
{ height: number, width: number },
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Proven to be less reliable than using resizeObserver directly.
|
||||
* @returns
|
||||
*/
|
||||
export const useDimensionsAlt1 = <T extends HTMLElement>() => {
|
||||
const ref = useRef<T>(null);
|
||||
const [size, setSize] = useState<DOMRect>();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const element = ref.current;
|
||||
if (element == null) return;
|
||||
setSize(element.getBoundingClientRect());
|
||||
}, [ref]);
|
||||
|
||||
useResizeObserver(ref, (entry) => setSize(entry.contentRect));
|
||||
|
||||
// Returning correctly with generics apparently requires type def here.
|
||||
return [
|
||||
ref,
|
||||
size ?? { width: 0, height: 0 },
|
||||
] as [
|
||||
React.RefObject<T>,
|
||||
DOMRect,
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Based off the code demo for useResizeObserver.
|
||||
* https://github.com/jaredLunde/react-hook/tree/master/packages/resize-observer
|
||||
* @param target React Ref
|
||||
* @returns
|
||||
*/
|
||||
export const useDimensionsAlt2 = <T extends HTMLElement>(target: React.RefObject<T>) => {
|
||||
const [size, setSize] = useState<DOMRect>();
|
||||
|
||||
useLayoutEffect(() => {
|
||||
setSize(target.current?.getBoundingClientRect());
|
||||
}, [target]);
|
||||
|
||||
useResizeObserver(target, entry => setSize(entry.contentRect));
|
||||
return size ?? { width: 0, height: 0, x: 0, y: 0 };
|
||||
};
|
||||
@@ -1430,6 +1430,11 @@
|
||||
"@types/yargs" "^16.0.0"
|
||||
chalk "^4.0.0"
|
||||
|
||||
"@juggle/resize-observer@^3.3.1":
|
||||
version "3.3.1"
|
||||
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.3.1.tgz#b50a781709c81e10701004214340f25475a171a0"
|
||||
integrity sha512-zMM9Ds+SawiUkakS7y94Ymqx+S0ORzpG3frZirN3l+UlXUmSUR7hF4wxCVqW+ei94JzV5kt0uXBcoOEAuiydrw==
|
||||
|
||||
"@nodelib/fs.scandir@2.1.5":
|
||||
version "2.1.5"
|
||||
resolved "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz"
|
||||
@@ -1466,6 +1471,27 @@
|
||||
schema-utils "^3.0.0"
|
||||
source-map "^0.7.3"
|
||||
|
||||
"@react-hook/latest@^1.0.2":
|
||||
version "1.0.3"
|
||||
resolved "https://registry.yarnpkg.com/@react-hook/latest/-/latest-1.0.3.tgz#c2d1d0b0af8b69ec6e2b3a2412ba0768ac82db80"
|
||||
integrity sha512-dy6duzl+JnAZcDbNTfmaP3xHiKtbXYOaz3G51MGVljh548Y8MWzTr+PHLOfvpypEVW9zwvl+VyKjbWKEVbV1Rg==
|
||||
|
||||
"@react-hook/passive-layout-effect@^1.2.0":
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@react-hook/passive-layout-effect/-/passive-layout-effect-1.2.1.tgz#c06dac2d011f36d61259aa1c6df4f0d5e28bc55e"
|
||||
integrity sha512-IwEphTD75liO8g+6taS+4oqz+nnroocNfWVHWz7j+N+ZO2vYrc6PV1q7GQhuahL0IOR7JccFTsFKQ/mb6iZWAg==
|
||||
|
||||
"@react-hook/resize-observer@^1.2.5":
|
||||
version "1.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@react-hook/resize-observer/-/resize-observer-1.2.5.tgz#b59e2300de98bc6ddc6946942f21243cde10f984"
|
||||
integrity sha512-qa0pPvRxq5VbdI8mMK2apPFsZOckhQ6D3Jc9yLuyHMNhui8yEih4qyFCZBDzzK3ymZS6LAltVSVg3l1Dg9vA0w==
|
||||
dependencies:
|
||||
"@juggle/resize-observer" "^3.3.1"
|
||||
"@react-hook/latest" "^1.0.2"
|
||||
"@react-hook/passive-layout-effect" "^1.2.0"
|
||||
"@types/raf-schd" "^4.0.0"
|
||||
raf-schd "^4.0.2"
|
||||
|
||||
"@react-spring/animated@~9.4.0":
|
||||
version "9.4.1"
|
||||
resolved "https://registry.yarnpkg.com/@react-spring/animated/-/animated-9.4.1.tgz#6b5e57725408ba8347f098a5111a9fb4de74a81b"
|
||||
@@ -2138,6 +2164,11 @@
|
||||
resolved "https://registry.npmjs.org/@types/q/-/q-1.5.5.tgz"
|
||||
integrity sha512-L28j2FcJfSZOnL1WBjDYp2vUHCeIFlyYI/53EwD/rKUBQ7MtUUfbQWiyKJGpcnv4/WgrhWsFKrcPstcAt/J0tQ==
|
||||
|
||||
"@types/raf-schd@^4.0.0":
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/raf-schd/-/raf-schd-4.0.1.tgz#1f9e03736f277fe9c7b82102bf18570a6ee19f82"
|
||||
integrity sha512-Ha+EnKHFIh9EKW0/XZJPUd3EGDFisEvauaBd4VVCRPKeOqUxNEc9TodiY2Zhk33XCgzJucoFEcaoNcBAPHTQ2A==
|
||||
|
||||
"@types/react-dom@^17.0.9":
|
||||
version "17.0.11"
|
||||
resolved "https://registry.npmjs.org/@types/react-dom/-/react-dom-17.0.11.tgz"
|
||||
@@ -6339,16 +6370,11 @@ lodash.uniq@^4.5.0:
|
||||
resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz"
|
||||
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
|
||||
|
||||
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.7.0:
|
||||
lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.7.0:
|
||||
version "4.17.21"
|
||||
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
|
||||
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
|
||||
|
||||
lodash@^4.17.21:
|
||||
version "4.17.21"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
|
||||
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
|
||||
|
||||
loose-envify@^1.1.0, loose-envify@^1.4.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz"
|
||||
@@ -7600,6 +7626,11 @@ quick-lru@^5.1.1:
|
||||
resolved "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz"
|
||||
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==
|
||||
|
||||
raf-schd@^4.0.2:
|
||||
version "4.0.3"
|
||||
resolved "https://registry.yarnpkg.com/raf-schd/-/raf-schd-4.0.3.tgz#5d6c34ef46f8b2a0e880a8fcdb743efc5bfdbc1a"
|
||||
integrity sha512-tQkJl2GRWh83ui2DiPTJz9wEiMN20syf+5oKfB03yYP7ioZcJwsIK8FjrtLwH1m7C7e+Tt2yYBlrOpdT+dyeIQ==
|
||||
|
||||
raf@^3.4.1:
|
||||
version "3.4.1"
|
||||
resolved "https://registry.npmjs.org/raf/-/raf-3.4.1.tgz"
|
||||
|
||||
Reference in new issue
Block a user