UNPKG

@sv-use/core

Version:

A collection of Svelte 5 utilities.

48 lines (47 loc) 1.5 kB
import { onDestroy } from 'svelte'; import { handleEventListener } from '../handle-event-listener/index.svelte.js'; import { isSupported } from '../__internal__/is.svelte.js'; import { noop } from '../__internal__/utils.svelte.js'; /** * Provides web developers with information from the physical orientation of the device running the web page. * @see https://svelte-librarian.github.io/sv-use/docs/core/get-device-orientation */ export function getDeviceOrientation(options = {}) { const { autoCleanup = true } = options; let cleanup = noop; const _isSupported = isSupported(() => window && 'DeviceOrientationEvent' in window); let _isAbsolute = $state(false); let _alpha = $state(0); let _beta = $state(0); let _gamma = $state(0); if (_isSupported.current) { cleanup = handleEventListener('deviceorientation', onDeviceOrientation, { autoCleanup }); } if (autoCleanup) { onDestroy(() => cleanup()); } function onDeviceOrientation(event) { _isAbsolute = event.absolute; _alpha = event.alpha; _beta = event.beta; _gamma = event.gamma; } return { get isSupported() { return _isSupported.current; }, get isAbsolute() { return _isAbsolute; }, get alpha() { return _alpha; }, get beta() { return _beta; }, get gamma() { return _gamma; }, cleanup }; }