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

browser-ui-state

v1.1.28

Published

Library which allows to determine current browser UI state (address bar visibility, on-screen keyboard, etc)

Downloads

164

Readme

Browser UI State

npm version single dependency build gzipped size

Live Demo - Open in Chrome, emulate to Nexus 5X and play with different modes (look for state property with values e.g. EXPANDED, COLLAPSED, KEYBOARD, etc) Live Demo Preview

Motivation

Unfortunately browsers does not provide any API to allow us to determine their UI state in context of address or other bars visibility, on-screen keyboard presence, etc.

Such need exist for SPAs (usually games or video content) which does not have long body to scroll through so that browser UI disappears. So they need some way to "expand" the browser to some kind of "full screen" to provide immersive user-experience.

There is HTML5 Fullscreen API to solve this problem, but it is not supported cross-browser (for example not supported in iOS as well as on some Android stock browsers). Also not all browsers are using unprefixed version of the API, leading to creation of vendor-agnostic wrapper libraries like Fscreen.

Fortunately with the help of a bit of mathematics, spreadsheets and some gathered browser's statistics - it is possible to determine the state by calculating deviation between current viewport aspect ratio (reported by window.innerWidth and height) and static screen aspect ratio (reported by screen.width and height).

This, being packaged into library, allows to build on top of it cross-browser full screen solutions like displaying overlay (only when needed) with message to the user e.g. "To use this app please swipe up the page".

This very library does not provide (and does not intended to) any out-of-the-box full screen solutions.

Installation

$ npm i browser-ui-state

NPM

Don't be afraid of caret (^) in your package.json for this dependency - semver will be used correctly for sure :hand::expressionless: :one:.:zero:.:zero:.

Usage

The library's API intended to be called whenever browser resize or orientation change happens:

import BrowserUiState from 'browser-ui-state'
  
const browserUiState = new BrowserUiState()
  
const resizeHandler = () => {
    console.log(browserUiState.orientation) //LANDSCAPE or PORTRAIT
    console.log(browserUiState.state) //COLLAPSED or EXPANDED or KEYBOARD or other, see states.js below
}
  
window.addEventListener('load', resizeHandler)
window.addEventListener('resize', resizeHandler)
window.addEventListener('orientationchange', resizeHandler)

states.js - see all supported states

Old school:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" 
        content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

    <script type="text/javascript" src="https://unpkg.com/browser-ui-state"></script>
    <script>
        var browserUiState = new BrowserUiState.default()
        
        function resizeHandler() {
            console.log(browserUiState.orientation)
            console.log(browserUiState.state) 
        }
        
        window.addEventListener('load', resizeHandler)
        window.addEventListener('resize', resizeHandler)
        window.addEventListener('orientationchange', resizeHandler)
    </script>
</head>
<body>
</body>
</html>

Detecting device orientation

You might wonder why this library also reports orientation.

Well... because it needs it for internal calculations and there is also a problem in Web to correctly determine current device's orientation: just check what your orientation media query will report to you in portrait when on-screen keyboard is shown (and it resizes viewport like in Chrome).

There is HTML5 Screen Orientation API but it's browser support is similar to the one with HTML5 Fullscreen API.

Advanced

Library also allows to access its internal calculations for whatever advanced usage:

console.log(browserUiState.screenAspectRatio.toFixed(2))
//Wider side devided to narrower side (screen.width & screen.height)
  
console.log(browserUiState.viewportAspectRatio.toFixed(2))
//Same as above but for window.innerWidth & window.innerHeight
  
console.log(browserUiState.delta.toFixed(2))
//Absolute delta between the 2 above
  
console.log(`${browserUiState.deviation.toFixed(2)}%`)
//Deviation between delta and screen aspect ratio
  
console.log(`${browserUiState.collapsedThreshold}%`)
//Deviation threshold for current user agent to treat state as collapsed (with address bar visible)
  
console.log(`${browserUiState.keyboardThreshold}%`)
//Deviation threshold for current user agent to treat state as the one when on-screen keyboard is visible

Dependencies

Only one: Fscreen - ~60 lines of code (used for vendor agnostic access to the Fullscreen API)

Matrix of supported devices and browsers

See Matrix

Engine explanation

See Engine