npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@quitsmx/fullscrn

v1.0.0

Published

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

Downloads

22

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