Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 36 ยท Inputs and Forms

Slider, Rating and Toggle Buttons

Capture numbers and preferences visually with Material UI: Slider with marks and ranges, Rating stars, and ToggleButtonGroup in exclusive or multiple mode.

Not every input is text. Sometimes the best control is a slider for a volume or price range, stars for a review, or a set of toggle buttons for a view mode. These MUI components cover those cases, and they all follow the same pattern: a value, an onChange handler and accessibility built in.

Slider

A Slider lets users pick a number (or a range) by dragging a thumb or using the arrow keys. It is controlled with value and onChange(event, newValue):

JSX
import Slider from '@mui/material/Slider';

const [volume, setVolume] = useState(30);

<Slider
  value={volume}
  onChange={(event, newValue) => setVolume(newValue)}
  min={0}
  max={100}
  step={5}
  valueLabelDisplay="auto"
  aria-label="Volume"
/>

Note the handler signature: the second argument is the value (not event.target.value). Important props:

Prop Purpose
min, max, step Range and granularity (step={null} limits to marks).
marks true shows a tick per step; or an array [{ value: 0, label: '0ยฐC' }].
valueLabelDisplay auto (on hover/drag), on, off.
valueLabelFormat Format the bubble, e.g. (v) => v + '%'.
getAriaValueText Text for screen readers ("20 degrees Celsius").
orientation horizontal or vertical.
track normal, inverted or false.
disabled, size, color As on other components.

Range slider

Give the slider an array value to get two thumbs:

JSX
const [price, setPrice] = useState([20, 70]);

<Slider
  value={price}
  onChange={(e, v) => setPrice(v)}
  valueLabelDisplay="auto"
  disableSwap
/>

disableSwap stops thumbs from crossing. The onChange event fires on every move; for expensive work (filtering a long list from the server) use onChangeCommitted, which fires when the user releases the thumb.

Stand-in: a range slider

The first stand-in emulates a two-thumb range using two native range inputs layered together, and applies the same "committed" idea: the list filters live, while the server call (simulated by a counter) runs only on release.

Stand-in: price range slider with onChangeCommitted

Rating

Rating shows stars the user can click, or a read-only average. Its value is a number; null means no rating yet:

JSX
import Rating from '@mui/material/Rating';

<Rating
  name="product-rating"
  value={rating}
  onChange={(event, newValue) => setRating(newValue)}
  precision={0.5}
  size="large"
/>
<Rating value={4.3} precision={0.1} readOnly />
<Rating name="score" max={10} defaultValue={7} />
  • precision allows half (0.5) or finer values; the default is whole stars.
  • readOnly displays; disabled greys out.
  • max sets the number of stars; icon and emptyIcon swap the star for hearts or any icon; getLabelText supplies accessible text.
  • onChange can return null when the user clicks the same star again to clear it; handle that so your state does not break.
  • Under the hood the stars are radio buttons, which is why name is important when you have more than one on a page.

ToggleButtonGroup

A ToggleButtonGroup is a row of buttons where the selection is part of the control: view mode (list or grid), text alignment, formatting toggles. It has two modes.

Exclusive: exactly one (or none) selected, value is a single item.

JSX
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';

const [view, setView] = useState('list');

<ToggleButtonGroup
  value={view}
  exclusive
  onChange={(event, next) => { if (next !== null) setView(next); }}
  aria-label="view mode"
>
  <ToggleButton value="list" aria-label="list view"><ViewListIcon /></ToggleButton>
  <ToggleButton value="grid" aria-label="grid view"><ViewModuleIcon /></ToggleButton>
</ToggleButtonGroup>

When the user clicks the selected button again, onChange gives null. The if (next !== null) guard keeps one option always selected.

Multiple (default without exclusive): value is an array, as in bold/italic/underline formatting:

JSX
<ToggleButtonGroup value={formats} onChange={(e, next) => setFormats(next)}>
  <ToggleButton value="bold" aria-label="bold"><b>B</b></ToggleButton>
  <ToggleButton value="italic" aria-label="italic"><i>I</i></ToggleButton>
</ToggleButtonGroup>

Choose toggle buttons for 2 to 5 short options. For more, a Select is better. As with icon buttons, buttons that only show an icon must have aria-label.

Stand-in: stars and toggle buttons together

The second stand-in has a rating (half-star precision through the hover position is omitted for brevity, whole stars with clear-on-reclick) and both toggle modes.

Stand-in: Rating and ToggleButtonGroup

Accessibility reminders

  • Sliders need a name: aria-label or aria-labelledby. Add getAriaValueText when the number alone is not meaningful (temperature, currency).
  • Keyboard: arrow keys change a slider by step, Page Up/Down by larger steps, Home/End jump to min/max.
  • Provide a text alternative to a rating (its label or the value) so the information is not only visual.
  • A toggle button communicates state with aria-pressed; MUI sets it for you.

Common mistakes

  • Reading event.target.value in a Slider handler; use the second argument.
  • Updating a server on every onChange of a slider instead of onChangeCommitted.
  • Passing a single number to a range slider (it needs an array) or an array to a normal slider.
  • Not guarding null in exclusive ToggleButtonGroup, leaving no selection.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the signature of Slider's onChange?

    Slider passes the new value as the second argument; event.target.value is not reliable.
  2. 2. How do you make a Slider with two thumbs?

    An array value produces a range slider; disableSwap prevents thumbs from crossing.
  3. 3. Which handler fires only when the user releases the slider thumb?

    onChangeCommitted is ideal for expensive actions such as network requests.
  4. 4. In an exclusive ToggleButtonGroup, what does onChange receive when the selected button is clicked again?

    It passes null, so guard it if one option must always stay selected.

Summary

  • Slider: value, onChange(event, newValue), min/max/step, marks, ranges via arrays, onChangeCommitted.
  • Rating: numeric value, precision, readOnly; handle null after clearing.
  • ToggleButtonGroup: exclusive for one value, otherwise an array; guard null.
  • Give every control an accessible name and sensible aria text.

Next: Form Validation with MUI