@quitsmx/fullscrn

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API, which lets you bring the page or any element into fullscreen.

Usage no npm install needed!

<script type="module">
  import quitsmxFullscrn from 'https://cdn.skypack.dev/@quitsmx/fullscrn';
</script>

README

@quitsmx/fullscrn

npm Version License Bundle Size Typings

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.

Install

yarn add @quitsmx/fullscrn
# or npm
npm i @quitsmx/fullscrn -S

Import

// CommonJS (node)
const { fullscrn } = require('@quitsmx/fullscreen')

// ES module
import { fullscrn } from '@quitsmx/fullscreen'

Why

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.

Examples

Fullscreen the page

document.getElementById('button-id').addEventListener('click', () => {
  fullscrn.request()
})

Fullscreen an element

document.getElementById('button-id').addEventListener('click', evt => {
  fullscrn.request(evt.currentTarget.parentElement)
})

Hide navigation user-interface on mobile devices

document.getElementById('button-id').addEventListener('click', evt => {
  fullscrn.request(evt.currentTarget.parentElement, { navigationUI: 'hide' })
})

Toggle fullscreen on a image with jQuery

$('img').on('click', evt => {
  fullscrn.toggle(evt.target)
})

Detect fullscreen changes

const remover = fullscrn.onchange(() => {
  console.log('Am I fullscreen?', fullscrn.isFullscreen ? 'Yes' : 'No')
})

// Later, you can remove the listener by calling `remover`
remover()

Detect fullscreen changes in a React FC

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')
    }),
  []
)

Detect fullscreen error

/**
 * 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)
})

API

request(element?: Element, options?: FullscreenOptions) => Promise<void>

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.

exit() => Promise<void>

Brings you out of fullscreen.

Returns a promise that resolves after the element exits fullscreen.

toggle(element?: Element, options?: FullscreenOptions) => Promise<void>

Requests fullscreen if not active, otherwise exits.

Accepts a DOM element and FullscreenOptions.

Returns a promise that resolves after the element enters/exits fullscreen.

onchange(EventListener) => function

Add a listener for when the browser switches in and out of fullscreen.

Returns a function that removes the event listener.

onerror(EventListener) => function

Add a listener for when there is an error.

Returns a function that removes the event listener.

element: Element | null

Readonly property that returns the element currently in fullscreen, otherwise null.

isFullscreen: boolean

Readonly property that indicates whether fullscreen is active.

isEnabled: boolean

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).

FAQ

How can I navigate to a new page when fullscreen?

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'
})

Why named export?

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'

Resources

License

The MIT License © 2021 by QuITS