-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useSize for observing the dimensions of an element
- Loading branch information
Showing
8 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
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
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,45 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { canUseDOM } from './utils'; | ||
|
||
/** | ||
* Tracks size of an element. | ||
* | ||
* ⚗️ _The underlying technology is experimental. Please be aware about browser compatibility before using this in production._ | ||
* | ||
* @param ref Attribute attached to the element under observation. | ||
* @param {TypeOf<ResizeObserver>} ResizeObserverOverride Replacement for `window.ResizeObserver`, e.g. [a polyfill](https://github.com/juggle/resize-observer). | ||
* | ||
* @returns Dimensions `[width, height]`, falling back to `[0, 0]` when unavailable. | ||
* | ||
* @example | ||
* function Component() { | ||
* const ref = useRef<HTMLElement>(null); | ||
* const [width, height] = useSize(ref); | ||
* // ... | ||
* return <ElementToObserve ref={ref} />; | ||
* } | ||
*/ | ||
export default function useSize( | ||
ref: React.RefObject<HTMLElement>, | ||
ResizeObserverOverride?: typeof ResizeObserver, | ||
): Readonly<[number, number]> { | ||
const [size, setSize] = useState<Readonly<[number, number]>>([0, 0]); | ||
|
||
const ResizeObserver = | ||
ResizeObserverOverride || (canUseDOM ? window.ResizeObserver : undefined); | ||
useEffect(() => { | ||
if (!ResizeObserver || !ref.current) return undefined; | ||
|
||
const observer = new ResizeObserver(([entry]) => { | ||
const { width, height } = entry.contentRect; | ||
setSize([width, height]); | ||
}); | ||
observer.observe(ref.current); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ResizeObserver, ref]); | ||
|
||
return size; | ||
} |
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