Spaces:
Running
Running
File size: 1,158 Bytes
dd87944 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import { useEffect, useState } from "react";
/** Tracks soft-keyboard overlap via Visual Viewport API (iOS Safari + Android Chrome). */
export function useVisualViewportKeyboard({ enabled = true, threshold = 80 } = {}) {
const [inset, setInset] = useState(0);
const [open, setOpen] = useState(false);
useEffect(() => {
if (!enabled || typeof window === "undefined") return undefined;
const vv = window.visualViewport;
if (!vv) return undefined;
const update = () => {
const next = Math.max(0, window.innerHeight - vv.height - vv.offsetTop);
setInset(next);
setOpen(next > threshold);
};
update();
vv.addEventListener("resize", update);
vv.addEventListener("scroll", update);
return () => {
vv.removeEventListener("resize", update);
vv.removeEventListener("scroll", update);
};
}, [enabled, threshold]);
return { inset, open };
}
export function prefersTouchKeyboard() {
if (typeof window === "undefined") return false;
return (
window.matchMedia("(pointer: coarse)").matches
|| window.matchMedia("(hover: none)").matches
|| "ontouchstart" in window
);
}
|