apps/www/content/docs/components/slider.mdx
import { Slider } from "@chakra-ui/react"
<Slider.Root>
<Slider.Label />
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb>
<Slider.DraggingIndicator />
<Slider.HiddenInput />
</Slider.Thumb>
<Slider.MarkerGroup>
<Slider.Marker />
</Slider.MarkerGroup>
</Slider.Control>
</Slider.Root>
:::info
If you prefer a closed component composition, check out the snippet below.
:::
Slider.ThumbsThis component renders the Slider.Thumb and Slider.HiddenInput components
for each value.
The code below works:
<Slider.Thumb index={0}>
<Slider.HiddenInput />
</Slider.Thumb>
but this might be better if you don't need to customize the thumb:
<Slider.Thumbs />
Slider.MarksThis component renders the Slider.MarkerGroup and Slider.Marker components
for each value.
The code below works:
<Slider.MarkerGroup>
<Slider.Marker value={0} />
<Slider.Marker value={50} />
</Slider.MarkerGroup>
but this might be better if you don't need to customize the marker:
<Slider.Marks marks={[0, 50]} />
Use the size prop to change the size of the slider.
Use the variant prop to change the visual style of the slider.
Use the colorPalette prop to change the color of the slider.
Use the label prop to add a label to the slider.
Set the value or defaultValue prop to an array to create a range slider.
Use the minStepsBetweenThumbs prop to avoid thumbs with the same value.
Use the thumbCollisionBehavior prop to control how thumbs interact when they
collide. Options are "none" (default), "push", or "swap".
Here's an example of customizing the thumb with custom icon and background.
<ExampleTabs name="slider-customization" />Use the Slider.ValueText component to show the current value of the slider.
Use the value and onValueChange props to control the value of the slider.
An alternative way to control the slider is to use the RootProvider component
and the useSlider store hook.
This way you can access the slider state and methods from outside the slider.
<ExampleTabs name="slider-with-store" />Here's an example of how to integrate a slider with react-hook-form.
Use the disabled prop to disable the slider.
Use the onValueChangeEnd prop to listen to the end of the slider change.
Use the step prop to set the step value of the slider.
Use the thumbAlignment and thumbSize prop to align the thumb within the
track. By default, the thumb is aligned to the start of the track.
Use the marks prop to display marks on the slider.
You can also add labels to the marks using the marks prop.
Use the orientation prop to change the orientation of the slider.
Here's an example of a vertical slider with marks.
<ExampleTabs name="slider-with-marks-vertical" />Render the Slider.DraggingIndicator component to show an indicator or tooltip
when dragging the thumb.
<ExampleTabs name="slider-with-dragging-indicator" />Pro Tip: You can render the
Slider.ValueTextcomponent inside theSlider.DraggingIndicatorto show the current value.
If you prefer a closed component composition, check out the snippet below.
<ExampleCode name="slider-closed-component" />If you want to automatically add the closed component to your project, run the command:
npx @chakra-ui/cli snippet add slider
Explore the Slider component parts interactively. Click on parts in the
sidebar to highlight them in the preview.