-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(useFullscreen)!: better cross-platform compatibility #2915
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,13 @@ | ||
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */ | ||
|
||
import { ref } from 'vue-demi' | ||
import { computed, ref, watchEffect } from 'vue-demi' | ||
import { tryOnScopeDispose } from '@vueuse/shared' | ||
import type { Fn } 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' | ||
|
||
type FunctionMap = [ | ||
'requestFullscreen', | ||
'exitFullscreen', | ||
'fullscreenElement', | ||
'fullscreenEnabled', | ||
'fullscreenchange', | ||
'fullscreenerror', | ||
] | ||
|
||
// from: https://github.com/sindresorhus/screenfull.js/blob/master/src/screenfull.js | ||
const functionsMap: FunctionMap[] = [ | ||
[ | ||
'requestFullscreen', | ||
'exitFullscreen', | ||
'fullscreenElement', | ||
'fullscreenEnabled', | ||
'fullscreenchange', | ||
'fullscreenerror', | ||
], | ||
// New WebKit | ||
[ | ||
'webkitRequestFullscreen', | ||
'webkitExitFullscreen', | ||
'webkitFullscreenElement', | ||
'webkitFullscreenEnabled', | ||
'webkitfullscreenchange', | ||
'webkitfullscreenerror', | ||
], | ||
// Safari iOS WebKit | ||
[ | ||
'webkitEnterFullscreen', | ||
'webkitExitFullscreen', | ||
'webkitFullscreenElement', | ||
'webkitFullscreenEnabled', | ||
'webkitfullscreenchange', | ||
'webkitfullscreenerror', | ||
], | ||
// Old WebKit | ||
[ | ||
'webkitRequestFullScreen', | ||
'webkitCancelFullScreen', | ||
'webkitCurrentFullScreenElement', | ||
'webkitCancelFullScreen', | ||
'webkitfullscreenchange', | ||
'webkitfullscreenerror', | ||
], | ||
[ | ||
'mozRequestFullScreen', | ||
'mozCancelFullScreen', | ||
'mozFullScreenElement', | ||
'mozFullScreenEnabled', | ||
'mozfullscreenchange', | ||
'mozfullscreenerror', | ||
], | ||
[ | ||
'msRequestFullscreen', | ||
'msExitFullscreen', | ||
'msFullscreenElement', | ||
'msFullscreenEnabled', | ||
'MSFullscreenChange', | ||
'MSFullscreenError', | ||
], | ||
] as any | ||
|
||
export interface UseFullscreenOptions extends ConfigurableDocument { | ||
/** | ||
* Automatically exit fullscreen when component is unmounted | ||
|
@@ -82,6 +17,13 @@ export interface UseFullscreenOptions extends ConfigurableDocument { | |
autoExit?: boolean | ||
} | ||
|
||
const eventHandlers = [ | ||
'fullscreenchange', | ||
'webkitfullscreenchange', | ||
'webkitendfullscreen', | ||
'mozfullscreenchange', | ||
'MSFullscreenChange'] as any as 'fullscreenchange' | ||
|
||
/** | ||
* Reactive Fullscreen API. | ||
* | ||
|
@@ -94,34 +36,84 @@ export function useFullscreen( | |
options: UseFullscreenOptions = {}, | ||
) { | ||
const { document = defaultDocument, autoExit = false } = options | ||
const targetRef = target || document?.querySelector('html') | ||
const targetRef = target ?? document?.querySelector('html') | ||
const isFullscreen = ref(false) | ||
let map: FunctionMap = functionsMap[0] | ||
let targetHandlerCleanup: Fn | ||
|
||
const isSupported = useSupported(() => { | ||
if (!document) { | ||
return false | ||
} | ||
else { | ||
const target = unrefElement(targetRef) | ||
const requestMethod = computed<'requestFullscreen' | undefined>(() => { | ||
const target = unrefElement(targetRef) | ||
return [ | ||
'requestFullscreen', | ||
'webkitRequestFullscreen', | ||
'webkitEnterFullscreen', | ||
'webkitEnterFullScreen', | ||
'webkitRequestFullScreen', | ||
'mozRequestFullScreen', | ||
'msRequestFullscreen', | ||
].find(m => (document && m in document) || (target && m in target)) as any | ||
}) | ||
const exitMethod = computed<'exitFullscreen' | undefined>(() => { | ||
const target = unrefElement(targetRef) | ||
return [ | ||
'exitFullscreen', | ||
'webkitExitFullscreen', | ||
'webkitExitFullScreen', | ||
'webkitCancelFullScreen', | ||
'mozCancelFullScreen', | ||
'msExitFullscreen', | ||
].find(m => (document && m in document) || (target && m in target)) as any | ||
}) | ||
const fullscreenEnabled = computed<'fullscreenEnabled' | undefined>(() => { | ||
const target = unrefElement(targetRef) | ||
return [ | ||
'fullScreen', | ||
'webkitIsFullScreen', | ||
'webkitDisplayingFullscreen', | ||
'mozFullScreen', | ||
'msFullscreenElement', | ||
].find(m => (document && m in document) || (target && m in target)) as any | ||
}) | ||
|
||
for (const m of functionsMap) { | ||
if (m[1] in document || (target && m[0] in target)) { | ||
map = m | ||
return true | ||
const isSupported = useSupported(() => | ||
unrefElement(targetRef) | ||
&& document | ||
&& requestMethod.value !== undefined | ||
&& exitMethod.value !== undefined | ||
&& fullscreenEnabled.value !== undefined, | ||
) | ||
|
||
const isElementFullScreen = (): boolean => { | ||
if (fullscreenEnabled.value) { | ||
if (document && document[fullscreenEnabled.value] !== undefined && document[fullscreenEnabled.value] !== null) { | ||
return document[fullscreenEnabled.value] | ||
} | ||
else { | ||
const target = unrefElement(targetRef) | ||
// @ts-expect-error - Fallback for WebKit and iOS Safari browsers | ||
if (target[fullscreenEnabled.value] !== undefined && target[fullscreenEnabled.value] !== null) { | ||
// @ts-expect-error - Fallback for WebKit and iOS Safari browsers | ||
return Boolean(target[fullscreenEnabled.value]) | ||
} | ||
} | ||
} | ||
return false | ||
}) | ||
|
||
const [REQUEST, EXIT, ELEMENT,, EVENT] = map | ||
} | ||
|
||
async function exit() { | ||
if (!isSupported.value) | ||
return | ||
if (document?.[ELEMENT]) | ||
await document[EXIT]() | ||
if (exitMethod.value) { | ||
if (document?.[exitMethod.value] !== undefined) { | ||
await document[exitMethod.value]() | ||
} | ||
else { | ||
const target = unrefElement(targetRef) | ||
// @ts-expect-error - Fallback for Safari iOS | ||
if (target[exitMethod.value] !== undefined) | ||
// @ts-expect-error - Fallback for Safari iOS | ||
await target[exitMethod.value]() | ||
} | ||
} | ||
|
||
isFullscreen.value = false | ||
} | ||
|
@@ -130,28 +122,34 @@ export function useFullscreen( | |
if (!isSupported.value) | ||
return | ||
|
||
await exit() | ||
if (isElementFullScreen()) | ||
await exit() | ||
|
||
const target = unrefElement(targetRef) | ||
if (target) { | ||
await target[REQUEST]() | ||
if (target && requestMethod.value && target[requestMethod.value] !== undefined) { | ||
await target[requestMethod.value]() | ||
isFullscreen.value = true | ||
} | ||
} | ||
|
||
async function toggle() { | ||
if (isFullscreen.value) | ||
await exit() | ||
else | ||
await enter() | ||
await (isFullscreen.value ? exit() : enter()) | ||
} | ||
|
||
if (document) { | ||
useEventListener(document, EVENT, () => { | ||
isFullscreen.value = !!document?.[ELEMENT] | ||
}, false) | ||
const handlerCallback = () => { | ||
isFullscreen.value = isElementFullScreen() | ||
} | ||
|
||
useEventListener(document, eventHandlers, handlerCallback, false) | ||
watchEffect(() => { | ||
const target = unrefElement(targetRef) | ||
|
||
if (targetHandlerCleanup) | ||
targetHandlerCleanup() | ||
|
||
targetHandlerCleanup = useEventListener(target, eventHandlers, handlerCallback, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering: Being this inside a |
||
}) | ||
|
||
if (autoExit) | ||
tryOnScopeDispose(exit) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, I'd like to confirm if this property name is really correct and expected. I didn't see the
fullScreen
property but see thefullscreen
property from the MDN documentation. 🤔