Skip to content
Sensors

useDeviceOrientation

Tracks the physical orientation of the device using the deviceorientation window event, exposing reactive observables for all orientation angles.

Device Orientation
Not Supported

Tracks device orientation angles via the DeviceOrientation API. Best tested on a mobile device.

Alpha (Z)
Beta (X)
Gamma (Y)
Absolute
No
import {
const useDeviceOrientation: (options?: DeepMaybeObservable<UseDeviceOrientationOptions>) => UseDeviceOrientationReturn
useDeviceOrientation
} from "@usels/web";
function
function MyComponent(): JSX.Element
MyComponent
() {
const {
const isSupported$: ReadonlyObservable<boolean>
isSupported$
,
const isAbsolute$: ReadonlyObservable<boolean>

Whether the orientation data is absolute (relative to the Earth's coordinate frame)

isAbsolute$
,
const alpha$: ReadonlyObservable<number | null>

Rotation around the z-axis in degrees (0–360)

alpha$
,
const beta$: ReadonlyObservable<number | null>

Rotation around the x-axis in degrees (-180–180)

beta$
,
const gamma$: ReadonlyObservable<number | null>

Rotation around the y-axis in degrees (-90–90)

gamma$
} =
function useDeviceOrientation(options?: DeepMaybeObservable<UseDeviceOrientationOptions>): UseDeviceOrientationReturn

Framework-agnostic reactive wrapper around the DeviceOrientationEvent.

Resolves the target window via resolveWindowSource, exposes orientation angles as observables, and integrates with the iOS permission flow via createPermissionAware.

useDeviceOrientation
();
return (
<
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>
<
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>Alpha: {
const alpha$: ReadonlyObservable<number | null>

Rotation around the z-axis in degrees (0–360)

alpha$
.
ImmutableObservableBase<number | null>.get(trackingType?: TrackingType | GetOptions): any
get
()}</
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>
<
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>Beta: {
const beta$: ReadonlyObservable<number | null>

Rotation around the x-axis in degrees (-180–180)

beta$
.
ImmutableObservableBase<number | null>.get(trackingType?: TrackingType | GetOptions): any
get
()}</
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>
<
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>Gamma: {
const gamma$: ReadonlyObservable<number | null>

Rotation around the y-axis in degrees (-90–90)

gamma$
.
ImmutableObservableBase<number | null>.get(trackingType?: TrackingType | GetOptions): any
get
()}</
JSX.IntrinsicElements.p: DetailedHTMLProps<HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p
>
</
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>
);
}
ParameterTypeDescription
optionsConfigurableWindow (optional)-

UseDeviceOrientationReturn

NameTypeDescription
hasRealData$ReadonlyObservable<boolean>Whether the device has real orientation sensor hardware. isSupported only checks API availability — desktop browsers expose the API but fire events with all-null values when no hardware exists. hasRealData$ becomes true on the first event where any of alpha/beta/gamma is non-null.
isAbsolute$ReadonlyObservable<boolean>Whether the orientation data is absolute (relative to the Earth’s coordinate frame)
alpha$ReadonlyObservable<number | null>Rotation around the z-axis in degrees (0–360)
beta$ReadonlyObservable<number | null>Rotation around the x-axis in degrees (-180–180)
gamma$ReadonlyObservable<number | null>Rotation around the y-axis in degrees (-90–90)
isSupported$ReadonlyObservable<boolean>-
permissionState$ReadonlyObservable<PermissionState>Current permission state. "unsupported" = API not available in this environment. Never undefined.
permissionGranted$ReadonlyObservable<boolean>true only when permissionState$ is "granted"
needsPermission$ReadonlyObservable<boolean>true when permissionState$ is "prompt" or "denied". "unsupported" → false.
ensurePermission() => Promise<void>Request permission. No-op if unsupported or already granted. If requestPermission throws, the error is propagated (state unchanged).

View on GitHub