{"version":3,"file":"subscribeKeyboardHeight.mjs","names":[],"sources":["../../../src/utils/subscribeKeyboardHeight/subscribeKeyboardHeight.ts"],"sourcesContent":["import { getKeyboardHeight } from '../getKeyboardHeight/index.ts';\nimport { isServer } from '../isServer/index.ts';\n\ntype SubscribeKeyboardHeightOptions = {\n  /**\n   * A function that will be called with the updated keyboard height in pixels.\n   */\n  callback: (height: number) => void;\n  /**\n   * If true, the callback will be invoked immediately with the current keyboard height.\n   * @default false\n   */\n  immediate?: boolean;\n  /**\n   * Throttle interval in milliseconds.\n   * Events within this interval will be ignored to improve performance.\n   * @default 16 (~60fps)\n   */\n  throttleMs?: number;\n};\n\ntype SubscribeKeyboardHeightResult = {\n  /**\n   * Unsubscribes all listeners and stops receiving keyboard height updates.\n   */\n  unsubscribe: () => void;\n};\n\n/**\n * @description\n * `subscribeKeyboardHeight` is a utility function that subscribes to changes in the on-screen keyboard height.\n *\n * The provided callback is invoked whenever the keyboard height may change,\n * including when the keyboard appears, disappears, or changes size.\n *\n * Internally, this function listens to both `resize` and `scroll` events\n * on the Visual Viewport:\n * - `resize`: triggered when the visual viewport height changes\n * - `scroll`: triggered when the visual viewport offset changes\n *   (important for iOS where the viewport can shift without resizing)\n *\n * Performance optimizations:\n * - Throttled by default (16ms, ~60fps) to prevent excessive callback invocations\n * - Skips callback when height hasn't changed (deduplication)\n *\n * @param {SubscribeKeyboardHeightOptions} options - Configuration options\n * @param {(height: number) => void} options.callback - A function that will be called with the updated keyboard height in pixels.\n * @param {boolean} [options.immediate=false] - If true, the callback will be invoked immediately with the current keyboard height.\n * @param {number} [options.throttleMs=16] - Throttle interval in milliseconds.\n *\n * @returns {SubscribeKeyboardHeightResult} An object containing the unsubscribe function.\n * - unsubscribe `() => void` - Unsubscribes all listeners and stops receiving keyboard height updates.\n *\n * @example\n * const { unsubscribe } = subscribeKeyboardHeight({\n *   callback: (height) => {\n *     footer.style.paddingBottom = `${height}px`;\n *   },\n *   immediate: true,\n * });\n *\n * // Later, when cleanup is needed\n * unsubscribe();\n */\nexport function subscribeKeyboardHeight({\n  callback,\n  immediate = false,\n  throttleMs = 16,\n}: SubscribeKeyboardHeightOptions): SubscribeKeyboardHeightResult {\n  if (isServer()) {\n    return { unsubscribe: () => {} };\n  }\n\n  const visualViewport = window.visualViewport;\n  if (!visualViewport) {\n    return { unsubscribe: () => {} };\n  }\n\n  let lastHeight: number | null = null;\n  let throttleTimer: ReturnType<typeof setTimeout> | null = null;\n\n  const handler = () => {\n    // Skip if throttled\n    if (throttleTimer != null) {\n      return;\n    }\n\n    const currentHeight = getKeyboardHeight();\n\n    // Skip if height hasn't changed (deduplication)\n    if (lastHeight === currentHeight) {\n      return;\n    }\n\n    lastHeight = currentHeight;\n    callback(currentHeight);\n\n    // Start throttle timer\n    throttleTimer = setTimeout(() => {\n      throttleTimer = null;\n    }, throttleMs);\n  };\n\n  if (immediate) {\n    const currentHeight = getKeyboardHeight();\n    lastHeight = currentHeight;\n    callback(currentHeight);\n  }\n\n  visualViewport.addEventListener('resize', handler);\n  visualViewport.addEventListener('scroll', handler);\n\n  return {\n    unsubscribe: () => {\n      visualViewport.removeEventListener('resize', handler);\n      visualViewport.removeEventListener('scroll', handler);\n      if (throttleTimer != null) {\n        clearTimeout(throttleTimer);\n      }\n    },\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,SAAgB,wBAAwB,EACtC,UACA,YAAY,OACZ,aAAa,MACmD;CAChE,IAAI,SAAS,GACX,OAAO,EAAE,mBAAmB,CAAC,EAAE;CAGjC,MAAM,iBAAiB,OAAO;CAC9B,IAAI,CAAC,gBACH,OAAO,EAAE,mBAAmB,CAAC,EAAE;CAGjC,IAAI,aAA4B;CAChC,IAAI,gBAAsD;CAE1D,MAAM,gBAAgB;EAEpB,IAAI,iBAAiB,MACnB;EAGF,MAAM,gBAAgB,kBAAkB;EAGxC,IAAI,eAAe,eACjB;EAGF,aAAa;EACb,SAAS,aAAa;EAGtB,gBAAgB,iBAAiB;GAC/B,gBAAgB;EAClB,GAAG,UAAU;CACf;CAEA,IAAI,WAAW;EACb,MAAM,gBAAgB,kBAAkB;EACxC,aAAa;EACb,SAAS,aAAa;CACxB;CAEA,eAAe,iBAAiB,UAAU,OAAO;CACjD,eAAe,iBAAiB,UAAU,OAAO;CAEjD,OAAO,EACL,mBAAmB;EACjB,eAAe,oBAAoB,UAAU,OAAO;EACpD,eAAe,oBAAoB,UAAU,OAAO;EACpD,IAAI,iBAAiB,MACnB,aAAa,aAAa;CAE9B,EACF;AACF"}