Files
NODEDC_DESIGN_GUIDELINE/packages/ui-react/src/rangeBand.ts
T
Codex cb534dbe39 feat(ui): add docking, viewport controls and composed settings
Add shared dock layouts, navigation cube, range bands, progress actions and comparison surfaces. Extend toolbar menus and loading composition, and align tokens, registry entries, documentation and the living catalog.
2026-10-29 09:38:25 +03:00

25 lines
1.2 KiB
TypeScript

export type RangeBand = readonly [number, number];
const clamp = (n: number, min: number, max: number) => Math.max(min, Math.min(max, n));
export function normalizeRangeBand(value: RangeBand, min: number, max: number): [number, number] {
const low = clamp(value[0], min, max);
return [low, clamp(value[1], low, max)];
}
export function nearestRangeBoundary([low, high]: RangeBand, value: number): 0 | 1 {
// Coincident boundaries remain separable: below pulls the lower, above the upper.
return value <= (low + high) / 2 ? 0 : 1;
}
export function moveRangeBoundary(value: RangeBand, boundary: 0 | 1, next: number,
min: number, max: number, step: string | number = 1): [number, number] {
const band = normalizeRangeBand(value, min, max);
const increment = Number(step);
const bounded = clamp(next, min, max);
// Exact endpoints stay reachable even when the range is not step-aligned.
const aligned = bounded === min || bounded === max || !Number.isFinite(increment) || increment <= 0
? bounded : Number((min + Math.round((bounded - min) / increment) * increment).toPrecision(12));
band[boundary] = boundary === 0 ? clamp(aligned, min, band[1]) : clamp(aligned, band[0], max);
return band;
}