Collapsible (접기/펼치기)
콜랩시블(Collapsible)은 콘텐츠를 펼치거나 접을 수 있는 대화형 컴포넌트예요. FAQ, 상세 정보, 메뉴처럼 필요한 때만 내용을 보여주고 싶을 때 유용하게 사용해요.
출처: 문서
본문
Import (불러오기)
import { Collapsible } from "@kobalte/core/collapsible";
// or
import { Root, Trigger, ... } from "@kobalte/core/collapsible";
// or (deprecated)
import { Collapsible } from "@kobalte/core";
Features (기능)
- WAI ARIA Disclosure 디자인 패턴을 따릅니다.
- 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
Anatomy (구조)
콜랩시블은 다음의 부분들로 이루어져요:
- Collapsible: 콜랩시블의 루트 컨테이너예요.
- Collapsible.Trigger: 콜랩시블 콘텐츠를 펼치거나 접는 버튼이에요.
- Collapsible.Content: 콜랩시블이 펼쳐졌을 때 렌더링할 콘텐츠를 담아요.
<Collapsible>
<Collapsible.Trigger />
<Collapsible.Content />
</Collapsible>
Example (예제)
기본 예제를 통해 콜랩시블의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.
Usage (사용법)
Animating content size (콘텐츠 크기 애니메이션)
콘텐츠가 열리고 닫힐 때 크기를 애니메이션으로 만들 수 있도록, CSS 커스텀 프로퍼티 --kb-collapsible-content-width와 --kb-collapsible-content-height를 제공해요.
.collapsible__content {
overflow: hidden;
animation: slideUp 300ms cubic-bezier(0.87, 0, 0.13, 1);
}
.collapsible__content[data-expanded] {
animation: slideDown 300ms cubic-bezier(0.87, 0, 0.13, 1);
}
@keyframes slideDown {
from {
height: 0;
}
to {
height: var(--kb-collapsible-content-height);
}
}
@keyframes slideUp {
from {
height: var(--kb-collapsible-content-height);
}
to {
height: 0;
}
}
API Reference (API 참조)
Collapsible
Collapsible는 @kobalte/core/collapsible의 Root 임포트(그리고 더 이상 사용되지 않는 Collapsible.Root)와 동일해요.
| Prop | Description |
|---|---|
| open | boolean The controlled open state of the collapsible. |
| defaultOpen | boolean The default open state when initially rendered. Useful when you do not need to control the open state. |
| onOpenChange | (open: boolean) => void Event handler called when the open state of the collapsible changes. |
| disabled | boolean Whether the collapsible is disabled. |
| forceMount | boolean Used to force mounting the collapsible content when more control is needed. Useful when controlling animation with SolidJS animation libraries. |
| Data attribute | Description |
|---|---|
| data-expanded | Present when the collapsible is expanded. |
| data-closed | Present when the collapsible is collapsed. |
| data-disabled | Present when the collapsible is disabled. |
Collapsible.Trigger와 Collapsible.Content는 동일한 data-attribute를 공유해요.
Rendered elements (렌더링되는 요소)
| Component | Default rendered element |
|---|---|
Collapsible |
div |
Collapsible.Trigger |
button |
Collapsible.Content |
div |
Accessibility (접근성)
Keyboard Interactions (키보드 상호작용)
| Key | Description |
|---|---|
| Space | When focus is on the trigger, opens/closes the collapsible. |
| Enter | When focus is on the trigger, opens/closes the collapsible. |
더 알아보기 (Learn more)
- Accordion 컴포넌트 — 여러 섹션을 다루는 콜랩시블 응용
- Kobalte 시작하기 — 프로젝트 설정
- WAI ARIA Disclosure 패턴 — 접근성 가이드라인