Skip to content
WebJs
Menu

An input where the user selects a value from within a given range.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

export default function SliderDemo() {
  return html`
    <ui-slider class="mx-auto w-full max-w-xs">
      <input type="range" min="0" max="100" step="1" value="75" aria-label="Value" class=${sliderClass()}>
    </ui-slider>
  `;
}
View Code

Installation

pnpm dlx @webjsdev/ui add slider

Usage

import { sliderClass } from '#components/ui/slider.ts';
<ui-slider>
  <input type="range" min="0" max="100" step="1" value="33" aria-label="Volume" class=${sliderClass()}>
</ui-slider>

Range

Use two range inputs for a range slider, one per thumb.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

export default function SliderRange() {
  return html`
    <ui-slider class="mx-auto w-full max-w-xs">
      <input type="range" min="0" max="100" step="5" value="25" aria-label="Minimum" class=${sliderClass()}>
      <input type="range" min="0" max="100" step="5" value="50" aria-label="Maximum" class=${sliderClass()}>
    </ui-slider>
  `;
}
View Code

Multiple Thumbs

Use more range inputs for more thumbs. A thumb never passes its neighbour.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

export default function SliderMultiple() {
  return html`
    <ui-slider class="mx-auto w-full max-w-xs">
      <input type="range" min="0" max="100" step="10" value="10" aria-label="First value" class=${sliderClass()}>
      <input type="range" min="0" max="100" step="10" value="20" aria-label="Second value" class=${sliderClass()}>
      <input type="range" min="0" max="100" step="10" value="70" aria-label="Third value" class=${sliderClass()}>
    </ui-slider>
  `;
}
View Code

Vertical

Use orientation="vertical" for a vertical slider.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

export default function SliderVertical() {
  return html`
    <div class="mx-auto flex w-full max-w-xs items-center justify-center gap-6">
      <ui-slider orientation="vertical" class="h-40">
        <input type="range" min="0" max="100" step="1" value="50" aria-label="First value" class=${sliderClass()}>
      </ui-slider>
      <ui-slider orientation="vertical" class="h-40">
        <input type="range" min="0" max="100" step="1" value="25" aria-label="Second value" class=${sliderClass()}>
      </ui-slider>
    </div>
  `;
}
View Code

Controlled

The inputs hold the value, so the server renders the current one into them and into any readout beside them. Listen for ui-value-change to follow the thumbs as they move: its detail.value is every thumb's value, in order.

0.3, 0.7
import { html } from '@webjsdev/core';
import { labelClass } from '#components/ui/label.ts';
import { sliderClass } from '#components/ui/slider.ts';

// The settled values are server-rendered beside the label. A listener for
// ui-value-change (detail.value) keeps them current as the thumbs move.
export default function SliderControlled() {
  const value = [0.3, 0.7];

  return html`
    <div class="mx-auto grid w-full max-w-xs gap-3">
      <div class="flex items-center justify-between gap-2">
        <label data-slot="label" for="slider-demo-temperature" class=${labelClass()}>Temperature</label>
        <span class="text-sm text-muted-foreground">
          ${value.join(', ')}
        </span>
      </div>
      <ui-slider>
        <input type="range" id="slider-demo-temperature" min="0" max="1" step="0.1" value=${value[0]} class=${sliderClass()}>
        <input type="range" min="0" max="1" step="0.1" value=${value[1]} aria-label="Maximum temperature" class=${sliderClass()}>
      </ui-slider>
    </div>
  `;
}
View Code

Disabled

Use the disabled attribute on the inputs to disable the slider.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

export default function SliderDisabled() {
  return html`
    <ui-slider class="mx-auto w-full max-w-xs">
      <input type="range" min="0" max="100" step="1" value="50" aria-label="Value" disabled class=${sliderClass()}>
    </ui-slider>
  `;
}
View Code

RTL

To enable RTL support, see the RTL configuration guide.

import { html } from '@webjsdev/core';
import { sliderClass } from '#components/ui/slider.ts';

const translations = {
  en: { dir: 'ltr' },
  ar: { dir: 'rtl' },
  he: { dir: 'rtl' },
};

export default function SliderRtl() {
  const lang = 'ar';
  const { dir } = translations[lang];

  return html`
    <ui-slider dir=${dir} lang=${lang} class="mx-auto w-full max-w-xs">
      <input type="range" min="0" max="100" step="1" value="75" aria-label="Value" class=${sliderClass()}>
    </ui-slider>
  `;
}
View Code

API Reference

ui-slider

<ui-slider> enhances one native <input type="range"> per thumb. Without JavaScript the page shows the inputs and the form submits them. With JavaScript the element hides them visually, draws the styled track, range and thumbs, and writes every move back into the inputs, so they still submit and reset with the form. min, max and step are read from the first input.

AttributeTypeDefault
orientation"horizontal" | "vertical""horizontal"
EventDetailFired when
ui-value-change{ value: number[] }A thumb moves.
ui-value-commit{ value: number[] }A drag ends or a key press moves a thumb.

Each input also fires its own input and change events. Every thumb is a focusable role="slider" named by its input's label or aria-label, so give every input a name.

sliderClass

sliderClass() styles one native range input. A single value needs nothing more and works with no JavaScript at all.