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/sliderComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/sliderCopy the files
src/components/ui/slider/index.ts
export { default as Slider } from './slider.astro'
export type { SliderProps } from './types.ts'
src/components/ui/slider/slider.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { SliderProps } from "./types.ts"
type Props = HTMLAttributes<"div"> & SliderProps
const {
class: className,
defaultValue,
min,
max,
step,
largeStep,
orientation = "horizontal",
thumbAlignment = "edge",
disabled,
...props
} = Astro.props
const serializedDefaultValue = Array.isArray(defaultValue)
? defaultValue.join(",")
: defaultValue
const thumbCount = String(serializedDefaultValue ?? "").includes(",") ? 2 : 1
---
<div
data-slot="slider"
data-default-value={serializedDefaultValue}
data-min={min}
data-max={max}
data-step={step}
data-large-step={largeStep}
data-orientation={orientation}
data-thumb-alignment={thumbAlignment}
data-disabled={disabled ? "true" : undefined}
class={cn(
"data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full",
className,
)}
{...props}
>
<div
data-slot="slider-control"
data-orientation={orientation}
data-disabled={disabled ? "true" : undefined}
class="flex touch-none select-none data-disabled:pointer-events-none data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-44 data-[orientation=horizontal]:w-full data-[orientation=horizontal]:min-w-44 data-[orientation=vertical]:flex-col data-disabled:opacity-64 relative items-center"
>
<div data-slot="slider-track" class="relative grow select-none before:absolute before:rounded-full before:bg-input data-[orientation=horizontal]:h-1 data-[orientation=vertical]:h-full data-[orientation=horizontal]:w-full data-[orientation=vertical]:w-1 data-[orientation=horizontal]:before:inset-x-0.5 data-[orientation=vertical]:before:inset-x-0 data-[orientation=horizontal]:before:inset-y-0 data-[orientation=vertical]:before:inset-y-0.5">
<div data-slot="slider-range" class="select-none rounded-full bg-primary data-[orientation=horizontal]:ms-0.5 data-[orientation=vertical]:mb-0.5 data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full"></div>
</div>
{Array.from({ length: thumbCount }).map(() => (
<div data-slot="slider-thumb" class="block size-5 shrink-0 select-none rounded-full border border-input bg-white not-dark:bg-clip-padding shadow-xs/5 outline-none transition-[box-shadow,scale] before:absolute before:inset-0 before:rounded-full before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:ring-[3px] focus-visible:ring-ring/24 data-dragging:scale-120 sm:size-4 dark:border-background dark:focus-visible:ring-ring/48 [:focus-visible,[data-dragging]]:shadow-none" />
))}
</div>
</div>
<script>
import { create } from "@data-slot/slider"
import type { SliderController } from "@data-slot/slider"
// Destroy bindings before a view transition so the next page-load binds once.
const controllers: SliderController[] = []
const initialize = () => controllers.push(...create())
initialize()
document.addEventListener("astro:page-load", initialize)
document.addEventListener("astro:before-swap", () => {
for (const controller of controllers) controller.destroy()
controllers.length = 0
})
</script>
src/components/ui/slider/types.ts
import type { SliderOptions } from '@data-slot/slider'
/**
* Slider configuration rendered as data attributes.
*/
export type SliderProps = Pick<
SliderOptions,
| 'defaultValue'
| 'min'
| 'max'
| 'step'
| 'largeStep'
| 'orientation'
| 'thumbAlignment'
| 'disabled'
>
Usage
---
import { Slider } from "@/components/ui/slider"
---
<Slider defaultValue={33} max={100} step={1} />
Examples
Range
Use an array with two values for a range slider.
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
booleanSee 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.