17 changed files with 117 additions and 67 deletions
@ -0,0 +1,29 @@ |
|||||||
|
/** Sync `--vh` to the visible viewport (px). Used for mobile min-height and legacy call sites. */ |
||||||
|
export function syncViewportHeightCssVar(): void { |
||||||
|
const h = window.visualViewport?.height ?? window.innerHeight |
||||||
|
document.documentElement.style.setProperty('--vh', `${h}px`) |
||||||
|
} |
||||||
|
|
||||||
|
/** Keep `--vh` aligned when the window, visual viewport, or root layout (e.g. font scaling) changes. */ |
||||||
|
export function installViewportHeightListeners(): () => void { |
||||||
|
const sync = () => syncViewportHeightCssVar() |
||||||
|
|
||||||
|
window.addEventListener('resize', sync) |
||||||
|
window.addEventListener('orientationchange', sync) |
||||||
|
window.visualViewport?.addEventListener('resize', sync) |
||||||
|
|
||||||
|
let ro: ResizeObserver | undefined |
||||||
|
if (typeof ResizeObserver !== 'undefined') { |
||||||
|
ro = new ResizeObserver(sync) |
||||||
|
ro.observe(document.documentElement) |
||||||
|
} |
||||||
|
|
||||||
|
sync() |
||||||
|
|
||||||
|
return () => { |
||||||
|
window.removeEventListener('resize', sync) |
||||||
|
window.removeEventListener('orientationchange', sync) |
||||||
|
window.visualViewport?.removeEventListener('resize', sync) |
||||||
|
ro?.disconnect() |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue