Перейти до вмісту

Slider

Цю сторінку ще не перекладено, тому вона показана мовою оригіналу.

A native range input (keyboard, touch and assistive tech for free) on a drawn track, filled in the current tone. The browser moves the thumb; the page sets --ms-slider-progress (0…1) on the wrapper as the value changes.

Example

<div style="display: grid; gap: 1.5rem; max-inline-size: 20rem">
  <span class="ms-slider" style="--ms-slider-progress: 0.5">
    <input
      type="range"
      class="ms-slider__input"
      min="2"
      max="16"
      value="9"
      aria-label="Corner radius"
      oninput="this.parentNode.style.setProperty('--ms-slider-progress', (this.value - this.min) / (this.max - this.min))"
    />
    <span class="ms-slider__track" aria-hidden="true"
      ><span class="ms-slider__fill"></span
    ></span>
  </span>
  <span class="ms-slider ms-tone-tertiary" style="--ms-slider-progress: 0.3">
    <input
      type="range"
      class="ms-slider__input"
      value="30"
      aria-label="Volume"
      oninput="this.parentNode.style.setProperty('--ms-slider-progress', this.value / 100)"
    />
    <span class="ms-slider__track" aria-hidden="true"
      ><span class="ms-slider__fill"></span
    ></span>
  </span>
  <span class="ms-slider" style="--ms-slider-progress: 0.7">
    <input
      type="range"
      class="ms-slider__input"
      value="70"
      aria-label="Disabled"
      disabled
    />
    <span class="ms-slider__track" aria-hidden="true"
      ><span class="ms-slider__fill"></span
    ></span>
  </span>
</div>

Usage

<span class="ms-slider" style="--ms-slider-progress: 0.5">
  <input
    type="range"
    class="ms-slider__input"
    min="2"
    max="16"
    value="9"
    aria-label="Corner radius"
  />
  <span class="ms-slider__track" aria-hidden="true">
    <span class="ms-slider__fill"></span>
  </span>
</span>

Keep the fill in step with the thumb: on input, set the progress, (value - min) / (max - min) (sliderProgress(value, min, max) from @mainset/ui-core computes it, clamped).

import { sliderProgress } from '@mainset/ui-core';

input.addEventListener('input', () => {
  slider.style.setProperty(
    '--ms-slider-progress',
    sliderProgress(input.valueAsNumber, Number(input.min), Number(input.max)),
  );
});
  • The fill is a span growing from the inline start, so it follows the dir of its section (a gradient to right couldn't).
  • Filled and knobbed in the current tone: ms-tone-* on the slider or any ancestor re-colors it.
  • The thumb gets the focus ring; a disabled input dims the track too.
  • Tokens: --ms-slider-track-height, --ms-slider-thumb-size, --ms-slider-track-color…, see the tokens.
  • React: Slider from @mainset/react/elements, controlled (value, onChange).