-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
useFullScreen.js
43 lines (35 loc) · 1.23 KB
/
useFullScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useState, useRef, useEffect } from "react";
const useFullScreen = () => {
const [isFS, setIsFS] = useState(false);
const elementFS = useRef();
const triggerFS = () => {
const el = elementFS.current;
if (el) {
el.requestFullscreen && el.requestFullscreen();
el.mozRequestFullScreen && el.mozRequestFullScreen();
el.webkitRequestFullscreen && el.webkitRequestFullscreen();
el.msRequestFullscreen && el.msRequestFullscreen();
}
};
const exitFS = () => {
const elFS = elementFS.current.ownerDocument.fullscreen;
if (isFS && elFS) {
document.exitFullscreen();
document.exitFullscreen && document.exitFullscreen();
document.mozCancelFullScreen && document.mozCancelFullScreen();
document.webkitExitFullscreen && document.webkitExitFullscreen();
document.msExitFullscreen && document.msExitFullscreen();
}
};
useEffect(() => {
const eventHandler = () => {
setIsFS((val) => !val);
};
document.addEventListener("fullscreenchange", eventHandler);
return () => {
document.removeEventListener("fullscreenchange", eventHandler);
};
}, [setIsFS])
return { elementFS, triggerFS, exitFS, isFS };
};
export default useFullScreen;