본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Accordion

원문 보기 위키 갱신

Accordion는 compound composition 방식으로 만들어진, 스타일이 없는(unstyled) 접근성 높은 아코디언 컴포넌트예요. 여러 하위 컴포넌트를 조합해 레이아웃과 스타일을 완전히 자유롭게 제어할 수 있어요.

출처: 문서

본문

기능 (Features)

  • 6개의 하위 컴포넌트(Root, Panel, Header, Trigger, Content, Indicator)로 구성된 compound 컴포넌트 API
  • 단일 또는 다중 패널 확장 지원

사용법 (Usage)

import { Accordion } from 'primereact/accordion';
<Accordion.Root>
    <Accordion.Panel>
        <Accordion.Header>
            <Accordion.Trigger>
                <Accordion.Indicator />
            </Accordion.Trigger>
        </Accordion.Header>
        <Accordion.Content />
    </Accordion.Panel>
</Accordion.Root>

동작 (Behavior)

모션 애니메이션 (Motion Animation)

Root에서 motionProps를 사용해 열림/닫힘 애니메이션을 설정할 수 있어요.

<Accordion.Root motionProps={{ name: 'accordion-slide', cssVarPrefix: 'accordion-content', hideStrategy: 'none' }}>...</Accordion.Root>

애니메이션 단계(phases), CSS 변수, hide 전략에 대해서는 Motion 문서를 참고하세요.

다형적 렌더링 (Polymorphic Rendering)

아무 하위 컴포넌트에 as를 사용해 렌더링되는 HTML 요소를 바꾸거나 커스텀 React 컴포넌트를 사용할 수 있어요.

<Accordion.Root as="section">
    <Accordion.Panel as="article">
        <Accordion.Header as="h3">
            <Accordion.Trigger as="div">...</Accordion.Trigger>
        </Accordion.Header>
        <Accordion.Content as="section">...</Accordion.Content>
    </Accordion.Panel>
</Accordion.Root>

커스텀 컴포넌트는 기본 요소에 적용될 모든 props를 그대로 전달받아요:

<Accordion.Trigger as={MyCustomButton}>...</Accordion.Trigger>

기본 요소: Root=div, Panel=div, Header=div, Trigger=button, Content=div, Indicator=span.

렌더 함수 자식 (Render Function Children)

Content는 렌더 함수를 자식으로 받아 컴포넌트 인스턴스에 접근할 수 있게 해 줘요. 인스턴스는 accordion(루트 인스턴스), accordionpanel(부모 패널 인스턴스), motion(애니메이션 상태)을 노출해요.

<Accordion.Content>{(instance) => <div>Panel is {instance.accordionpanel?.state.open ? 'open' : 'closed'}</div>}</Accordion.Content>

패스 스루 (Pass Through)

<DocPTViewer name="accordion-pt" components={['AccordionRoot']} />

API

AccordionRoot

Attribute Value
data-scope "accordion"
data-part "root"
data-disabled Present when disabled
Label Type Description
root AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
panel AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the panel's DOM element.
header AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the header's DOM element.
trigger AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the trigger's DOM element.
content AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the content's DOM element.
contentOuter AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the content outer wrapper's DOM element.
contentInner AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the content inner wrapper's DOM element.
indicator AccordionRootPassThroughType<HTMLAttributes> Used to pass attributes to the indicator's DOM element.

AccordionPanel

Attribute Value
data-scope "accordion"
data-part "panel"
data-open Present when expanded
data-closed Present when collapsed
data-disabled Present when disabled
Label Type Description
root AccordionPanelPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AccordionHeader

Attribute Value
data-scope "accordion"
data-part "header"
data-open Present when expanded
data-closed Present when collapsed
Label Type Description
root AccordionHeaderPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AccordionTrigger

Attribute Value
data-scope "accordion"
data-part "trigger"
data-content-open Present when content open
data-content-closed Present when content closed
data-disabled Present when disabled
Label Type Description
root AccordionTriggerPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AccordionContent

Attribute Value
data-scope "accordion"
data-part "content"
data-open Present when expanded
data-closed Present when collapsed
data-disabled Present when disabled
Label Type Description
root AccordionContentPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

AccordionIndicator

Attribute Value
data-scope "accordion"
data-part "indicator"
data-open Present when expanded
data-closed Present when collapsed
Label Type Description
root AccordionIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

Accordion 헤더는 버튼이에요. aria-controls로 콘텐츠 영역을 참조하고, aria-expanded로 표시 상태를 반영해요. 발표되는 라벨은 pt를 통해 aria-label 또는 aria-labelledby로 커스터마이즈할 수 있어요.

콘텐츠는 role="region"을 사용하고, 헤더 버튼의 aria-controls와 일치하는 id를 가져요.

헤더 키보드 지원 (Header Keyboard Support)

Key Function
tab Moves focus to the next focusable element in the page tab sequence.
shift + tab Moves focus to the previous focusable element in the page tab sequence.
enter Toggles the visibility of the content.
space Toggles the visibility of the content.
down arrow Moves focus to the next header. If focus is on the last header, moves focus to the first header.
up arrow Moves focus to the previous header. If focus is on the first header, moves focus to the last header.
home Moves focus to the first header.
end Moves focus to the last header.

더 알아보기 (Learn more)