Slider

Touch-friendly sliders for single values and ranges.

Volume60%
Budget range$20–$80
Disabled

Installation

pnpm dlx apen-ui@latest add slider
Also installsradix-ui

Manual installation

Copy the source into components/ui/slider.tsx and install radix-ui.

components/ui/slider.tsx
"use client"

import * as React from "react"
import { Slider as SliderPrimitive } from "radix-ui"

import { cn } from "@/lib/utils"

type SliderProps = React.ComponentProps<typeof SliderPrimitive.Root> & {
  /** Accessible names for the thumbs, in value order. */
  thumbLabels?: string[]
}

function Slider({
  className,
  defaultValue,
  value,
  min = 0,
  max = 100,
  thumbLabels,
  "aria-label": ariaLabel,
  "aria-labelledby": ariaLabelledBy,
  ...props
}: SliderProps) {
  const values = value ?? defaultValue ?? [min]

  return (
    <SliderPrimitive.Root
      data-slot="slider"
      value={value}
      defaultValue={defaultValue ?? [min]}
      min={min}
      max={max}
      aria-label={ariaLabel}
      aria-labelledby={ariaLabelledBy}
      className={cn(
        "relative flex w-full touch-none items-center select-none data-[disabled]:opacity-50 data-[orientation=horizontal]:h-5 data-[orientation=vertical]:h-44 data-[orientation=vertical]:w-5 data-[orientation=vertical]:flex-col",
        className
      )}
      {...props}
    >
      <SliderPrimitive.Track
        data-slot="slider-track"
        className="relative grow overflow-hidden rounded-full bg-muted data-[orientation=horizontal]:h-2 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-2"
      >
        <SliderPrimitive.Range
          data-slot="slider-range"
          className="absolute rounded-full bg-brand-gradient data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full"
        />
      </SliderPrimitive.Track>
      {values.map((_, index) => (
        <SliderPrimitive.Thumb
          key={index}
          data-slot="slider-thumb"
          aria-label={thumbLabels?.[index] ?? (values.length === 1 ? ariaLabel ?? "Value" : `${ariaLabel ?? "Value"} ${index + 1}`)}
          aria-labelledby={thumbLabels?.[index] ? undefined : ariaLabelledBy}
          className="block size-5 shrink-0 rounded-full border-2 border-primary bg-card shadow-md transition-shadow outline-none focus-visible:ring-4 focus-visible:ring-ring/25 data-[disabled]:pointer-events-none data-[disabled]:cursor-not-allowed"
        />
      ))}
    </SliderPrimitive.Root>
  )
}

export { Slider, type SliderProps }