|
1 | | -import React from "react"; |
2 | | -// |
3 | | -import onResize from "../utils/detectElementResize"; |
| 1 | +import React from 'react' |
4 | 2 |
|
5 | | -export default function useHyperResponsive(ref) { |
6 | | - const [{ width, height }, setState] = React.useState({ |
7 | | - width: 0, |
8 | | - height: 0 |
9 | | - }); |
| 3 | +import observeRect from '@reach/observe-rect' |
10 | 4 |
|
11 | | - const dimsRef = React.useRef(); |
12 | | - dimsRef.current = { |
13 | | - width, |
14 | | - height |
15 | | - }; |
| 5 | +import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect' |
16 | 6 |
|
17 | | - const resize = React.useCallback( |
18 | | - () => { |
19 | | - if (!ref.current) { |
20 | | - return; |
21 | | - } |
| 7 | +export default function useRect(nodeRef) { |
| 8 | + const [element, setElement] = React.useState(nodeRef.current?.parentElement) |
| 9 | + const [rect, setRect] = React.useState({ width: 0, height: 0 }) |
| 10 | + const initialRectSet = React.useRef(false) |
22 | 11 |
|
23 | | - const computed = window.getComputedStyle(ref.current.parentElement); |
| 12 | + useIsomorphicLayoutEffect(() => { |
| 13 | + if (nodeRef.current?.parentElement !== element) { |
| 14 | + setElement(nodeRef.current?.parentElement) |
| 15 | + } |
| 16 | + }) |
24 | 17 |
|
25 | | - const { |
26 | | - paddingTop, |
27 | | - paddingBottom, |
28 | | - paddingLeft, |
29 | | - paddingRight, |
30 | | - boxSizing, |
31 | | - borderTopWidth, |
32 | | - borderLeftWidth, |
33 | | - borderRightWidth, |
34 | | - borderBottomWidth |
35 | | - } = computed; |
| 18 | + useIsomorphicLayoutEffect(() => { |
| 19 | + if (element && !initialRectSet.current) { |
| 20 | + initialRectSet.current = true |
| 21 | + setRect(element.getBoundingClientRect()) |
| 22 | + } |
| 23 | + }, [element]) |
36 | 24 |
|
37 | | - let { width: newWidth, height: newHeight } = computed; |
| 25 | + React.useEffect(() => { |
| 26 | + if (!element) { |
| 27 | + return |
| 28 | + } |
38 | 29 |
|
39 | | - newWidth = parseInt(newWidth); |
40 | | - newHeight = parseInt(newHeight); |
| 30 | + const observer = observeRect(element, setRect) |
41 | 31 |
|
42 | | - if (boxSizing === "border-box") { |
43 | | - newWidth -= parseInt(paddingLeft); |
44 | | - newWidth -= parseInt(paddingRight); |
45 | | - newHeight -= parseInt(paddingTop); |
46 | | - newHeight -= parseInt(paddingBottom); |
| 32 | + observer.observe() |
47 | 33 |
|
48 | | - newWidth -= parseInt(borderLeftWidth); |
49 | | - newWidth -= parseInt(borderRightWidth); |
50 | | - newHeight -= parseInt(borderTopWidth); |
51 | | - newHeight -= parseInt(borderBottomWidth); |
52 | | - } |
| 34 | + return () => { |
| 35 | + observer.unobserve() |
| 36 | + } |
| 37 | + }, [element]) |
53 | 38 |
|
54 | | - if ( |
55 | | - newWidth !== dimsRef.current.width || |
56 | | - newHeight !== dimsRef.current.height |
57 | | - ) { |
58 | | - setState(() => ({ |
59 | | - width: newWidth, |
60 | | - height: newHeight |
61 | | - })); |
62 | | - } |
63 | | - }, |
64 | | - [ref] |
65 | | - ); |
66 | | - |
67 | | - React.useEffect( |
68 | | - () => { |
69 | | - const stopListening = onResize(ref.current.parentElement, resize); |
70 | | - return () => { |
71 | | - stopListening(); |
72 | | - }; |
73 | | - }, |
74 | | - [ref, resize] |
75 | | - ); |
76 | | - |
77 | | - return [{ width, height }]; |
| 39 | + return { width: rect.width, height: rect.height } |
78 | 40 | } |
0 commit comments