Slider
Choose a number, or a range, by dragging. Same markup and props in every style: switch the style in the header to see it change.
Basic
A native range input with its label and value. In HTML, add data-vin-slider and include vinui.js so the fill and the value follow the thumb.
<div class="vin-field" style="width: 100%; max-width: 320px">
<div class="vin-slider__head">
<label class="vin-label" for="volume">Volume</label>
<output class="vin-slider__value" for="volume">40</output>
</div>
<input id="volume" class="vin-slider" type="range" min="0" max="100" value="40" style="--vin-fill: 40%" data-vin-slider>
</div>import { Slider } from "@/components/vin/slider";
export function SliderBasic() {
return <Slider label="Volume" defaultValue={40} showValue style={{ width: "100%", maxWidth: 320 }} />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Slider from "@/components/vin/slider.vue";
</script>
<template>
<Slider label="Volume" :default-value="40" show-value style="width: 100%; max-width: 320px" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Slider from "$lib/components/vin/slider.svelte";
</script>
<Slider label="Volume" value={40} showValue style="width: 100%; max-width: 320px" />Range
Pass [from, to] for two thumbs. Each thumb is its own slider with its own name, and they can't cross. Click the track to move the nearest thumb.
<div class="vin-field" style="width: 100%; max-width: 320px" role="group" aria-labelledby="price-label">
<div class="vin-slider__head">
<span class="vin-label" id="price-label">Price</span>
<output class="vin-slider__value" for="price-from price-to">₹200 – ₹700</output>
</div>
<div class="vin-range" style="--vin-from: .2; --vin-to: .7" data-min="0" data-max="1000" data-step="50" data-format="₹{value}" data-vin-slider>
<span class="vin-range__thumb" id="price-from" role="slider" tabindex="0" aria-label="Minimum price" aria-valuemin="0" aria-valuemax="700" aria-valuenow="200" aria-valuetext="₹200"></span>
<span class="vin-range__thumb" id="price-to" role="slider" tabindex="0" aria-label="Maximum price" aria-valuemin="200" aria-valuemax="1000" aria-valuenow="700" aria-valuetext="₹700"></span>
<input type="hidden" name="price-min" value="200">
<input type="hidden" name="price-max" value="700">
</div>
</div>import { Slider } from "@/components/vin/slider";
export function SliderRange() {
return (
<Slider
label="Price"
defaultValue={[200, 700]}
min={0}
max={1000}
step={50}
showValue
formatValue={(v) => `₹${v}`}
thumbLabels={["Minimum price", "Maximum price"]}
name="price"
style={{ width: "100%", maxWidth: 320 }}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Slider from "@/components/vin/slider.vue";
</script>
<template>
<Slider
label="Price"
:default-value="[200, 700]"
:min="0"
:max="1000"
:step="50"
show-value
:format-value="(v: number) => `₹${v}`"
:thumb-labels="['Minimum price', 'Maximum price']"
name="price"
style="width: 100%; max-width: 320px"
/>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Slider from "$lib/components/vin/slider.svelte";
</script>
<Slider
label="Price"
value={[200, 700]}
min={0}
max={1000}
step={50}
showValue
formatValue={(v) => `₹${v}`}
thumbLabels={["Minimum price", "Maximum price"]}
name="price"
style="width: 100%; max-width: 320px"
/>Steps and disabled
Snap to steps with step. A disabled slider keeps its value visible.
<div style="display: grid; gap: 20px; width: 100%; max-width: 320px">
<div class="vin-field">
<div class="vin-slider__head">
<label class="vin-label" for="brightness">Brightness</label>
<output class="vin-slider__value" for="brightness" data-format="{value}%">70%</output>
</div>
<input id="brightness" class="vin-slider" type="range" min="0" max="100" step="10" value="70" style="--vin-fill: 70%" data-vin-slider>
</div>
<div class="vin-field">
<div class="vin-slider__head">
<label class="vin-label" for="bass">Bass (locked)</label>
</div>
<input id="bass" class="vin-slider" type="range" min="0" max="100" value="30" style="--vin-fill: 30%" disabled data-vin-slider>
</div>
</div>import { Slider } from "@/components/vin/slider";
export function SliderStates() {
return (
<div style={{ display: "grid", gap: 20, width: "100%", maxWidth: 320 }}>
<Slider label="Brightness" defaultValue={70} step={10} showValue formatValue={(v) => `${v}%`} />
<Slider label="Bass (locked)" defaultValue={30} disabled />
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Slider from "@/components/vin/slider.vue";
</script>
<template>
<div style="display: grid; gap: 20px; width: 100%; max-width: 320px">
<Slider label="Brightness" :default-value="70" :step="10" show-value :format-value="(v: number) => `${v}%`" />
<Slider label="Bass (locked)" :default-value="30" disabled />
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Slider from "$lib/components/vin/slider.svelte";
</script>
<div style="display: grid; gap: 20px; width: 100%; max-width: 320px">
<Slider label="Brightness" value={70} step={10} showValue formatValue={(v) => `${v}%`} />
<Slider label="Bass (locked)" value={30} disabled />
</div>Installation
React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:
npx @vinish/ui add sliderimport { Slider } from "@/components/vin/slider";import Slider from "@/components/vin/slider.vue";import Slider from "$lib/components/vin/slider.svelte";The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.
Plain HTML: the classes come with the style's stylesheet. This component also needs vinui.js for its keyboard and pointer behaviour.
API
From spec/slider.json, which every framework version is tested against.
| Prop | Values | Default | In HTML |
|---|---|---|---|
value / defaultValueOne number, or [from, to] for a range with two thumbs. Use onValueChange to hear about changes. | number | [number, number] | — | value + style="--vin-fill: 40%"; a range: aria-valuenow on each thumb + style="--vin-from: .2; --vin-to: .7" |
min / max / step | number | 0 / 100 / 1 | min, max, step attributes (a range: data-min, data-max, data-step) |
labelVisible label. Without it, pass an aria-label. | ReactNode | — | <label class="vin-label" for="…"> |
showValueShow the value next to the label. | boolean | false | <output class="vin-slider__value" for="…"> |
formatValueFormat the shown value. | (v: number) => string | — | data-format="₹{value}" on the output (a range: also on .vin-range, for aria-valuetext) |
thumbLabelsRange only: names for the two thumbs. | [string, string] | ["Minimum", "Maximum"] | aria-label on each .vin-range__thumb |
disabled | boolean | false | disabled attribute (a range: aria-disabled="true" on .vin-range) |
Parts
| React, Vue, Svelte | HTML classes |
|---|---|
<Slider> | .vin-slider.vin-range.vin-range__thumb.vin-slider__head.vin-slider__value |
Accessibility
- One value: a native <input type="range">; arrow keys, Page Up/Down, Home/End and touch all work, and screen readers announce the value.
- A range follows the WAI-ARIA multi-thumb slider pattern: two role="slider" thumbs with aria-valuenow, aria-valuemin/max and aria-valuetext, each in the Tab order.
- Every thumb has a name: the label, or an aria-label for each thumb of a range.
- The value is also shown as text in an <output>, so it is not only conveyed by position.
- Thumbs have a 30px touch area and a visible focus ring in every style and mode.
Customize
Saved in this browser. Style and accent are in the header.