UNPKG

@zag-js/splitter

Version:

Core logic for the splitter widget implemented as a state machine

112 lines (110 loc) 3.33 kB
import "../chunk-QZ7TP4HQ.mjs"; // src/utils/size.ts var sizeRegex = /^(-?\d*\.?\d+)(%|px|em|rem|vw|vh)?$/; var percentRegex = /^(-?\d*\.?\d+)%$/; function getRootSize(rootEl, orientation) { if (!rootEl) return 0; const rect = rootEl.getBoundingClientRect(); return orientation === "horizontal" ? rect.width : rect.height; } function getGroupSize(rootEl, orientation) { return getRootSize(rootEl, orientation); } function toPixelValue(value, unit, rootEl) { const win = rootEl.ownerDocument.defaultView; if (!win) return void 0; switch (unit) { case "px": return value; case "em": { const fontSize = Number.parseFloat(win.getComputedStyle(rootEl).fontSize); return value * fontSize; } case "rem": { const fontSize = Number.parseFloat(win.getComputedStyle(rootEl.ownerDocument.documentElement).fontSize); return value * fontSize; } case "vw": return value / 100 * win.innerWidth; case "vh": return value / 100 * win.innerHeight; default: return void 0; } } function parsePanelSize(size, rootEl, orientation) { if (size == null) return void 0; if (typeof size === "number") { return size; } const match = size.trim().match(sizeRegex); if (!match) return void 0; const value = Number.parseFloat(match[1]); if (!Number.isFinite(value)) return void 0; const unit = match[2]; if (unit == null || unit === "%") { return value; } if (!rootEl) return void 0; const rootSize = getRootSize(rootEl, orientation); if (rootSize === 0) return void 0; const px = toPixelValue(value, unit, rootEl); return px == null ? void 0 : px / rootSize * 100; } function toCssPanelSize(size) { if (size == null) return void 0; if (typeof size === "number") { return `${size}%`; } const trimmed = size.trim(); if (percentRegex.test(trimmed)) { return trimmed; } const match = trimmed.match(sizeRegex); if (!match) return void 0; const value = Number.parseFloat(match[1]); if (!Number.isFinite(value)) return void 0; const unit = match[2]; return unit == null ? `${value}%` : `${value}${unit}`; } function resolvePanelSizes({ sizes, panels, rootEl, orientation }) { const nextSize = Array(panels.length); let remainingSize = 100; let numPanelsWithSizes = 0; for (let index = 0; index < panels.length; index++) { const size = parsePanelSize(sizes?.[index], rootEl, orientation); if (size == null) continue; numPanelsWithSizes++; nextSize[index] = size; remainingSize -= size; } for (let index = 0; index < panels.length; index++) { if (nextSize[index] != null) continue; const numRemainingPanels = panels.length - numPanelsWithSizes; const size = numRemainingPanels > 0 ? remainingSize / numRemainingPanels : 0; numPanelsWithSizes++; nextSize[index] = size; remainingSize -= size; } return nextSize; } function normalizePanels(panels, rootEl, orientation) { return panels.map((panel) => ({ ...panel, minSize: parsePanelSize(panel.minSize, rootEl, orientation), maxSize: parsePanelSize(panel.maxSize, rootEl, orientation), collapsedSize: parsePanelSize(panel.collapsedSize, rootEl, orientation) })); } export { getGroupSize, normalizePanels, parsePanelSize, resolvePanelSizes, toCssPanelSize };