Skip to content

Slider

A track with a thumb to drag. For a number where feeling «a bit more, a bit less» matters more than typing the exact figure. The value is always written beside it.

import { Slider } from '@sypnia/ui/react'

On this page
  1. A share
  2. With a unit, saved on release
  3. Disabled
  4. When to use it
  5. For everyone
  6. Props

A share

Drag the thumb, or focus it and use the arrows. The hint says what the value means.

25%

25 of every 100 sessions are recorded.

Code
import { useState } from 'react';
import { Field, Slider } from '@sypnia/ui/react';

export default function Basic() {
  const [rate, setRate] = useState(0.25);
  return (
    <Field label="Sample rate" hint={`${Math.round(rate * 100)} of every 100 sessions are recorded.`}>
      <Slider value={rate} onChange={setRate} max={1} step={0.05} formatOptions={{ style: 'percent' }} />
    </Field>
  );
}

With a unit, saved on release

The number follows the thumb; «Saved» appears only when it is let go.

40 GB

Saved: 40 GB. Uploads stop when the project reaches it.

Code
import { useState } from 'react';
import { Field, Slider } from '@sypnia/ui/react';

export default function Units() {
  const [saved, setSaved] = useState(40);
  return (
    <Field label="Storage limit" hint={`Saved: ${saved} GB. Uploads stop when the project reaches it.`}>
      <Slider defaultValue={40} min={10} max={200} step={10} formatOptions={{ style: 'unit', unit: 'gigabyte' }} onChangeEnd={setSaved} />
    </Field>
  );
}

Disabled

It keeps its value in sight and says why it cannot be moved.

1

Replicas come with the Pro plan.

Code
import { Field, Slider } from '@sypnia/ui/react';

export default function States() {
  return (
    <Field label="Read replicas" hint="Replicas come with the Pro plan.">
      <Slider disabled defaultValue={1} max={5} />
    </Field>
  );
}

When to use it

Use it when

  • The value is a matter of degree inside a known range: a sample rate, a volume, a zoom, a share.
  • The effect is seen at once while dragging. Save on onChangeEnd, when the person lets go.

Do not use it when

  • The exact figure matters or the range is huge: NumberInput.
  • There are only a handful of stops with names (low, medium, high): SegmentedControl or RadioGroup.
  • It shows progress and the person cannot change it: Progress or Meter.

For everyone

  • Left and right arrows move one step; Page Up and Page Down move more; Home and End go to the ends.
  • It is announced as a slider with its name, its value written with its unit, and its limits.
  • The thumb is the only thing that carries the light, and on paper it has an ink ring. Its touch area is 44 px even though it looks smaller.
  • A slider cannot be «wrong»: if a value is not allowed, set min, max and step so it cannot be reached.

Props

SliderTypeDefault
aria-describedby string
aria-label

The name for screen readers when there is no Field around it.

string
aria-labelledby string
className string
defaultValue number
disabled boolean
formatOptions

How the value is written beside the track, as in Intl.NumberFormat: `{ style: 'percent' }`, `{ style: 'unit', unit: 'gigabyte' }`.

NumberFormatOptions
id

Inside a Field it comes from the Field.

string
max number
min number
onChange

Called while the thumb moves.

((value: number) => void)
onChangeEnd

Called once, when the person lets go: the moment to save.

((value: number) => void)
step number
value number
  1. A share
  2. With a unit, saved on release
  3. Disabled
  4. When to use it
  5. For everyone
  6. Props

    Search pages, sections and terms