Skip to content

Instantly share code, notes, and snippets.

@FlyInk13
Created October 14, 2022 10:12
Show Gist options
  • Save FlyInk13/f36d818dc95dc7c4ac5c75dc49d2ca67 to your computer and use it in GitHub Desktop.
Save FlyInk13/f36d818dc95dc7c4ac5c75dc49d2ca67 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from "react";
type VisualViewportData = {
visualHeight: number,
windowHeight: number,
};
export const useViewportHeight = (): VisualViewportData => {
const [data, setData] = useState<VisualViewportData>({
visualHeight: 0,
windowHeight: 0,
});
useEffect(() => {
const onChange = () => {
setData({
visualHeight: window.visualViewport.height,
windowHeight: window.innerHeight,
});
};
onChange();
window.visualViewport.addEventListener('resize', onChange);
return () => window.visualViewport.removeEventListener('resize', onChange);
}, []);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment