Slider (슬라이더)
슬라이더는 주어진 범위 안에서 사용자가 값을 선택할 수 있게 해주는 입력 요소예요. 볼륨 조절이나 가격 범위 설정처럼 연속적인 값을 다룰 때 유용합니다.
출처: 문서
본문
Import (불러오기)
import { Slider } from "@kobalte/core/slider";
// or
import { Root, Track, ... } from "@kobalte/core/slider";
// or (deprecated)
import { Slider } from "@kobalte/core";
Features (기능)
- WAI ARIA Slider 디자인 패턴을 따릅니다.
- 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
- 여러 개의 썸(thumb)을 지원해요.
- 썸 사이의 최소 간격(step)을 지원해요.
- 트랙을 클릭하거나 터치하여 값을 변경할 수 있어요.
- 오른쪽 또는 왼쪽 방향을 지원해요.
- 사용자 지정 값 라벨을 지원해요.
Anatomy (구조)
슬라이더는 다음의 부분들로 이루어져요:
- Slider: 슬라이더의 루트 컨테이너예요.
- Slider.Track: 슬라이더 트랙을 시각적으로 나타내는 컴포넌트예요.
- Slider.Fill: 슬라이더 값을 시각적으로 나타내는 컴포넌트예요.
- Slider.Thumb: 슬라이더의 값을 시각적으로 나타내는 썸(thumb)이에요.
- Slider.Input: 슬라이더 썸에서 시각적으로 숨겨진 네이티브 html input이에요.
- Slider.Label: 슬라이더에 대한 정보를 사용자에게 알려주는 라벨이에요.
- Slider.ValueLabel: 현재 값을 사람이 읽을 수 있는 형식으로 나타내는 접근 가능한 라벨 텍스트예요.
- Slider.Description: 슬라이더에 대한 더 많은 정보를 사용자에게 알려주는 설명이에요.
- Slider.ErrorMessage: 슬라이더의 검증 오류를 어떻게 고치는지에 대한 정보를 사용자에게 알려주는 오류 메시지예요.
<Slider>
<Slider.Label />
<Slider.ValueLabel />
<Slider.Track>
<Slider.Fill />
<Slider.Thumb>
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
<Slider.Description />
<Slider.ErrorMessage />
</Slider>
Example (예제)
기본 예제를 통해 슬라이더의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.
Usage (사용법)
Multiple Thumbs (여러 썸)
defaultValue에 배열 값을 전달하면 여러 개의 썸(쉽게 말해 두 개 이상의 값)을 가진 슬라이더를 만들 수 있어요. 가격 범위를 선택할 때 주로 사용해요.
import { Slider } from "@kobalte/core/slider";
function App() {
return (
<Slider class="SliderRoot" defaultValue={[0, 20]}>
<div class="SliderLabel">
<Slider.Label>Label</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
);
}
Modify step size (간격 크기 조절)
step prop으로 값을 이동하는 간격을 지정할 수 있어요. 아래 예제처럼 step={8}, step={10}, step={20}을 비교해 보면 간격이 어떻게 달라지는지 알 수 있어요.
import { Slider } from "@kobalte/core/slider";
function App() {
return (
<>
<Slider class="SliderRoot" step={8}>
<div class="SliderLabel">
<Slider.Label>Step size 8</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
<Slider class="SliderRoot" step={10}>
<div class="SliderLabel">
<Slider.Label>Step size 10</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
<Slider class="SliderRoot" step={20}>
<div class="SliderLabel">
<Slider.Label>Step size 20</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
</>
);
}
Steps between thumbs (썸 사이의 간격)
minStepsBetweenThumbs prop으로 썸과 썸 사이의 최소 간격(step 수)을 지정할 수 있어요. 아래 예제에서는 썸 사이에 최소 10만큼의 간격이 유지됩니다.
import { Slider } from "@kobalte/core/slider";
function App() {
return (
<Slider class="SliderRoot" defaultValue={[10, 20]} minStepsBetweenThumbs={10}>
<div class="SliderLabel">
<Slider.Label>Label</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
);
}
Vertical Slider (세로 슬라이더)
orientation="vertical" prop을 사용하면 세로 방향 슬라이더를 만들 수 있어요. 스타일링(CSS)도 함께 조정해야 합니다.
import { Slider } from "@kobalte/core/slider";
function App() {
return (
<Slider class="SliderRoot" orientation="vertical">
<div class="SliderLabel">
<Slider.Label>Label</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
);
}
Custom Value Label (사용자 지정 값 라벨)
getValueLabel prop을 사용하면 값 라벨의 표시 형식을 원하는 대로 지정할 수 있어요. 예를 들어 값을 화폐 단위($10 - $500)로 보여줄 수 있답니다.
import { Slider } from "@kobalte/core/slider";
function App() {
return (
<Slider
class="SliderRoot"
minValue={10}
maxValue={2000}
defaultValue={[20, 500]}
getValueLabel={params => `$${params.values[0]} - $${params.values[1]}`}
>
<div class="SliderLabel">
<Slider.Label>Money</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
);
}
Controlled Value (제어 방식 값)
value prop과 onChange 이벤트를 함께 사용하면 값을 제어할 수 있어요.
import { createSignal } from "solid-js";
import { Slider } from "@kobalte/core/slider";
function App() {
const [values, setValues] = createSignal<number[]>([40]);
return (
<Slider class="SliderRoot" value={values()} onChange={setValues}>
<div class="SliderLabel">
<Slider.Label>Label</Slider.Label>
<Slider.ValueLabel />
</div>
<Slider.Track class="SliderTrack">
<Slider.Fill class="SliderRange" />
<Slider.Thumb class="SliderThumb">
<Slider.Input />
</Slider.Thumb>
</Slider.Track>
</Slider>
);
}
API Reference (API 참조)
Slider
Slider은 @kobalte/core/slider의 Root 임포트(그리고 더 이상 사용되지 않는 Slider.Root)와 동일해요.
| Prop | Description |
|---|---|
| value | number[] The controlled values of the slider. Must be used in conjunction with onChange. |
| defaultValue | number[] The value of the slider when initially rendered. Use when you do not need to control the state of the slider. |
| onChange | (value: number[]) => void Event handler called when the value changes. |
| onChangeEnd | (value: number[]) => void Event handler called when the value changes at the end of an interaction. |
| inverted | boolean Whether the slider is visually inverted. Defaults to false. |
| minValue | number The minimum slider value. Defaults to 0 |
| maxValue | number The maximum slider value. Defaults to 100 |
| step | number The stepping interval. Defaults to 1 |
| minStepsBetweenThumbs | number The minimum permitted steps between thumbs. Defaults to 0 |
| getValueLabel | (params: GetValueLabelParams) => string A function to get the accessible label text representing the current value in a human-readable format. If not provided, the value label will be read as a percentage of the max value. |
| orientation | 'horizontal' | 'vertical' The orientation of the slider. |
| name | string The name of the slider, used when submitting an HTML form. |
| validationState | 'valid' | 'invalid' Whether the slider should display its "valid" or "invalid" visual styling. |
| required | boolean Whether the user must check a radio group item before the owning form can be submitted. |
| disabled | boolean Whether the radio group is disabled. |
| readOnly | boolean Whether the radio group items can be selected but not changed by the user. |
| Data attribute | Description |
|---|---|
| data-orientation='horizontal' | Present when the slider has horizontal orientation. |
| data-orientation='vertical' | Present when the slider has vertical orientation. |
| data-valid | Present when the slider is valid according to the validation rules. |
| data-invalid | Present when the slider is invalid according to the validation rules. |
| data-required | Present when the user must slider an item before the owning form can be submitted. |
| data-disabled | Present when the slider is disabled. |
| data-readonly | Present when the slider is read only. |
Slider.ValueLabel, Slider.Fill, Slider.Input, Slider.Thumb 및 Slider.Track는 동일한 data-attribute를 공유해요.
Rendered elements (렌더링되는 요소)
| Component | Default rendered element |
|---|---|
Slider |
div |
Slider.Track |
div |
Slider.Fill |
div |
Slider.Thumb |
span |
Slider.Input |
input |
Slider.ValueLabel |
div |
Slider.Description |
div |
Slider.ErrorMessage |
div |
Accessibility (접근성)
Keyboard Interactions (키보드 상호작용)
| Key | Description |
|---|---|
| PageUp | Increases the value of the focused thumb by a larger step. |
| PageDown | Decreases the value of the focused thumb by a larger step. |
| ArrowDown | Decreases the value of the focused thumb by the step amount. |
| ArrowUp | Increases the value of the focused thumb by the step amount. |
| ArrowRight | Increments/decrements by the step value depending on orientation. |
| ArrowLeft | Increments/decrements by the step value depending on orientation. |
| Home | Sets the value of the first thumb to the minimum value. |
| End | Sets the value of the last thumb to the maximum value. |
더 알아보기 (Learn more)
- WAI ARIA Slider 패턴 — 접근성 가이드라인
- Kobalte 시작하기 — 프로젝트 설정