# Ratio balancer

> A multi-segment allocation track. Drag dividers to rebalance constrained percentages with zero initial jump.

Distribute a shared 100% budget among multiple competing categories. Thin hairline dividers glide between segments with 1:1 physical tracking, numbers roll to settled totals, and expanded touch targets make dragging effortless on mobile and desktop. Built with pure flex-grow transitions, zero animation libraries, and zero shadows.

## Install

```bash
npx shadcn@latest add @hairline/ratio-balancer
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/ratio-balancer
- Registry item (source): https://hairlineui.com/r/ratio-balancer.json

## Usage

```tsx
import { RatioBalancer } from "@/components/ratio-balancer";

<RatioBalancer
  label="Compute allocation"
  defaultSegments={[
    { id: "eval", label: "Evaluation", ratio: 45 },
    { id: "batch", label: "Batch tasks", ratio: 35 },
    { id: "dev", label: "Dev testing", ratio: 20 },
  ]}
  onValueCommit={setRatios}
/>
```

## Allocate resource quotas in cloud settings

Use RatioBalancer to configure server cluster allocation or team spend limits. It maintains total conservation (always 100%) with fluid responsive drag.

```tsx
"use client";
import { useState } from "react";
import { RatioBalancer, type RatioSegment } from "@/components/ratio-balancer";

export function QuotaManager() {
  const [segments, setSegments] = useState<RatioSegment[]>([
    { id: "gpu", label: "GPU Clusters", ratio: 60 },
    { id: "cpu", label: "General CPU", ratio: 25 },
    { id: "storage", label: "NVMe Cache", ratio: 15 },
  ]);

  return (
    <div className="rounded-2xl border border-border bg-card p-6">
      <RatioBalancer
        label="Cluster resources"
        segments={segments}
        onValueCommit={(ratios) => {
          setSegments(prev => prev.map((s, i) => ({ ...s, ratio: ratios[i] })));
        }}
      />
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | – | Accessible heading displayed above the allocation bar. |
| `segments / defaultSegments` | `RatioSegment[]` | – | Array of segment objects with id, label, and ratio (summing to 100). |
| `minRatio` | `number` | `5` | Minimum allowed percentage for any segment during drag. |
| `disabled` | `boolean` | `false` | Disables all divider dragging. |
| `onValueChange` | `(ratios: number[]) => void` | – | Fired during divider movement with immediate ratio updates. |
| `onValueCommit` | `(ratios: number[]) => void` | – | Fired upon pointer release or keyboard adjustment with clean normalized integers. |

## States

- **rebalance**: Dragging a divider updates adjacent segment flex-grow styles and DOM text nodes directly.
- **settle**: Releasing the pointer normalizes all ratios to 100% and rolls final percentage digits via NumberRoll.
- **keyboard**: Dividers accept focus; Left and Right arrow keys nudge allocations by 1% (or 5% with Shift).

## What it handles

Dividing 100% between several things is awkward with separate number fields that never add up. This is one bar with draggable dividers, where the total is always 100%.

- **Always 100%**: On release every ratio is normalised to whole numbers that add up to 100.
- **Drag the dividers**: Moving a divider rebalances the two segments beside it. It tracks your finger one to one, with no jump at the start.
- **A minimum share**: No segment can drop below minRatio, 5% by default.
- **Numbers that roll**: The percentages roll to their settled totals when you let go.
- **Commit when done**: onValueChange fires while dragging, and onValueCommit fires on release or a keyboard change.
- **Keyboard and screen readers**: Dividers take focus, Left and Right nudge by 1% (5% with Shift), and each divider reports its value.

## Replaces

linked sliders, percentage inputs that must sum to 100, normalisation logic, a minimum share rule, touch target sizing

## Where it fits

- **Traffic splits**: Divide traffic between variants of a test or a rollout.
- **Budget allocation**: Split a budget between channels or teams.
- **Resource quotas**: Share compute or storage across projects in cloud settings.
- **Revenue and payout splits**: Divide earnings between collaborators.

## Notes

- Dividers act as semantic separators with vertical orientation and aria-valuenow.
- Delta-based drag ensures 1:1 finger tracking without initial jumping on touch devices.
- Segments maintain pure DOM styles during drag; React state updates occur only on commit.
- Reduced motion removes flex-grow easing transitions and rolls digits in place.

License: MIT (https://hairlineui.com/license#components)
