@zag-js/splitter
Version:
Core logic for the splitter widget implemented as a state machine
65 lines (63 loc) • 1.68 kB
JavaScript
import "../chunk-QZ7TP4HQ.mjs";
// src/utils/aria.ts
import { ensure } from "@zag-js/utils";
function calculateAriaValues({
size,
panels,
pivotIndices
}) {
let currentMinSize = 0;
let currentMaxSize = 100;
let totalMinSize = 0;
let totalMaxSize = 0;
const firstIndex = pivotIndices[0];
ensure(firstIndex, () => "No pivot index found");
panels.forEach((panel, index) => {
const { maxSize = 100, minSize = 0 } = panel;
if (index === firstIndex) {
currentMinSize = minSize;
currentMaxSize = maxSize;
} else {
totalMinSize += minSize;
totalMaxSize += maxSize;
}
});
const valueMax = Math.min(currentMaxSize, 100 - totalMinSize);
const valueMin = Math.max(currentMinSize, 100 - totalMaxSize);
const valueNow = size[firstIndex];
return {
valueMax,
valueMin,
valueNow
};
}
function getAriaValue(size, panels, handleId) {
const [beforeId, afterId] = handleId.split(":");
const beforeIndex = panels.findIndex((panel) => panel.id === beforeId);
const afterIndex = panels.findIndex((panel) => panel.id === afterId);
if (beforeIndex === -1 || afterIndex === -1) {
return {
beforeId: beforeId || void 0,
afterId: afterId || void 0,
valueMax: void 0,
valueMin: void 0,
valueNow: void 0
};
}
const { valueMax, valueMin, valueNow } = calculateAriaValues({
size,
panels,
pivotIndices: [beforeIndex, afterIndex]
});
return {
beforeId,
afterId,
valueMax: Math.round(valueMax),
valueMin: Math.round(valueMin),
valueNow: valueNow != null ? Math.round(valueNow) : void 0
};
}
export {
calculateAriaValues,
getAriaValue
};