GooseUI

Components

Quantity Slider

New

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

:has()

Styles elements based on their contents

Your browser:
:focus-within

Highlights the focused quantity field

Your browser:

Preview

Use the buttons, type a number, drag the slider or click a tick.

Total
$96

$12 per seat a month

Installation

npx shadcn@latest add @gooseui/quantity-slider
pnpm dlx shadcn@latest add @gooseui/quantity-slider
yarn dlx shadcn@latest add @gooseui/quantity-slider
bunx --bun shadcn@latest add @gooseui/quantity-slider

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.

kg
Total
$37.00
2 kg in base units
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

PropTypeDefaultDescription
valuenumber-Quantity in base units, controlled mode
defaultValuenumberminInitial quantity in uncontrolled mode
onValueChange(value: number) => void-Called with the new quantity in base units
minnumber1Lowest quantity in base units
maxnumber100Highest quantity in base units
stepnumber1Step in base units
unitPricenumber-Price of one base unit. Without it there is no total
formatTotal(total: number) => stringUSDFormats the total
formatValue(value: number) => stringtoLocaleStringFormats tick labels and aria-valuetext
unitsQuantityUnit[]-Units to switch between: { id, label, factor, step? }[]
unitstring-Selected unit id in controlled mode
defaultUnitstringunits[0].idInitial unit id
onUnitChange(unitId: string) => void-Called with the new unit id
ticksnumber5Number of tick labels under the slider
disabledbooleanfalseTurns off interaction
namestring-Form field name. Without units the number field carries it, with units a hidden input sends the base quantity
labelsPartial<QuantitySliderLabels>-Texts: quantity, decrease, increase, total, unit
classNamestring-Additional CSS classes