Skip to Content
ohooks

useScreenOrientation

Tracks the device screen orientation and provides lock and unlock controls.

Loading preview...

The useScreenOrientation hook subscribes to screen.orientation changes via useSyncExternalStore and exposes the current orientation type ('portrait-primary', 'landscape-primary', etc.), rotation angle, and lock / unlock helper methods.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useScreenOrientation.json

Usage

Examples

Displaying Current Orientation and Angle

Read type and angle to adapt layouts or prompt users to rotate their device.

Locking Orientation to Landscape

Call lock("landscape") (typically while in fullscreen mode on mobile browsers) and unlock() to release the lock.

API Reference

ScreenOrientationState

Reactive screen orientation state properties.

Property
Specification
type
OrientationType
Current orientation type (e.g. 'portrait-primary', 'landscape-primary').
angle
number
Current orientation angle in degrees (0, 90, 180, or 270).
isSupported
union
Whether the screen.orientation API is natively supported.

UseScreenOrientationReturn

Screen orientation state and lock/unlock methods returned by the hook.

Property
Specification
lock
function
Locks the screen orientation to the specified type (if supported by the browser).
unlock
function
Unlocks the screen orientation.
type
OrientationType
Current orientation type (e.g. 'portrait-primary', 'landscape-primary').
angle
number
Current orientation angle in degrees (0, 90, 180, or 270).
isSupported
union
Whether the screen.orientation API is natively supported.