forked from EFUB/efub4-react-hooks-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#2.6.useFullscreen.js
40 lines (37 loc) · 1.3 KB
/
#2.6.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
import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
const useFullscreen = (callback) => {
const element = useRef();
const triggerFull = () => {
if (element.current) {
element.current.requestFullscreen();
if (callback && typeof callback === "function") {
callback(true);
}
}
};
const exitFull = () => {
document.exitFullscreen();
if (callback && typeof callback === "function") {
callback(false);
}
};
return { element, triggerFull, exitFull };
};
const App = () => {
const callback = (isFull) => {
console.log(isFull ? "We are full" : "We are small");
};
const { element, triggerFull, exitFull } = useFullscreen(callback);
return (
<div className="App" style={{ height: "1000vh" }}>
<div ref={element}>
<img src="https://search.pstatic.net/common/?src=http%3A%2F%2Fblogfiles.naver.net%2FMjAyNDAzMTBfMTkw%2FMDAxNzEwMDY2NzQyNDYw.Inm4oKue1dzbt2es8nSPMdTnsMMM6Mnls4LvpDvWGYMg.TVNS8LO_l2taj9LXx-SaK2j00X4WLHy9_gzhqAuO-Sog.PNG%2F14.png&type=sc960_832" />
<button onClick={exitFull}>Exit fullscreen</button>
</div>
<button onClick={triggerFull}>Make fullscreen</button>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);