UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

52 lines (49 loc) • 2.14 kB
import { ScreenOrientation as ScreenOrientation$1 } from "./unavailable.mjs"; import { validateCallback } from "../validateCallback.mjs"; const values = { "portrait-primary": "portrait", "portrait-secondary": "portraitUpsideDown", "landscape-primary": "landscapeLeft", "landscape-secondary": "landscapeRight" }; const locks = { portrait: "portrait-primary", portraitUpsideDown: "portrait-secondary", landscapeLeft: "landscape-primary", landscapeRight: "landscape-secondary", landscape: "landscape" }; function orientation() { return values[window.screen.orientation?.type] ?? "unknown"; } const ScreenOrientation = Object.freeze({ getOrientation: async () => typeof window === "undefined" ? "unknown" : orientation(), lock: async (value) => { if (typeof window === "undefined") return ScreenOrientation$1.lock(value); if (!locks[value]) throw new TypeError("ScreenOrientation.lock: invalid orientation"); const screenOrientation = window.screen.orientation; if (!screenOrientation?.lock) throw new Error("ScreenOrientation.lock: orientation locking is unavailable"); await screenOrientation.lock(locks[value]); const result = orientation(); if (value === "landscape" ? !result.startsWith("landscape") : result !== value) { throw new Error("ScreenOrientation.lock: screen did not reach the requested orientation"); } return result; }, unlock: async () => { if (typeof window === "undefined") return ScreenOrientation$1.unlock(); if (!window.screen.orientation?.unlock) throw new Error("ScreenOrientation.unlock: orientation locking is unavailable"); window.screen.orientation.unlock(); return orientation(); }, addChangeListener: (onChange) => { validateCallback(onChange, "ScreenOrientation.addChangeListener requires a function"); if (typeof window === "undefined" || !window.screen.orientation) return () => {}; const target = window.screen.orientation; const listener = () => onChange(orientation()); target.addEventListener("change", listener); return () => target.removeEventListener("change", listener); } }); export { ScreenOrientation }; //# sourceMappingURL=index.mjs.map