-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfullscreen.ts
More file actions
43 lines (41 loc) · 1.13 KB
/
Copy pathfullscreen.ts
File metadata and controls
43 lines (41 loc) · 1.13 KB
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
type FullScreenReturns = {
toggle: () => void,
setTarget: (ele: Element) => void,
error: (cb: (e: Error) => void) => void
}
/**
* @DOM
* [useFullScreen description]
* @return {FullScreenReturns} [description]
*/
export function useFullScreen(): FullScreenReturns {
let _switch = false
let _targetDOM: Element = document.body;
let _errorHandler: (e: Error) => void
const full = (options?: FullscreenOptions) => {
if (_switch && !document.fullscreenElement) {
_targetDOM.requestFullscreen(options).catch((reason) => {
_errorHandler(reason)
})
}else if(!_switch && document.fullscreenElement){
document.exitFullscreen().catch((reason)=>{
_errorHandler(reason)
})
}
}
const toggle = (options?: FullscreenOptions) => {
_switch = !_switch
full(options)
}
const error = (cb: (e: Error) => void) => {
_errorHandler = cb
}
const setTarget = (ele: Element = document.body) => {
_targetDOM = ele
}
return {
toggle,
setTarget,
error,
}
}