Git Product home page Git Product logo

o9n's Introduction

o9n (orientation)

A screen.orientation ponyfill ๐Ÿ–ฅ

This is a ponyfill for the new, currently in-draft screen.orientation API.

Installation

Use npm:

npm install o9n

Usage

Using browserify:

var orientation = require('o9n').orientation;

or build it and include the file in a <script>-tag:

<script src="dist/o9n.js"></script>

It will be available in window.o9n.

SSR Usage

If you're in an environment where window may be undefined like during an SSR, you're able to call the included getOrientation method when window is defined in order to get the same orientation object

var getOrientation = require('o9n').getOrientation;
var orientation = getOrientation();

Supported browsers

It should work in these browsers although I couldn't test them all manually (or automatically).

  • IE10+
  • Edge 12+
  • Firefox 6+
  • Safari 6+
  • Chrome 9+
  • Opera 15+
  • iOS Safari 5.1+
  • Android browser 3+
  • Blackberry browser 10+
  • Opera mobile 30+
  • Chrome for Android (some versions ago)
  • Firefox for Android (some versions ago)
  • IE Mobile 10+

Building

npm run build

creates o9n.js in the dist dir

Implementation

This ponyfill leverages all the browser APIs there are at the moment:

Methods:

  • screen.msLockOrientation (IE11, Win8.1, Edge 12+)
  • screen.msUnlockOrientation (IE11, Win8.1, Edge 12+)
  • screen.mozLockOrientation (Firefox 18+)
  • screen.mozUnlockOrientation (Firefox 18+)

Events:

  • window.onorientationchange (iOS)
  • screen.onmsorientationchange (IE11, Win8.1, Edge 12+)
  • screen.onmozorientationchange (Firefox 18+)

Getters:

  • window.orientation (iOS)
  • screen.msOrientation (IE11, Win8.1, Edge 12+)
  • screen.mozOrientation (Firefox 18+)

It normalises them to the aforementioned API draft which is implemented in Chrome 38+, Opera 25+. Also it falls back to a media-query solution for browsers that don't support orientation events at all (all that were not mentioned yet).

API

Methods

o9n.orientation.lock()

Locks the orientation on devices that support it. Possible values are:

  • any
  • natural
  • landscape
  • portrait
  • portrait-primary
  • portrait-secondary
  • landscape-primary
  • landscape-secondary

It returns a promise which resolves, when the orientation was locked. The promise will be rejected if the device could not be locked or locking is not supported at all.

o9n.orientation.unlock()

Unlocks the orientation on devices that support it. Returns undefined.

Events

'change' on o9n.orientation

o9n.orientation can listen to the change event that fires every time the orientation was changed. Just use o9n.orientation.addEventListener or o9n.orientation.onchange like you would normally do.

Getters

o9n.orientation.type

Equals to one of the following

  • portrait-primary
  • portrait-secondary (portrait upside down)
  • landscape-primary
  • landscape-secondary (landscape upside down)

o9n.orientation.angle

Currently equals to 0 for browsers that don't support it.

Polyfill (not recommended)

It is also possible to install o9n as a polyfill. Just call

o9n.install();

before using any of the orientation API.

It will be then available as window.screen.orientation.

This implementation already differs a bit from the current browser implementation in Blink. So it is not recommended to use it as a polyfill to not confuse tools like Modernizr.

Running the tests

npm test

Contributing

I'm happy for every contribution or manual tests in browsers I could not test. Just open an issue or directly file a PR.

License

MIT

o9n's People

Contributors

chmanie avatar crutchcorn avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

o9n's Issues

Usage in NextJS

Currently, due to the check in the initial code block, we're unable to use o9n in SSR projects like NextJS without having an error thrown. I would love to see createOrientation and a new getOrientation function exported for usage in SSR utilities.

I'm willing to take on development of this work, I just wanted to raise it as an issue to spark initial discussion

[Bug report] Event target compatibility with Safari

hi, @chmanie. when o9n.orientation.onchange being fired under Safari. The event target seens not a standard screen.orientation object?

I figure out it's a non-standard implement by Safari. Safari pass a window object when the event being fired.

Could you fix this?


Here is a screen shot:

iOS safari ("5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1"):
image

Chrome ("5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.68 Mobile Safari/537.36"):
image

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.