Slider for Astro
Astro range controls with data-slot pointer and keyboard input.
Installation
bunx --bun shadcn@latest add @gnostic/slidernpx shadcn@latest add @gnostic/sliderpnpm dlx shadcn@latest add @gnostic/sliderInstall component packages
bun add @data-slot/core @data-slot/slidernpm install @data-slot/core @data-slot/sliderpnpm add @data-slot/core @data-slot/sliderCopy the files
src/components/ui/slider/index.tssrc/components/ui/slider/slider.astrosrc/components/ui/slider/slider-form.tssrc/components/ui/slider/slider-value.astrosrc/components/ui/slider/slider-value.tssrc/components/ui/slider/types.ts
Usage
---
import { Slider, SliderValue } from "@/components/ui/slider"
---
<Slider defaultValue={33} aria-label="Volume">
<SliderValue />
</Slider>
Put SliderValue inside Slider to display its current value. It finds the nearest parent slider, even through a layout wrapper. The readout shows the initial value when the slider initializes and updates with pointer, keyboard, and programmatic changes. A range displays both values, such as 25 – 75.
Children appear above the drag control. To place a readout elsewhere, give the slider a unique id and pass that ID to SliderValue as sliderId.
Examples
Field
Set format on the slider to apply the same number format to each readout and thumb. For a volume slider from 0 to 100, this displays 40% for a value of 40.
<Slider name="volume" defaultValue={40} aria-label="Volume" format={{ style: "unit", unit: "percent" }}>
<SliderValue />
</Slider>
For a scale from 0 to 1, use format={{ style: "percent" }} to display 0.4 as 40%. Set locale to choose a language for the formatted text. Forms submit the numeric value as a string.
Set name="volume" to submit the current value with a form. Read it with new FormData(form).get("volume"). Set form to a form ID if the slider is outside that form. Disabled sliders are excluded from submission. A form reset restores the initial setting and the readout.
Range
Use an array with two values for a range slider. Both thumbs use the same field name. With name="price", new FormData(form).getAll("price") returns the lower and upper values as strings.
Vertical
Use orientation="vertical" for a vertical slider.
Disabled
Use the disabled prop to disable the slider.
API Reference
SliderProps
defaultValue?number | [number, number]
Initial value(s) - number or [min, max] for range
number | [number, number]min?number
Minimum value
numbermax?number
Maximum value
numberstep?number
Step increment
numberlargeStep?number
Larger step for PageUp/PageDown/Shift+Arrow
numberorientation?"horizontal" | "vertical"
Slider orientation
"horizontal" | "vertical"thumbAlignment?ThumbAlignment
Thumb alignment when the value is at the track edges. "edge-client-only" is accepted for Base UI compatibility and behaves the same as "edge".
ThumbAlignmentdisabled?boolean
Disable the slider
booleanformat?Intl.NumberFormatOptions
Number format for the readout and accessible thumb values.
Intl.NumberFormatOptionslocale?string | string[]
Language tag or ordered list of tags for number formatting. Defaults to the browser locale.
string | string[]name?string
Name submitted with each thumb value. Omit to exclude the slider from form data.
stringform?string
ID of the form that owns the slider. Defaults to the enclosing form.
stringSliderValueProps
sliderId?string
ID of the slider when the readout is outside it. Defaults to the nearest parent slider.
stringSliderValue renders an <output> by default. Use as to change the element. The default slot supplies fallback content. The first render of the live value replaces it. Use the slider’s format prop for units, currency, or decimal precision.
See the data-slot docs for more information on interactivity.
Events
Dispatch these events on the slider root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
slider:change |
{ value: number | [number, number] } |
slider:commit |
{ value: number | [number, number] } |
Inbound events
| Event | Detail |
|---|---|
slider:set |
{ value: number | [number, number] } |
Read the installed source for the complete prop types.
When to use Slider
Use Slider when an approximate position within a range is easier to choose than a typed value. Set bounds and step for the real data. Keep a visible label and, where useful, a readable current value.
Keyboard and behavior
The data-slot controller moves the thumb and handles keyboard input. Test endpoints, arrow keys, and disabled behavior. For values that require precision, provide another way to enter or confirm the exact number.
Theme and layout
Track, range, and thumb use input and primary tokens. Check focus contrast and touch use at narrow widths. Value persistence and any effect on application data remain your responsibility.