-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.tsx
163 lines (142 loc) · 4.31 KB
/
index.tsx
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as React from 'react';
const key: any = {
fullscreenEnabled: 0,
fullscreenElement: 1,
requestFullscreen: 2,
exitFullscreen: 3,
fullscreenchange: 4,
fullscreenerror: 5,
};
const webkit = [
'webkitFullscreenEnabled',
'webkitFullscreenElement',
'webkitRequestFullscreen',
'webkitExitFullscreen',
'webkitfullscreenchange',
'webkitfullscreenerror',
];
const moz = [
'mozFullScreenEnabled',
'mozFullScreenElement',
'mozRequestFullScreen',
'mozCancelFullScreen',
'mozfullscreenchange',
'mozfullscreenerror',
];
const ms = [
'msFullscreenEnabled',
'msFullscreenElement',
'msRequestFullscreen',
'msExitFullscreen',
'MSFullscreenChange',
'MSFullscreenError',
];
// so it doesn't throw if no window or document
const document: any = typeof window !== 'undefined' && typeof window.document !== 'undefined' ? window.document : {};
const vendor = (
('fullscreenEnabled' in document && Object.keys(key)) ||
(webkit[0] in document && webkit) ||
(moz[0] in document && moz) ||
(ms[0] in document && ms) ||
[]
);
class fscreen {
static requestFullscreen(element: any) { return element[vendor[key.requestFullscreen]]() };
static requestFullscreenFunction(element: any) { return element[vendor[key.requestFullscreen]] };
static get exitFullscreen() {
return document[vendor[key.exitFullscreen]].bind(document);
};
static addEventListener(type: any, handler: any, options: any) { return document.addEventListener(vendor[key[type]], handler, options) };
static removeEventListener(type: any, handler: any, options: any) { return document.removeEventListener(vendor[key[type]], handler, options) };
static get fullscreenEnabled() {
return Boolean(document[vendor[key.fullscreenEnabled]]);
};
static set fullscreenEnabled(val) {
};
static get fullscreenElement() {
return document[vendor[key.fullscreenElement]];
};
static set fullscreenElement(val) {
};
static get onfullscreenchange() {
return document[`on${vendor[key.fullscreenchange]}`.toLowerCase()];
};
static set onfullscreenchange(handler) {
document[`on${vendor[key.fullscreenchange]}`.toLowerCase()] = handler;
};
static get onfullscreenerror() {
return document[`on${vendor[key.fullscreenerror]}`.toLowerCase()];
};
static set onfullscreenerror(handler) {
document[`on${vendor[key.fullscreenerror]}`.toLowerCase()] = handler;
};
};
export type IFullScreenProps = {
onClose?: () => void,
onOpen?: () => void,
onChange?: (state: boolean) => void,
enabled?: boolean
};
/**
* The returned value indicates whether fullscreen mode is supported by the browser or not.
* If the value is true, it means fullscreen is enabled and supported.
* If the value is false, it means fullscreen is not enabled or supported.
*/
export const getFullScreenEnabled = () => {
// It returns the value of the fullscreenEnabled property from the fscreen object.
return fscreen.fullscreenEnabled;
};
export default class FullScreen extends React.Component<React.PropsWithChildren<IFullScreenProps>, never> {
static defaultProps = {
enabled: false,
};
node: any;
constructor(props: IFullScreenProps) {
super(props);
}
componentDidMount() {
fscreen.addEventListener('fullscreenchange', this.detectFullScreen, {});
}
componentWillUnmount() {
fscreen.removeEventListener('fullscreenchange', this.detectFullScreen, {});
}
componentDidUpdate() {
this.handleProps(this.props);
}
handleProps(props: IFullScreenProps) {
const enabled = fscreen.fullscreenElement;
if (enabled && !props.enabled) {
this.leaveFullScreen();
} else if (!enabled && props.enabled) {
this.enterFullScreen();
}
}
detectFullScreen = () => {
if (this.props.onChange) {
this.props.onChange(!!fscreen.fullscreenElement);
}
if (this.props.onOpen && !!fscreen.fullscreenElement) {
this.props.onOpen();
}
if (this.props.onClose && !fscreen.fullscreenElement) {
this.props.onClose();
}
}
enterFullScreen = () => {
fscreen.requestFullscreen(this.node);
}
leaveFullScreen = () => {
fscreen.exitFullscreen();
}
render() {
return (
<div
className="FullScreen"
ref={node => (this.node = node)}
style={{ height: '100%', width: '100%' }}
>
{this.props.children}
</div>
);
}
}