component
Slider
A number picked by dragging, when about right is right: a 4px track, a green fill and a 20px thumb. The arrow keys move it a step at a time.
Try it
Every variant
DefaultDisabledHover, press or tab to one to see the other 1 states.
import { Slider } from 'rost/components/slider/Slider'
<Slider label="Share set aside for tax" value={share} valueText={`${share}%`} onValueChange={setShare} /> src/components/slider/Slider.tsx
import { useRef, type HTMLAttributes, type KeyboardEvent, type PointerEvent } from 'react'
import './slider.css'
export interface SliderProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> {
/** Between `min` and `max`. */
readonly value: number
readonly min?: number
readonly max?: number
/** What one arrow press moves it by. */
readonly step?: number
/** What it sets. The thumb's name for screen readers. */
readonly label: string
/** The value in words, like "30%". */
readonly valueText?: string
readonly disabled?: boolean
readonly onValueChange?: (value: number) => void
}
const b = 'rost-slider'
const THUMB = 'var(--spacing-size-xsmall)'
/**
* A number picked by dragging. The thumb is the `role="slider"`; the arrow
* keys, Page Up and Down, Home and End move it. Built from
* contracts/slider.contract.yaml.
*/
export function Slider({ value, min = 0, max = 100, step = 1, label, valueText, disabled = false, onValueChange, className, ...rest }: SliderProps) {
const root = useRef<HTMLDivElement>(null)
const thumb = useRef<HTMLSpanElement>(null)
const span = max - min || 1
const share = Math.min(Math.max((value - min) / span, 0), 1)
const snap = (v: number) => {
const s = Math.round((v - min) / step) * step + min
return Math.min(Math.max(Number(s.toFixed(10)), min), max)
}
const set = (v: number) => { const n = snap(v); if (n !== value) onValueChange?.(n) }
const at = (e: PointerEvent) => {
const r = root.current!.getBoundingClientRect()
const t = thumb.current!.offsetWidth
return min + Math.min(Math.max((e.clientX - r.left - t / 2) / (r.width - t || 1), 0), 1) * span
}
const key = (e: KeyboardEvent) => {
if (disabled) return
const to = { ArrowRight: value + step, ArrowUp: value + step, ArrowLeft: value - step, ArrowDown: value - step, PageUp: value + step * 10, PageDown: value - step * 10, Home: min, End: max }[e.key]
if (to === undefined) return
e.preventDefault()
set(to)
}
return (
<div
ref={root}
className={[b, className].filter(Boolean).join(' ')}
onPointerDown={(e) => {
if (disabled || e.button !== 0) return
e.currentTarget.setPointerCapture(e.pointerId)
thumb.current!.focus()
set(at(e))
}}
onPointerMove={(e) => { if (!disabled && e.currentTarget.hasPointerCapture(e.pointerId)) set(at(e)) }}
{...rest}
>
<span className={`${b}__track`} />
<span className={`${b}__fill`} style={{ width: `calc(${share} * (100% - ${THUMB}) + ${THUMB} / 2)` }} />
<span
ref={thumb}
className={`${b}__thumb`}
role="slider"
tabIndex={disabled ? -1 : 0}
aria-label={label}
aria-valuenow={value}
aria-valuemin={min}
aria-valuemax={max}
aria-valuetext={valueText}
aria-disabled={disabled || undefined}
aria-orientation="horizontal"
style={{ left: `calc(${share} * (100% - ${THUMB}))` }}
onKeyDown={key}
>
<span className={`${b}__focus-ring`} />
</span>
</div>
)
}
The source for everything on this page: contracts/slider.contract.yaml (opens in a new tab). The CSS, the docs and the checks in each app are built from it.
# yaml-language-server: $schema=../schema/component-contract.schema.json
schemaVersion: 2
id: slider
name: Slider
kind: component
status: review
summary: "A number picked by dragging, when about right is right: a 4px track, a green fill and a 20px thumb. The arrow keys move it a step at a time."
category: inputs
related: [ progress, input, select ]
source:
tool: figma
fileKey: 1Zy0wiWKXQAvsfDBX3KR0d
nodeId: "748:25"
url: https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=748-25
extractedAt: "2026-11-17"
extractedBy: hand
frozenAt: "2026-11-17"
props:
value: { type: number, required: true, notes: "Between `min` and `max`. Controlled: pair it with `onValueChange`." }
min: { type: number, default: 0 }
max: { type: number, default: 100 }
step: { type: number, default: 1, notes: "What one arrow press moves it by. Page Up and Page Down move ten steps." }
label: { type: string, required: true, notes: "What it sets. The thumb's accessible name." }
valueText: { type: string, notes: "The value in words, when the number alone doesn't say it: 30%, 3 months." }
disabled: { type: boolean, default: false }
states: [ Default, Focus, Disabled ]
stateSelectors:
Focus: ":has(.rost-slider__thumb:focus-visible)"
Disabled: ":has(.rost-slider__thumb[aria-disabled=\"true\"])"
elements:
root:
notes: "The area you press and drag in: as wide as its parent, 24px tall, so the thumb has room and a press near the track still counts."
styles:
display: { value: block, assert: false }
position: { value: relative, assert: false }
height: { token: spacing/size/small, value: 24px }
touch-action: { value: none, assert: false }
cursor: { value: pointer, assert: false }
track:
notes: "Progress's 4px grey track, centred in the 24px."
styles:
position: { value: absolute, assert: false }
top: { value: "calc(50% - var(--spacing-gap-2xsmall) / 2)", untokenised: "Composed from a token: the middle of the root, less half the track." }
left: { value: "0", assert: false }
right: { value: "0", assert: false }
height: { token: spacing/gap/2xsmall, value: 4px }
border-radius: { token: spacing/radius/full, value: 10000px }
background: { token: slider/track, value: "#3a3a3a" }
fill:
notes: "From the start to the middle of the thumb, in the primary green. Its width is set from `value`."
styles:
position: { value: absolute, assert: false }
top: { value: "calc(50% - var(--spacing-gap-2xsmall) / 2)", untokenised: "Composed from a token: on the track." }
left: { value: "0", assert: false }
height: { token: spacing/gap/2xsmall, value: 4px }
border-radius: { token: spacing/radius/full, value: 10000px }
background: { token: slider/fill, value: "#00d669" }
thumb:
notes: "A 20px dot in the main text colour, centred on the track. It's the part that has the focus and the `role=\"slider\"`."
styles:
position: { value: absolute, assert: false }
top: { token: spacing/gap/3xsmall, value: 2px }
width: { token: spacing/size/xsmall, value: 20px }
height: { token: spacing/size/xsmall, value: 20px }
border-radius: { token: spacing/radius/full, value: 10000px }
background: { token: slider/thumb, value: "#ededed" }
outline: { value: none, assert: false }
focus-ring:
notes: "Checkbox's ring, 4px out from the thumb, round like it."
styles:
display: { value: none, assert: false }
position: { value: absolute, assert: false }
inset: { value: "calc(-1 * var(--spacing-gap-2xsmall))", untokenised: "Composed from a token: the ring sits spacing/gap/2xsmall outside the thumb." }
border-style: { value: solid, assert: false }
border-width: { token: spacing/outline/medium, value: 2px }
border-radius: { token: spacing/radius/full, value: 10000px }
pointer-events: { value: none, assert: false }
variants:
- when: { state: Default }
figmaNode: "748:12"
- when: { state: Focus }
figmaNode: "748:16"
elements:
focus-ring:
display: { value: block, assert: false }
border-color: { token: slider/focus-ring, value: "#999999" }
- when: { state: Disabled }
figmaNode: "748:21"
elements:
root:
opacity: { token: opacity/gentle, value: "0.24" }
cursor: { value: default, assert: false }
guidance:
status: proposed
use:
- title: Roughly how much
text: "A share to set aside, a zoom level, how many months to show: where nudging until it looks right beats typing."
show: state=Default
avoid:
- title: An exact amount
text: "A figure someone already knows, like an invoice total. That's an **Input**."
- title: A handful of options
text: "Three or four named choices aren't a range. That's a **ToggleGroup** or **Radios**."
behaviour: "Press anywhere along it and the thumb jumps there; drag to move it. The arrow keys move one step, Page Up and Page Down ten, Home and End to the ends. It calls `onValueChange` with the new number; show that number next to it."
accessibility:
element: "The thumb is `role=\"slider\"` with `aria-valuenow`, `aria-valuemin`, `aria-valuemax` and `aria-valuetext` when it's given. The track and fill are drawing."
name: "`label`, as `aria-label` on the thumb, or point `aria-labelledby` at a visible label."
colour: "The thumb is 17:1 on the dark page and the fill 10:1. The track is 1.8:1, a quiet edge: the fill and thumb carry the value, not the track."
content: "Write the value beside it, in its units. The label says what it sets: Share set aside for tax."
Install
npx github:Runceanu/rost add slider It lands in src/rost/slider/, with what it needs and its contract. Install has the rest.
When to use
Roughly how much
A share to set aside, a zoom level, how many months to show: where nudging until it looks right beats typing.
When not to use
An exact amount
A figure someone already knows, like an invoice total. That's an Input.
A handful of options
Three or four named choices aren't a range. That's a ToggleGroup or Radios.
Behaviour
Press anywhere along it and the thumb jumps there; drag to move it. The arrow keys move one step, Page Up and Page Down ten, Home and End to the ends. It calls onValueChange with the new number; show that number next to it.
Accessibility
- Element
The thumb is
role="slider"witharia-valuenow,aria-valuemin,aria-valuemaxandaria-valuetextwhen it's given. The track and fill are drawing. - Name
label, asaria-labelon the thumb, or pointaria-labelledbyat a visible label. - Colour
The thumb is 17:1 on the dark page and the fill 10:1. The track is 1.8:1, a quiet edge: the fill and thumb carry the value, not the track.
Content
Write the value beside it, in its units. The label says what it sets: Share set aside for tax.
Props
| Prop | Type | Values | Default |
|---|---|---|---|
value | number | | |
min | number | | 0 |
max | number | | 100 |
step | number | | 1 |
label | string | | |
valueText | string | | |
disabled | boolean | | false |
States: Default, Focus, Disabled.
Values
Straight from the contract. Each value sits next to its token, because six tokens here all come out as #3a3a3a and the colour alone can't tell you which one is right.
Never changes
root
-
height24pxspacing/size/small
displayblock · positionrelative · touch-actionnone · cursorpointer
track
-
topcalc(50% - var(--spacing-gap-2xsmall) / 2)on purpose: Composed from a token: the middle of the root, less half the track. -
height4pxspacing/gap/2xsmall -
border-radius10000pxspacing/radius/full -
background#3a3a3aslider/track
positionabsolute · left0 · right0
fill
-
topcalc(50% - var(--spacing-gap-2xsmall) / 2)on purpose: Composed from a token: on the track. -
height4pxspacing/gap/2xsmall -
border-radius10000pxspacing/radius/full -
background#00d669slider/fill
positionabsolute · left0
thumb
-
top2pxspacing/gap/3xsmall -
width20pxspacing/size/xsmall -
height20pxspacing/size/xsmall -
border-radius10000pxspacing/radius/full -
background#edededslider/thumb
positionabsolute · outlinenone
focus-ring
-
border-width2pxspacing/outline/medium -
border-radius10000pxspacing/radius/full
displaynone · positionabsolute · insetcalc(-1 * var(--spacing-gap-2xsmall)) · border-stylesolid · pointer-eventsnone
Only in some variants
These don't change: they're just set in some variants and not others.
Focus
-
focus-ring.border-color#999999slider/focus-ring -
focus-ring.displayblocklayout
Disabled
-
root.cursordefaultFigma frame size -
root.opacity0.24opacity/gentle