Slider

A skeuomorphic range slider with a recessed groove, a raised fill and a thin handle line.

Preview

Setup

Install with the shadcn CLI

$npx shadcn@latest add https://kinetikui.singhgourav.site/r/slider.json

One command adds every file, the npm packages and any Kinetik components it builds on. Needs a components.json; the CLI offers to create one if your project has none.

Or install manually

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge
  2. 2

    Copy lib/utils.ts from the Code tab. Skip if you already have cn().

  3. 3

    Copy styles/tokens.css from the Code tab and import it in your global CSS after Tailwind: @import "./styles/tokens.css";

  4. 4

    Create a components/ui/slider/ folder and copy every file from the Code tab into it. Each file does one job, so you can read or change a piece without the rest.

  5. 5

    Import and render:

    Example
    import { Slider } from "@/components/ui/slider"
    
    <Slider label="Volume" defaultValue={40} />

Usage

Leave value unset and the slider keeps its own value, starting at defaultValue. Pass value and onValueChange to control it yourself.

Example
import { Slider } from "@/components/ui/slider"

export function Settings() {
  const [images, setImages] = useState(2)

  return (
    <Slider
      label="Num Images"
      description="The number of images to generate."
      value={images}
      defaultValue={1}
      min={1}
      max={4}
      onValueChange={setImages}
    />
  )
}

For ranges that go negative, set bipolar. The fill then grows outwards from the centre, and at the midpoint it shows as a small centred knob.

Example
<Slider label="Wind" min={-1} max={1} step={0.05} defaultValue={0} bipolar accent="#38b6ff" />

Double-click the track to reset it to defaultValue.

Set variant="flat" to drop the raised shading. The groove is plain, the fill is only a shade lighter, and a 1px shine border, brightest along the top edge, outlines it. The handle line stays muted until you grab it, then lights up in accent.

Example
<Slider label="Volume" defaultValue={40} variant="flat" accent="#ff7a12" />

Props

PropTypeDefaultNotes
labelstring—Title above the track, linked to the input.
descriptionstring—Smaller line under the label.
valuenumber—Controlled value. Leave unset for the slider to keep its own.
defaultValuenumbermin, or centre if bipolarStarting value, shown as Default, and what double-click resets to.
onValueChange(value: number) => void—Fires on every change.
minnumber0
maxnumber100
stepnumber1
format(value: number) => stringup to 2 decimalsHow numbers are shown beside the track and in the meta row.
bipolarbooleanfalseFill outwards from the centre.
dotsnumber5Step dots along the empty groove.
accentstring—Colour the handle line glows while dragging or focused.
variant"depth" | "flat""depth"Raised fill in a recessed groove, or a flat fill with a 1px shine border.
showValuebooleantrueShow the value beside the track.
showMetabooleantrueShow the Default / Min / Max row.
disabledbooleanfalse
namestring—Submitted with a form.

Accessibility

  • A native input type="range" sits over the track, so arrow keys, Page Up/Down, Home/End and screen readers all work as with any slider
  • aria-valuetext reads the formatted value, e.g. "L 40" rather than "-0.4"
  • The handle glows in accent while focused, so keyboard focus is visible