fn

useDeviceMotion

v0.0.14testeddemo

Reactive DeviceMotionEvent exposing acceleration (with and without gravity), rotation rate, and the hardware sampling interval. SSR-safe, uses a single passive listener, and supports the iOS 13+ permission flow.

Examples

ts
const { acceleration, rotationRate, interval } = useDeviceMotion();
ts
// iOS 13+: request permission from a user gesture, throttle updates
const { ensurePermissions, acceleration } = useDeviceMotion({ eventFilter: throttleFilter(100) });
button.addEventListener('click', ensurePermissions);

Demo

Loading demo…

Signature

ts
export function useDeviceMotion(options: UseDeviceMotionOptions ={ ... }

Parameters

ParameterTypeDescription
options?UseDeviceMotionOptionsOptions

Returns

UseDeviceMotionReturnReactive motion data plus support/permission state
PropertyTypeDescription
isSupportedComputedRef<boolean>Whether the DeviceMotionEvent API is supported.
requirePermissionsComputedRef<boolean>Whether an explicit permission grant is required (iOS 13+).
permissionGrantedShallowRef<boolean>Whether motion permission has been granted.
accelerationShallowRef<DeviceMotionEventAcceleration | null>Acceleration of the device on the three axes, excluding gravity (m/s²).
accelerationIncludingGravityShallowRef<DeviceMotionEventAcceleration | null>Acceleration of the device on the three axes, including gravity (m/s²).
rotationRateShallowRef<DeviceMotionEventRotationRate | null>Rate of change of the device's orientation on the three axes (deg/s).
intervalShallowRef<number>Interval, in ms, at which data is obtained from the underlying hardware.
ensurePermissions() => Promise<void>Request motion permission (iOS 13+). Must be triggered by a user gesture. Resolves once the request settles; on grant the listener is attached.