@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
31 lines (30 loc) • 840 B
JavaScript
import { useEffect, useRef, useState } from 'react';
export default function useMeasure() {
const ref = useRef(null);
const [dims, setDims] = useState({
width: undefined,
height: undefined,
});
useEffect(() => {
if (!ref.current) {
return;
}
const RS = typeof window !== 'undefined' && 'ResizeObserver' in window
? window.ResizeObserver
: undefined;
if (!RS) {
return;
}
const observer = new RS(entries => {
setDims({
width: entries[0].contentRect.width,
height: entries[0].contentRect.height,
});
});
observer.observe(ref.current);
return () => {
observer.disconnect();
};
}, []);
return [ref, dims];
}