mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-13 07:59:23 -05:00
1ebc8a79cb
Upgrade Prettier to v2 GitOrigin-RevId: 85aa3fa1acb6332c4f58c46165a43d1a51471f33
36 lines
975 B
JavaScript
36 lines
975 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
|