mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
aef02eb0a4
Re-enable react-hooks/exhaustive-deps GitOrigin-RevId: 2a767596d2cfd41b8bfbd3928dc43c266ec7dc0c
36 lines
974 B
JavaScript
36 lines
974 B
JavaScript
import { useLayoutEffect, useRef, useCallback } from 'react'
|
|
|
|
function useResizeObserver(observedElement, observedData, callback) {
|
|
const resizeObserver = useRef()
|
|
|
|
const observe = useCallback(() => {
|
|
resizeObserver.current = new ResizeObserver(function(elementsObserved) {
|
|
callback(elementsObserved[0])
|
|
})
|
|
}, [callback])
|
|
|
|
function unobserve(observedCurrent) {
|
|
resizeObserver.current.unobserve(observedCurrent)
|
|
}
|
|
|
|
useLayoutEffect(() => {
|
|
if ('ResizeObserver' in window) {
|
|
const observedCurrent = observedElement && observedElement.current
|
|
if (observedCurrent) {
|
|
observe(observedElement.current)
|
|
}
|
|
|
|
if (resizeObserver.current && observedCurrent) {
|
|
resizeObserver.current.observe(observedCurrent)
|
|
}
|
|
|
|
return () => {
|
|
if (observedCurrent) {
|
|
unobserve(observedCurrent)
|
|
}
|
|
}
|
|
}
|
|
}, [observedElement, observedData, observe])
|
|
}
|
|
|
|
export default useResizeObserver
|