HextaUI

Slider: Controlled

onValueChange fires on every move and onValueCommitted once on release, which is the moment to save. Setting the value from outside glides too.

Brightness
40
Saved: 40
"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

export function SliderControlled() {
  const [value, setValue] = React.useState(40)
  const [saved, setSaved] = React.useState(40)

  return (
    <div className="flex w-full max-w-xs flex-col gap-4">
      <Slider
        value={value}
        onValueChange={setValue}
        onValueCommitted={setSaved}
      >
        <SliderLabel>Brightness</SliderLabel>
        <SliderValue />
      </Slider>
      <div className="flex items-center justify-between gap-3">
        <span className="text-sm text-muted-foreground">Saved: {saved}</span>
        <div className="flex gap-2">
          <Button variant="outline" size="sm" onClick={() => setValue(0)}>
            Off
          </Button>
          <Button variant="outline" size="sm" onClick={() => setValue(100)}>
            Max
          </Button>
        </div>
      </div>
    </div>
  )
}

This adds the example as components/examples/slider/controlled.tsx, together with the Slider component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/slider-controlled.json

To add only the component, follow the Slider installation.

Props in this example

The Slider props this example sets, from the API reference.

PropTypeDefault
valueThe controlled value. One thumb per item.
number | number[]–
onValueChangeFires on every change while dragging.
(value, eventDetails) => void–
onValueCommittedFires once when a change ends.
(value, eventDetails) => void–

Every prop is in the Slider API reference.

See them all, with keyboard support and accessibility notes, on the Slider page.