one
Version:
One is a React framework for web, iOS, and Android apps.
71 lines (70 loc) • 3.08 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var screen_orientation_exports = {};
__export(screen_orientation_exports, { ScreenOrientation: () => ScreenOrientation });
module.exports = __toCommonJS(screen_orientation_exports);
var import_unavailable = require("./unavailable.cjs");
var import_validateCallback = require("../validateCallback.cjs");
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 import_unavailable.ScreenOrientation.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 import_unavailable.ScreenOrientation.unlock();
if (!window.screen.orientation?.unlock) throw new Error("ScreenOrientation.unlock: orientation locking is unavailable");
window.screen.orientation.unlock();
return orientation();
},
addChangeListener: (onChange) => {
(0, import_validateCallback.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);
}
});