Skip to main content

Slider

The Slider is used to allow users to make selections from a range of values.

Import

Chakra UI exports 5 components for Slider:

  • Slider: The wrapper that provides context and functionality for all children.
  • SliderTrack: The empty part of the slider that shows the track.
  • SliderFilledTrack: The filled part of the slider.
  • SliderThumb: The handle that's used to change the slider value.
  • SliderMark: The label or mark that shows names for specific slider values
import {
Slider,
SliderTrack,
SliderFilledTrack,
SliderThumb,
SliderMark,
} from "@chakra-ui/react";

Usage

Sliders reflect a range of values along a bar, from which users may select a single value. They are ideal for adjusting settings such as volume, brightness, or applying image filters.

Note: We recommend adding a aria-label or aria-labelledby prop to provide an accessible label for the Slider.

Changing the slider color scheme

Changing the slider orientation

Customizing the Slider

Slider component was designed to be composed to make it easy for you to customize its styles.

Discrete Sliders

Discrete sliders allow you to snap to predefined sets of values. This can be accomplished using the step prop.

Slider with custom labels and marks

You can have custom labels and marks by using SliderMark component

function SliderMarkExample() {
const [sliderValue, setSliderValue] = useState(50);

const labelStyles = {
mt: "2",
ml: "-2.5",
fontSize: "sm",
};

return (
<Box pt={6} pb={2}>
<Slider aria-label="slider-ex-6" onChange={(val) => setSliderValue(val)}>
<SliderMark value={25} {...labelStyles}>
25%
</SliderMark>
<SliderMark value={50} {...labelStyles}>
50%
</SliderMark>
<SliderMark value={75} {...labelStyles}>
75%
</SliderMark>
<SliderMark
value={sliderValue}
textAlign="center"
bg="blue.500"
color="white"
mt="-10"
ml="-5"
w="12"
>
{sliderValue}%
</SliderMark>
<SliderTrack>
<SliderFilledTrack />
</SliderTrack>
<SliderThumb />
</Slider>
</Box>
);
}

And you can also use Tooltip with SliderThumb

function SliderThumbWithTooltip() {
const [sliderValue, setSliderValue] = React.useState(5);
const [showTooltip, setShowTooltip] = React.useState(false);
return (
<Slider
id="slider"
defaultValue={5}
min={0}
max={100}
colorScheme="teal"
onChange={(v) => setSliderValue(v)}
onMouseEnter={() => setShowTooltip(true)}
onMouseLeave={() => setShowTooltip(false)}
>
<SliderMark value={25} mt="1" ml="-2.5" fontSize="sm">
25%
</SliderMark>
<SliderMark value={50} mt="1" ml="-2.5" fontSize="sm">
50%
</SliderMark>
<SliderMark value={75} mt="1" ml="-2.5" fontSize="sm">
75%
</SliderMark>
<SliderTrack>
<SliderFilledTrack />
</SliderTrack>
<Tooltip
hasArrow
bg="teal.500"
color="white"
placement="top"
isOpen={showTooltip}
label={`${sliderValue}%`}
>
<SliderThumb />
</Tooltip>
</Slider>
);
}

Getting the final value when dragging the slider

Dragging the slider can trigger lots of updates and the user might only be interested in the final result after sliding is complete. You can use onChangeEnd for this.

<Slider aria-label="slider-ex-5" onChangeEnd={(val) => console.log(val)}>
<SliderTrack>
<SliderFilledTrack />
</SliderTrack>
<SliderThumb />
</Slider>

Configure thumb focus with focusThumbOnChange

By default SliderThumb will receive focus whenever value changes. You can opt-out of this behavior by setting the value of focusThumbOnChange to false. This is normally used with a "controlled" slider value.

<Slider aria-label="slider-ex-5" value={value} focusThumbOnChange={false}>
<SliderTrack>
<SliderFilledTrack />
</SliderTrack>
<SliderThumb />
</Slider>

useSlider

We've exported the useSlider hook to help users manage and build custom slider UIs.

Props

Slider Props

The Slider component wraps all its children in the Box component, so you can pass all Box props to change its style.

aria-label

Description

The static string to use used for `aria-label` if no visible label is used.

Type
string

aria-labelledby

Description

The static string `aria-labelledby` that points to the ID of the element that serves as label for the slider

Type
string

aria-valuetext

Description

The static string to use used for `aria-valuetext`

Type
string

colorScheme

Type
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
Default
"blue"

defaultValue

Description

The initial value of the slider in uncontrolled mode

Type
number

direction

Description

The writing mode

Type
"ltr" | "rtl"
Default
"ltr"

focusThumbOnChange

Description

If false, the slider handle will not capture focus when value changes.

Type
boolean
Default
true

getAriaValueText

Description

Function that returns the `aria-valuetext` for screen readers. It is mostly used to generate a more human-readable representation of the value for assistive technologies

Type
((value: number) => string)

id

Description

The base id to use for the slider and its components

Type
string

isDisabled

Description

If true, the slider will be disabled

Type
boolean

isReadOnly

Description

If true, the slider will be in `read-only` state

Type
boolean

isReversed

Description

If true, the value will be incremented or decremented in reverse.

Type
boolean

max

Description

The maximum allowed value of the slider. Cannot be less than min.

Type
number
Default
100

min

Description

The minimum allowed value of the slider. Cannot be greater than max.

Type
number
Default
0

name

Description

The name attribute of the hidden input field. This is particularly useful in forms

Type
string

onChange

Description

Function called whenever the slider value changes (by dragging or clicking)

Type
((value: number) => void)

onChangeEnd

Description

Function called when the user is done selecting a new value (by dragging or clicking)

Type
((value: number) => void)

onChangeStart

Description

Function called when the user starts selecting a new value (by dragging or clicking)

Type
((value: number) => void)

size

Type
"lg" | "md" | "sm"
Default
"md"

step

Description

The step in which increments/decrements have to be made

Type
number
Default
1

value

Description

The value of the slider in controlled mode

Type
number

variant

Description

Variants for Slider are not implemented in the default theme. You can extend the theme to implement them.

Type
string

SliderThumb Props

SliderThumb composes Box so you can pass all Box props to change its style.

SliderFilledTrack Props

SliderFilledTrack composes Box so you can pass all Box props to change its style.

SliderTrack Props

SliderTrack composes Box so you can pass all Box props to change its style.