Slider (슬라이더)
Slider는 범위 안에서 사용자가 하나 이상의 값을 선택할 수 있게 해주는 컴포넌트예요. 볼륨, 밝기, 가격 범위처럼 연속된 값을 다룰 때 딱 좋아요.
출처: HeroUI Slider
본문
사용법 (Usage)
import { Slider } from '@heroui/react';
{"name": "slider-default"}
구조 (Anatomy)
import { Slider, Label } from '@heroui/react';
export default () => (
<Slider>
<Label />
<Slider.Output />
<Slider.Track>
<Slider.Fill />
<Slider.Thumb />
</Slider.Track>
</Slider>
)
예시 (Examples)
비활성화 (Disabled)
{"name": "slider-disabled"}
범위 슬라이더 구조 (Range Slider Anatomy)
import { Slider, Label } from '@heroui/react';
export default () => (
<Slider defaultValue={[25, 75]}>
<Label />
<Slider.Output />
<Slider.Track>
{({state}) => (
<>
<Slider.Fill />
{state.values.map((_, i) => (
<Slider.Thumb key={i} index={i} />
))}
</>
)}
</Slider.Track>
</Slider>
)
세로 (Vertical)
{"name": "slider-vertical"}
범위 (Range)
{"name": "slider-range"}
제어된 값 (Controlled Value)
value와 onChange로 값을 제어할 수 있어요.
{"name": "slider-controlled"}
사용자 지정 값 형식 (Custom Value Formatting)
formatOptions로 표시되는 값을 포맷할 수 있어요. 이 예시는 USD로 포맷한 경우예요.
{"name": "slider-custom-value-formatting"}
사용자 지정 출력 (Custom Output)
Slider.Output를 커스터마이징해 선택된 값이 어떻게 표시될지 제어할 수 있어요.
{"name": "slider-custom-output"}
렌더 함수 (Render Function)
{"name": "slider-render-function"}
커스터마이징 (Customization)
Tailwind CSS
{"name": "slider-custom-styles"}
Global CSS
Slider 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
더 알아보기.
@layer components {
.slider {
@apply flex flex-col gap-2;
}
.slider__output {
@apply text-muted-fg text-sm;
}
.slider-track {
@apply relative h-2 w-full rounded-full bg-surface-secondary;
}
.slider-fill {
@apply absolute h-full rounded-full bg-accent;
}
.slider-thumb {
@apply size-4 rounded-full bg-accent border-2 border-background;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 쉽게 커스터마이징할 수 있게 해요.
CSS 클래스 (CSS Classes)
Slider 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 클래스 (Base Classes) [!toc]
.slider- 기본 슬라이더 컨테이너.slider__output- 현재 값을 표시하는 출력 요소.slider-track- 채움과 썸을 담는 트랙 요소.slider-fill- 선택된 범위를 보여주는 채움 요소.slider-thumb- 개별 썸 요소
상태 클래스 (State Classes) [!toc]
.slider[data-disabled="true"]- 비활성화된 슬라이더 상태.slider[data-orientation="vertical"]- 세로 방향.slider-thumb[data-dragging="true"]- 드래그 중인 썸.slider-thumb[data-focus-visible="true"]- 키보드로 포커스된 썸.slider-thumb[data-disabled="true"]- 비활성화된 썸 상태.slider-track[data-fill-start="true"]- 채움이 시작 지점에서 시작.slider-track[data-fill-end="true"]- 채움이 끝 지점에서 끝
상호작용 상태 (Interactive States)
컴포넌트는 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover): 썸에
:hover또는[data-hovered="true"] - 포커스 (Focus): 썸에
:focus-visible또는[data-focus-visible="true"] - 드래그 (Dragging): 썸에
[data-dragging="true"] - 비활성화 (Disabled): 슬라이더 또는 썸에
:disabled또는[data-disabled="true"]
API 참고 (API Reference)
Slider
| Prop | Type | Default | Description |
|---|---|---|---|
value |
number | number[] |
- | The current value (controlled) |
defaultValue |
number | number[] |
- | The default value (uncontrolled) |
onChange |
(value: number | number[]) => void |
- | Handler called when the value changes |
onChangeEnd |
(value: number | number[]) => void |
- | Handler called when dragging ends |
minValue |
number |
0 |
The slider's minimum value |
maxValue |
number |
100 |
The slider's maximum value |
step |
number |
1 |
The slider's step value |
formatOptions |
Intl.NumberFormatOptions |
- | The display format of the value label |
orientation |
"horizontal" | "vertical" |
"horizontal" |
The orientation of the slider |
isDisabled |
boolean |
- | Whether the slider is disabled |
aria-label |
string |
- | Accessibility label for the slider |
aria-labelledby |
string |
- | ID of element that labels the slider |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Slider content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SliderRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Slider.Output
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Output content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SliderOutputRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Slider.Track
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Track content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SliderTrackRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Slider.Fill
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
- | Additional CSS classes |
style |
CSSProperties |
- | Inline styles |
Slider.Thumb
| Prop | Type | Default | Description |
|---|---|---|---|
index |
number |
0 |
Index of the thumb within the slider |
isDisabled |
boolean |
- | Whether this thumb is disabled |
name |
string |
- | The name of the input element, used when submitting an HTML form |
className |
string |
- | Additional CSS classes |
children |
ReactNode | RenderFunction |
- | Thumb content or render function |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, SliderThumbRenderProps> |
- | Overrides the default DOM element with a custom render function. |
Render Props
Slider.Output 또는 Slider.Track에 렌더 함수를 사용하면 이 값들이 제공돼요:
| Prop | Type | Description |
|---|---|---|
state |
SliderState |
The state of the slider |
values |
number[] |
Values managed by the slider by thumb index |
getThumbValueLabel |
(index: number) => string |
Returns the string label for the specified thumb's value |
orientation |
"horizontal" | "vertical" |
The orientation of the slider |
isDisabled |
boolean |
Whether the slider is disabled |
접근성 (Accessibility)
Slider 컴포넌트는 ARIA 슬라이더 패턴을 구현하며 다음을 제공해요:
- 완전한 키보드 탐색 지원 (화살표 키, Home, End, Page Up/Down)
- 값 변경에 대한 스크린 리더 안내
- 올바른 포커스 관리
- 비활성화 상태 지원
- 숨겨진 입력 요소를 통한 HTML 폼 통합
- 로케일을 고려한 값 포맷의 국제화 지원
- 오른쪽에서 왼쪽(RTL) 언어 지원
자세한 내용은 React Aria Slider 문서를 참고하세요.