Skip to content

Instantly share code, notes, and snippets.

@Yassir4
Last active March 18, 2022 12:26
Show Gist options
  • Save Yassir4/dd677d93acd78a2d1578d65ac697b515 to your computer and use it in GitHub Desktop.
Save Yassir4/dd677d93acd78a2d1578d65ac697b515 to your computer and use it in GitHub Desktop.
// states to hold the trigger and menu dimensions
const [triggerDimension, setTriggerDimension] = useState({
top: 0,
left: 0,
width: 0,
height: 0,
});
const [modalDimensions, setModalDimensions] = useState({
width: 0,
height: 0,
});
const calculateDimensions = () => {
triggerWrapperRef?.current?.measureInWindow((x, y, width, height) => {
setTriggerDimensions({
top: Math.max(y, 0),
left: x,
width,
height,
});
});
setTimeout(() => {
itemsWrapperRef?.current?.measureInWindow((x, y, width, height) => {
setModalDimensions({ width, height });
});
}, 200);
};
// run the calculateDimensions each time the menu is visible
useEffect(() => {
if (menuVisible) {
if (triggerWrapperRef?.current) calculateDimensions();
}
}, [menuVisible]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment