Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import { useState, useEffect, useRef } from 'react';
import ResizeObserver from '@juggle/resize-observer';
export default function useResizeObserver<T extends HTMLElement>(
predefinedRefs?: React.RefObject<T>,
): [React.RefObject<T>, DOMRectReadOnly | undefined] {
const ref = useRef<T>(null);
const [dimensions, setDimensions] = useState<DOMRectReadOnly>();
useEffect(() => {
const refToUse = predefinedRefs || ref;
if (!refToUse.current) {
return;
}
const resizeObserver = new ResizeObserver(entries => {
setDimensions(entries[0].contentRect as any);
});
resizeObserver.observe(refToUse.current);
return () => {
if (refToUse.current) {
resizeObserver.unobserve(refToUse.current);
}
};
}, [(predefinedRefs || ref).current]);
return [predefinedRefs || ref, dimensions];
}