UNPKG

react-native-vision-camera

Version:

VisionCamera is the fastest and most powerful Camera for react-native.

30 lines (29 loc) 1.32 kB
import { useCallback, useRef, useSyncExternalStore } from 'react'; import { useOrientationManager } from './internal/useOrientationManager'; /** * Reactively use the current {@linkcode source} {@linkcode CameraOrientation}. * - {@linkcode OrientationSource | 'interface'} will listen to UI-orientation. * - {@linkcode OrientationSource | 'device'} will listen to physical phone orientation. * - `undefined` will return `undefined` and not listen to anything. * * @example * ```ts * const orientation = useOrientation('device') * // orientation: 'up' | 'right' | 'down' | 'left' | undefined * ``` */ export function useOrientation(source) { const orientationManager = useOrientationManager(source); const currentOrientation = useRef(orientationManager?.currentOrientation); const subscribe = useCallback((onStoreChange) => { if (orientationManager == null) return () => { }; orientationManager.startOrientationUpdates((orientation) => { currentOrientation.current = orientation; onStoreChange(); }); return () => orientationManager.stopOrientationUpdates(); }, [orientationManager]); const getSnapshot = useCallback(() => currentOrientation.current, []); return useSyncExternalStore(subscribe, getSnapshot, getSnapshot); }