The static string to use used for `aria-label` if no visible label is used.
Slider
The Slider is used to allow users to make selections from a range of values.
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
aria-label
string
aria-labelledby
aria-labelledby
The static string `aria-labelledby` that points to the ID of the element that serves as label for the slider
string
aria-valuetext
aria-valuetext
The static string to use used for `aria-valuetext`
string
colorScheme
colorScheme
The visual color appearance of the component
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
blue
defaultValue
defaultValue
The initial value of the slider in uncontrolled mode
number
direction
direction
The writing mode
"ltr" | "rtl"
ltr
focusThumbOnChange
focusThumbOnChange
If false
, the slider handle will not capture focus when value changes.
boolean
true
getAriaValueText
getAriaValueText
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
(value: number) => string
id
id
The base id
to use for the slider and its components
string
isDisabled
isDisabled
If true
, the slider will be disabled
boolean
false
isReadOnly
isReadOnly
If true
, the slider will be in `read-only` state
boolean
false
isReversed
isReversed
If true
, the value will be incremented or decremented in reverse.
boolean
max
max
The maximum allowed value of the slider. Cannot be less than min.
number
100
min
min
The minimum allowed value of the slider. Cannot be greater than max.
number
0
name
name
The name attribute of the hidden input
field.
This is particularly useful in forms
string
onChange
onChange
Function called whenever the slider value changes (by dragging or clicking)
(value: number) => void
onChangeEnd
onChangeEnd
Function called when the user is done selecting a new value (by dragging or clicking)
(value: number) => void
onChangeStart
onChangeStart
Function called when the user starts selecting a new value (by dragging or clicking)
(value: number) => void
size
size
The size of the Slider
"lg" | "md" | "sm"
md
step
step
The step in which increments/decrements have to be made
number
1
value
value
The value of the slider in controlled mode
number
variant
variant
The variant of the Slider
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.
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
aria-label
The static string to use used for `aria-label` if no visible label is used.
string
aria-labelledby
aria-labelledby
The static string `aria-labelledby` that points to the ID of the element that serves as label for the slider
string
aria-valuetext
aria-valuetext
The static string to use used for `aria-valuetext`
string
colorScheme
colorScheme
The visual color appearance of the component
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
blue
defaultValue
defaultValue
The initial value of the slider in uncontrolled mode
number
direction
direction
The writing mode
"ltr" | "rtl"
ltr
focusThumbOnChange
focusThumbOnChange
If false
, the slider handle will not capture focus when value changes.
boolean
true
getAriaValueText
getAriaValueText
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
(value: number) => string
id
id
The base id
to use for the slider and its components
string
isDisabled
isDisabled
If true
, the slider will be disabled
boolean
false
isReadOnly
isReadOnly
If true
, the slider will be in `read-only` state
boolean
false
isReversed
isReversed
If true
, the value will be incremented or decremented in reverse.
boolean
max
max
The maximum allowed value of the slider. Cannot be less than min.
number
100
min
min
The minimum allowed value of the slider. Cannot be greater than max.
number
0
name
name
The name attribute of the hidden input
field.
This is particularly useful in forms
string
onChange
onChange
Function called whenever the slider value changes (by dragging or clicking)
(value: number) => void
onChangeEnd
onChangeEnd
Function called when the user is done selecting a new value (by dragging or clicking)
(value: number) => void
onChangeStart
onChangeStart
Function called when the user starts selecting a new value (by dragging or clicking)
(value: number) => void
size
size
The size of the Slider
"lg" | "md" | "sm"
md
step
step
The step in which increments/decrements have to be made
number
1
value
value
The value of the slider in controlled mode
number
variant
variant
The variant of the Slider
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.
The Slider
component is a multipart component. The styling needs to be applied
to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
container
- B:
track
- C:
thumb
- D:
filledTrack
- E:
mark
Theming properties#
The properties that affect the theming of the Slider
component are:
size
: The size of the slider. Defaults tomd
.colorScheme
: The color scheme of the slider. Defaults toblue
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { sliderAnatomy as parts } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(parts.keys)
Customizing the default theme#
import { sliderAnatomy as parts } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(parts.keys)const baseStyle = definePartsStyle({// define the part you're going to stylethumb: {bg: 'orange.400', // change the background of the thumb to orange.400},filledTrack: {bg: 'blue.600', // change the background of the filled track to blue.600},})export const sliderTheme = defineMultiStyleConfig({ baseStyle })
After customizing the slider theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { sliderTheme } from './theme/components/slider.ts'export const theme = extendTheme({components: { Slider: sliderTheme },})
This is a crucial step to make sure that any changes that we make to the slider theme are applied.
Adding a custom size#
Let's assume we want to include an extra large slider size. Here's how we can do that:
import { sliderAnatomy as parts } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(parts.keys)const sizes = {xl: definePartsStyle({container: defineStyle({w: "50%",}),track: defineStyle({h: 7,}),thumb: defineStyle({boxSize: 7,}),}),};export const sliderTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Slider size="xl">...</Slider>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Creating a custom variant#
Let's assume we want to create a custom square variant. Here's how we can do that:
import { sliderAnatomy as parts } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(parts.keys)const square = definePartsStyle({thumb: defineStyle({rounded: "none",}),});export const sliderTheme = defineMultiStyleConfig({variants: { square },})// Now we can use the new `square` variant<Slider variant="square">...</Slider>
Changing the default properties#
Let's assume we want to change the default size and variant of every slider in our app. Here's how we can do that:
import { sliderAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(sliderAnatomy.keys,)export const sliderTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'square',},})// This saves you time, instead of manually setting the size and variant every time you use a slider:<Slider size="xl" variant="square">...</Slider>
Showcase#
import { Slider, SliderTrack, SliderFilledTrack, SliderThumb, SliderMark, Text, Box, Flex, useColorMode, IconButton, } from "@chakra-ui/react"; import { useState } from "react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); const [sliderValue, setSliderValue] = useState(50); return ( <> <Flex h={"100vh"} textAlign="center" gap={5} p={8} direction="column"> <Box display="flex" gap={3} flexDirection="column"> <Text fontSize="lg">Default variant</Text> <Slider aria-label="slider-ex-1"> <SliderTrack> <SliderFilledTrack /> </SliderTrack> <SliderThumb /> </Slider> </Box> <Box display="flex" gap={3} flexDirection="column"> <Text fontSize="lg">Custom size</Text> <Slider aria-label="slider-ex-1" size="xl"> <SliderTrack> <SliderFilledTrack /> </SliderTrack> <SliderThumb /> </Slider> </Box> <Box display="flex" gap={3} flexDirection="column"> <Text fontSize="lg">Square variant</Text> <Slider aria-label="slider-ex-1" variant="square"> <SliderTrack> <SliderFilledTrack /> </SliderTrack> <SliderThumb /> </Slider> </Box> <Box display="flex" gap={3} flexDirection="column"> <Text fontSize="lg">Custom variant with markers</Text> <Slider aria-label="slider-ex-1" variant="colorful" onChange={(val) => setSliderValue(val)}> <SliderMark value={sliderValue}> {sliderValue}% </SliderMark> <SliderTrack> <SliderFilledTrack /> </SliderTrack> <SliderThumb /> </Slider> </Box> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Flex> </> ); }