---
title: Slider for Astro
description: "Astro range controls with data-slot pointer and keyboard input."
seo:
    title: Slider for Astro | Shadcn & Tailwind CSS
related:
    - /components/input
    - /components/progress
sidebar:
    label: "Slider"
---

```astro
---
import { Slider } from "@/components/ui/slider"
---

<Slider
  defaultValue={33}
  max={100}
  step={1}
  thumbAlignment="edge"
  class="w-full max-w-md"
/>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/slider
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/slider
```

#### src/components/ui/slider/index.ts

```ts
export { default as Slider } from './slider.astro'
export type { SliderProps } from './types.ts'
```

#### src/components/ui/slider/slider.astro

```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

```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'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro
---
import { Slider } from "@/components/ui/slider"
---
```

```astro
<Slider defaultValue={33} max={100} step={1} />
```

## Examples

### Range

Use an array with two values for a range slider.

```astro
---
import { Slider } from "@/components/ui/slider"
---

<Slider
  defaultValue={[25, 75]}
  max={100}
  step={1}
  thumbAlignment="edge"
  class="w-full max-w-md"
/>
```

### Vertical

Use `orientation="vertical"` for a vertical slider.

```astro
---
import { Slider } from "@/components/ui/slider"
---

<div class="flex h-48 items-center">
  <Slider
    orientation="vertical"
    defaultValue={60}
    max={100}
    step={5}
    thumbAlignment="edge"
    class="h-full"
  />
</div>
```

### Disabled

Use the `disabled` prop to disable the slider.

```astro
---
import { Slider } from "@/components/ui/slider"
---

<Slider
  defaultValue={50}
  disabled
  thumbAlignment="edge"
  class="w-full max-w-md"
/>
```

## API Reference

### SliderProps

<AutoTypeTable path="../../packages/ui/src/components/ui/slider/types.ts" name="SliderProps" />

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/slider/README.md) 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](/r/slider.json) 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.
