Slider

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

Source@chakra-ui/react

Import#

import { Slider } from '@chakra-ui/react'

Usage#

<Slider.Root defaultValue={30}>
<Slider.Control>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

Changing the color palette#

<Slider.Root colorPalette='pink' defaultValue={30}>
<Slider.Control>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

Changing the orientation#

<Slider.Root defaultValue={30} orientation='vertical'>
<Slider.Control minH='32'>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

Customizing the styles#

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

<Slider.Root defaultValue={30}>
<Slider.Control>
<Slider.Track bg='red.100'>
<SliderFilledTrack bg='tomato' />
</Slider.Track>
<SliderThumb boxSize={6}>
<Box color='tomato' as={MdGraphicEq} />
</SliderThumb>
</Slider.Control>
</Slider.Root>

Changing the step value#

Set the step prop to snap the slider to predefined sets of values.

<Slider.Root defaultValue={60} min={0} max={300} step={30}>
<Slider.Control>
<Slider.Track bg='red.100'>
<SliderFilledTrack bg='tomato' />
</Slider.Track>
<SliderThumb boxSize={6} />
</Slider.Control>
</Slider.Root>

Rendering the value text#

Use the Slider.ValueText component to render the value of the slider.

<Slider.Root defaultValue={30}>
<Flex justify='space-between'>
<Slider.Label> Price</Slider.Label>
<Slider.ValueText />
</Flex>
<Slider.Control>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

Listening to slider changes#

Use the onChange and onChangeEnd to listen to change events on the slider.

<Slider.Root onChangeEnd={(val) => console.log(val)}>
<Slider.Control>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

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.Root aria-label='slider-ex-5' value={value} focusThumbOnChange={false}>
<Slider.Control>
<Slider.Track>
<Slider.FilledTrack />
</Slider.Track>
<SliderThumb />
</Slider.Control>
</Slider.Root>

useSlider#

The useSlider hook is exported with state and focus management logic for use in creating tailor-made slider component for your application.

Read more about the useSlider hook here.

Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel