{"version":3,"file":"useVirtualKeyboardVisible.mjs","names":[],"sources":["../../../../../../../../src/lib/hooks/dom/useVirtualKeyboardVisible.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\n\n/**\n * Minimum shrinkage (px) of the visual viewport relative to the layout viewport\n * that we treat as \"the on-screen keyboard is open\". Filters out the much\n * smaller changes caused by mobile browser chrome (e.g. the address bar).\n */\nexport const VIRTUAL_KEYBOARD_MIN_HEIGHT = 150;\n\n/**\n * Pure predicate: the visual viewport is at least `threshold` px shorter than\n * the layout viewport (i.e. the on-screen keyboard is open).\n */\nexport const isVirtualKeyboardVisible = (\n  layoutViewportHeight: number,\n  visualViewportHeight: number,\n  threshold: number = VIRTUAL_KEYBOARD_MIN_HEIGHT,\n): boolean => layoutViewportHeight - visualViewportHeight > threshold;\n\n/**\n * Tracks whether the on-screen (virtual) keyboard is currently open, using the\n * VisualViewport API. On iOS and Android the layout viewport stays while the\n * visual viewport shrinks by the keyboard height. Returns `false` where\n * VisualViewport is unavailable (SSR, very old browsers).\n */\nexport const useVirtualKeyboardVisible = (): boolean => {\n  const [visible, setVisible] = useState(false);\n\n  useEffect(() => {\n    const viewport = window.visualViewport;\n    if (!viewport) {\n      return;\n    }\n    const update = (): void => {\n      setVisible(isVirtualKeyboardVisible(window.innerHeight, viewport.height));\n    };\n    update();\n    viewport.addEventListener(\"resize\", update);\n    viewport.addEventListener(\"scroll\", update);\n    return () => {\n      viewport.removeEventListener(\"resize\", update);\n      viewport.removeEventListener(\"scroll\", update);\n    };\n  }, []);\n\n  return visible;\n};\n"],"mappings":";;;;;;;AAaA,IAAa,4BACX,sBACA,sBACA,YAAA,QACY,uBAAuB,uBAAuB;;;;;;;AAQ5D,IAAa,kCAA2C;CACtD,MAAM,CAAC,SAAS,cAAc,SAAS,KAAK;CAE5C,gBAAgB;EACd,MAAM,WAAW,OAAO;EACxB,IAAI,CAAC,UACH;EAEF,MAAM,eAAqB;GACzB,WAAW,yBAAyB,OAAO,aAAa,SAAS,MAAM,CAAC;EAC1E;EACA,OAAO;EACP,SAAS,iBAAiB,UAAU,MAAM;EAC1C,SAAS,iBAAiB,UAAU,MAAM;EAC1C,aAAa;GACX,SAAS,oBAAoB,UAAU,MAAM;GAC7C,SAAS,oBAAoB,UAAU,MAAM;EAC/C;CACF,GAAG,CAAC,CAAC;CAEL,OAAO;AACT"}