Code Monkey home page Code Monkey logo

browser-ui-state's Introduction

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 ✋😑 1️⃣.0️⃣.0️⃣.

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

browser-ui-state's People

Contributors

thebit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

browser-ui-state's Issues

Run tests on CI

You should run your tests on the travis CI before build, so if it fails - then it marks you commit with X - cross icon.

Remove roundings in the spreadsheet!

Due to historical reasons - some important cells are rounded (all aspect ratio cells actually).
Which gives not-precise calculations of deviation (not the same as on the real device), as so thresholds in the spreadsheet may not fit real device's needs!
I can't believe I was so stupid...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.