@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 6.47 kB
Source Map (JSON)
{"version":3,"file":"use-fullscreen.cjs","names":[],"sources":["../../src/use-fullscreen/use-fullscreen.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nfunction getFullscreenElement(): HTMLElement | null {\n const _document = window.document as any;\n\n const fullscreenElement =\n _document.fullscreenElement ||\n _document.webkitFullscreenElement ||\n _document.mozFullScreenElement ||\n _document.msFullscreenElement;\n\n return fullscreenElement;\n}\n\nfunction exitFullscreen() {\n const _document = window.document as any;\n\n if (typeof _document.exitFullscreen === 'function') {\n return _document.exitFullscreen();\n }\n if (typeof _document.msExitFullscreen === 'function') {\n return _document.msExitFullscreen();\n }\n if (typeof _document.webkitExitFullscreen === 'function') {\n return _document.webkitExitFullscreen();\n }\n if (typeof _document.mozCancelFullScreen === 'function') {\n return _document.mozCancelFullScreen();\n }\n\n return null;\n}\n\nfunction enterFullScreen(element: HTMLElement) {\n const _element = element as any;\n\n return (\n _element.requestFullscreen?.() ||\n _element.msRequestFullscreen?.() ||\n _element.webkitEnterFullscreen?.() ||\n _element.webkitRequestFullscreen?.() ||\n _element.mozRequestFullscreen?.()\n );\n}\n\nconst prefixes = ['', 'webkit', 'moz', 'ms'];\n\ninterface FullscreenEvents {\n onFullScreen: (event: Event) => void;\n onError: (event: Event) => void;\n}\n\nfunction addEvents(element: HTMLElement, { onFullScreen, onError }: FullscreenEvents) {\n prefixes.forEach((prefix) => {\n element.addEventListener(`${prefix}fullscreenchange`, onFullScreen);\n element.addEventListener(`${prefix}fullscreenerror`, onError);\n });\n}\n\nfunction removeEvents(element: HTMLElement, { onFullScreen, onError }: FullscreenEvents) {\n prefixes.forEach((prefix) => {\n element.removeEventListener(`${prefix}fullscreenchange`, onFullScreen);\n element.removeEventListener(`${prefix}fullscreenerror`, onError);\n });\n}\n\nexport interface UseFullscreenElementReturnValue<T extends HTMLElement = any> {\n ref: React.RefCallback<T | null>;\n toggle: () => Promise<void>;\n fullscreen: boolean;\n}\n\nexport function useFullscreenElement<\n T extends HTMLElement = any,\n>(): UseFullscreenElementReturnValue<T> {\n const [fullscreen, setFullscreen] = useState<boolean>(false);\n const refElement = useRef<T | null>(null);\n const prevNodeRef = useRef<T | null>(null);\n\n const handleFullscreenChange = useCallback(() => {\n setFullscreen(refElement.current === getFullscreenElement());\n }, []);\n\n const handleFullscreenError = useCallback(() => {\n setFullscreen(false);\n }, []);\n\n const toggle = useCallback(async () => {\n if (!getFullscreenElement() && refElement.current) {\n await enterFullScreen(refElement.current);\n } else {\n await exitFullscreen();\n }\n }, []);\n\n const refCallback: React.RefCallback<T | null> = useCallback((node) => {\n if (prevNodeRef.current && prevNodeRef.current !== node) {\n removeEvents(prevNodeRef.current, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }\n\n if (node) {\n addEvents(node, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }\n\n refElement.current = node;\n prevNodeRef.current = node;\n }, []);\n\n return { ref: refCallback, toggle, fullscreen };\n}\n\nexport interface UseFullscreenDocumentReturnValue {\n toggle: () => Promise<void>;\n fullscreen: boolean;\n}\n\nexport function useFullscreenDocument(): UseFullscreenDocumentReturnValue {\n const [fullscreen, setFullscreen] = useState<boolean>(false);\n\n const handleFullscreenChange = useCallback(() => {\n setFullscreen(getFullscreenElement() === window.document.documentElement);\n }, []);\n\n const handleFullscreenError = useCallback(() => {\n setFullscreen(false);\n }, []);\n\n const toggle = useCallback(async () => {\n if (!getFullscreenElement()) {\n await enterFullScreen(window.document.documentElement);\n } else {\n await exitFullscreen();\n }\n }, []);\n\n useEffect(() => {\n return addEvents(window.document.documentElement, {\n onFullScreen: handleFullscreenChange,\n onError: handleFullscreenError,\n });\n }, []);\n\n return { toggle, fullscreen };\n}\n"],"mappings":";;;AAEA,SAAS,uBAA2C;CAClD,MAAM,YAAY,OAAO;AAQzB,QALE,UAAU,qBACV,UAAU,2BACV,UAAU,wB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