Collapsible
Collapsible (아코디언 패널)
버튼으로 제어되는 접이식 패널 컴포넌트예요. 버튼을 누르면 패널이 열리고 닫혀요. 고품질의 비스타일(unstyled) React collapse 컴포넌트예요.
본문
버튼으로 제어되는 접이식 패널을 표시하는 고품질의 비스타일 React collapse 컴포넌트예요.
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
import * as React from 'react';
import { Collapsible } from '@base-ui/react/collapsible';
export default function ExampleCollapsible() {
return (
<Collapsible.Root className="flex min-h-36 w-48 flex-col justify-center text-neutral-950 dark:text-white">
<Collapsible.Trigger className="group flex h-8 items-center justify-between gap-2 rounded-none border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 pl-3 pr-2 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400">
Recovery keys
<CaretRightIcon className="transition-transform duration-100 ease-[ease-out] group-data-panel-open:rotate-90" />
</Collapsible.Trigger>
<Collapsible.Panel className="flex h-[var(--collapsible-panel-height)] flex-col justify-end overflow-hidden text-sm transition-[height] duration-150 ease-[ease-out] [&[hidden]:not([hidden='until-found'])]:hidden data-ending-style:h-0 data-starting-style:h-0">
<div className="flex flex-col gap-2 px-3.5 py-2">
<div>alien-bean-pasta</div>
<div>wild-irish-burrito</div>
<div>horse-battery-staple</div>
</div>
</Collapsible.Panel>
</Collapsible.Root>
);
}
export function CaretRightIcon(props: React.ComponentProps<'svg'>) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path d="M6 12V4l4.5 4z" />
</svg>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Collapsible {
display: flex;
width: 12rem;
min-height: 9rem;
flex-direction: column;
justify-content: center;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Icon {
transition: transform 100ms ease-out;
}
.Trigger {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
height: 2rem;
padding: 0 0.5rem 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
color: oklch(14.5% 0 0deg);
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
&[data-panel-open] .Icon {
transform: rotate(90deg);
}
&[data-disabled] {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
}
.Panel {
display: flex;
height: var(--collapsible-panel-height);
flex-direction: column;
justify-content: end;
overflow: hidden;
font-size: 0.875rem;
line-height: 1.25rem;
transition: height 150ms ease-out;
&[hidden]:not([hidden='until-found']) {
display: none;
}
&[data-starting-style],
&[data-ending-style] {
height: 0;
}
}
.Content {
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0.5rem 0.875rem;
}
/* index.tsx */
import * as React from 'react';
import { Collapsible } from '@base-ui/react/collapsible';
import styles from './index.module.css';
export default function ExampleCollapsible() {
return (
<Collapsible.Root className={styles.Collapsible}>
<Collapsible.Trigger className={styles.Trigger}>
Recovery keys
<CaretRightIcon className={styles.Icon} />
</Collapsible.Trigger>
<Collapsible.Panel className={styles.Panel}>
<div className={styles.Content}>
<div>alien-bean-pasta</div>
<div>wild-irish-burrito</div>
<div>horse-battery-staple</div>
</div>
</Collapsible.Panel>
</Collapsible.Root>
);
}
export function CaretRightIcon(props: React.ComponentProps<'svg'>) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path d="M6 12V4l4.5 4z" />
</svg>
);
}
애너토미 (Anatomy)
컴포넌트를 임포트하고 파트를 조립해요.
import { Collapsible } from '@base-ui/react/collapsible';
<Collapsible.Root>
<Collapsible.Trigger />
<Collapsible.Panel />
</Collapsible.Root>;
예시 (Examples)
찾을 때까지 숨김 (Hidden until found)
hiddenUntilFound prop은 닫힌 패널을 hidden="until-found"으로 숨겨서 브라우저가 페이지 내 검색(find-in-page) — Ctrl+F (macOS에선 Cmd+F) — 으로 그 내용을 검색하고, 일치 항목이 있으면 패널을 펼칠 수 있게 해줘요. 닫힌 패널은 항상 DOM에 마운트된 채로 유지되어 그 내용이 검색 엔진에도 색인되게 해줘요.
hidden="until-found"를 지원하지 않는 오래된 브라우저는 트리거가 열 때까지 패널을 숨겨두고, 페이지 내 검색은 그 내용을 건너뛰어요.
<Collapsible.Root>
<Collapsible.Trigger>Shipping details</Collapsible.Trigger>
{/* @highlight-text "hiddenUntilFound" */}
<Collapsible.Panel hiddenUntilFound>Standard shipping takes 3–5 business days.</Collapsible.Panel>
</Collapsible.Root>
인터랙티브 데모는 Accordion 예시를 참고하세요.
API 레퍼런스 (API reference)
Root
collapsible의 모든 파트를 그룹화해요.
<div> 요소를 렌더링해요.
Root Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultOpen | boolean |
false |
Whether the collapsible panel is initially open. To render a controlled collapsible, use the open prop instead. |
| open | boolean |
- | Whether the collapsible panel is currently open. To render an uncontrolled collapsible, use the defaultOpen prop instead. |
| onOpenChange | ((open: boolean, eventDetails: Collapsible.Root.ChangeEventDetails) => void) |
- | Event handler called when the panel is opened or closed. |
| disabled | boolean |
false |
Whether the component should ignore user interaction. |
| className | string | ((state: Collapsible.Root.State) => string | undefined) |
- | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Collapsible.Root.State) => React.CSSProperties | undefined) |
- | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: Collapsible.Root.State) => ReactElement) |
- | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Root Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the collapsible is open. |
| data-closed | - | Present when the collapsible is closed. |
| data-starting-style | - | Present when the collapsible begins animating in. |
| data-ending-style | - | Present when the collapsible is animating out. |
Root.Props
Root props의 re-export예요.
Root.State
type CollapsibleRootState = {
/** Whether the collapsible panel is currently open. */
open: boolean;
/** Whether the component should ignore user interaction. */
disabled: boolean;
transitionStatus: TransitionStatus;
};
Root.ChangeEventReason
type CollapsibleRootChangeEventReason = 'trigger-press' | 'none';
Root.ChangeEventDetails
type CollapsibleRootChangeEventDetails = (
| { reason: 'trigger-press'; event: MouseEvent | PointerEvent | TouchEvent | KeyboardEvent }
| { reason: 'none'; event: Event }
) & {
/** Cancels Base UI from handling the event. */
cancel: () => void;
/** Allows the event to propagate in cases where Base UI will stop the propagation. */
allowPropagation: () => void;
/** Indicates whether the event has been canceled. */
isCanceled: boolean;
/** Indicates whether the event is allowed to propagate. */
isPropagationAllowed: boolean;
/** The element that triggered the event, if applicable. */
trigger: Element | undefined;
};
Trigger
collapsible 패널을 열고 닫는 버튼이에요.
<button> 요소를 렌더링해요.
Trigger Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| nativeButton | boolean |
true |
Whether the component renders a native <button> element when replacing it
via the render prop.
Set to false if the rendered element is not a button (for example, <div>). |
| className | string | ((state: Collapsible.Trigger.State) => string | undefined) |
- | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Collapsible.Trigger.State) => React.CSSProperties | undefined) |
- | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: Collapsible.Trigger.State) => ReactElement) |
- | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Trigger Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-panel-open | - | Present when the collapsible panel is open. |
Trigger.Props
Trigger props의 re-export예요.
Trigger.State
type CollapsibleTriggerState = {
/** Whether the collapsible panel is currently open. */
open: boolean;
/** Whether the component should ignore user interaction. */
disabled: boolean;
transitionStatus: TransitionStatus;
};
Panel
collapsible 내용물을 담은 패널이에요.
<div> 요소를 렌더링해요.
Panel Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| hiddenUntilFound | boolean |
false |
Allows the browser's built-in page search to find and expand the panel contents. Overrides the keepMounted prop and uses hidden="until-found"
to hide the element without removing it from the DOM. |
| className | string | ((state: Collapsible.Panel.State) => string | undefined) |
- | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: Collapsible.Panel.State) => React.CSSProperties | undefined) |
- | Style applied to the element, or a function that returns a style object based on the component's state. |
| keepMounted | boolean |
false |
Whether to keep the element in the DOM while the panel is hidden.
This prop is ignored when hiddenUntilFound is used. |
| render | ReactElement | ((props: HTMLProps, state: Collapsible.Panel.State) => ReactElement) |
- | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Panel Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-open | - | Present when the collapsible panel is open. |
| data-closed | - | Present when the collapsible panel is closed. |
| data-starting-style | - | Present when the panel begins animating in. |
| data-ending-style | - | Present when the panel is animating out. |
Panel CSS Variables:
| Variable | Type | Description |
|---|---|---|
--collapsible-panel-height |
number |
The collapsible panel's height. |
--collapsible-panel-width |
number |
The collapsible panel's width. |
Panel.Props
Panel props의 re-export예요.
Panel.State
type CollapsiblePanelState = {
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the collapsible panel is currently open. */
open: boolean;
/** Whether the component should ignore user interaction. */
disabled: boolean;
};
내보내기 그룹 (Export Groups)
Collapsible.Root:Collapsible.Root,Collapsible.Root.State,Collapsible.Root.Props,Collapsible.Root.ChangeEventReason,Collapsible.Root.ChangeEventDetailsCollapsible.Trigger:Collapsible.Trigger,Collapsible.Trigger.State,Collapsible.Trigger.PropsCollapsible.Panel:Collapsible.Panel,Collapsible.Panel.State,Collapsible.Panel.PropsDefault:CollapsibleRootState,CollapsibleRootProps,CollapsibleRootChangeEventReason,CollapsibleRootChangeEventDetails,CollapsibleTriggerState,CollapsibleTriggerProps,CollapsiblePanelState,CollapsiblePanelProps
Canonical Types
Canonical: Alias 매핑 — 네임스페이스가 이미 임포트되어 있다면 Canonical을, 그렇지 않으면 Alias를 사용하세요.
Collapsible.Root.State:CollapsibleRootStateCollapsible.Root.Props:CollapsibleRootPropsCollapsible.Root.ChangeEventReason:CollapsibleRootChangeEventReasonCollapsible.Root.ChangeEventDetails:CollapsibleRootChangeEventDetailsCollapsible.Trigger.State:CollapsibleTriggerStateCollapsible.Trigger.Props:CollapsibleTriggerPropsCollapsible.Panel.State:CollapsiblePanelStateCollapsible.Panel.Props:CollapsiblePanelProps