Slider
Pick a value by dragging. All built on the native range input, so keyboards and screen readers work out of the box.
Standard
Pick a value in a range. Built on the native range input, with the value always visible beside the label.
Installation
$npx shadcn@latest add @stillui/sliderOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/slider
import { AnimatePresence, motion, useReducedMotion, useSpring, useTransform } from "motion/react";
import { useEffect, useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const PRESS = { type: "spring", stiffness: 600, damping: 32, mass: 0.6 } as const;
// Damped past critical: the thumb glides to a clicked spot and tracks a drag without ever rebounding.
const GLIDE = { stiffness: 700, damping: 50, mass: 0.5 };
type SliderProps = {
label: ReactNode;
value?: number;
defaultValue?: number;
onValueChange?: (value: number) => void;
min?: number;
max?: number;
step?: number;
/** Formats the number shown next to the label and in the bubble. */
format?: (value: number) => string;
disabled?: boolean;
className?: string;
};
export function Slider({
label,
value,
defaultValue = 50,
onValueChange,
min = 0,
max = 100,
step = 1,
format = String,
disabled,
className,
}: SliderProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState(defaultValue);
const [active, setActive] = useState(false);
const current = value ?? internal;
const percent = ((current - min) / (max - min)) * 100;
// Thumb and fill read a spring that follows the value, so a click far along the track glides there.
const glide = useSpring(percent, GLIDE);
useEffect(() => {
if (reduce) glide.jump(percent);
else glide.set(percent);
}, [percent, reduce, glide]);
const fillWidth = useTransform(glide, (p) => `${p}%`);
const thumbLeft = useTransform(glide, (p) => `${p}%`);
const change = (next: number) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div className={cn("grid gap-3", disabled && "opacity-50", className)}>
<div className="flex items-baseline justify-between gap-4">
<label htmlFor={id} className="text-sm font-medium">
{label}
</label>
<span className="text-sm tabular-nums text-muted-foreground">{format(current)}</span>
</div>
<div className="relative h-5">
{/* Visuals. The native range input on top handles pointer, keyboard and screen readers. */}
<div className="absolute inset-x-0 top-1/2 h-1.5 -translate-y-1/2 overflow-hidden rounded-full bg-secondary">
<motion.div className="h-full rounded-full bg-primary" style={{ width: fillWidth }} />
</div>
<motion.div
className="pointer-events-none absolute top-1/2 size-5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-raised ring-1 ring-black/5 dark:bg-foreground"
style={{ left: thumbLeft }}
animate={{ scale: active && !reduce ? 1.15 : 1 }}
transition={PRESS}
>
<AnimatePresence>
{active ? (
<motion.span
className="absolute bottom-full left-1/2 mb-2 -translate-x-1/2 rounded-md bg-primary px-2 py-1 text-[12px] font-medium tabular-nums text-primary-foreground shadow-overlay"
initial={{ opacity: 0, y: reduce ? 0 : 4 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, transition: { duration: 0.1 } }}
transition={{ duration: 0.14, ease: [0.16, 1, 0.3, 1] }}
aria-hidden
>
{format(current)}
</motion.span>
) : null}
</AnimatePresence>
</motion.div>
<input
id={id}
type="range"
min={min}
max={max}
step={step}
value={current}
disabled={disabled}
aria-valuetext={format(current)}
onChange={(e) => change(Number(e.target.value))}
onPointerDown={() => setActive(true)}
onPointerUp={() => setActive(false)}
onPointerCancel={() => setActive(false)}
onBlur={() => setActive(false)}
className="peer absolute inset-0 size-full cursor-pointer opacity-0 disabled:cursor-not-allowed"
/>
{/* Keyboard focus ring drawn around the track, since the input itself is invisible. */}
<div className="pointer-events-none absolute -inset-1.5 rounded-full peer-focus-visible:outline-2 peer-focus-visible:outline-ring" />
</div>
</div>
);
}States
- Resting
- Formatted value sits next to the label in tabular numbers, so it doesn’t wobble.
- Dragging
- Thumb grows slightly and a bubble shows the value above your finger.
- Keyboard
- Arrow keys, Page Up/Down, Home and End all work natively. Focus ring around the track.
- Screen readers
- Announces the formatted value (e.g. “$1,200”), not the raw number.
- Disabled
- Dims and stops responding.
Notes
If people need an exact number, pair the slider with a text field. Sliders are for “roughly this much”.
Stepped
A slider that snaps to named stops like Off, Weekly, Daily. Each stop has a dot and a label you can click.
A short summary once a week.
Installation
$npx shadcn@latest add @stillui/slider-steppedOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/slider#stepped
import { motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const SETTLE = { type: "spring", stiffness: 520, damping: 34 } as const;
type SliderSteppedProps = {
label: ReactNode;
/** The named stops, from lowest to highest. */
steps: string[];
value?: number;
defaultValue?: number;
onValueChange?: (index: number) => void;
/** One line under the slider describing the current step. */
describe?: (index: number) => ReactNode;
className?: string;
};
/** A slider that snaps to named stops, like Off, Low, Medium, High. Each stop is labelled under its dot. */
export function SliderStepped({ label, steps, value, defaultValue = 0, onValueChange, describe, className }: SliderSteppedProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState(defaultValue);
const current = value ?? internal;
const last = steps.length - 1;
const percent = (current / last) * 100;
const change = (next: number) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div className={cn("grid gap-3", className)}>
<div className="flex items-baseline justify-between gap-4">
<label htmlFor={id} className="text-sm font-medium">
{label}
</label>
<span className="text-sm text-muted-foreground">{steps[current]}</span>
</div>
<div className="relative mx-2.5 h-5">
<div className="absolute inset-x-0 top-1/2 h-1 -translate-y-1/2 rounded-full bg-secondary" />
<motion.div
className="absolute left-0 top-1/2 h-1 -translate-y-1/2 rounded-full bg-primary"
initial={false}
animate={{ width: `${percent}%` }}
transition={reduce ? { duration: 0 } : SETTLE}
/>
{steps.map((step, i) => (
<span
key={step}
aria-hidden
className={cn(
"absolute top-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full transition-colors duration-200",
i <= current ? "bg-primary" : "bg-input",
)}
style={{ left: `${(i / last) * 100}%` }}
/>
))}
<motion.div
aria-hidden
className="pointer-events-none absolute top-1/2 size-5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-raised ring-1 ring-black/5 dark:bg-foreground"
initial={false}
animate={{ left: `${percent}%` }}
transition={reduce ? { duration: 0 } : SETTLE}
/>
<input
id={id}
type="range"
min={0}
max={last}
step={1}
value={current}
aria-valuetext={steps[current]}
onChange={(e) => change(Number(e.target.value))}
className="peer absolute -inset-x-2.5 inset-y-0 h-full w-[calc(100%+1.25rem)] cursor-pointer opacity-0"
/>
<div className="pointer-events-none absolute -inset-x-4 -inset-y-1.5 rounded-full peer-focus-visible:outline-2 peer-focus-visible:outline-ring" />
</div>
{/* Stop names, each centered under its dot; the ends align to the edges so they never overflow. */}
<div className="relative mx-2.5 h-5 text-[12px]" aria-hidden>
{steps.map((step, i) => (
<button
key={step}
type="button"
tabIndex={-1}
onClick={() => change(i)}
className={cn(
"absolute top-0 whitespace-nowrap transition-colors duration-200",
i === 0 ? "-translate-x-2.5" : i === last ? "-translate-x-[calc(100%-0.625rem)]" : "-translate-x-1/2",
i === current ? "font-medium text-foreground" : "text-muted-foreground hover:text-foreground",
)}
style={{ left: `${(i / last) * 100}%` }}
>
{step}
</button>
))}
</div>
{describe ? <p className="text-[13px] leading-5 text-muted-foreground">{describe(current)}</p> : null}
</div>
);
}States
- Resting
- Dots fill up to the current stop; its name is bold below and repeated beside the label.
- Snapping
- The thumb and fill settle on the next stop with a short spring.
- Description
- An optional line under the slider explains what the current stop means.
- Shortcuts
- Clicking a stop’s name jumps straight to it.
- Keyboard
- A native range input, so arrows, Home and End work. The stop name is read out, not its number.
- Reduced motion
- The thumb jumps between stops.
Notes
Use it for three to six ordered choices. For unordered choices use a segmented control.
Range
Pick a band, such as a price range, with two thumbs on one track. Built on two native range inputs.
Installation
$npx shadcn@latest add @stillui/slider-rangeOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/slider#range
import { motion, useReducedMotion, useSpring, useTransform, type MotionValue } from "motion/react";
import { useEffect, useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
type Range = [number, number];
// Damped past critical: positions glide to a clicked spot and track a drag without ever rebounding.
const GLIDE = { stiffness: 700, damping: 50, mass: 0.5 };
const PRESS = { type: "spring", stiffness: 600, damping: 32, mass: 0.6 } as const;
/** A spring that follows a percentage, or jumps to it when motion is reduced. */
function useGlide(percent: number, reduce: boolean): MotionValue<number> {
const glide = useSpring(percent, GLIDE);
useEffect(() => {
if (reduce) glide.jump(percent);
else glide.set(percent);
}, [percent, reduce, glide]);
return glide;
}
type SliderRangeProps = {
label: ReactNode;
value?: Range;
defaultValue?: Range;
onValueChange?: (value: Range) => void;
min?: number;
max?: number;
step?: number;
/** The smallest gap allowed between the two ends. */
minGap?: number;
format?: (value: number) => string;
className?: string;
};
// Native inputs stacked on one track. Only their thumbs catch the pointer, so each end can be grabbed.
const input = cn(
"pointer-events-none absolute inset-0 h-full w-full appearance-none bg-transparent opacity-0 focus-visible:opacity-0",
"[&::-webkit-slider-thumb]:pointer-events-auto [&::-webkit-slider-thumb]:size-6 [&::-webkit-slider-thumb]:cursor-grab [&::-webkit-slider-thumb]:appearance-none",
"[&::-moz-range-thumb]:pointer-events-auto [&::-moz-range-thumb]:size-6 [&::-moz-range-thumb]:cursor-grab [&::-moz-range-thumb]:border-0",
);
/** Pick a range, such as a price band, with two thumbs on one track. */
export function SliderRange({
label,
value,
defaultValue = [20, 80],
onValueChange,
min = 0,
max = 100,
step = 1,
minGap = 0,
format = String,
className,
}: SliderRangeProps) {
const id = useId();
const reduce = useReducedMotion() ?? false;
const [internal, setInternal] = useState<Range>(defaultValue);
const [held, setHeld] = useState<0 | 1 | null>(null);
const [low, high] = value ?? internal;
const [focused, setFocused] = useState<0 | 1 | null>(null);
const percent = (n: number) => ((n - min) / (max - min)) * 100;
// Both ends and the fill between them glide, so keyboard steps and quick drags move smoothly.
const lowGlide = useGlide(percent(low), reduce);
const highGlide = useGlide(percent(high), reduce);
const fillLeft = useTransform(lowGlide, (p) => `${p}%`);
const fillRight = useTransform(highGlide, (p) => `${100 - p}%`);
const thumbLeft = [useTransform(lowGlide, (p) => `${p}%`), useTransform(highGlide, (p) => `${p}%`)];
const change = (next: Range) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div role="group" aria-labelledby={id} className={cn("grid gap-3", className)}>
<div className="flex items-baseline justify-between gap-4">
<span id={id} className="text-sm font-medium">
{label}
</span>
<span className="text-sm tabular-nums text-muted-foreground">
{format(low)} – {format(high)}
</span>
</div>
<div className="relative mx-2.5 h-5">
<div className="absolute inset-x-0 top-1/2 h-1.5 -translate-y-1/2 rounded-full bg-secondary" />
<motion.div className="absolute top-1/2 h-1.5 -translate-y-1/2 rounded-full bg-primary" style={{ left: fillLeft, right: fillRight }} />
{[0, 1].map((i) => (
<motion.div
key={i}
aria-hidden
className={cn(
"pointer-events-none absolute top-1/2 size-5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-white shadow-raised ring-1 ring-black/5 dark:bg-foreground",
focused === i && "outline-2 outline-offset-2 outline-ring",
)}
style={{ left: thumbLeft[i] }}
animate={{ scale: held === i && !reduce ? 1.15 : 1 }}
transition={PRESS}
/>
))}
<div className="absolute -inset-x-2.5 inset-y-0">
<input
type="range"
aria-label="Minimum"
aria-valuetext={format(low)}
min={min}
max={max}
step={step}
value={low}
onChange={(e) => change([Math.min(Number(e.target.value), high - minGap), high])}
onFocus={(e) => e.currentTarget.matches(":focus-visible") && setFocused(0)}
onBlur={() => setFocused(null)}
onPointerDown={() => setHeld(0)}
onPointerUp={() => setHeld(null)}
onPointerCancel={() => setHeld(null)}
className={cn(input, low > max - (max - min) / 10 && "z-10")}
/>
<input
type="range"
aria-label="Maximum"
aria-valuetext={format(high)}
min={min}
max={max}
step={step}
value={high}
onChange={(e) => change([low, Math.max(Number(e.target.value), low + minGap)])}
onFocus={(e) => e.currentTarget.matches(":focus-visible") && setFocused(1)}
onBlur={() => setFocused(null)}
onPointerDown={() => setHeld(1)}
onPointerUp={() => setHeld(null)}
onPointerCancel={() => setHeld(null)}
className={input}
/>
</div>
</div>
</div>
);
}States
- Resting
- The fill spans between the thumbs; both ends are written beside the label.
- Dragging
- Each thumb can be grabbed on its own. They can’t cross, and stay minGap apart.
- Stacked at the top
- When the low thumb is pushed to the far end it moves on top, so it can still be grabbed.
- Keyboard
- Two tab stops, named Minimum and Maximum, each with arrows, Home and End.
- Focus
- The focused thumb gets the focus ring, since the inputs themselves are invisible.
Notes
No animation library needed. The thumbs follow the pointer directly, which feels best for precise picks.
Ruler
A ruler of ticks under a large readout. Ticks up to the value darken, so the amount reads at a glance.
Installation
$npx shadcn@latest add @stillui-pro/slider-rulerA paid component. The command needs the token from your account in STILLUI_TOKEN; your account page shows the one-time setup.
This is a paid component. Its source and install come with access.
States
- Resting
- A big number with its unit, and a marker standing on the ruler.
- Dragging
- The marker stretches slightly and ticks darken or lighten as it passes.
- Major ticks
- Every fifth tick is taller, like a real ruler, to help estimate without reading.
- Keyboard
- A native range input, so arrows, Page Up/Down, Home and End all work.
- Screen readers
- The value is read with its unit, e.g. “25 min”.
- Reduced motion
- The marker doesn’t stretch.
Notes
Keep the range under about a hundred steps so ticks stay at least a few pixels apart.