UNPKG

@guruhotel/aura-hooks

Version:

🪝 Hooks library designed by the Guruhotel team for Aura UI

1 lines 1.74 kB
exports.__esModule=!0,exports.useFullscreen=function(){const[r,t]=(0,e.useState)(!1),u=(0,e.useRef)(),c=(0,e.useCallback)((e=>{t(e.target===n())}),[t]),o=(0,e.useCallback)((e=>{t(!1),console.error(`[@guruhotel/hooks] use-fullscreen: Error attempting full-screen mode method: ${e} (${e.target})`)}),[t]),s=(0,e.useCallback)((async()=>{n()?await async function(){const e=window.document;return"function"==typeof e.exitFullscreen?e.exitFullscreen():"function"==typeof e.msExitFullscreen?e.msExitFullscreen():"function"==typeof e.webkitExitFullscreen?e.webkitExitFullscreen():"function"==typeof e.mozCancelFullScreen?e.mozCancelFullScreen():null}():await async function(e){const n=e;return(null==n.requestFullscreen?void 0:n.requestFullscreen())||(null==n.msRequestFullscreen?void 0:n.msRequestFullscreen())||(null==n.webkitRequestFullscreen?void 0:n.webkitRequestFullscreen())||(null==n.mozRequestFullscreen?void 0:n.mozRequestFullscreen())}(u.current)}),[]),i=(0,e.useCallback)((e=>{u.current=null===e?window.document.documentElement:e}),[]);return(0,e.useEffect)((()=>!u.current&&window.document?(u.current=window.document.documentElement,l(u.current,{onFullScreen:c,onError:o})):u.current?l(u.current,{onFullScreen:c,onError:o}):void 0),[]),{ref:i,toggle:s,fullscreen:r}};var e=require("react");function n(){const e=window.document;return e.fullscreenElement||e.webkitFullscreenElement||e.mozFullScreenElement||e.msFullscreenElement}const r=["","webkit","moz","ms"];function l(e,{onFullScreen:n,onError:l}){return r.forEach((r=>{e.addEventListener(`${r}fullscreenchange`,n),e.addEventListener(`${r}fullscreenerror`,l)})),()=>{r.forEach((r=>{e.removeEventListener(`${r}fullscreenchange`,n),e.removeEventListener(`${r}fullscreenerror`,l)}))}}