Simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which lets you bring the page or any element into fullscreen. Smoothens out the browser implementation differences, so you don't have to.
This package is based on on Sindre Sorhus' screenfull.
yarn add @quitsmx/fullscrn
# or npm
npm i @quitsmx/fullscrn -S
// CommonJS (node)
const { fullscrn } = require('@quitsmx/fullscreen')
// ES module
import { fullscrn } from '@quitsmx/fullscreen'
Size.
TL;DR
Sindre Sorhus's screenfull.js package is excelent, as is Rafael Pedicini's fscreen, but we don't need support for old browsers, nor do we wanna write code to exclude unsupported browsers.
This package is for ES2018 browsers (and Safari 10+ in Mac/iPad) and does not require conditionals to exclude unsupported browsers or devices, such as the iPhone.
Note: The lack of iPhone support is a limitation in the browser, not in this package.
document.getElementById('button-id').addEventListener('click', () => {
fullscrn.request()
})
document.getElementById('button-id').addEventListener('click', evt => {
fullscrn.request(evt.currentTarget.parentElement)
})
document.getElementById('button-id').addEventListener('click', evt => {
fullscrn.request(evt.currentTarget.parentElement, { navigationUI: 'hide' })
})
$('img').on('click', evt => {
fullscrn.toggle(evt.target)
})
const remover = fullscrn.onchange(() => {
console.log('Am I fullscreen?', fullscrn.isFullscreen ? 'Yes' : 'No')
})
// Later, you can remove the listener by calling `remover`
remover()
Note: The removal of the listener is automatic if you return the result of the
onchange
method to the useEffect
hook.
useEffect(
() =>
fullscrn.onchange(() => {
console.log('Am I fullscreen?', fullscrn.isFullscreen ? 'Yes' : 'No')
}),
[]
)
/**
* Register the error handler with onerror and store the result (`remover`).
* Later, you can remove the listener by calling `remover()`.
*/
const remover = fullscrn.onerror(evt => {
console.error('Failed to enable fullscreen', evt)
})
Make an element fullscreen.
Accepts a DOM element and FullscreenOptions
.
The default element is <html>
. If called with another element than the currently active, it will switch to that if it's a decendant.
If your page is inside an <iframe>
you will need to add a allowfullscreen
attribute (+ webkitallowfullscreen
and mozallowfullscreen
).
Keep in mind that the browser will only enter fullscreen when initiated by user events like click, touch, key.
Returns a promise that resolves after the element enters fullscreen.
Brings you out of fullscreen.
Returns a promise that resolves after the element exits fullscreen.
Requests fullscreen if not active, otherwise exits.
Accepts a DOM element and FullscreenOptions
.
Returns a promise that resolves after the element enters/exits fullscreen.
Add a listener for when the browser switches in and out of fullscreen.
Returns a function that removes the event listener.
Add a listener for when there is an error.
Returns a function that removes the event listener.
Readonly property that returns the element currently in fullscreen, otherwise null
.
Readonly property that indicates whether fullscreen is active.
Readonly property that indicates whether you are allowed to enter fullscreen.
If your page is inside an <iframe>
you will need to add a allowfullscreen
attribute (+ webkitallowfullscreen
).
That's not supported by browsers for security reasons. There is, however, a dirty workaround. Create a seamless iframe that fills the screen and navigate to the page in that instead.
document.getElementById('new-page-btn').addEventListener('click', () => {
const iframe = document.createElement('iframe')
iframe.setAttribute('id', 'external-iframe')
iframe.setAttribute('src', 'https://new-page-website.com')
iframe.setAttribute('frameborder', 'no')
iframe.style.position = 'absolute'
iframe.style.top = '0'
iframe.style.right = '0'
iframe.style.bottom = '0'
iframe.style.left = '0'
document.body.prepend(iframe)
document.body.style.overflow = 'hidden'
})
Because Intellisense.
But if you don't like it, just re-export as default
in any file...
export { fullscrn as default } from '@quitsmx/fullscrn'
and use it:
import fullscrn from './fullscrn'
- Using the Fullscreen API in web browsers
- MDN - Fullscreen API
- W3C Fullscreen spec
- Building an amazing fullscreen mobile experience
The MIT License © 2021 by QuITS