# Slider

> Drag a thumb along a track to pick a number or a range, snapping to steps.

Docs: https://plumbago.ochotzas.com/components/slider

## Installation

```sh
npx plumbago add slider
```

This copies `components/ui/slider.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

```tsx
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

<Slider defaultValue={3} max={5} step={1}>
  <SliderLabel>Spice level</SliderLabel>
  <SliderValue />
</Slider>
```

## Examples

### Preview

```tsx
import { BlurSwap } from "@/lib/blur-swap"
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

const levels = ["None", "Mild", "Medium", "Hot", "Very hot", "Fire"]

export default function SliderDemo() {
  return (
    <Slider defaultValue={3} min={0} max={5} step={1} getAriaValueText={(_, value) => levels[value] ?? ""} className="max-w-sm">
      <SliderLabel>Spice level</SliderLabel>
      <SliderValue className="min-w-16">{(_, values) => <BlurSwap text={levels[values[0] ?? 0] ?? ""} className="justify-items-end" />}</SliderValue>
    </Slider>
  )
}
```

### In a settings row

```tsx
import { Island } from "@/components/ui/island"
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

export default function SliderInASettingsRow() {
  return (
    <Island className="w-full max-w-md">
      <Slider defaultValue={20} min={5} max={60} step={5}>
        <SliderLabel>Prep time</SliderLabel>
        <SliderValue>{(formatted) => `${formatted[0]} min`}</SliderValue>
      </Slider>
    </Island>
  )
}
```

### Range

```tsx
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

const euro = new Intl.NumberFormat("en", { style: "currency", currency: "EUR", maximumFractionDigits: 0 })

export default function SliderRange() {
  return (
    <Slider defaultValue={[10, 40]} min={0} max={60} step={5} thumbLabels={["Lowest price", "Highest price"]} className="max-w-sm">
      <SliderLabel>Price</SliderLabel>
      <SliderValue>{(_, values) => values.map((price) => euro.format(price)).join(" – ")}</SliderValue>
    </Slider>
  )
}
```

### Disabled

```tsx
import { Slider, SliderLabel, SliderValue } from "@/components/ui/slider"

export default function SliderDisabled() {
  return (
    <Slider defaultValue={2} min={0} max={5} step={1} disabled className="max-w-sm">
      <SliderLabel>Spice level</SliderLabel>
      <SliderValue />
    </Slider>
  )
}
```

## Built on Base UI

Slider wraps these Base UI parts and keeps their props:

- [Slider](https://base-ui.com/react/components/slider)

## Source

```tsx
import { Slider as BaseSlider } from "@base-ui/react/slider"
import { cn } from "@/lib/cn"
import type { ComponentProps } from "react"

type WithClassName<T> = Omit<T, "className"> & { className?: string }

export type SliderProps = WithClassName<ComponentProps<typeof BaseSlider.Root>> & {
  thumbLabels?: string[]
  getAriaValueText?: ComponentProps<typeof BaseSlider.Thumb>["getAriaValueText"]
}

export function Slider({ className, children, thumbLabels, getAriaValueText, defaultValue, value, min = 0, max = 100, step = 1, ...props }: SliderProps) {
  const current = value ?? defaultValue ?? min
  const glide = cn(
    "duration-spring-fast ease-spring-fast motion-reduce:transition-none",
    (max - min) / step > 20 && "group-data-dragging/slider:transition-none",
  )
  const thumbCount = Array.isArray(current) ? current.length : 1
  const thumbIndexes = Array.from({ length: thumbCount }, (_, index) => index)

  return (
    <BaseSlider.Root
      data-slot="slider"
      defaultValue={defaultValue}
      value={value}
      min={min}
      max={max}
      step={step}
      thumbAlignment="edge"
      className={cn("group/slider grid w-full grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 gap-y-2 data-disabled:opacity-40", className)}
      {...props}
    >
      {children}
      <BaseSlider.Control
        data-slot="slider-control"
        className="col-start-1 -my-2 flex h-11 cursor-pointer touch-none items-center select-none data-disabled:cursor-default"
      >
        <BaseSlider.Track data-slot="slider-track" className="relative h-1.5 w-full rounded-full bg-track select-none">
          <BaseSlider.Indicator data-slot="slider-indicator" className={cn("rounded-full bg-ink transition-[width,inset-inline-start] select-none", glide)} />
          {thumbIndexes.map((index) => (
            <BaseSlider.Thumb
              key={index}
              index={thumbCount > 1 ? index : undefined}
              aria-label={thumbLabels?.[index]}
              getAriaValueText={getAriaValueText}
              data-slot="slider-thumb"
              className={cn(
                "size-7 rounded-full bg-white shadow-[0_2px_6px_rgba(0,0,0,0.18)] transition-[inset-inline-start] select-none",
                glide,
                "has-focus-visible:ring-2 has-focus-visible:ring-accent has-focus-visible:ring-offset-2 has-focus-visible:ring-offset-bg in-[.on-surface]:has-focus-visible:ring-offset-surface",
                "after:absolute after:-inset-2.5 after:content-['']",
              )}
            />
          ))}
        </BaseSlider.Track>
      </BaseSlider.Control>
    </BaseSlider.Root>
  )
}

export type SliderLabelProps = WithClassName<ComponentProps<typeof BaseSlider.Label>>

export function SliderLabel({ className, ...props }: SliderLabelProps) {
  return <BaseSlider.Label data-slot="slider-label" className={cn("col-span-full text-body font-medium text-ink", className)} {...props} />
}

export type SliderValueProps = WithClassName<ComponentProps<typeof BaseSlider.Value>>

export function SliderValue({ className, ...props }: SliderValueProps) {
  return (
    <BaseSlider.Value
      data-slot="slider-value"
      className={cn("order-last col-start-2 min-w-8 text-right text-footnote text-ink-2 tabular-nums", className)}
      {...props}
    />
  )
}
```
