Color Slider (색상 슬라이더)
색상 값의 개별 채널을 조정할 수 있게 해주는 슬라이더 컴포넌트예요. 가로·세로 방향 모두 지원하며, 트랙을 클릭하거나 터치해 값을 변경할 수 있어요.
출처: 문서
본문
임포트 (Import)
import { ColorSlider } from "@kobalte/core/color-slider";
// or
import { Root, Track, ... } from "@kobalte/core/color-slider";
기능 (Features)
- 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.
- 화면 낭독기 사용자를 위한 현지화된 색상 설명을 제공해요.
- RGBA, HSLA, HSBA 색상의 단일 채널 조정을 가로·세로 방향 모두 지원해요.
- 트랙 클릭 또는 터치로 값을 변경할 수 있어요.
- 오른쪽 또는 왼쪽 방향을 지원해요.
- 사용자 정의 값 라벨을 지원해요.
구조 (Anatomy)
색상 슬라이더는 다음 요소로 구성돼요.
- ColorSlider: 색상 슬라이더의 루트 컨테이너예요.
- ColorSlider.Track: 색상 슬라이더 트랙을 시각적으로 나타내는 컴포넌트예요.
- ColorSlider.Thumb: 색상 슬라이더에서 값을 시각적으로 표시하는 데 사용되는 썸(thumb)이에요.
- ColorSlider.Input: 색상 슬라이더 썸 안에 시각적으로 숨겨진 네이티브 html 입력 요소예요.
- ColorSlider.Label: 색상 슬라이더에 대한 정보를 사용자에게 알려주는 라벨이에요.
- ColorSlider.ValueLabel: 현재 값을 사람이 읽을 수 있는 형식으로 나타내는 접근 가능한 라벨 텍스트예요.
<ColorSlider>
<ColorSlider.Label />
<ColorSlider.ValueLabel />
<ColorSlider.Track>
<ColorSlider.Thumb>
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
예제 (Example)
기본 예제는 공식 문서 페이지에서 ColorSlider, Label, ValueLabel, Track, Thumb, Input 조합과 스타일시트로 확인할 수 있어요.
사용법 (Usage)
defaultValue 또는 value에 제공하는 값은 Color 객체여야 해요. parseColor 함수를 사용해 문자열에서 Color 객체를 얻을 수 있어요. 또한 channel prop으로 슬라이더가 표시할 색 채널을 반드시 지정해야 해요.
이 값은 색상 값에 포함된 채널 중 하나여야 해요. 예를 들어 RGB 색상의 경우 "red", "green", "blue" 채널을 사용할 수 있어요.
기본값 (Default value)
import { ColorSlider } from "@kobalte/core/color-slider";
import { parseColor } from "@kobalte/core/colors";
import "./style.css";
function App() {
return (
<ColorSlider
class="ColorSliderRoot"
defaultValue={parseColor("hsl(0, 100%, 50%)")}
channel="hue"
>
<div class="ColorSliderLabel">
<ColorSlider.Label>Hue</ColorSlider.Label>
<ColorSlider.ValueLabel />
</div>
<ColorSlider.Track class="ColorSliderTrack">
<ColorSlider.Thumb class="ColorSliderThumb">
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
);
}
.ColorSliderRoot {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
user-select: none;
touch-action: none;
width: 200px;
}
.ColorSliderTrack {
position: relative;
border-radius: 6px;
height: 24px;
width: 100%;
}
.ColorSliderThumb {
display: block;
top: 4px;
width: 16px;
height: 16px;
border-radius: 9999px;
border: 2px solid #fff;
box-shadow: 0 0 0 1px #0000006b;
}
.ColorSliderThumb:focus {
outline: none;
}
.ColorSliderLabel {
display: flex;
justify-content: space-between;
width: 100%;
}
제어값 (Controlled value)
import { createSignal } from "solid-js";
import { ColorSlider } from "@kobalte/core/color-slider";
import { parseColor } from "@kobalte/core/colors";
import "./style.css";
function App() {
const [value, setValue] = createSignal(parseColor("hsl(0, 100%, 50%)"));
return (
<ColorSlider class="ColorSliderRoot" value={value()} onChange={setValue} channel="hue">
<div class="ColorSliderLabel">
<ColorSlider.Label>Hue</ColorSlider.Label>
<ColorSlider.ValueLabel />
</div>
<ColorSlider.Track class="ColorSliderTrack">
<ColorSlider.Thumb class="ColorSliderThumb">
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
);
}
.ColorSliderRoot {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
user-select: none;
touch-action: none;
width: 200px;
}
.ColorSliderTrack {
position: relative;
border-radius: 6px;
height: 24px;
width: 100%;
}
.ColorSliderThumb {
display: block;
top: 4px;
width: 16px;
height: 16px;
border-radius: 9999px;
border: 2px solid #fff;
box-shadow: 0 0 0 1px #0000006b;
}
.ColorSliderThumb:focus {
outline: none;
}
.ColorSliderLabel {
display: flex;
justify-content: space-between;
width: 100%;
}
세로 슬라이더 (Vertical Slider)
import { ColorSlider } from "@kobalte/core/color-slider";
import { parseColor } from "@kobalte/core/colors";
import "./style.css";
function App() {
return (
<ColorSlider
class="ColorSliderRoot"
defaultValue={parseColor("#ff00ff")}
channel="red"
orientation="vertical"
>
<div class="ColorSliderLabel">
<ColorSlider.Label>Hue</ColorSlider.Label>
<ColorSlider.ValueLabel />
</div>
<ColorSlider.Track class="ColorSliderTrack">
<ColorSlider.Thumb class="ColorSliderThumb">
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
);
}
.ColorSliderRoot {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
user-select: none;
touch-action: none;
height: 200px;
}
.ColorSliderTrack {
position: relative;
border-radius: 6px;
height: 100%;
width: 24px;
}
.ColorSliderThumb {
display: block;
left: 4px;
width: 16px;
height: 16px;
border-radius: 9999px;
border: 2px solid #fff;
box-shadow: 0 0 0 1px #0000006b;
}
.ColorSliderThumb:focus {
outline: none;
}
.ColorSliderLabel {
display: flex;
justify-content: space-between;
width: 100%;
}
사용자 정의 값 라벨 (Custom Value Label)
import { ColorSlider } from "@kobalte/core/color-slider";
import { parseColor } from "@kobalte/core/colors";
import "./style.css";
function App() {
return (
<ColorSlider
class="ColorSliderRoot"
defaultValue={parseColor("#ff00ff")}
channel="green"
getValueLabel={param => `${param.toFormat("rgb")}`}
>
<div class="ColorSliderLabel">
<ColorSlider.ValueLabel />
</div>
<ColorSlider.Track class="ColorSliderTrack">
<ColorSlider.Thumb class="ColorSliderThumb">
<ColorSlider.Input />
</ColorSlider.Thumb>
</ColorSlider.Track>
</ColorSlider>
);
}
.ColorSliderRoot {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
user-select: none;
touch-action: none;
width: 200px;
}
.ColorSliderTrack {
position: relative;
border-radius: 6px;
height: 24px;
width: 100%;
}
.ColorSliderThumb {
display: block;
top: 4px;
width: 16px;
height: 16px;
border-radius: 9999px;
border: 2px solid #fff;
box-shadow: 0 0 0 1px #0000006b;
}
.ColorSliderThumb:focus {
outline: none;
}
.ColorSliderLabel {
display: flex;
justify-content: space-between;
width: 100%;
}
API 참조 (API Reference)
ColorSlider
ColorSlider는 @kobalte/core/color-slider의 Root 임포트와 동일해요.
| Prop | Description |
|---|---|
| value | Color The controlled value of the slider. Must be used in conjunction with onChange. |
| defaultValue | Color The value of the slider when initially rendered. Use when you do not need to control the state of the slider. |
| channel | ColorChannel The color channel that the slider manipulates. |
| colorSpace | ColorSpace The color space that the slider operates in. The channel must be in this color space. |
| onChange | (value: Color) => void Event handler called when the value changes. |
| onChangeEnd | (value: Color) => void Event handler called when the value changes at the end of an interaction. |
| getValueLabel | (param: Color) => string A function to get the accessible label text representing the current value in a human-readable format. |
| 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. |
| translations | ColorIntlTranslations Localization strings. |
| 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. |
ColorSlider.ValueLabel, ColorSlider.Input, ColorSlider.Thumb 및 ColorSlider.Track는 동일한 data-attributes를 공유해요.
ColorSlider.Thumb
현재 색상은 사용자 정의 CSS 속성 --kb-color-current를 통해 썸에서 확인할 수 있어요.
렌더링 요소 (Rendered elements)
| Component | Default rendered element |
|---|---|
ColorSlider |
div |
ColorSlider.Track |
div |
ColorSlider.Thumb |
span |
ColorSlider.Input |
input |
ColorSlider.ValueLabel |
div |
ColorSlider.Label |
label |
ColorSlider.Description |
div |
ColorSlider.ErrorMessage |
div |
접근성 (Accessibility)
키보드 상호작용 (Keyboard Interactions)
| Key | Description |
|---|---|
| PageUp | Increases the value of the thumb by a larger step. |
| PageDown | Decreases the value of the thumb by a larger step. |
| ArrowDown | Decreases the value of the thumb by the step amount. |
| ArrowUp | Increases the value of the 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 thumb to the minimum value. |
| End | Sets the value of the thumb to the maximum value. |