Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
Last active July 11, 2022 11:50
Show Gist options
  • Select an option

  • Save AvocadoVenom/8ba6c8faafc249d2d1f0c0065efda215 to your computer and use it in GitHub Desktop.

Select an option

Save AvocadoVenom/8ba6c8faafc249d2d1f0c0065efda215 to your computer and use it in GitHub Desktop.
const supportedScreenOrientation =
(screen?.orientation || {}).type ?? (screen as any).mozOrientation ?? (screen as any).msOrientation
const safariScreenOrientation: OrientationType =
!screen?.orientation && matchMedia('(orientation: portrait)').matches ? 'portrait-primary' : 'landscape-primary'
const initialScreenOrientation = supportedScreenOrientation ?? safariScreenOrientation ?? 'portrait-primary'
let screenOrientation: OrientationType = initialScreenOrientation
if (screen.orientation) {
screen.orientation.addEventListener(
'change',
(ev: Event) => (screenOrientation = (ev.target ?? ({} as any)).type)
)
}
const isLandscapeOrientation = () => ['landscape-primary', 'landscape-secondary'].includes(screenOrientation)
const isPortraitOrientation = () => ['portrait-primary', 'portrait-secondary'].includes(screenOrientation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment