组件

Slider

Pick a number, or two, by dragging a paper knob along a hand-ruled groove. The range is one marker pass drawn full length and revealed by a CSS clip, so nothing is redrawn while you drag. Ticks, a highlighter or blue-felt range, vertical, and range sliders. Radix Slider underneath.

安装

terminal
npx shadcn@latest add @wobko/slider

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Default

Drag the knob, or focus it and use the arrow keys, Home, End and Page Up / Down. The marker range follows the value; nothing is redrawn while you drag.

Range, ticks and tones

Two values make a range slider. `ticks` hangs pencil marks under the groove (one per step, or a number of intervals); `tone` swaps the marker for a highlighter or blue felt.

Vertical

A mixing desk: `orientation="vertical"` stands the groove up; the range grows from the bottom. The last fader is disabled.

Brush settings

Three controlled sliders in a panel, each with its own readout, and a reset that puts every knob back where it started. The values are the only state; the drawings do not change.

Demo

Official `slider-demo`: a single value from 0 to 100 at 60% width, written as a component that forwards its props (so a caller can pass `defaultValue`, `max`, `step` or `className`). The marker range is drawn once and clipped to the value; the only addition is an `aria-label`, which the knob needs.

API 参考

Slider

Radix Slider.Root with the track, range and one thumb per value already inside. Give it one value for a plain slider, two or more for a range slider. The three drawings are separate hosts, and the range host is clipped to the values (the drawing never sees them).

属性类型默认值说明
value / defaultValue / onValueChange / onValueCommitnumber[]-Controlled or uncontrolled values. The number of values is the number of thumbs. Without either, one thumb at `min`.
min / max / stepnumber0 / 100 / 1The span and its granularity. Thumbs snap to `step`.
orientation"horizontal" | "vertical""horizontal"Vertical grows from the bottom (from the top when `inverted`).
minStepsBetweenThumbsnumber0Range sliders: keep the thumbs this many steps apart.
ticksboolean | number-Pencil tick marks hanging under the groove: `true` marks every step when there are 20 or fewer, else ten intervals; a number is that many intervals. The end marks are longer.
getValueText(value: number, index: number) => string-aria-valuetext of each thumb: what a screen reader says instead of the bare number ("12 px", "72 %", "Soft"). Use it whenever you show the value with a unit next to the slider.
tone"marker" | "hi" | "blue""marker"What colors the range: terracotta marker, highlighter or blue felt.
inverted / dirboolean / "ltr" | "rtl"-Reverse the direction the values grow in; right-to-left starts at the right.
disabledboolean-Dashed pencil groove, graphite range and a dashed knob. Not focusable.
name / formstring-Names the hidden inputs Radix adds inside a form (one per thumb, `name[]` for a range).
aria-label / aria-labelledby / aria-describedbystring-Names and describes the slider: on the thumb for one value, on a role=group Root for several (thumbs keep Radix's Minimum / Maximum).
aria-invalidboolean | "true" | "false"-On every thumb (that is where the role=slider is), and the groove's two pulls and the knob's loop are drawn in red pencil. FormControl sets it for you.
seedstring | number-Pins the hand: the same seed always draws the same slider. Default: derived from useId().

无障碍

  • Each thumb is a role="slider" with aria-valuemin, aria-valuemax, aria-valuenow and aria-orientation. Arrow keys move by one step, Page Up / Page Down and Shift+Arrows by ten, Home and End go to the ends (Radix; the arrows follow the reading direction in RTL).
  • Name it. With one value put aria-label or aria-labelledby on the Slider and it lands on the thumb (the Root is a plain span, so a name there would name nothing). With several values the Root becomes a role=group carrying that name, and the thumbs keep Radix's "Minimum" and "Maximum".
  • Pointer: dragging the thumb or pressing anywhere on the groove works, on touch too (touch-action: none on the slider); the thumb's hit area is larger than the drawing.
  • The drawing is aria-hidden, pointer-events: none and never focusable. Keyboard focus draws a blue felt ring around the knob. Windows high contrast replaces the ink by real boxes (a bordered groove, a Highlight range, a solid knob).
  • The value is not shown by the slider: pair it with an <output> or text next to it, as the examples do.
  • In a react-hook-form Form, FormLabel's htmlFor cannot name a slider (a span with role=slider is not a labelable element): give the FormLabel an `id` and the Slider `aria-labelledby` with it, as the Form "Every control" example does. FormControl's aria-describedby and aria-invalid do reach the thumb.
  • A non-finite value (NaN) is held at `min`. Form reset: a name'd slider goes back to its defaultValue and the range follows.

源码

CLI 会复制进你项目的文件。

正在加载源码