GooseUI

Components

Range Slider

New

Picks a range with two thumbs. Under the hood there are two native range inputs stacked on top of each other, so keyboard, touch and screen readers work the way the browser already handles them. The fill between the thumbs is pure CSS: each thumb paints a band with border-image, so the slider looks right and submits its values before hydration. A few lines of client code keep the thumbs from crossing, update the value labels and call onValueChange. An edge value can be reported as null, meaning no limit.

Browser Support

Nesting

Groups related styles

Your browser:
color-mix()

Colors the slider focus ring

Your browser:

Preview

Drag the thumbs or focus one and use the arrow keys.

[20, 80]

Installation

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

Usage

import { RangeSlider } from "@/components/ui/range-slider"

export default function Example() {
  const [value, setValue] = useState<[number, number]>([20, 80])

  return <RangeSlider value={value} onValueChange={setValue} />
}

Examples

With number fields and open edges

With nullableEdges a thumb resting on its edge reports null, which fits filters like price from or price up to. The fields and the slider share one state, so typing a number moves a thumb and dragging updates the field.

Price$250 –

{ min: 250, max: null }

const [value, setValue] = useState<NullableRangeValue>([250, null])
const [from, to] = value

<RangeSlider
  nullableEdges
  min={0}
  max={2000}
  step={50}
  minGap={100}
  value={value}
  onValueChange={setValue}
  showValues={false}
/>
<Input
  type="number"
  placeholder="0"
  value={from ?? ""}
  onChange={(e) => setValue([e.target.value ? Number(e.target.value) : null, to])}
/>
<Input
  type="number"
  placeholder="2000"
  value={to ?? ""}
  onChange={(e) => setValue([from, e.target.value ? Number(e.target.value) : null])}
/>

Formatted values

formatValue changes the labels under the track and the aria-valuetext that screen readers announce. Here minGap keeps the range at least two hours long.

Working hours
<RangeSlider
  min={0}
  max={24}
  minGap={2}
  defaultValue={[9, 18]}
  formatValue={(v) => `${String(v).padStart(2, "0")}:00`}
/>

Props

PropTypeDefaultDescription
valueRangeValue | NullableRangeValue-Current range in controlled mode
defaultValueRangeValue | NullableRangeValue[min, max]Initial range in uncontrolled mode
onValueChange(value) => void-Called with the new range on every move
minnumber0Lowest value
maxnumber100Highest value
stepnumber1Step of both thumbs
minGapnumber0Smallest allowed distance between the thumbs
nullableEdgesbooleanfalseReport a thumb on its edge as null
formatValue(value: number) => stringStringFormats value labels and aria-valuetext
showValuesbooleantrueShow formatted values under the track
namestring-Form field prefix: inputs get name-min and name-max
disabledbooleanfalseTurns off interaction
labels{ min?: string; max?: string }-Accessible names of the lower and upper thumb
classNamestring-Additional CSS classes

Features

  • Native inputs: arrow keys, Page Up and Page Down, Home and End work without extra code
  • No crossing: each thumb stops at the other one plus minGap. The upper thumb is always on top, so when both sit at the end, drag the upper one first
  • CSS fill: the thumbs paint the fill themselves, no width math in JavaScript and no jump on hydration