Skip to content

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.

40
slider-basic.html
<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>

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.

Price₹200 – ₹700
slider-range.html
<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>

Steps and disabled

Snap to steps with step. A disabled slider keeps its value visible.

70%
slider-states.html
<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>

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:

Terminal
npx @vinish/ui add slider
React
import { Slider } from "@/components/vin/slider";
Vue
import Slider from "@/components/vin/slider.vue";
Svelte
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.

PropValuesDefaultIn HTML
value / defaultValue
One 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 / stepnumber0 / 100 / 1min, max, step attributes (a range: data-min, data-max, data-step)
label
Visible label. Without it, pass an aria-label.
ReactNode—<label class="vin-label" for="…">
showValue
Show the value next to the label.
booleanfalse<output class="vin-slider__value" for="…">
formatValue
Format the shown value.
(v: number) => string—data-format="₹{value}" on the output (a range: also on .vin-range, for aria-valuetext)
thumbLabels
Range only: names for the two thumbs.
[string, string]["Minimum", "Maximum"]aria-label on each .vin-range__thumb
disabledbooleanfalsedisabled attribute (a range: aria-disabled="true" on .vin-range)

Parts

React, Vue, SvelteHTML 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.