Slider

Picks a number, or a range, by dragging a thumb along a scale.

Drag the thumb, or focus it and use the arrow keys. Name a single thumb with aria-label, or put the slider in a Field. For a range, pass two values and give each thumb its own name.

Preview
basic.tsx
import { Inline } from "~/components/ui/inline"
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from "~/components/ui/slider"
import { Text } from "~/components/ui/text"

export default function Example() {
  return (
    <Slider defaultValue={24} min={14} max={40} className="max-w-sm">
      <Inline justify="between">
        <Text render={<span />} size="sm" weight="medium">
          Text size
        </Text>
        <SliderValue />
      </Inline>
      <SliderControl>
        <SliderTrack>
          <SliderIndicator />
          <SliderThumb aria-label="Text size" />
        </SliderTrack>
      </SliderControl>
    </Slider>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/slider.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/slider.tsx
import {
  Slider as SliderPrimitive,
  SliderControl as SliderControlPrimitive,
  SliderIndicator as SliderIndicatorPrimitive,
  SliderThumb as SliderThumbPrimitive,
  SliderTrack as SliderTrackPrimitive,
  SliderValue as SliderValuePrimitive,
} from "@ui-sdk/react/slider"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/*
 * Picks a number, or a range with two thumbs, along a scale.
 *
 * <Slider defaultValue={50}>
 *   <SliderValue />
 *   <SliderControl>
 *     <SliderTrack>
 *       <SliderIndicator />
 *       <SliderThumb aria-label="…" />
 *     </SliderTrack>
 *   </SliderControl>
 * </Slider>
 */

/** Holds the value. Name a single thumb with a `FieldLabel` around it, or `aria-label` on the thumb. */
function Slider({ className, ...props }: React.ComponentProps<typeof SliderPrimitive>) {
  return (
    <SliderPrimitive
      className={cnState(
        "flex w-full flex-col gap-sm data-disabled:cursor-not-allowed data-disabled:opacity-50",
        className,
      )}
      {...props}
    />
  )
}

/** The formatted value, or a range joined with an en dash. */
function SliderValue({ className, ...props }: React.ComponentProps<typeof SliderValuePrimitive>) {
  return (
    <SliderValuePrimitive
      className={cnState(textVariants({ size: "xs", weight: "bold", tabular: true }), className)}
      {...props}
    />
  )
}

/** Where presses land: it moves the nearest thumb there and starts a drag. */
function SliderControl({
  className,
  ...props
}: React.ComponentProps<typeof SliderControlPrimitive>) {
  return (
    <SliderControlPrimitive
      className={cnState("flex h-5 w-full cursor-pointer touch-none items-center", className)}
      {...props}
    />
  )
}

/** The rail the thumbs move along. */
function SliderTrack({ className, ...props }: React.ComponentProps<typeof SliderTrackPrimitive>) {
  return (
    <SliderTrackPrimitive
      className={cnState("relative h-1.5 w-full rounded-full bg-secondary", className)}
      {...props}
    />
  )
}

/** Fills the track up to the thumb, or between two. */
function SliderIndicator({
  className,
  ...props
}: React.ComponentProps<typeof SliderIndicatorPrimitive>) {
  return (
    <SliderIndicatorPrimitive
      className={cnState("h-full rounded-full bg-primary", className)}
      {...props}
    />
  )
}

/** What you drag. Its hidden range input takes focus, so the ring shows on the thumb. */
function SliderThumb({ className, ...props }: React.ComponentProps<typeof SliderThumbPrimitive>) {
  return (
    <SliderThumbPrimitive
      className={cnState(
        [
          "top-1/2 size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-primary bg-background shadow-sm",
          "transition-[box-shadow] duration-150 has-[:focus-visible]:ring-4 has-[:focus-visible]:ring-ring/30 data-dragging:ring-4 data-dragging:ring-ring/20",
        ].join(" "),
        className,
      )}
      {...props}
    />
  )
}

export { Slider, SliderControl, SliderIndicator, SliderThumb, SliderTrack, SliderValue }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Slider

Holds the value. Name a single thumb with a FieldLabel around it, or aria-label on the thumb.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: SliderState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: SliderState) => string)

controller

Moves the slider from anywhere. Create one with createSliderController().

Type { getValue: () => number | number[]; setValue(value: number | number[], options?: SliderChangeOptions): boolean; focus: (thumb?: number) => void; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

The value at first, when uncontrolled. A form reset returns to it.

Type number | number[] · Default min

disabled

Disables it like a native control: thumbs can't be focused or moved, and nothing is submitted.

Type boolean

form

The id of a form to belong to, when it isn't inside that form.

Type string

format

How values read, for screen readers and SliderValue: Intl.NumberFormat options, like { style: "currency", currency: "EUR" }, or a function of the value.

Type NumberFormat

largeStep

How far Page Up, Page Down and Shift with an arrow key move.

Type number · Default 10 steps

locale

The locale values are formatted in. Defaults to the page's lang, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client.

Type string

max

The highest value. When the steps from min don't land on it, the highest step below it is the end.

Type number · Default 100

min

The lowest value.

Type number · Default 0

minStepsBetweenThumbs

How many steps apart neighbouring thumbs stay. Thumbs never pass each other.

Type number · Default 0

name

The name it submits under: the number, or a range as "start/end". Without one, it submits nothing.

Type string

onValueChange

Runs while the value changes: on every step of a drag or a key. details.thumb says which thumb moved, and details.cancel() stops it.

Type (value: number | number[], details: SliderValueChangeDetails) => void

onValueCommit

Runs once an interaction that changed the value ends: the pointer lifts, the key comes up, or a screen reader adjusts it. For saving.

Type (value: number | number[], details: SliderValueCommitDetails) => void

orientation

The slider's axis. Vertical sliders grow upward.

Type "horizontal" | "vertical" · Default "horizontal"

readOnly

Keeps it focusable and submitted, but stops drags, keys and screen reader adjustments from changing it.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<SliderState>

required

Marks it required, with data-required. A slider always has a value, so it never blocks a form.

Type boolean

step

How far one step moves, from min.

Type number · Default 1

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: SliderState) => CSSProperties)

value

The value: a number for one thumb, or an array with one number per thumb, like [20, 80] for a range. Pass it to control the slider.

Type number | number[]

SliderControl

Where presses land: it moves the nearest thumb there and starts a drag.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<SliderState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<SliderState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<SliderState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<SliderState>) => CSSProperties)

SliderIndicator

Fills the track up to the thumb, or between two.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<SliderState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<SliderState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<SliderState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<SliderState>) => CSSProperties)

SliderThumb

What you drag. Its hidden range input takes focus, so the ring shows on the thumb.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: SliderThumbState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: SliderThumbState) => string)

index

Which value the thumb shows, from 0. Give each thumb of a range its own.

Type number · Default 0

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<SliderThumbState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: SliderThumbState) => CSSProperties)

SliderTrack

The rail the thumbs move along.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<SliderState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<SliderState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<SliderState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<SliderState>) => CSSProperties)

SliderValue

The formatted value, or a range joined with an en dash.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: SliderValueState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: SliderValueState) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<SliderValueState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: SliderValueState) => CSSProperties)