UNPKG

one

Version:

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

111 lines (110 loc) • 3.45 kB
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 reservedRegionsContext_exports = {}; __export(reservedRegionsContext_exports, { ReservedRegionsContext: () => ReservedRegionsContext, segmentsFor: () => segmentsFor, useReady: () => useReady, useRegions: () => useRegions, useSegments: () => useSegments, useSpanning: () => useSpanning }); module.exports = __toCommonJS(reservedRegionsContext_exports); var import_react = require("react"); const ReservedRegionsContext = (0, import_react.createContext)(null); function useSnapshot(hook) { const snapshot = (0, import_react.useContext)(ReservedRegionsContext); if (!snapshot) { throw new Error(`One.${hook} must be used inside One.UI.ReservedRegions.Provider`); } return snapshot; } function useRegions(options) { const { regions } = useSnapshot("useReservedRegions"); const kind = options?.kind; const includeInactive = options?.includeInactive === true; return (0, import_react.useMemo)(() => regions.filter((region) => (!kind || region.kind === kind) && (includeInactive || region.isActive)), [ regions, kind, includeInactive ]); } function useReady() { return useSnapshot("useReservedRegionsReady").ready; } function useSegments() { const { bounds, ready, regions } = useSnapshot("useWindowSegments"); return (0, import_react.useMemo)(() => { if (!ready || !bounds || bounds.width <= 0 || bounds.height <= 0) return []; return segmentsFor(bounds, regions); }, [ bounds, ready, regions ]); } function useSpanning() { return useSegments().length > 1; } function segmentsFor(bounds, regions) { const edgeTolerance = .5; let segments = [{ x: 0, y: 0, ...bounds }]; for (const region of regions) { if (region.kind !== "division" || !region.isActive) continue; const { x, y, width, height } = region.frame; segments = segments.flatMap((segment) => { const right = segment.x + segment.width; const bottom = segment.y + segment.height; if (y <= segment.y + edgeTolerance && y + height >= bottom - edgeTolerance && x > segment.x && x + width < right) { return [{ x: segment.x, y: segment.y, width: x - segment.x, height: segment.height }, { x: x + width, y: segment.y, width: right - x - width, height: segment.height }]; } if (x <= segment.x + edgeTolerance && x + width >= right - edgeTolerance && y > segment.y && y + height < bottom) { return [{ x: segment.x, y: segment.y, width: segment.width, height: y - segment.y }, { x: segment.x, y: y + height, width: segment.width, height: bottom - y - height }]; } return [segment]; }); } return segments.sort((a, b) => a.y - b.y || a.x - b.x); }