{"version":3,"sources":["../../src/use-fullscreen/index.ts"],"sourcesContent":["import { useCreation } from '../use-creation'\nimport { useEventListener } from '../use-event-listener'\nimport { useLatest } from '../use-latest'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useSupported } from '../use-supported'\nimport { useTargetElement } from '../use-target-element'\nimport { useUnmount } from '../use-unmount'\nimport { isDefined, isFunction, noNullish } from '../utils/basic'\nimport { EventHandlerList, getCompatibleAttr } from './compatible-attributes'\n\nimport type { ElementTarget } from '../use-target-element'\n\nexport interface UseFullscreenOptions {\n  /**\n   * Automatically exit fullscreen when component is unmounted\n   *\n   * @defaultValue false\n   */\n  autoExit?: boolean\n}\n\nexport interface UseFullscreenReturns {\n  /**\n   * Whether the browser supports fullscreen API\n   */\n  isSupported: boolean\n  /**\n   * Whether the element is in fullscreen mode\n   */\n  isFullscreen: boolean\n  /**\n   * Whether the element itself is in fullscreen mode\n   */\n  isSelfFullscreen: boolean\n  /**\n   * Enter fullscreen mode\n   */\n  enter(): Promise<void>\n  /**\n   * Exit fullscreen mode\n   */\n  exit(): Promise<void>\n  /**\n   * Toggle fullscreen mode\n   */\n  toggle(): Promise<void>\n}\n\n/**\n * A React Hook that allows you to use the [requestFullscreen API](https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullscreen) with ease.\n */\nexport function useFullscreen(\n  target: ElementTarget = 'html',\n  options: UseFullscreenOptions = {},\n): UseFullscreenReturns {\n  const { autoExit = false } = options\n\n  const el = useTargetElement(target)\n  const [isFullscreen, setIsFullscreen] = useSafeState(false)\n\n  const isSupported = useSupported(() => {\n    const attrs = getCompatibleAttr(el.current)\n    return Object.values(attrs).every(isDefined)\n  }, [el.current])\n\n  const latest = useLatest({ isFullscreen, isSupported })\n\n  const isCurrentElementFullscreen = useStableFn(() => {\n    const { fsElement } = getCompatibleAttr(el.current)\n\n    if (fsElement) {\n      return Boolean(document && document[fsElement as 'fullscreenElement'] === el.current)\n    }\n\n    return false\n  })\n\n  const isElementFullscreen = useStableFn(() => {\n    const { fsEnabled } = getCompatibleAttr(el.current)\n\n    if (fsEnabled) {\n      if (document && noNullish(document[fsEnabled as 'fullscreen'])) {\n        return document[fsEnabled as 'fullscreen']\n      }\n      if (el.current && noNullish(el.current[fsEnabled as never])) {\n        return Boolean(el.current[fsEnabled as never])\n      }\n    }\n    return false\n  })\n\n  const exit = useStableFn(async () => {\n    const { exit } = getCompatibleAttr(el.current)\n    const { isFullscreen, isSupported } = latest.current\n\n    if (!el.current || !document || !isSupported || !isFullscreen) return\n\n    if (isFunction(document[exit as 'exitFullscreen'])) {\n      await document[exit as 'exitFullscreen']()\n    } else {\n      if (isFunction(el.current[exit as never])) {\n        await (el.current[exit as never] as any)?.()\n      }\n    }\n\n    setIsFullscreen(false)\n  })\n\n  const enter = useStableFn(async () => {\n    const { enter } = getCompatibleAttr(el.current)\n    const { isFullscreen, isSupported } = latest.current\n\n    if (!el.current || !isSupported || isFullscreen) return\n    if (isElementFullscreen()) await exit()\n\n    if (isFunction(el.current[enter as 'requestFullscreen'])) {\n      await el.current[enter as 'requestFullscreen']()\n      setIsFullscreen(true)\n    }\n  })\n\n  const toggle = useStableFn(() => (latest.current.isFullscreen ? exit() : enter()))\n\n  const handlerFullscreenChange = useStableFn(() => {\n    const isFullscreenValue = isElementFullscreen()\n\n    if (!isFullscreenValue || (isFullscreenValue && isCurrentElementFullscreen())) {\n      setIsFullscreen(isFullscreenValue)\n    }\n  })\n\n  useEventListener(() => document, [...EventHandlerList], handlerFullscreenChange, false)\n  useEventListener(el, [...EventHandlerList], handlerFullscreenChange, false)\n\n  useUnmount(() => autoExit && exit())\n\n  const isSelfFullscreen = useCreation(() => isFullscreen && isCurrentElementFullscreen(), [isFullscreen])\n\n  return {\n    isSupported,\n    isFullscreen,\n    isSelfFullscreen,\n    enter,\n    exit,\n    toggle,\n  }\n}\n"],"names":["useFullscreen","target","options","autoExit","el","useTargetElement","useSafeState","isFullscreen","setIsFullscreen","isSupported","useSupported","attrs","getCompatibleAttr","current","Object","values","every","isDefined","latest","useLatest","isCurrentElementFullscreen","useStableFn","fsElement","Boolean","document","isElementFullscreen","fsEnabled","noNullish","exit","isFunction","enter","toggle","handlerFullscreenChange","isFullscreenValue","useEventListener","EventHandlerList","useUnmount","isSelfFullscreen","useCreation"],"mappings":";;;;+BAoDgBA;;;eAAAA;;;2BApDY;gCACK;yBACP;4BACG;2BACD;4BACC;gCACI;0BACN;qBACsB;oCACG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2C7C,SAASA;QACdC,SAAAA,iEAAwB,QACxBC,UAAAA,iEAAgC,CAAC;IAEjC,wBAA6BA,QAArBC,UAAAA,0CAAW;IAEnB,IAAMC,KAAKC,IAAAA,kCAAgB,EAACJ;IAC5B,IAAwCK,iCAAAA,IAAAA,0BAAY,EAAC,YAA9CC,eAAiCD,kBAAnBE,kBAAmBF;IAExC,IAAMG,cAAcC,IAAAA,0BAAY,EAAC;QAC/B,IAAMC,QAAQC,IAAAA,uCAAiB,EAACR,GAAGS,OAAO;QAC1C,OAAOC,OAAOC,MAAM,CAACJ,OAAOK,KAAK,CAACC,gBAAS;IAC7C,GAAG;QAACb,GAAGS,OAAO;KAAC;IAEf,IAAMK,SAASC,IAAAA,oBAAS,EAAC;QAAEZ,cAAAA;QAAcE,aAAAA;IAAY;IAErD,IAAMW,6BAA6BC,IAAAA,wBAAW,EAAC;QAC7C,IAAM,AAAEC,YAAcV,IAAAA,uCAAiB,EAACR,GAAGS,OAAO,EAA1CS;QAER,IAAIA,WAAW;YACb,OAAOC,QAAQC,YAAYA,QAAQ,CAACF,UAAiC,KAAKlB,GAAGS,OAAO;QACtF;QAEA,OAA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