mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-25 04:37:13 -05:00
30 lines
844 B
TypeScript
30 lines
844 B
TypeScript
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
|
|
|
|
const CSS_VARIABLE = "--visual-viewport-height";
|
|
|
|
/** Syncs `--visual-viewport-height` on the root; CSS can't read the visual viewport, which elements above the mobile keyboard need. */
|
|
export function useVisualViewportHeight() {
|
|
useIsomorphicLayoutEffect(() => {
|
|
const viewport = window.visualViewport;
|
|
if (!viewport) return;
|
|
|
|
const update = () => {
|
|
document.documentElement.style.setProperty(
|
|
CSS_VARIABLE,
|
|
`${viewport.height}px`,
|
|
);
|
|
};
|
|
|
|
update();
|
|
|
|
viewport.addEventListener("resize", update);
|
|
viewport.addEventListener("scroll", update);
|
|
|
|
return () => {
|
|
viewport.removeEventListener("resize", update);
|
|
viewport.removeEventListener("scroll", update);
|
|
document.documentElement.style.removeProperty(CSS_VARIABLE);
|
|
};
|
|
}, []);
|
|
}
|