Skip to main content

orientation

Read device orientation events and lock/unlock the screen orientation via the DeviceOrientation and Screen Orientation APIs.

Import: @rtorcato/browser-common/orientation

๐Ÿ“– MDN: DeviceOrientationEvent ยท ๐Ÿ“Š caniuse: deviceorientation

iOS 13+ Safari requires an explicit user-gesture permission grant (see motion's requestMotionPermission, which covers orientation too).

Exampleโ€‹

import {
isDeviceOrientationAvailable,
onDeviceOrientation,
lockScreenOrientation,
unlockScreenOrientation,
} from '@rtorcato/browser-common/orientation'

if (isDeviceOrientationAvailable()) {
const off = onDeviceOrientation((e) => console.log(e.alpha, e.beta, e.gamma))
off()
}

await lockScreenOrientation('landscape-primary')
unlockScreenOrientation()

Exportsโ€‹

  • isDeviceOrientationAvailable() โ€” feature check for DeviceOrientationEvent
  • onDeviceOrientation(callback) โ€” subscribes to orientation changes, returns an unsubscribe function
  • getScreenOrientationType() โ€” current orientation type (e.g. 'portrait-primary')
  • lockScreenOrientation(type) โ€” locks the screen to an orientation, if supported
  • unlockScreenOrientation() โ€” unlocks the screen orientation, if supported

See the API reference for full signatures.