forked from stevenpersia/captain-hook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useFullscreen.js
37 lines (28 loc) · 970 Bytes
/
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
import { useState, useRef } 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();
}
setIsFS(true);
};
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();
}
setIsFS(false);
};
return { elementFS, triggerFS, exitFS, isFS };
};
export default useFullscreen;