Components / Forms

Slider20

Pick a value, or a range, by dragging. In Sticker the thumb has weight: it lags behind, stretches and sways as you pull it. Retro hops from step to step, Comic leaves speed lines and Arcade lights up a row of LEDs.

Slider · PlaygroundLive
Volume
valueBubble
JSX
<Slider label="Volume" defaultValue={40} />

Usage

TSX
import { Slider } from "plonk-ui";

export function Volume() {
  return <Slider label="Volume" defaultValue={40} />;
}

onChange fires on every move, also while dragging. onChangeEnd fires once the drag ends, and on every keyboard step: use it to save or to search.

Ranges

Pass [start, end] as the value and the slider gets two thumbs. minGap keeps them apart, like a price range at least $100 wide. formatOptions formats the bubbles, the marks and what screen readers hear with Intl.NumberFormat, in the provider's locale.

Slider · Price rangeLive
Price
Searching $200 – $800
import { useState } from "react";
import { Slider } from "plonk-ui";

const PRICE = {
  style: "currency",
  currency: "USD",
  maximumFractionDigits: 0,
} as const;

export default function Range() {
  const [searched, setSearched] = useState<[number, number]>([200, 800]);

  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <Slider
        label="Price"
        defaultValue={[200, 800]}
        min={0}
        max={1000}
        step={50}
        minGap={100}
        formatOptions={PRICE}
        thumbLabels={["Minimum price", "Maximum price"]}
        onChangeEnd={setSearched}
      />
      <span className="font-bold">
        Searching {searched.map((v) => `$${v}`).join(" – ")}
      </span>
    </div>
  );
}

When the thumbs get so close that their bubbles would overlap, the bubbles merge into one ("$650 – $800") between the thumbs, and split again as the thumbs move apart.

Name the two thumbs for screen readers with thumbLabels, in your app's language.

Marks and custom text

marks draws ticks under the track, as numbers or { value, label }. formatValue writes the text yourself, for steps that aren't numbers. valueBubble="active" shows the bubble only while the slider is used.

Slider · MarksLive
Noise reduction
TSX
import { Slider } from "plonk-ui";

const LEVELS = ["Off", "Low", "Medium", "High"];

export default function Marks() {
  return (
    <Slider
      className="w-full max-w-xs"
      label="Noise reduction"
      defaultValue={2}
      min={0}
      max={3}
      formatValue={(value) => LEVELS[value] ?? ""}
      marks={[0, 1, 2, 3]}
      valueBubble="active"
    />
  );
}

In a form

With a name, the value goes into a hidden input. A range sends both values under the same name; read them with getAll.

Slider · FormLive
Volume
Working hours
import { useState, type FormEvent } from "react";
import { Button, Slider } from "plonk-ui";

export default function Form() {
  const [sent, setSent] = useState<string>();

  function handleSubmit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    setSent(
      JSON.stringify({
        volume: data.get("volume"),
        hours: data.getAll("hours"),
      }),
    );
  }

  return (
    <form
      onSubmit={handleSubmit}
      className="flex w-full max-w-xs flex-col gap-6"
    >
      <Slider
        label="Volume"
        name="volume"
        defaultValue={60}
        valueBubble="active"
      />
      <Slider
        label="Working hours"
        name="hours"
        defaultValue={[9, 17]}
        min={0}
        max={24}
        minGap={1}
        formatValue={(h) => `${String(h).padStart(2, "0")}:00`}
      />
      <Button type="submit" className="self-start">
        Save
      </Button>
      <code className="empty:hidden">{sent}</code>
    </form>
  );
}

Personalities

  • Sticker: the thumb lags a little behind the pointer, stretches in the direction you pull and sways when you let go, then lands with a wobble.
  • Retro: the thumb hops from step to step, one frame each.
  • Comic: a red ball that leaves speed lines when moved fast, a halftone fill and a speech bubble for the value.
  • Arcade: the track is a row of LED blocks, the thumb a joystick ball, and the bubble a scoreboard that flashes on every step.

With reduced motion the thumb follows the pointer exactly, without lag or stretch.

Accessibility

  • Every thumb is a slider with its value, minimum and maximum; the text read is the formatted value ("$200").
  • ← ↓ and → ↑ move one step, Page Up and Page Down a tenth of the range, Home and End jump to the ends. In right-to-left languages, ← and → are reversed.
  • In a range, each thumb's limits follow the other thumb and minGap, and screen readers hear those limits too.
  • The focus ring shows for keyboard users only; dragging with a mouse doesn't leave one behind.

API

PropTypeDefaultDescription
valueSliderValue—Controlled value: a number, or [start, end] for a range with two thumbs.
defaultValueSliderValue—Value on first render, when uncontrolled.
onChange(value: T) => void—Called on every change, also while dragging.
onChangeEnd(value: T) => void—Called when a drag ends, and on every keyboard step. Use it to save or to search.
minnumber0Smallest value.
maxnumber100Largest value.
stepnumber1Distance between values.
minGapnumber0In a range, the smallest distance the two thumbs keep, like a price range at least $100 wide. A multiple of step.
formatOptionsNumberFormatOptions—Intl.NumberFormat options for the bubble, the marks and screen readers, like { style: "currency", currency: "USD" }. Uses the provider's locale.
formatValue(value: number) => string—Write the text yourself: (value) => ["Off", "Low", "High"][value]. Wins over formatOptions.
marks(number | SliderMark)[]—Ticks under the track: numbers, or { value, label }.
valueBubble"always" | "active" | "never""always"The value above the thumb: always, only while it's used, or never.
thumbLabels[string, string]["Minimum", "Maximum"]Screen reader names of the two thumbs in a range. Translate them for your app.
namestring—Field name: the value goes into a hidden input; a range sends two values under the same name.
isDisabledbooleanfalseDisables the slider.
aria-labelstring—Accessible name. Required when there's no label.
classNamestring—Extra classes for the outer wrapper.
labelReactNode—Visible label, linked to the slider.
descriptionReactNode—Help text under the slider.