비교
비교 (Compare)
컴파운드 구성(compound composition) 방식의, 스타일 없는 접근성 좋은 before/after 비교 컴포넌트예요.
출처: 문서
본문
Features (기능)
- 4개의 하위 컴포넌트(
Root,Item,Handle,Indicator)를 갖춘 컴파운드 API예요. - before/after 레이어를 위한
clipPath계산을 자동으로 처리해요. - 가로/세로 방향(orientation)을 지원해요.
- 포인터를 따라가는 슬라이드-온-호버(slide-on-hover) 모드를 지원해요.
Usage (사용법)
import { Compare } from 'primereact/compare';
<Compare.Root>
<Compare.Item position="before"></Compare.Item>
<Compare.Item position="after"></Compare.Item>
<Compare.Handle>
<Compare.Indicator />
</Compare.Handle>
</Compare.Root>
Behavior (동작)
다형성 렌더링 (Polymorphic Rendering)
어떤 하위 컴포넌트든 as를 써서 렌더링되는 HTML 요소를 바꿀 수 있어요.
<Compare.Root as="section">
<Compare.Item as="figure" position="before">
...
</Compare.Item>
<Compare.Item as="figure" position="after">
...
</Compare.Item>
<Compare.Handle as="div">
<Compare.Indicator as="div">...</Compare.Indicator>
</Compare.Handle>
</Compare.Root>
기본 요소: Root=div, Item=div, Handle=span, Indicator=span이에요.
렌더 함수 children (Render Function Children)
Indicator는 children으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요. 인스턴스는 compare(루트 인스턴스로 state.value와 state.isDragging 포함)를 노출해요.
<Compare.Indicator>{(instance) => <span>{instance.compare?.state.value}%</span>}</Compare.Indicator>
API
CompareRoot
| Attribute | Value |
|---|---|
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-dragging |
Present when dragging |
CompareItem
| Attribute | Value |
|---|---|
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-dragging |
Present when dragging |
CompareHandle
| Attribute | Value |
|---|---|
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-dragging |
Present when dragging |
CompareIndicator
| Attribute | Value |
|---|---|
data-orientation |
"horizontal" or "vertical" |
data-disabled |
Present when disabled |
data-dragging |
Present when dragging |
Accessibility (접근성)
스크린 리더 (Screen Reader)
Compare 컴포넌트는 내부적으로 네이티브 range slider를 사용해요. 컴포넌트를 설명하는 값을 aria-labelledby와 aria-label props로 정의할 수 있어요.
<span id="compare_label">Compare Images</span>
<Compare.Root aria-labelledby="compare_label">...</Compare.Root>
<Compare.Root aria-label="Compare Images">...</Compare.Root>
키보드 지원 (Keyboard Support)
| Key | Function |
|---|---|
tab |
Moves focus to the component. |
left arrow / up arrow |
Decrements the value. |
right arrow / down arrow |
Increments the value. |
home |
Set the minimum value. |
end |
Set the maximum value. |
page up |
Increments the value by 10 steps. |
page down |
Decrements the value by 10 steps. |