탐색

v2.1.0
폼

범위 슬라이더

최저가와 최고가처럼 하나의 트랙에서 두 값을 선택하고 그 사이를 색으로 채웁니다.

이 페이지의 내용

범위 슬라이더는 최저가와 최고가, 클립의 시작점과 끝점처럼 하나의 눈금에서 두 값을 선택하는 데 사용합니다. 네이티브 슬라이더 두 개를 하나의 그룹으로 묶습니다. 그룹은 트랙 하나를 그려 두 손잡이 사이를 색으로 채웁니다. 각 입력 요소에는 고유한 키보드 조작, 레이블, 폼 값이 유지됩니다.

범위 슬라이더

<div class="flex flex-col gap-sm max-w(360) mx-auto">  <div class="flex items-baseline justify-between text-sm">    <span class="fg-text-strong" id="price">Price</span>    <output class="fg-text-muted" id="price-output">$120 to $380</output>  </div>  <div class="range" role="group" aria-labelledby="price">    <input type="range" class="slider range-start" min="0" max="500" step="10" value="120" aria-label="Lowest price" aria-valuetext="$120">    <input type="range" class="slider range-end" min="0" max="500" step="10" value="380" aria-label="Highest price" aria-valuetext="$380">  </div></div><script>(function () {var range = document.querySelector('.range')var output = document.getElementById('price-output')range.addEventListener('input', function () {  var values = Array.from(range.querySelectorAll('.slider'), function (slider) {    // A thumb says its value as the page prints it, in dollars, not as a bare number.    slider.setAttribute('aria-valuetext', '$' + slider.value)    return Number(slider.value)  })  output.textContent = '$' + Math.min.apply(null, values) + ' to $' + Math.max.apply(null, values)})})()</script>
어느 손잡이든 드래그하거나 포커스를 둔 뒤 방향키로 조작하세요. 스크립트는 트랙 위에 숫자 두 개를 표시하는 데만 사용됩니다.

#스크립트 없는 채우기

단일 슬라이더가 채울 구간을 계산할 때와 마찬가지로, 각 손잡이는 해당 입력 요소 안에서 뷰 타임라인을 이룹니다. 그룹은 timeline-scope로 두 타임라인을 가져와 각각에 애니메이션을 하나씩 적용해 백분율 두 개를 구합니다. 작은 값부터 큰 값까지 칠하므로 두 손잡이가 서로 지나쳐도 그 사이가 채워지고, 두 값 중 작은 값이 계속 범위의 시작점이 됩니다.

폼 핸들러에서도 두 값 중 작은 값을 하한으로 처리하세요. 두 값이 서로 교차하면 안 되거나 일정 간격을 유지해야 한다면 동작을 연결하세요. 이런 제한이 없어도 채우기 표시는 동작합니다.

브라우저가 스크롤 기반 애니메이션을 지원하지 않으면 두 손잡이 아래의 트랙은 채워지지 않은 오목한 영역으로 표시되며, 두 입력 요소는 평소대로 동작합니다. Chromium과 Safari 26에서는 손잡이 사이가 채워집니다.

#두 손잡이의 트랙 공유 방식

두 입력 요소는 같은 그리드 셀에 놓입니다. 각 입력 요소는 자체 트랙을 표시하지 않고 손잡이에서만 포인터 입력을 받습니다. 따라서 손잡이 사이를 눌러도 어느 쪽에도 입력이 전달되지 않으며, 손잡이가 다른 입력 요소의 빈 트랙에 가려지지도 않습니다. 움직이는 손잡이는 맨 앞으로 올라옵니다. 트랙의 빈 부분을 누르면 두 손잡이 중 어느 쪽을 움직여야 할지 알 수 없으므로 아무 동작도 일어나지 않습니다.

#간격 유지

range 동작은 두 손잡이가 서로 떨어져 있게 합니다. range-start는 range-end를 넘어가지 않으며, distance를 지정하면 두 손잡이가 그 간격만큼 떨어진 곳에서 멈춥니다. 움직이는 손잡이가 멈추며, 가만히 있는 손잡이가 밀려나는 일은 없습니다. 이 동작은 손잡이를 움직여 발생한 input 이벤트를 처리하는 중에 값을 보정합니다. 보정은 입력 요소나 주변 요소의 리스너가 이벤트를 받기 전에 이루어집니다. 따라서 모든 리스너는 간격이 유지된 값 쌍을 읽고, 손잡이에는 거부된 값이 표시되지 않습니다. 키보드 조작, 드래그, 폼 값은 그대로 입력 요소가 담당합니다.

간격 유지

<div class="flex flex-col gap-sm max-w(360) mx-auto">  <div class="flex items-baseline justify-between text-sm">    <span class="fg-text-strong" id="hours">Opening hours</span>    <output class="fg-text-muted" id="hours-output">9:00 to 17:00</output>  </div>  <div class="range" id="hours-range" role="group" aria-labelledby="hours">    <input type="range" class="slider range-start" min="0" max="24" value="9" aria-label="Opens at" aria-valuetext="9:00">    <input type="range" class="slider range-end" min="0" max="24" value="17" aria-label="Closes at" aria-valuetext="17:00">  </div></div><script>addEventListener('DOMContentLoaded', function () {var range = document.getElementById('hours-range')MasterUI.range(range, { distance: 2 })range.addEventListener('input', function () {  var start = range.querySelector('.range-start')  var end = range.querySelector('.range-end')  // Each thumb says its hour as a time, as the page prints it.  start.setAttribute('aria-valuetext', start.value + ':00')  end.setAttribute('aria-valuetext', end.value + ':00')  document.getElementById('hours-output').textContent = start.value + ':00 to ' + end.value + ':00'})})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
한쪽 손잡이를 다른 손잡이 쪽으로 드래그해 보세요. 두 손잡이는 두 시간 간격을 두고 멈춥니다.

attach()는 범위 슬라이더를 플랫폼 기본 상태 그대로 두므로 두 손잡이가 교차할 수 있습니다. 두 손잡이를 떨어뜨려 두려면 이 미리 보기처럼 범위 슬라이더에 range를 호출하고, 일정 간격을 유지하려면 distance를 함께 지정하세요. 같은 루트에 두 번째로 호출하면 첫 번째 호출을 대체합니다. MasterUI는 미리 보기가 불러오는 번들입니다. 프로젝트에서는 range를 가져오세요.

app.js
import { range } from '@master/ui/behaviors/range.js'const detach = range(document.querySelector('.range'), { distance: 2 })

간격은 입력 요소 자체의 단위를 따릅니다. step의 배수로 지정하세요. 입력 요소는 기록된 값을 단계에 맞춰 반올림하며, 동작은 간격을 유지하는 가장 가까운 단계 값을 사용합니다.

#비활성화

두 입력 요소를 모두 비활성화하세요. 손잡이와 채워진 구간이 함께 흐려집니다.

비활성화

<div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>  <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled></div>
<div className="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" className="slider range-start" defaultValue="20" aria-label="Fewest seats" disabled />  <input type="range" className="slider range-end" defaultValue="60" aria-label="Most seats" disabled /></div>
<div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>  <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled></div>
<template>  <div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">    <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>    <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled>  </div></template>

#접근성

  • 그룹의 역할은 role="group"으로 지정하고 이름도 부여하세요. 각 입력 요소에는 aria-label 속성으로 별도의 이름을 지정하세요. ‘시작’과 ‘끝’ 대신 ‘최저가’와 ‘최고가’처럼 값의 의미가 드러나는 이름을 사용하세요.
  • 트랙 근처에 두 값을 표시하세요. 손잡이 자체에는 숫자가 표시되지 않습니다.
  • 탭 키로 각 손잡이에 포커스를 둘 수 있으며, 네이티브 슬라이더와 동일한 키로 조작할 수 있습니다. 터치스크린에서 손잡이 크기는 24px입니다. 손잡이는 그 자체 말고는 조작 영역이 없어 가장자리 밖으로 조작 영역을 넓힐 수 없으므로, 라이브러리에서 유일하게 커지는 부분입니다.
  • 눈금 위에서 값을 조절하는 감각보다 정확한 수치 입력이 중요하다면 스테퍼 두 개나 숫자 입력 필드 두 개도 제공하세요.

#클래스

클래스설명
range두 슬라이더를 감싸는 그룹입니다. 트랙 하나를 공유하며 두 손잡이 사이를 채웁니다. role="group"과 이름을 지정하세요.
range-start두 슬라이더 입력 요소 중 하나에 적용해 채움 영역의 기준이 되는 손잡이를 식별합니다.
range-end나머지 슬라이더 입력 요소에 적용해 채움 영역의 기준이 되는 다른 손잡이를 식별합니다.

#사용자 지정 속성

속성기본값설명
--range-track.375rem트랙 두께
--range-colorvar(--color-surface-inverse)채움 색상

#소스

@master/ui/components/range.css
/* * Range: two values on one track: a lowest and a highest price, the start and the end of a clip. * It is two native range inputs, each a slider, laid over each other in one group. The group draws the track * and fills it with ink between the two thumbs; the inputs keep only their thumbs, and only the thumbs take * the pointer, so either one can be reached where they stand close. Each input keeps its own keys, its own * label and its own value in the form. * The fill needs no script. Each thumb is a view timeline in its own input, and the group reads both: it * paints from the lesser reading to the greater, so the thumbs may cross and the fill is still between them. * Without scroll-driven animations the track stays a plain well under the two thumbs. */@property --range-from {  syntax: '<percentage>';  inherits: true;  initial-value: 0%;}@property --range-to {  syntax: '<percentage>';  inherits: true;  initial-value: 100%;}@layer components.ui {  .range {    --range-track: .375rem;    --range-color: var(--color-surface-inverse);    /* Until a timeline says otherwise the fill is empty: both ends stand at the start. */    --range-fill-from: 0%;    --range-fill-to: 0%;    position: relative;    /* The slider in use comes forward within the range, never over the page around it. */    isolation: isolate;    display: grid;    min-width: 0;    margin: 0;    padding: 0;    border: 0;    /* The track: one well for both thumbs, filled between them. */    &::before {      content: '';      position: absolute;      inset: 50% 0 auto;      height: var(--range-track);      translate: 0 -50%;      border-radius: 999px;      background:        linear-gradient(90deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)        var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      pointer-events: none;    }    &:dir(rtl)::before {      background:        linear-gradient(270deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)        var(--color-surface-inset);    }    /*     * Disabled, the range fades as one control, as a lone slider fades as one input. Were each input faded on its     * own, a thumb would turn translucent over the group's track and the fill would show through it.     */    &:has(.slider:disabled) {      opacity: .5;    }    /*     * The two sliders share one cell. Each gives up its own track and takes the pointer only on its thumb.     * The one being moved comes to the front, so a thumb that was under the other can be picked up again.     */    .slider {      --slider-color: transparent;      /* Positioned, so the thumbs stand over the group's track. */      position: relative;      grid-area: 1 / 1;      pointer-events: none;      &:is(:active, :focus-visible) {        z-index: 1;      }      /* The group does the fading, so the thumb stays solid over the track. */      &:disabled {        opacity: 1;      }      &::-webkit-slider-runnable-track {        background: transparent;        box-shadow: none;      }      &::-webkit-slider-thumb {        pointer-events: auto;      }      &::-moz-range-track {        background: transparent;        box-shadow: none;      }      &::-moz-range-progress {        background: transparent;      }      &::-moz-range-thumb {        pointer-events: auto;      }      /*       * A thumb here is its own only target, and a thumb cannot carry a pseudo-element to reach further, so on       * touch it is drawn at 24px, the least a finger is owed. It is the one part of the library that grows.       */      @media (pointer: coarse) {        --slider-thumb: 1.5rem;      }    }    @media (forced-colors: active) {      --range-color: Highlight;      &::before {        forced-color-adjust: none;        background:          linear-gradient(90deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)          Canvas;        box-shadow: inset 0 0 0 1px CanvasText;      }      &:dir(rtl)::before {        background:          linear-gradient(270deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)          Canvas;      }      /* Disabled, the fill takes the system's disabled ink, as a lone slider's does. */      &:has(.slider:disabled) {        --range-color: GrayText;      }    }  }  /*   * The fill. Each thumb names a view timeline in its input, as a slider's does, and the group makes both   * names its own, runs one animation on each and reads two percentages.   */  @supports (view-timeline: --range-start inline) and (timeline-scope: --range-start) {    .range {      --range-fill-from: min(var(--range-from), var(--range-to));      --range-fill-to: max(var(--range-from), var(--range-to));      timeline-scope: --range-start, --range-end;      animation-name: range-from, range-to;      animation-timing-function: linear;      animation-fill-mode: both;      animation-timeline: --range-start, --range-end;      animation-range: contain;      &:dir(rtl) {        animation-direction: reverse;      }      /* A slider inside a range hands its timeline to the group and runs no fill of its own. */      .slider {        timeline-scope: none;        animation-name: none;      }      .range-start::-webkit-slider-thumb {        view-timeline: --range-start inline;        view-timeline-inset: var(--slider-bleed);      }      .range-end::-webkit-slider-thumb {        view-timeline: --range-end inline;        view-timeline-inset: var(--slider-bleed);      }    }  }  /* The two ends. The classes only name the thumbs for the fill; which value is the lower is the form's to read. */  .range-start, .range-end {    min-width: 0;  }}@keyframes range-from {  from {    --range-from: 100%;  }  to {    --range-from: 0%;  }}@keyframes range-to {  from {    --range-to: 100%;  }  to {    --range-to: 0%;  }}