|
| 1 | +/** |
| 2 | + * This file is part of VILLASweb. |
| 3 | + * |
| 4 | + * VILLASweb is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * VILLASweb is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with VILLASweb. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + ******************************************************************************/ |
| 17 | + |
| 18 | +import { useState, useEffect, useRef } from "react"; |
| 19 | + |
| 20 | +// this is essentially a wrapper around standart Fullscreen API |
| 21 | + |
| 22 | +const useFullscreen = () => { |
| 23 | + const fullscreenTargetRef = useRef(null); |
| 24 | + const [isFullscreen, setIsFullscreen] = useState(false); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const updateFullscreenState = () => |
| 28 | + setIsFullscreen(!!document.fullscreenElement); |
| 29 | + |
| 30 | + document.addEventListener("fullscreenchange", updateFullscreenState); |
| 31 | + document.addEventListener("webkitfullscreenchange", updateFullscreenState); |
| 32 | + |
| 33 | + return () => { |
| 34 | + document.removeEventListener("fullscreenchange", updateFullscreenState); |
| 35 | + document.removeEventListener( |
| 36 | + "webkitfullscreenchange", |
| 37 | + updateFullscreenState |
| 38 | + ); |
| 39 | + }; |
| 40 | + }, []); |
| 41 | + |
| 42 | + const toggleFullscreen = () => { |
| 43 | + if (isFullscreen) { |
| 44 | + if (document.exitFullscreen) document.exitFullscreen(); |
| 45 | + } else { |
| 46 | + if (fullscreenTargetRef.current.requestFullscreen) |
| 47 | + fullscreenTargetRef.current.requestFullscreen(); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + return { fullscreenTargetRef, isFullscreen, toggleFullscreen }; |
| 52 | +}; |
| 53 | + |
| 54 | +export default useFullscreen; |
0 commit comments