Ir al contenido
Español

Cómo funciona

Position comes from one CSS variable, --_p (0 to 1): the runner is translated by --_p of the travel distance and the fill is scaled by it, so no pixels are computed and width changes cannot misalign it. While dragging, pointer events write the variable directly, so the thumb is continuous even with a large step and slides to the nearest step on release. On touch, a press on the track only counts once the finger drags sideways or lifts.

Úsalo para

  • Picking an approximate value in a range where the process matters more than precision: duration, price cap, volume, zoom.
  • When the interface should update live as you adjust, such as an estimated cost or a result count.
  • On mobile, instead of a number field, to avoid opening the keyboard.

Busca otra opción cuando

  • Large ranges that need exact values, such as 1 to 10,000: use a number field, or a field with a slider.
  • Only 2 to 4 discrete options: a segmented control is more direct.
  • Choosing both a lower and an upper bound: this component has one thumb.
  • A one-off confirmation: use a slide-to-confirm control.

Uso

import { useState } from 'react'
import { RangeSlider } from './RangeSlider.jsx'

export function ParkingTime() {
  const [hours, setHours] = useState(3)
  return (
    <RangeSlider
      aria-label="Parking time"
      min={2}
      max={12}
      step={1}
      value={hours}
      onChange={setHours} // update the UI while dragging
      onChangeEnd={(v) => api.quote(v)} // request a quote after release
      formatValue={(v) => `${v} hours`}
      marks={[2, 4, 6, 8, 10, 12].map((v) => ({ value: v, label: String(v) }))}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
valuenumber—Controlled current value.
defaultValuenumberminInitial value when uncontrolled.
onChange(value) => void—Called when the value changes: once per step crossed while dragging, once per key press.
onChangeEnd(value) => void—Called when an interaction ends (release, or after each key press) if the value changed. A good place to send a request.
minnumber0Minimum value.
maxnumber100Maximum value.
stepnumber1Step size, decimals allowed. max can be reached even when it is off the step grid.
disabledbooleanfalseDimmed to half opacity, not draggable, not focusable.
formatValue(value) => string—Formats the value as text for the bubble and screen readers (aria-valuetext), such as (v) => `${v} hours`.
marksboolean | Array<number | { value: number, label?: ReactNode }>falseMarks: true draws one per step (none beyond 40 steps); an array places them, and entries with a label show text under the track.
showBubblebooleantrueShows the value in an ink pill with a caret above the thumb while dragging or keyboard-focused.
namestring—Form field name. When set, a hidden input is rendered so the value is submitted with the form.
aria-label / aria-labelledbystring—Accessible name of the slider. One of them is required; it goes on the role="slider" thumb.
...restHTMLAttributes<HTMLDivElement>—Other attributes, such as className and style, go to the root element.

Accesibilidad

  • The thumb is role="slider" with aria-valuemin, max and now; with formatValue, aria-valuetext reads as text such as “3 hours”.
  • Keyboard: Left and Down subtract a step, Right and Up add one, PageDown and PageUp move 10 steps, Home and End go to the minimum and maximum.
  • Name the slider with aria-label or aria-labelledby.
  • The hit area is 44px high and the thumb's touch area 44×44; touch-action: pan-y keeps vertical page scrolling.
  • Reduced motion makes jumps and snapping instant; dragging still follows 1:1.

Movimiento

No transition while dragging. Track-click jumps, key steps and release snapping take 240ms ease-out. On press the thumb grows to 1.15 with a 240ms spring, lifts on a deeper shadow and gains a 7px accent halo. The value pill fades in over 160ms and rises in 240ms with a slight spring.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

RangeSlider.jsxrange-slider.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.