Slider
Slider (슬라이더)
범위 선택을 허용하는 컨트롤 요소예요. 단일 값 또는 범위(여러 thumb)를 선택할 수 있고, 마커·수직 방향·원점 등 다양한 동작을 지원해요.
출처: 문서
본문
구조 (Anatomy)
슬라이더의 기본 구조는 다음과 같아요. Root 안에 Label, ValueText, Control(트랙과 thumb), 그리고 필요한 경우 MarkerGroup을 배치해요.
<Slider.Root>
<Slider.Label />
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
<Slider.MarkerGroup>
<Slider.Marker />
</Slider.MarkerGroup>
</Slider.Root>
예제 (Examples)
기본 (Basic)
defaultValue로 선택될 항목을 지정해요. Slider.Thumb의 index prop으로 thumb 순서를 정해요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const Basic = () => {
return (
<Slider.Root className={styles.Root} defaultValue={[40]}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
범위 (Range)
Slider.Thumb를 여러 개 추가해서 슬라이더에 여러 thumb를 넣을 수 있어요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const Range = () => {
return (
<Slider.Root defaultValue={[30, 60]} className={styles.Root}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
<Slider.Thumb index={1} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
마커 (Marks)
Slider.MarkerGroup과 Slider.Marker 컴포넌트를 사용해서 슬라이더 트랙에 마커를 추가할 수 있어요.
Slider.Marker 컴포넌트는 value prop을 제공해서 트랙 기준으로 위치를 정해요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const WithMarks = () => {
return (
<Slider.Root className={styles.Root} defaultValue={[50]}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
<Slider.MarkerGroup className={styles.MarkerGroup}>
{[0, 25, 50, 75, 100].map((value) => (
<Slider.Marker key={value} value={value} className={styles.Marker}>
{value}
</Slider.Marker>
))}
</Slider.MarkerGroup>
</Slider.Root>
)
}
최소값과 최대값 (Min and Max)
기본적으로 최소값은 0, 최대값은 100이에요. 원하지 않는다면 min과/또는 max prop 값을 바꿔서 쉽게 범위를 지정할 수 있어요.
예를 들어 -10과 10 사이의 값을 사용자에게 요청하려면 다음과 같이 할 수 있어요:
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const MinMax = () => {
return (
<Slider.Root min={-10} max={10} defaultValue={[5]} className={styles.Root}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
세밀도 (Granularity)
기본적으로 세밀도(granularity)는 1이라서 값이 항상 정수예요. step 속성을 바꿔서 세밀도를 제어할 수 있어요.
예를 들어 5와 10 사이의 값을 소수점 둘째 자리까지 정확하게 필요하다면 step 값을 0.01로 설정해야 해요:
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const Step = () => {
return (
<Slider.Root step={0.01} min={5} max={10} defaultValue={[7.5]} className={styles.Root}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
변경 이벤트 (Change Events)
슬라이더 값이 바뀌면 onValueChange와 onValueChangeEnd 콜백이 호출돼요. 이것을 사용해 앱에서 커스텀 동작을 설정할 수 있어요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const OnEvent = () => {
return (
<Slider.Root
onValueChange={(details) => console.log('onValueChange', details.value)}
onValueChangeEnd={(details) => console.log('onValueChangeEnd', details.value)}
className={styles.Root}
>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
방향 (Orientation)
기본적으로 슬라이더는 가로 방향으로 가정돼요. 방향을 세로로 바꾸려면 머신 컨텍스트의 orientation 속성을 vertical로 설정해요.
이 모드에서 슬라이더는 위/아래 화살표 키로 값을 증가/감소시켜요.
세로 슬라이더의 높이를 지정해서 스타일을 바꾸는 것을 잊지 마세요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const Vertical = () => {
return (
<Slider.Root orientation="vertical" className={styles.Root}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
원점 (Origin)
기본적으로 슬라이더의 원점은 트랙의 시작점이에요. 원점을 트랙의 중앙으로 바꾸려면 origin prop을 center로 설정해요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const CenterOrigin = () => {
return (
<Slider.Root origin="center" className={styles.Root} defaultValue={[75]}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
루트 프로바이더 (Root Provider)
슬라이더를 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useSlider 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.
import { Slider, useSlider } from '@ark-ui/react/slider'
import button from 'styles/button.module.css'
import styles from 'styles/slider.module.css'
export const RootProvider = () => {
const slider = useSlider()
return (
<>
<button className={button.Root} onClick={() => slider.focus()}>
Focus
</button>
<Slider.RootProvider value={slider} className={styles.Root}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.RootProvider>
</>
)
}
드래그 표시기 (Dragging Indicator)
Slider.DraggingIndicator 컴포넌트를 Slider.Thumb 안에 사용해서 thumb가 드래그되는 동안 시각적 표시기를 표시해요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const DraggingIndicator = () => {
return (
<Slider.Root className={styles.Root} defaultValue={[40]}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.DraggingIndicator className={styles.DraggingIndicator} />
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
Thumb 겹침 방지 (Thumb Overlap)
minStepsBetweenThumbs prop을 사용해서 범위 슬라이더의 thumb가 서로 겹치지 않도록 해요. 이렇게 하면 thumb 사이에 최소 간격이 보장되며, 가격 범위 필터 같은 사용 사례에 유용해요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const ThumbOverlap = () => {
return (
<Slider.Root minStepsBetweenThumbs={5} defaultValue={[25, 60]} className={styles.Root}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
<Slider.Thumb index={1} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
Thumb 충돌 (Thumb Collision)
thumbCollisionBehavior prop을 사용해서 포인터 상호작용 중 thumb가 충돌할 때의 동작을 제어해요. 지원되는 값은 push(기본값), swap, none이에요.
import { Slider } from '@ark-ui/react/slider'
import styles from 'styles/slider.module.css'
export const ThumbCollision = () => {
return (
<Slider.Root thumbCollisionBehavior="push" defaultValue={[25, 60]} className={styles.Root}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Slider.Label className={styles.Label}>Label</Slider.Label>
<Slider.ValueText className={styles.ValueText} />
</div>
<Slider.Control className={styles.Control}>
<Slider.Track className={styles.Track}>
<Slider.Range className={styles.Range} />
</Slider.Track>
<Slider.Thumb index={0} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
<Slider.Thumb index={1} className={styles.Thumb}>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
</Slider.Root>
)
}
API 레퍼런스 (API Reference)
속성 (Props)
Root
Props
aria-label
Type: string[]
Required: false
Default Value: undefined
Description: The aria-label of each slider thumb. Useful for providing an accessible name to the slider
aria-labelledby
Type: string[]
Required: false
Default Value: undefined
Description: The id of the elements that labels each slider thumb. Useful for providing an accessible name to the slider
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
defaultValue
Type: number[]
Required: false
Default Value: undefined
Description: The initial value of the slider when rendered.
Use when you don't need to control the value of the slider.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: Whether the slider is disabled
form
Type: string
Required: false
Default Value: undefined
Description: The associate form of the underlying input element.
getAriaValueText
Type: (details: ValueTextDetails) => string
Required: false
Default Value: undefined
Description: Function that returns a human readable value for the slider thumb
id
Type: string
Required: false
Default Value: undefined
Description: The unique identifier of the machine.
ids
Type: Partial<{ root: string thumb: (index: number) => string hiddenInput: (index: number) => string control: string track: string range: string label: string valueText: string marker: (index: number) => string }>
Required: false
Default Value: undefined
Description: The ids of the elements in the slider. Useful for composition.
invalid
Type: boolean
Required: false
Default Value: undefined
Description: Whether the slider is invalid
largeStep
Type: number
Required: false
Default Value: 10 * step
Description: The step value of the slider when the Shift key is held, or the
PageUp/PageDown keys are used.
max
Type: number
Required: false
Default Value: 100
Description: The maximum value of the slider
min
Type: number
Required: false
Default Value: 0
Description: The minimum value of the slider
minStepsBetweenThumbs
Type: number
Required: false
Default Value: 0
Description: The minimum permitted steps between multiple thumbs.
minStepsBetweenThumbs * step should reflect the gap between the thumbs.
step: 1andminStepsBetweenThumbs: 10=> gap is10step: 10andminStepsBetweenThumbs: 2=> gap is20
name
Type: string
Required: false
Default Value: undefined
Description: The name associated with each slider thumb (when used in a form)
onFocusChange
Type: (details: FocusChangeDetails) => void
Required: false
Default Value: undefined
Description: Function invoked when the slider's focused index changes
onValueChange
Type: (details: ValueChangeDetails) => void
Required: false
Default Value: undefined
Description: Function invoked when the value of the slider changes
onValueChangeEnd
Type: (details: ValueChangeDetails) => void
Required: false
Default Value: undefined
Description: Function invoked when the slider value change is done
orientation
Type: 'horizontal' | 'vertical'
Required: false
Default Value: "horizontal"
Description: The orientation of the slider
origin
Type: 'center' | 'start' | 'end'
Required: false
Default Value: "start"
Description: The origin of the slider range. The track is filled from the origin
to the thumb for single values.
- "start": Useful when the value represents an absolute value
- "center": Useful when the value represents an offset (relative)
- "end": Useful when the value represents an offset from the end
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: Whether the slider is read-only
step
Type: number
Required: false
Default Value: 1
Description: The step value of the slider
thumbAlignment
Type: 'center' | 'contain'
Required: false
Default Value: "contain"
Description: The alignment of the slider thumb relative to the track
center: the thumb will extend beyond the bounds of the slider track.contain: the thumb will be contained within the bounds of the track.
thumbCollisionBehavior
Type: 'none' | 'push' | 'swap'
Required: false
Default Value: "none"
Description: Controls how thumbs behave when they collide during pointer interactions.
none(default): Thumbs cannot move past each other; excess movement is ignored.push: Thumbs push each other without restoring their previous positions when dragged back.swap: Thumbs swap places when dragged past each other.
thumbSize
Type: { width: number; height: number }
Required: false
Default Value: undefined
Description: The slider thumbs dimensions
value
Type: number[]
Required: false
Default Value: undefined
Description: The controlled value of the slider
데이터 속성 (Data Attributes)
data-scope: slider
data-part: root
data-disabled: Present when disabled
data-orientation: The orientation of the slider
data-dragging: Present when in the dragging state
data-invalid: Present when invalid
data-focus: Present when focused
Control
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: control
data-dragging: Present when in the dragging state
data-disabled: Present when disabled
data-orientation: The orientation of the control
data-invalid: Present when invalid
data-focus: Present when focused
DraggingIndicator
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: dragging-indicator
data-orientation: The orientation of the draggingindicator
data-state: "open" | "closed"
HiddenInput
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Label
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: label
data-disabled: Present when disabled
data-orientation: The orientation of the label
data-invalid: Present when invalid
data-dragging: Present when in the dragging state
data-focus: Present when focused
MarkerGroup
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: marker-group
data-orientation: The orientation of the markergroup
Marker
Props
value
Type: number
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: marker
data-orientation: The orientation of the marker
data-value: The value of the item
data-disabled: Present when disabled
data-state:
Range
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: range
data-dragging: Present when in the dragging state
data-focus: Present when focused
data-invalid: Present when invalid
data-disabled: Present when disabled
data-orientation: The orientation of the range
RootProvider
Props
value
Type: UseSliderReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Thumb
Props
index
Type: number
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
name
Type: string
Required: false
Default Value: undefined
Description: undefined
데이터 속성 (Data Attributes)
data-scope: slider
data-part: thumb
data-index: The index of the item
data-name:
data-disabled: Present when disabled
data-orientation: The orientation of the thumb
data-focus: Present when focused
data-dragging: Present when in the dragging state
Track
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: track
data-disabled: Present when disabled
data-invalid: Present when invalid
data-dragging: Present when in the dragging state
data-orientation: The orientation of the track
data-focus: Present when focused
ValueText
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: slider
data-part: value-text
data-disabled: Present when disabled
data-orientation: The orientation of the valuetext
data-invalid: Present when invalid
data-focus: Present when focused
컨텍스트 (Context)
API:
| Property | Type | Description |
|---|---|---|
value |
number[] |
The value of the slider. |
dragging |
boolean |
Whether the slider is being dragged. |
focused |
boolean |
Whether the slider is focused. |
setValue |
(value: number[]) => void |
Function to set the value of the slider. |
getThumbValue |
(index: number) => number |
Returns the value of the thumb at the given index. |
setThumbValue |
(index: number, value: number) => void |
Sets the value of the thumb at the given index. |
getValuePercent |
(value: number) => number |
Returns the percent of the thumb at the given index. |
getPercentValue |
(percent: number) => number |
Returns the value of the thumb at the given percent. |
getThumbPercent |
(index: number) => number |
Returns the percent of the thumb at the given index. |
setThumbPercent |
(index: number, percent: number) => void |
Sets the percent of the thumb at the given index. |
getThumbMin |
(index: number) => number |
Returns the min value of the thumb at the given index. |
getThumbMax |
(index: number) => number |
Returns the max value of the thumb at the given index. |
increment |
(index: number) => void |
Function to increment the value of the slider at the given index. |
decrement |
(index: number) => void |
Function to decrement the value of the slider at the given index. |
focus |
VoidFunction |
Function to focus the slider. This focuses the first thumb. |
접근성 (Accessibility)
Slider WAI-ARIA design pattern을 준수해요.
키보드 지원 (Keyboard Support)
ArrowRight
Description: Increments the slider based on defined step
ArrowLeft
Description: Decrements the slider based on defined step
ArrowUp
Description: Increases the value by the step amount.
ArrowDown
Description: Decreases the value by the step amount.
PageUp
Description: Increases the value by the largeStep amount.
PageDown
Description: Decreases the value by the largeStep amount.
Shift + ArrowUp
Description: Increases the value by the largeStep amount.
Shift + ArrowDown
Description: Decreases the value by the largeStep amount.
Home
Description: Sets the value to its minimum.
End
Description: Sets the value to its maximum.