-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useCallback, useLayoutEffect, useRef, useState } from "react"; | ||
|
||
import ResizeObserver from 'resize-observer-polyfill' | ||
|
||
type RectResult = { | ||
bottom: number; | ||
height: number; | ||
left: number; | ||
right: number; | ||
top: number; | ||
width: number; | ||
} | null; | ||
|
||
const getRect = (element: HTMLElement | null): RectResult | null => { | ||
if (!element) return null; | ||
return element.getBoundingClientRect(); | ||
}; | ||
|
||
const useRect = (): [ | ||
RectResult, | ||
React.MutableRefObject<HTMLDivElement | null> | ||
] => { | ||
const ref = useRef<HTMLDivElement | null>(null); | ||
const current = ref.current || null; | ||
const [rect, setRect] = useState(getRect(current)); | ||
|
||
const handleResize = useCallback(() => { | ||
if (!ref.current) return; | ||
|
||
// Update client rect | ||
setRect(getRect(ref.current)); | ||
}, [ref]); | ||
|
||
useLayoutEffect(() => { | ||
const element = ref.current; | ||
if (!element) return; | ||
|
||
handleResize(); | ||
if (typeof ResizeObserver === "function") { | ||
let resizeObserver: ResizeObserver | null = new ResizeObserver(() => | ||
handleResize() | ||
); | ||
resizeObserver.observe(element); | ||
|
||
return () => { | ||
if (!resizeObserver) return; | ||
resizeObserver.disconnect(); | ||
resizeObserver = null; | ||
}; | ||
} | ||
// set resize listener | ||
window.addEventListener("resize", handleResize); | ||
// remove resize listener | ||
return () => window.removeEventListener("resize", handleResize); | ||
}, [handleResize]); | ||
|
||
return [rect, ref]; | ||
}; | ||
|
||
export default useRect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters