Command palette

@yasinsensoy/web

Web · Components

Slider

Slider for picking a value or a range of values.

4 exports

Live example

Preview

Try dragging, tapping the track or the ←/→, Shift, Home/End keys.

Code

import { SliderField } from "@yasinsensoy/web";

<SliderField label="Price range" name="price" max={1000} step={10} minStepsBetween={5} draggableRange
  defaultValue={[200, 600]}
  marks={[{ value: 0, label: "₺0" }, …]}
  format={{ style: "currency", currency: "TRY", maximumFractionDigits: 0 }}
  onValueCommit={search} />

Events

  1. Events appear here when you interact with the preview.

Volume: a single value, bubble on hover

Storage: marks only (1, 2, 4, 8, 16 GB)

Balance: fill in both directions from the middle (origin)

Working hours: three thumbs, value always visible

Vertical: equalizer

Controlled: catch the value with onValueCommit

Live: [25,75] · On release: [25,75]

Import

import { Slider } from "@yasinsensoy/web";

Slider

componentClient component

Slider for picking a value or a range of values. Each thumb contains a real <input type="range">; so it works out of the box with labels, forms, the keyboard and screen readers (including swipe up/down in iOS VoiceOver).

  • Single value or range: defaultValue={50} is a single thumb, defaultValue={[20, 80]} a range. Thumbs don't pass each other; minStepsBetween sets the minimum gap. More than two thumbs work too ([10, 40, 70]).
  • Mouse and touch: Clicking the track moves the nearest thumb there; grabbing a thumb drags it without jumping. For stacked thumbs the right one is chosen by drag direction. With draggableRange the fill in between can be grabbed to move the whole range.
  • Mobile: Swiping up/down over a horizontal slider scrolls the page and doesn't change the value; the value changes only when pulled sideways or tapped. Supported devices vibrate lightly when snapping to marks. The touch area is larger than the visual.
  • Keyboard: ←/→ (↑/↓) one step, with Shift or PageUp/PageDown a big step, Home/End to the ends. In right-to-left languages horizontal arrows work in reverse.
  • Format: With format it shows like $1,250, 40%, 16 GB; the bubble, mark labels and screen reader use the same text.
  • Form: With name each thumb's value joins FormData; on form reset it returns to the initial value.

To use it with a label, value display and error message see SliderField.

Slider({ value, defaultValue, onValueChange, onValueCommit, min, max, step, largeStep, minStepsBetween, marks, snapToMarks: onlyMarks, valueLabel, format, locale: localeProp, orientation, origin, draggableRange, tone, size, name, form, disabled, readOnly, thumbLabels, id, className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, onPointerDown, ...props }: SliderProps<V>): JSX.Element

Examples

<Slider aria-label="Volume" defaultValue={40} />

<Slider aria-label="Price" name="price" defaultValue={[500, 2500]} min={0} max={5000} step={50}
  minStepsBetween={4} format={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }} />

<Slider aria-label="Storage" defaultValue={4} min={1} max={16} snapToMarks
  marks={[1, 2, 4, 8, 16].map((v) => ({ value: v, label: `${v} GB` }))} />

<Slider aria-label="Balance" min={-50} max={50} origin={0} defaultValue={0} marks={[0]} />

Value of the slider: a number for a single thumb, a number array for a range.

type SliderValue = number | readonly number[]

SliderProps

interface

Props of the Slider component.

Extends: Omit<ComponentProps<"div">, "defaultValue" | "onChange" | "children">

NameTypeDefaultDescription
value?VValue in controlled use. A number draws a single thumb, an array draws a thumb for each element ([20, 80] → range selection). onValueChange returns the value in the same form it was given.
defaultValue?VminInitial value in uncontrolled use.
onValueChange?(value: V) => voidCalled whenever the value changes: on every step while dragging, with keys and when clicking the track.
onValueCommit?(value: V) => voidCalled with the final value when the user lets go (drag ends, a key is pressed). For expensive work like a server request or URL update.
min?number0Minimum value.
max?number100Maximum value.
step?number1Increment of the value.
largeStep?number10 times `step` (at most a tenth of the range)Step of Shift + arrow, PageUp and PageDown.
minStepsBetween?number0Minimum number of steps that must stay between thumbs in a range (2 and step={5} → at least 10 apart).
marks?boolean | readonly (number | SliderMark)[]Marks on the track. true puts a dot at every step (at most 100 steps). A number array draws only dots, a { value, label } array also draws labels below. Clicking a label goes to that value.
snapToMarks?booleanfalseThe value can only be one of the marks (e.g. [1, 2, 4, 8, 16] GB). Marks are spread evenly on the track (even if their values aren't proportional); arrows go to the next mark.
valueLabel?"auto" | "always" | "never""auto"Value bubble above the thumb: "auto" while dragging, on hover and on keyboard focus; "always" always; "never" never.
format?Intl.NumberFormatOptions | ((value: number) => ReactNode)How the value is written: Intl.NumberFormat options ({ style: "currency", currency: "TRY" }) or a function ((v) => \${v} GB\``). Used in the bubble, labels and screen reader.
locale?string{@link UIProvider}'s `localeLanguage of numbers (BCP 47).
orientation?"horizontal" | "vertical""horizontal"Direction. Vertically it increases from bottom to top.
origin?numberminValue where the fill starts for a single thumb. In ranges like -50 … 50, passing 0 makes the fill grow from the middle in both directions.
draggableRange?booleanfalseIn a range the fill between two thumbs can be grabbed and dragged; the whole range moves while keeping its width.
tone?Tone"primary"Color.
size?Size"md"Thickness and thumb size.
name?string | readonly string[]Name submitted with the form. In a range each thumb is submitted with the same name (formData.getAll("price") → ["100", "500"]) or a name per thumb is given (["minPrice", "maxPrice"]).
form?stringId of the form it belongs to (if used outside the form).
disabled?booleanDisabled.
readOnly?booleanThe value is visible but can't be changed; it is submitted with the form.
thumbLabels?readonly string[]Thumb names for screen readers. If omitted, a range uses "Başlangıç" / "Bitiş" (UIMessages).

A mark on the slider: a value and (optional) label.

type SliderMark = CoreSliderMark<ReactNode>
NameTypeDescription
valuenumberValue of the mark.
label?LabelLabel written under the mark (beside it when vertical). Only a dot is drawn when omitted.