-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
175 lines (153 loc) · 4.86 KB
/
index.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
import { computed, ref } from 'vue-demi'
import { tryOnScopeDispose } from '@vueuse/shared'
import type { MaybeElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
import { useEventListener } from '../useEventListener'
import type { ConfigurableDocument } from '../_configurable'
import { defaultDocument } from '../_configurable'
import { useSupported } from '../useSupported'
export interface UseFullscreenOptions extends ConfigurableDocument {
/**
* Automatically exit fullscreen when component is unmounted
*
* @default false
*/
autoExit?: boolean
}
const eventHandlers = [
'fullscreenchange',
'webkitfullscreenchange',
'webkitendfullscreen',
'mozfullscreenchange',
'MSFullscreenChange',
] as any as 'fullscreenchange'[]
/**
* Reactive Fullscreen API.
*
* @see https://vueuse.org/useFullscreen
* @param target
* @param options
*/
export function useFullscreen(
target?: MaybeElementRef,
options: UseFullscreenOptions = {},
) {
const {
document = defaultDocument,
autoExit = false,
} = options
const targetRef = computed(() => unrefElement(target) ?? document?.querySelector('html'))
const isFullscreen = ref(false)
const requestMethod = computed<'requestFullscreen' | undefined>(() => {
return [
'requestFullscreen',
'webkitRequestFullscreen',
'webkitEnterFullscreen',
'webkitEnterFullScreen',
'webkitRequestFullScreen',
'mozRequestFullScreen',
'msRequestFullscreen',
].find(m => (document && m in document) || (targetRef.value && m in targetRef.value)) as any
})
const exitMethod = computed<'exitFullscreen' | undefined>(() => {
return [
'exitFullscreen',
'webkitExitFullscreen',
'webkitExitFullScreen',
'webkitCancelFullScreen',
'mozCancelFullScreen',
'msExitFullscreen',
].find(m => (document && m in document) || (targetRef.value && m in targetRef.value)) as any
})
const fullscreenEnabled = computed<'fullscreenEnabled' | undefined>(() => {
return [
'fullScreen',
'webkitIsFullScreen',
'webkitDisplayingFullscreen',
'mozFullScreen',
'msFullscreenElement',
].find(m => (document && m in document) || (targetRef.value && m in targetRef.value)) as any
})
const fullscreenElementMethod = [
'fullscreenElement',
'webkitFullscreenElement',
'mozFullScreenElement',
'msFullscreenElement',
].find(m => (document && m in document)) as 'fullscreenElement' | undefined
const isSupported = useSupported(() =>
targetRef.value
&& document
&& requestMethod.value !== undefined
&& exitMethod.value !== undefined
&& fullscreenEnabled.value !== undefined,
)
const isCurrentElementFullScreen = (): boolean => {
if (fullscreenElementMethod)
return document?.[fullscreenElementMethod] === targetRef.value
return false
}
const isElementFullScreen = (): boolean => {
if (fullscreenEnabled.value) {
if (document && document[fullscreenEnabled.value] != null) {
return document[fullscreenEnabled.value]
}
else {
const target = targetRef.value
// @ts-expect-error - Fallback for WebKit and iOS Safari browsers
if (target?.[fullscreenEnabled.value] != null) {
// @ts-expect-error - Fallback for WebKit and iOS Safari browsers
return Boolean(target[fullscreenEnabled.value])
}
}
}
return false
}
async function exit() {
if (!isSupported.value)
return
if (exitMethod.value) {
if (document?.[exitMethod.value] != null) {
await document[exitMethod.value]()
}
else {
const target = targetRef.value
// @ts-expect-error - Fallback for Safari iOS
if (target?.[exitMethod.value] != null)
// @ts-expect-error - Fallback for Safari iOS
await target[exitMethod.value]()
}
}
isFullscreen.value = false
}
async function enter() {
if (!isSupported.value)
return
if (isElementFullScreen())
await exit()
const target = targetRef.value
if (requestMethod.value && target?.[requestMethod.value] != null) {
await target[requestMethod.value]()
isFullscreen.value = true
}
}
async function toggle() {
await (isFullscreen.value ? exit() : enter())
}
const handlerCallback = () => {
const isElementFullScreenValue = isElementFullScreen()
if (!isElementFullScreenValue || (isElementFullScreenValue && isCurrentElementFullScreen()))
isFullscreen.value = isElementFullScreenValue
}
useEventListener(document, eventHandlers, handlerCallback, false)
useEventListener(() => unrefElement(targetRef), eventHandlers, handlerCallback, false)
if (autoExit)
tryOnScopeDispose(exit)
return {
isSupported,
isFullscreen,
enter,
exit,
toggle,
}
}
export type UseFullscreenReturn = ReturnType<typeof useFullscreen>