fn
useDeviceMotion
v0.0.14testeddemoReactive 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
| Parameter | Type | Description |
|---|---|---|
options? | UseDeviceMotionOptions | Options |
Returns
UseDeviceMotionReturnReactive motion data plus support/permission state| Property | Type | Description |
|---|---|---|
isSupported | ComputedRef<boolean> | Whether the DeviceMotionEvent API is supported. |
requirePermissions | ComputedRef<boolean> | Whether an explicit permission grant is required (iOS 13+). |
permissionGranted | ShallowRef<boolean> | Whether motion permission has been granted. |
acceleration | ShallowRef<DeviceMotionEventAcceleration | null> | Acceleration of the device on the three axes, excluding gravity (m/s²). |
accelerationIncludingGravity | ShallowRef<DeviceMotionEventAcceleration | null> | Acceleration of the device on the three axes, including gravity (m/s²). |
rotationRate | ShallowRef<DeviceMotionEventRotationRate | null> | Rate of change of the device's orientation on the three axes (deg/s). |
interval | ShallowRef<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. |