仕組み
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.
向いている場面
- 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.
向かない場面
- 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.
使い方
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) }))}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
value | number | — | Controlled current value. |
defaultValue | number | min | Initial 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. |
min | number | 0 | Minimum value. |
max | number | 100 | Maximum value. |
step | number | 1 | Step size, decimals allowed. max can be reached even when it is off the step grid. |
disabled | boolean | false | Dimmed 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`. |
marks | boolean | Array<number | { value: number, label?: ReactNode }> | false | Marks: true draws one per step (none beyond 40 steps); an array places them, and entries with a label show text under the track. |
showBubble | boolean | true | Shows the value in an ink pill with a caret above the thumb while dragging or keyboard-focused. |
name | string | — | Form field name. When set, a hidden input is rendered so the value is submitted with the form. |
aria-label / aria-labelledby | string | — | Accessible name of the slider. One of them is required; it goes on the role="slider" thumb. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes, such as className and style, go to the root element. |
アクセシビリティ
- 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.
モーション
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.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
RangeSlider.jsxrange-slider.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





