Slider
A range control with a filled track, a grabber and optional labelled marks, for volumes and thresholds.
A slider is for a value the reader feels their way to, such as a volume or a sensitivity threshold. When the exact number matters more than its position on a scale, use a Number Input instead.
It is built on Radix Slider in React and Reka UI Slider in Vue. The primitive owns the pointer, the keyboard and the announced value. The value is an array with one number per grabber, so [40] draws one grabber and [20, 80] draws two.
newt-slider"use client"
import * as React from "react"
import { Slider } from "@/registry/bases/newt/ui/slider"
const MARKS = [
{ value: 0, label: "0%" },
{ value: 100, label: "100%" },
{ value: 200, label: "200%" },
]
export default function SliderDemo() {
const [volume, setVolume] = React.useState([80])
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<div className="flex items-baseline justify-between text-sm">
<span className="font-semibold text-newt-text-primary">
Output volume
</span>
<span className="font-mono text-newt-text-muted">{volume[0]}%</span>
</div>
<Slider
aria-label="Output volume"
min={0}
max={200}
value={volume}
onValueChange={setVolume}
marks={MARKS}
/>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Slider } from "~~/registry/bases/newt/ui/slider"
const MARKS = [
{ value: 0, label: "0%" },
{ value: 100, label: "100%" },
{ value: 200, label: "200%" },
]
const volume = ref([80])
</script>
<template>
<div class="flex w-full max-w-sm flex-col gap-3">
<div class="flex items-baseline justify-between text-sm">
<span class="font-semibold text-newt-text-primary">Output volume</span>
<span class="font-mono text-newt-text-muted">{{ volume[0] }}%</span>
</div>
<Slider
v-model="volume"
aria-label="Output volume"
:min="0"
:max="200"
:marks="MARKS"
/>
</div>
</template>Installation
npx newtui@latest add slidernpx shadcn@latest add @newtui/slidernpx shadcn-vue@latest add @newtui/sliderpnpm dlx newtui@latest add sliderpnpm dlx shadcn@latest add @newtui/sliderpnpm dlx shadcn-vue@latest add @newtui/slideryarn dlx newtui@latest add slideryarn dlx shadcn@latest add @newtui/slideryarn dlx shadcn-vue@latest add @newtui/sliderbunx newtui@latest add sliderbunx shadcn@latest add @newtui/sliderbunx shadcn-vue@latest add @newtui/sliderdeno run -A npm:newtui@latest add sliderdeno run -A npm:shadcn@latest add @newtui/sliderdeno run -A npm:shadcn-vue@latest add @newtui/slidervlx newtui@latest add slidervlx shadcn@latest add @newtui/slidervlx shadcn-vue@latest add @newtui/slidervp dlx newtui@latest add slidervp dlx shadcn@latest add @newtui/slidervp dlx shadcn-vue@latest add @newtui/slidernubx newtui@latest add slidernubx shadcn@latest add @newtui/slidernubx shadcn-vue@latest add @newtui/slidernlx newtui@latest add slidernlx shadcn@latest add @newtui/slidernlx shadcn-vue@latest add @newtui/sliderupx newtui@latest add sliderupx shadcn@latest add @newtui/sliderupx shadcn-vue@latest add @newtui/slider- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install radix-uipnpm add radix-uiyarn add radix-uibun add radix-uideno add npm:radix-uivlt install radix-uivp add radix-uinub add radix-uini radix-uiupm add radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install reka-uipnpm add reka-uiyarn add reka-uibun add reka-uideno add npm:reka-uivlt install reka-uivp add reka-uinub add reka-uini reka-uiupm add reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Copy the source into your project
"use client"
import { Slider as SliderPrimitive } from "radix-ui"
import * as React from "react"
import { cn } from "@/lib/utils"
/** A named point on the track, drawn above it. */
export interface SliderMark {
value: number
/** What the mark says. Omitted, it shows the number. */
label?: string
}
export interface SliderProps extends React.ComponentPropsWithoutRef<
typeof SliderPrimitive.Root
> {
/** Labelled points along the track. */
marks?: readonly SliderMark[]
}
/*
* Radix owns the behaviour: pointer and touch dragging, arrow keys, Page Up
* and Page Down, Home and End, and the hidden input a form reads. Only the
* look is newt/ui's. The value is an array, one entry per grabber.
*/
const Slider = React.forwardRef<
React.ComponentRef<typeof SliderPrimitive.Root>,
SliderProps
>(
(
{
className,
marks,
min = 0,
max = 100,
value,
defaultValue,
disabled,
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
"aria-valuetext": ariaValueText,
...props
},
ref
) => {
const span = max - min
const ratioOf = (point: number) =>
span === 0 ? 0 : Math.min(1, Math.max(0, (point - min) / span))
const thumbs = (value ?? defaultValue ?? [min]).length
/*
* `role="slider"` is on the grabber, so that is where the name goes. Two
* grabbers keep Radix's "Minimum" and "Maximum", and the name moves to a
* group around them.
*/
const single = thumbs === 1
return (
<div
className={cn(
"flex w-full flex-col gap-1",
disabled && "opacity-50",
className
)}
>
{marks === undefined || marks.length === 0 ? null : (
// Inset by half a grabber, which is where the grabber's centre stops.
<div aria-hidden="true" className="relative mx-[5px] h-[22px]">
{marks.map((mark) => (
<span
key={mark.value}
className="absolute top-0 flex translate-x-[calc(var(--newt-dir)*-50%)] flex-col items-center gap-0.5 text-[10px] font-bold leading-3 text-newt-text-muted after:h-1.5 after:w-0.5 after:rounded-full after:bg-newt-bg-active after:content-['']"
style={{ insetInlineStart: `${ratioOf(mark.value) * 100}%` }}
>
{mark.label ?? mark.value}
</span>
))}
</div>
)}
<SliderPrimitive.Root
ref={ref}
min={min}
max={max}
value={value}
defaultValue={defaultValue}
disabled={disabled}
role={single ? undefined : "group"}
aria-label={single ? undefined : ariaLabel}
aria-labelledby={single ? undefined : ariaLabelledby}
className="relative flex h-6 w-full cursor-pointer touch-none select-none items-center data-[disabled]:cursor-not-allowed"
{...props}
>
<SliderPrimitive.Track className="relative h-2 w-full grow overflow-hidden rounded-full bg-newt-bg-active">
<SliderPrimitive.Range className="absolute h-full bg-newt-brand" />
</SliderPrimitive.Track>
{Array.from({ length: thumbs }, (_, index) => (
<SliderPrimitive.Thumb
key={index}
/* An explicit undefined would replace the "Minimum" and "Maximum" names. */
{...(single
? {
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
"aria-valuetext": ariaValueText,
}
: {})}
className="block h-6 w-2.5 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-on-accent shadow-elevation-low focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link"
/>
))}
</SliderPrimitive.Root>
</div>
)
}
)
Slider.displayName = "Slider"
export { Slider }<script setup lang="ts">
import {
SliderRange,
SliderRoot,
SliderThumb,
SliderTrack,
useForwardPropsEmits,
type SliderRootEmits,
type SliderRootProps,
} from "reka-ui"
import { computed, useAttrs, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import type { SliderMark } from "."
/*
* Reka owns the behaviour: pointer and touch dragging, arrow keys, Page Up
* and Page Down, Home and End, and the hidden input a form reads. Only the
* look is newt/ui's. The value is an array, one entry per grabber.
* Attributes other than `class` land on the slider root.
*/
defineOptions({ inheritAttrs: false })
const props = withDefaults(
defineProps<
SliderRootProps & {
class?: HTMLAttributes["class"]
/** Labelled points along the track. */
marks?: readonly SliderMark[]
}
>(),
{ min: 0, max: 100, marks: () => [] }
)
const emits = defineEmits<SliderRootEmits>()
const delegated = computed(() => {
const {
class: _class,
marks: _marks,
defaultValue: _defaultValue,
...rest
} = props
return rest
})
const forwarded = useForwardPropsEmits(delegated, emits)
/* Reka starts an uncontrolled slider at 0; ours starts at `min`. */
const initial = computed(() => props.defaultValue ?? [props.min])
const thumbs = computed(() => (props.modelValue ?? initial.value).length)
/*
* `role="slider"` is on the grabber, so that is where the name goes. Two
* grabbers keep Reka's "Minimum" and "Maximum", and the name moves to a
* group around them.
*/
const THUMB_ATTRS = new Set(["aria-label", "aria-labelledby", "aria-valuetext"])
const attrs = useAttrs()
const single = computed(() => thumbs.value === 1)
const rootAttrs = computed(() => {
const rest = Object.fromEntries(
Object.entries(attrs).filter(([name]) => !THUMB_ATTRS.has(name))
)
if (single.value) return rest
return {
...rest,
role: "group",
"aria-label": attrs["aria-label"],
"aria-labelledby": attrs["aria-labelledby"],
}
})
const thumbAttrs = computed(() =>
single.value
? Object.fromEntries(
Object.entries(attrs).filter(([name]) => THUMB_ATTRS.has(name))
)
: {}
)
function ratioOf(point: number): number {
const span = props.max - props.min
return span === 0 ? 0 : Math.min(1, Math.max(0, (point - props.min) / span))
}
</script>
<template>
<div
:class="
cn(
'flex w-full flex-col gap-1',
props.disabled && 'opacity-50',
props.class
)
"
>
<!-- Inset by half a grabber, which is where the grabber's centre stops. -->
<div
v-if="props.marks.length > 0"
aria-hidden="true"
class="relative mx-[5px] h-[22px]"
>
<span
v-for="mark in props.marks"
:key="mark.value"
class="absolute top-0 flex translate-x-[calc(var(--newt-dir)*-50%)] flex-col items-center gap-0.5 text-[10px] font-bold leading-3 text-newt-text-muted after:h-1.5 after:w-0.5 after:rounded-full after:bg-newt-bg-active after:content-['']"
:style="{ insetInlineStart: `${ratioOf(mark.value) * 100}%` }"
>
{{ mark.label ?? mark.value }}
</span>
</div>
<SliderRoot
v-bind="{ ...forwarded, ...rootAttrs }"
:default-value="initial"
class="relative flex h-6 w-full cursor-pointer touch-none select-none items-center data-[disabled]:cursor-not-allowed"
>
<SliderTrack
class="relative h-2 w-full grow overflow-hidden rounded-full bg-newt-bg-active"
>
<SliderRange class="absolute h-full bg-newt-brand" />
</SliderTrack>
<SliderThumb
v-for="index in thumbs"
:key="index"
v-bind="thumbAttrs"
class="block h-6 w-2.5 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-on-accent shadow-elevation-low focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link"
/>
</SliderRoot>
</div>
</template>export { default as Slider } from "./Slider.vue"
/** A named point on the track, drawn above it. */
export interface SliderMark {
value: number
/** What the mark says. Omitted, it shows the number. */
label?: string
}Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Slider to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Slider to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { Slider } from "@/components/ui/slider"
export function Example() {
return <Slider />
}<script setup lang="ts">
import { Slider } from "@/components/ui/slider"
</script>
<template>
<Slider />
</template>Examples
Named steps
With a small max and a mark at every stop, the slider becomes a short scale of named levels. Pass aria-valuetext so a screen reader hears the name and not the number.
newt-slider"use client"
import * as React from "react"
import { Slider } from "@/registry/bases/newt/ui/slider"
const LEVELS = [
{ value: 0, label: "Off" },
{ value: 1, label: "Low" },
{ value: 2, label: "Medium" },
{ value: 3, label: "High" },
]
/* A mark at every stop turns the track into a short list of named levels. */
export default function SliderStepsDemo() {
const [level, setLevel] = React.useState([2])
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<span className="text-sm font-semibold text-newt-text-primary">
Noise suppression
</span>
<Slider
aria-label="Noise suppression"
aria-valuetext={LEVELS[level[0] ?? 0]?.label}
min={0}
max={3}
step={1}
value={level}
onValueChange={setLevel}
marks={LEVELS}
/>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Slider } from "~~/registry/bases/newt/ui/slider"
const LEVELS = [
{ value: 0, label: "Off" },
{ value: 1, label: "Low" },
{ value: 2, label: "Medium" },
{ value: 3, label: "High" },
]
/* A mark at every stop turns the track into a short list of named levels. */
const level = ref([2])
</script>
<template>
<div class="flex w-full max-w-sm flex-col gap-3">
<span class="text-sm font-semibold text-newt-text-primary">
Noise suppression
</span>
<Slider
v-model="level"
aria-label="Noise suppression"
:aria-valuetext="LEVELS[level[0] ?? 0]?.label"
:min="0"
:max="3"
:step="1"
:marks="LEVELS"
/>
</div>
</template>Custom range
min, max and step set the scale: this one runs from 8 to 96 in steps of 8. Without marks the track is drawn alone, so show the current value beside it.
newt-slider"use client"
import * as React from "react"
import { Slider } from "@/registry/bases/newt/ui/slider"
export default function SliderRangeDemo() {
const [bitrate, setBitrate] = React.useState([64])
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<div className="flex items-baseline justify-between text-sm">
<span className="font-semibold text-newt-text-primary">Bitrate</span>
<span className="font-mono text-newt-text-muted">
{bitrate[0]} kbps
</span>
</div>
<Slider
aria-label="Bitrate"
aria-valuetext={`${bitrate[0]} kilobits per second`}
min={8}
max={96}
step={8}
value={bitrate}
onValueChange={setBitrate}
/>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Slider } from "~~/registry/bases/newt/ui/slider"
const bitrate = ref([64])
</script>
<template>
<div class="flex w-full max-w-sm flex-col gap-3">
<div class="flex items-baseline justify-between text-sm">
<span class="font-semibold text-newt-text-primary">Bitrate</span>
<span class="font-mono text-newt-text-muted">{{ bitrate[0] }} kbps</span>
</div>
<Slider
v-model="bitrate"
aria-label="Bitrate"
:aria-valuetext="`${bitrate[0]} kilobits per second`"
:min="8"
:max="96"
:step="8"
/>
</div>
</template>Minimum and maximum
Pass two values to get two grabbers, one for each end of a span. The aria-label names the group around them, and each grabber is read as “Minimum” or “Maximum”. minStepsBetweenThumbs (min-steps-between-thumbs in Vue) keeps them at least one step apart.
newt-slider"use client"
import * as React from "react"
import { Slider } from "@/registry/bases/newt/ui/slider"
/*
* Two values give two grabbers. The name goes on the group around them, and
* each grabber keeps its own "Minimum" or "Maximum".
*/
export default function SliderMinMaxDemo() {
const [members, setMembers] = React.useState([100, 500])
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<div className="flex items-baseline justify-between text-sm">
<span className="font-semibold text-newt-text-primary">
Server size
</span>
<span className="font-mono text-newt-text-muted">
{members[0]} to {members[1]} members
</span>
</div>
<Slider
aria-label="Server size"
min={0}
max={1000}
step={50}
minStepsBetweenThumbs={1}
value={members}
onValueChange={setMembers}
/>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Slider } from "~~/registry/bases/newt/ui/slider"
const members = ref([100, 500])
</script>
<template>
<!--
Two values give two grabbers. The name goes on the group around them, and
each grabber keeps its own "Minimum" or "Maximum".
-->
<div class="flex w-full max-w-sm flex-col gap-3">
<div class="flex items-baseline justify-between text-sm">
<span class="font-semibold text-newt-text-primary">Server size</span>
<span class="font-mono text-newt-text-muted">
{{ members[0] }} to {{ members[1] }} members
</span>
</div>
<Slider
v-model="members"
aria-label="Server size"
:min="0"
:max="1000"
:step="50"
:min-steps-between-thumbs="1"
/>
</div>
</template>Disabled
disabled dims the whole control and takes it out of the tab order. Say why nearby, because the slider itself cannot.
newt-sliderimport { Slider } from "@/registry/bases/newt/ui/slider"
const MARKS = [
{ value: 0, label: "0%" },
{ value: 100, label: "100%" },
]
export default function SliderDisabledDemo() {
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<div className="flex items-baseline justify-between text-sm">
<span className="font-semibold text-newt-text-primary">
Input volume
</span>
<span className="text-newt-text-muted">No microphone found</span>
</div>
<Slider aria-label="Input volume" value={[40]} marks={MARKS} disabled />
</div>
)
}<script setup lang="ts">
import { Slider } from "~~/registry/bases/newt/ui/slider"
const MARKS = [
{ value: 0, label: "0%" },
{ value: 100, label: "100%" },
]
</script>
<template>
<div class="flex w-full max-w-sm flex-col gap-3">
<div class="flex items-baseline justify-between text-sm">
<span class="font-semibold text-newt-text-primary">Input volume</span>
<span class="text-newt-text-muted">No microphone found</span>
</div>
<Slider
aria-label="Input volume"
:model-value="[40]"
:marks="MARKS"
disabled
/>
</div>
</template>API reference
Slider
value / v-model?number[]
The current value, one number per grabber. In React, leave it undefined and pass `defaultValue` to let the slider own it.
number[]defaultValue?number[]
The value on first render, for an uncontrolled slider.
number[][min]onValueChange?(value: number[]) => void
React only. Called on every change; Vue emits `update:modelValue`.
(value: number[]) => voidonValueCommit?(value: number[]) => void
React only. Called when a drag or a key press ends; Vue emits `valueCommit`.
(value: number[]) => voidmin?number
The low end of the range.
number0max?number
The high end of the range.
number100step?number
The distance between two values.
number1minStepsBetweenThumbs?number
The smallest gap, in steps, between two grabbers.
number0marks?{ value: number; label?: string }[]
Labelled points drawn above the track. A mark without a label shows its number.
{ value: number; label?: string }[]name?string
The field name. Inside a `<form>`, the slider submits its value under this name.
stringdir?"ltr" | "rtl"
The direction the value grows in. Set it to `rtl` in a right-to-left layout.
"ltr" | "rtl""ltr"disabled?boolean
Dims the control and stops it taking input.
booleanfalseSlider accepts every other prop of the primitive root, such as inverted. Other attributes go to the slider root. className (React) or class (Vue) goes to the wrapper around the marks and the track.
Accessibility
Each grabber has role="slider" and takes keyboard focus. The arrow keys move it by one step. Page Up, Page Down and Shift with an arrow key move it by ten steps. Home and End go to the ends of the range.
Give the slider a name with aria-label or aria-labelledby. With one grabber, the name and aria-valuetext go to that grabber. With two grabbers, the name goes to a role="group" around them, and the grabbers are named “Minimum” and “Maximum”. The marks are decoration and are hidden from assistive technology. A grabber shows a 2px --newt-text-link outline while it has keyboard focus.
Tokens
Slider reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-slider.
--newt-radius-full--newt-bg-active--newt-brand--newt-border--newt-radius-sm--newt-on-accent--newt-shadow-elevation-low--newt-text-link--newt-text-muted--newt-dir