A quantity picker for carts, plans and order forms. It combines minus and plus buttons, a number field you can type into, a slider with clickable ticks and a total whose digits flip one by one through Sliding Number. Optional units convert the amount when the buyer switches between them. The field, the slider and the unit switch are native inputs, and the slider fill is CSS. Client code keeps the field and the slider in sync, converts units and counts the total, because CSS can't do arithmetic on input values.
Browser Support
Styles elements based on their contents
Highlights the focused quantity field
Preview
Use the buttons, type a number, drag the slider or click a tick.
$12 per seat a month
Installation
Usage
import { QuantitySlider } from "@/components/ui/quantity-slider"
export default function Example() {
const [seats, setSeats] = useState(8)
return (
<QuantitySlider
min={1}
max={50}
value={seats}
onValueChange={setSeats}
unitPrice={12}
/>
)
}Examples
Unit switching
Each unit has a factor: how many base units it holds. value, min, max and unitPrice stay in base units, so switching units keeps the same amount and only changes what is shown. A unit can set its own step.
const units = [
{ id: "kg", label: "kg", factor: 1, step: 0.25 },
{ id: "bag", label: "bags", factor: 0.25, step: 1 },
{ id: "sack", label: "sacks", factor: 5, step: 1 },
]
<QuantitySlider
min={0.25}
max={50}
step={0.25}
value={kg}
onValueChange={setKg}
units={units}
unitPrice={18.5}
/>Without a total
Leave out unitPrice and the total disappears. ticks sets how many shortcut labels appear under the slider, 0 hides them.
<QuantitySlider min={0} max={1000} step={10} defaultValue={250} ticks={3} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | - | Quantity in base units, controlled mode |
| defaultValue | number | min | Initial quantity in uncontrolled mode |
| onValueChange | (value: number) => void | - | Called with the new quantity in base units |
| min | number | 1 | Lowest quantity in base units |
| max | number | 100 | Highest quantity in base units |
| step | number | 1 | Step in base units |
| unitPrice | number | - | Price of one base unit. Without it there is no total |
| formatTotal | (total: number) => string | USD | Formats the total |
| formatValue | (value: number) => string | toLocaleString | Formats tick labels and aria-valuetext |
| units | QuantityUnit[] | - | Units to switch between: { id, label, factor, step? }[] |
| unit | string | - | Selected unit id in controlled mode |
| defaultUnit | string | units[0].id | Initial unit id |
| onUnitChange | (unitId: string) => void | - | Called with the new unit id |
| ticks | number | 5 | Number of tick labels under the slider |
| disabled | boolean | false | Turns off interaction |
| name | string | - | Form field name. Without units the number field carries it, with units a hidden input sends the base quantity |
| labels | Partial<QuantitySliderLabels> | - | Texts: quantity, decrease, increase, total, unit |
| className | string | - | Additional CSS classes |