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

ToggleButton

원문 보기 위키 갱신

단일 및 다중 모드의 그룹 선택을 지원하는, 스타일 없는 토글 버튼 컴포넌트예요.

출처: 문서

본문

기능 (Features)

  • 하위 컴포넌트 구성 API: Root, Indicator
  • boolean 눌림(pressed) 상태를 가진 단독 토글
  • 단일/다중 모드를 위한 ToggleButtonGroup으로 그룹 선택
  • 그룹에서 최소 하나 이상의 선택을 요구하는 설정 가능한 allowEmpty

사용법 (Usage)

import { ToggleButton } from 'primereact/togglebutton';
import { ToggleButtonGroup } from 'primereact/togglebuttongroup';
<ToggleButton.Root>
    <ToggleButton.Indicator></ToggleButton.Indicator>
</ToggleButton.Root>

<ToggleButtonGroup>
    <ToggleButton.Root value="a">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
    <ToggleButton.Root value="b">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
</ToggleButtonGroup>

동작 (Behavior)

다형적 렌더링 (Polymorphic Rendering)

as를 사용해 하위 컴포넌트가 렌더링할 요소를 바꿀 수 있어요.

<ToggleButton.Root as="div"></ToggleButton.Root>
<ToggleButton.Indicator as="span"></ToggleButton.Indicator>

기본 요소: Root=button, Indicator=span, ToggleButtonGroup=div.

렌더 함수 자식 (Render Function Children)

Indicator는 컴포넌트 인스턴스에 접근할 수 있는 렌더 함수를 children으로 받아요. 인스턴스는 togglebutton(부모 인스턴스, state.pressed 포함)을 노출해요.

<ToggleButton.Indicator>{({ togglebutton }: ToggleButtonIndicatorInstance) => (togglebutton?.state.pressed ? 'On' : 'Off')}</ToggleButton.Indicator>

Pass Through

<DocPTViewer name="togglebutton-pt" components={['ToggleButtonRoot']} />

API

ToggleButtonRoot

Attribute Value
data-pressed Present when pressed
data-disabled Present when disabled
data-invalid Present when invalid
Label Type Description
root ToggleButtonRootPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.
indicator ToggleButtonRootPassThroughType<HTMLAttributes> Used to pass attributes to the indicator's DOM element.

ToggleButtonIndicator

Attribute Value
data-pressed Present when pressed
data-disabled Present when disabled
data-invalid Present when invalid
Label Type Description
root ToggleButtonIndicatorPassThroughType<HTMLAttributes> Used to pass attributes to the root's DOM element.

접근성 (Accessibility)

스크린 리더 (Screen Reader)

ToggleButton은 aria-pressed로 토글 상태를, 비활성 상태에서는 aria-disabled를 사용해요. 각 버튼에 접근 가능한 이름을 주려면 aria-label이나 보이는 텍스트 콘텐츠를 제공하세요. ToggleButtonGroup은 role="group"을 사용하며 그룹 목적을 설명하는 aria-label을 가져야 해요.

<ToggleButton.Root aria-label="Favorite">
    <ToggleButton.Indicator></ToggleButton.Indicator>
</ToggleButton.Root>

<ToggleButtonGroup aria-label="Text alignment">
    <ToggleButton.Root value="left" aria-label="Align left">
        <ToggleButton.Indicator></ToggleButton.Indicator>
    </ToggleButton.Root>
</ToggleButtonGroup>

키보드 지원 (Keyboard Support)

Key Function
Space Toggles the pressed state.
Enter Toggles the pressed state.
Tab Moves focus to the next element.

더 알아보기 (Learn more)