Skip to content

useDeviceMotion ​

Hook that work with device motion

utilities
test coverage
Last changed: last month

TIP

This hook uses DeviceMotionEvent browser api to provide enhanced functionality. Make sure to check for compatibility with different browsers when using this api

Installation ​

Library
CLI
Manual
typescript
import { useDeviceMotion } from '@siberiacancode/reactuse';

Usage ​

typescript
const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion();

Demo ​

Api ​

Parameters

NameTypeDefaultNote
delay?number1000The delay in milliseconds
callback?(event: DeviceMotionEvent) => void-The callback function to be invoked
enabled?booleantrueWhether to enable the hook

Returns

UseDeviceMotionReturn

Type declaration ​

typescript
export interface UseDeviceMotionReturn {
  acceleration: DeviceMotionEventAcceleration;
  accelerationIncludingGravity: DeviceMotionEventAcceleration;
  interval: DeviceMotionEvent['interval'];
  rotationRate: DeviceMotionEventRotationRate;
}

export interface UseDeviceMotionParams {
  /** The delay in milliseconds */
  delay?: number;
  /** Whether to enable the hook */
  enabled?: boolean;
  /** The callback function to be invoked */
  callback?: (event: DeviceMotionEvent) => void;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
M
Michael Mironychev
Michael Mironychev

Released under the MIT License.