Slider Primitive
Use Slider to choose a value in a range, such as a volume level. Move the thumb with the pointer or arrow keys.
Drag the thumb or use the arrow keys.
Anatomy
Use the Astro primitive adapter to render Slider anatomy with the Runtime wiring included.
---import { Slider } from "@starwind-ui/astro/slider";---
<Slider.Root> <Slider.Label>Volume</Slider.Label> <Slider.Control> <Slider.Track> <Slider.Indicator /> <Slider.Thumb /> </Slider.Track> </Slider.Control></Slider.Root>Use the React primitive adapter when Slider state participates in React rendering.
import { Slider } from "@starwind-ui/react/slider";
export function Example() { return ( <Slider.Root> <Slider.Label>Volume</Slider.Label> <Slider.Control> <Slider.Track> <Slider.Indicator /> <Slider.Thumb /> </Slider.Track> </Slider.Control> </Slider.Root> );}Use the Vue components to build Slider.
<script setup lang="ts">import Slider from "@starwind-ui/vue/slider";</script>
<template> <Slider.Root> <Slider.Label>Volume</Slider.Label> <Slider.Control> <Slider.Track> <Slider.Indicator /> <Slider.Thumb /> </Slider.Track> </Slider.Control> </Slider.Root></template>Use the Svelte components to build Slider.
<script lang="ts"> import { Slider } from "@starwind-ui/svelte/slider";</script>
<Slider.Root> <Slider.Label>Volume</Slider.Label> <Slider.Control> <Slider.Track> <Slider.Indicator /> <Slider.Thumb /> </Slider.Track> </Slider.Control></Slider.Root>Render the Slider data-sw-* contract yourself, then initialize createSlider.
<div data-sw-slider role="group"> <span data-sw-slider-label>Volume</span> <div data-sw-slider-control> <div data-sw-slider-track> <div data-sw-slider-indicator></div> <div data-sw-slider-thumb></div> </div> </div></div>
<script type="module"> import { createSlider } from "@starwind-ui/runtime/slider";
const root = document.querySelector("[data-sw-slider]"); if (root) { createSlider(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
API Reference
Root
The main element that owns the Slider Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-slider
- Role
- group
Props
value SliderValue -
- Description
- Controls the current Slider value.
- Kind
- control
- Targets
- -
- Full type
- SliderValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
defaultValue SliderValue 0
- Description
- Sets the initial Slider value for uncontrolled usage.
- Kind
- control
- Targets
- -
- Full type
- SliderValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
disabled boolean false
- Description
- Disables the Root part.
- Kind
- option
- Targets
- -
- Full type
- boolean
form string -
- Description
- Associates the control with a form element.
- Kind
- option
- Targets
- -
- Full type
- string
largeStep number 10
- Description
- Configures the large step option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- number
max number 100
- Description
- Configures the max option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- number
min number 0
- Description
- Configures the min option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- number
minStepsBetweenValues number 0
- Description
- Configures the min steps between values option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- number
name string -
- Description
- Sets the submitted form field name.
- Kind
- option
- Targets
- -
- Full type
- string
orientation SliderOrientation "horizontal"
- Description
- Sets the Slider orientation.
- Kind
- option
- Targets
- -
- Full type
- SliderOrientation
step number 1
- Description
- Configures the step option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- number
onValueChange (value: SliderValue, details: SliderValueChangeDetails) => void -
- Description
- Runs when the Slider value changes.
- Kind
- callback
- Targets
- -
- Full type
- (value: SliderValue, details: SliderValueChangeDetails) => void
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
onValueCommitted (value: SliderValue, details: SliderValueCommitDetails) => void -
- Description
- Runs when on value committed changes for Slider.
- Kind
- callback
- Targets
- -
- Full type
- (value: SliderValue, details: SliderValueCommitDetails) => void
Events
valueChange onValueChange value: SliderValue
- Description
- Fires when the value changes for Slider.
- DOM event
- starwind:value-change
- Details type
- SliderValueChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
valueCommitted onValueCommitted value: SliderValue
- Description
- Fires when the user commits the value for Slider.
- DOM event
- starwind:value-committed
- Details type
- SliderValueCommitDetails
- Timing
- -
- Cancelable
- No
State
value SliderValue value / defaultValue
- Description
- Tracks the current Slider value.
- Initial attribute
- data-default-value
- Runtime getter
- getValue
- Runtime setter
- setValue
- React
Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-default-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
Control
Groups the interactive controls for Slider.
- Default element
- div
- Discovery hook
- data-sw-slider-control
- Role
- -
Data Attributes
Runtime hooks
Track
The track that contains the Slider range.
- Default element
- div
- Discovery hook
- data-sw-slider-track
- Role
- -
Data Attributes
Runtime hooks
Indicator
Visual state indicator rendered by Slider.
- Default element
- div
- Discovery hook
- data-sw-slider-indicator
- Role
- -
Data Attributes
Runtime hooks
Label
Text label associated with Slider.
- Default element
- span
- Discovery hook
- data-sw-slider-label
- Role
- -
Data Attributes
Runtime hooks
Thumb
The draggable thumb for Slider.
- Default element
- div
- Discovery hook
- data-sw-slider-thumb
- Role
- -
Props
index number -
- Description
- Sets the index attribute on the Thumb part.
- Kind
- attribute
- Targets
- thumb
- Full type
- number
Data Attributes
Runtime hooks
Metadata
Input
The native input synchronized by Slider.
- Default element
- input
- Discovery hook
- data-sw-slider-input
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createSlider- Import
@starwind-ui/runtime/slider- Root hook
- root
data-sw-slider - Option props
- defaultValue, disabled, form, largeStep, max, min, minStepsBetweenValues, name, orientation, step, value
Option Lifecycles
| Option | Lifecycle |
|---|---|
| disabled | setter-backed |
| form | setter-backed |
| largeStep | setter-backed |
| max | setter-backed |
| min | setter-backed |
| minStepsBetweenValues | setter-backed |
| name | setter-backed |
| orientation | setter-backed |
| step | setter-backed |
| value | setter-backed |
Runtime Setters
Form Participation
| Fact | Value |
|---|---|
| Form props | form, name, value |
| Hidden input | input (range) |
| Field integration | Yes |
Related Styled Components
Changelog
View version history v1.2.0 6 releases
v1.2.0
- Slider pointer, keys, range fill, and thumbs map to the same RTL values. Range values keep their order in both directions.
- Slider now sets `inset-inline-start` on the thumb and indicator in place of `left`. If your CSS animates or overrides `left` on these parts, or a script reads `style.left`, use `inset-inline-start`.
- In Svelte, the `children` snippet of `Slider.Root` no longer receives arguments. If you read the value from a snippet argument, use `bind:value` on the root instead.
- Slider now shows a `value` that the parent sets after the first render. This applies when `value` starts as `undefined` and gets a value later. Before, React and Vue ignored that value. A value that needs no correction does not fire a change callback, a change event, or a model update. If the Runtime corrects the value, for example a value above `max`, Vue `v-model` and Svelte `bind:value` receive the corrected value, so your state matches what the user sees.
- After that late value, a one-way parent that renders again with the same old value no longer undoes the user's last change. This also fixes Svelte Slider, where a later render undid a key press.
- Vue Slider no longer sends an old value to `v-model` after the parent sets a new one.
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.1.0
- In Astro, an attribute that you set on a Slider part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `value` that you pass when Slider mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Slider keeps its own state.
- Fixed a bound Slider in Svelte that reset to 0.
- In Vue, `Slider.Thumb` now accepts `inputRef`, as it does in React and Svelte. Pass a function to receive the hidden range input.
v1.0.1
- Reduced the JavaScript loaded by Slider when it is used without Field.
- Reduced repeated JavaScript when Astro Slider is used with other Starwind components.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
- In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.
v0.1.0
- First release. Slider supports one value or many values, dragging, and the keyboard. It sends its value with the form.