Collapsible
펼치기/접기 인터페이스를 만들기 위한, 스타일 없는 접이식(collapsible) 컴포넌트예요. 배치와 스타일링을 완전히 제어할 수 있어요.
출처: 문서
본문
기능 (Features)
- 하위 컴포넌트 구성 API:
Root,Trigger,Content - 제어/비제어 방식의 열림 상태 관리
- 명령형 메서드
toggle,open,close - 콜백 props:
onOpen,onClose,onOpenChange
사용법 (Usage)
import { Collapsible } from 'primereact/collapsible';
<Collapsible.Root>
<Collapsible.Trigger></Collapsible.Trigger>
<Collapsible.Content></Collapsible.Content>
</Collapsible.Root>
동작 (Behavior)
동작 애니메이션 (Motion Animation)
Root에 motionProps를 전달해 콘텐츠 요소에 애니메이션을 적용할 수 있어요.
<Collapsible.Root motionProps={{ transition: { duration: 200 } }}>
<Collapsible.Trigger></Collapsible.Trigger>
<Collapsible.Content></Collapsible.Content>
</Collapsible.Root>
애니메이션 단계, CSS 변수, 숨김 전략은 Motion 문서를 참고하세요.
다형적 렌더링 (Polymorphic Rendering)
as를 사용해 하위 컴포넌트가 렌더링할 HTML 요소를 바꿀 수 있어요.
<Collapsible.Root as="section"></Collapsible.Root>
<Collapsible.Trigger as="div"></Collapsible.Trigger>
기본 요소: Root=div, Trigger=button, Content=div.
렌더 함수 자식 (Render Function Children)
Root는 컴포넌트 인스턴스에 접근할 수 있는 렌더 함수를 children으로 받아요.
<Collapsible.Root>{(instance) => <span>{instance.state.open ? 'Open' : 'Closed'}</span>}</Collapsible.Root>
API
CollapsibleRoot
| Attribute | Value |
|---|---|
data-scope |
"collapsible" |
data-part |
"root" |
data-open |
Present when open |
data-closed |
Present when closed |
data-disabled |
Present when disabled |
CollapsibleTrigger
| Attribute | Value |
|---|---|
data-scope |
"collapsible" |
data-part |
"trigger" |
data-content-open |
Present when content open |
data-content-closed |
Present when content closed |
CollapsibleContent
| Attribute | Value |
|---|---|
data-scope |
"collapsible" |
data-part |
"content" |
data-open |
Present when open |
data-closed |
Present when closed |
접근성 (Accessibility)
스크린 리더 (Screen Reader)
트리거는 열림 상태를 반영하는 aria-expanded와 콘텐츠 요소의 id를 참조하는 aria-controls를 가져요. 콘텐츠는 role="region"이며 접힌 상태에서는 aria-hidden이 적용돼요.
키보드 지원 (Keyboard Support)
| Key | Function |
|---|---|
tab |
Moves focus to the trigger element. |
enter |
Toggles the collapsible content open or closed. |
space |
Toggles the collapsible content open or closed. |