ToggleButton
켜짐/꺼짐 또는 선택됨/선택 안 됨 상태를 위한 인터랙티브 토글 컨트롤이에요.
본문
사용법 (Usage)
import { ToggleButton } from '@heroui/react';
{"component": "toggle-button-basic"}
예시 (Examples)
변형 (Variants)
{"component": "toggle-button-variants"}
아이콘만 (Icon Only)
{"component": "toggle-button-icon-only"}
크기 (Sizes)
{"component": "toggle-button-sizes"}
비활성화 (Disabled)
{"component": "toggle-button-disabled"}
제어 (Controlled)
{"component": "toggle-button-controlled"}
커스터마이징 (Customization)
Tailwind CSS
{"component": "toggle-button-custom-styles"}
전역 CSS (Global CSS)
ToggleButton 컴포넌트 클래스를 커스터마이징하려면 @layer components 디렉티브를 사용하세요.
자세히 알아보기.
@layer components {
.toggle-button {
@apply bg-purple-500 text-white;
}
.toggle-button--icon-only {
@apply rounded-lg;
}
}
스타일링 참고 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들어요.
CSS 클래스 (CSS Classes)
ToggleButton 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 & 크기 클래스 [!toc]
.toggle-button- 기본 토글 버튼 스타일.toggle-button--sm- 작은 크기 변형.toggle-button--md- 중간 크기 변형 (기본값).toggle-button--lg- 큰 크기 변형
변형 클래스 [!toc]
.toggle-button--default- 채워진 배경의 기본 변형.toggle-button--ghost- 투명한 배경의 고스트 변형
수식어 클래스 [!toc]
.toggle-button--icon-only- 아이콘만 있는 토글 버튼.toggle-button--icon-only.toggle-button--sm- 작은 아이콘 전용.toggle-button--icon-only.toggle-button--lg- 큰 아이콘 전용
인터랙티브 상태 (Interactive States)
토글 버튼은 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 선택됨:
[data-selected="true"](강조 배경 및 전경) - 호버:
:hover또는[data-hovered="true"] - 활성/누름:
:active또는[data-pressed="true"](스케일 변형 포함) - 포커스:
:focus-visible또는[data-focus-visible="true"](포커스 링 표시) - 비활성화:
:disabled또는[aria-disabled="true"](불투명도 감소, 포인터 이벤트 없음)
API 참고 (API Reference)
ToggleButton
React Aria ToggleButton에서 상속받아요.
| Prop | Type | Default | Description |
|---|---|---|---|
variant |
'default' | 'ghost' |
'default' |
Visual style variant |
size |
'sm' | 'md' | 'lg' |
'md' |
Size of the toggle button |
isIconOnly |
boolean |
false |
Whether the button contains only an icon |
isSelected |
boolean |
- | Controlled selected state |
defaultSelected |
boolean |
false |
Default selected state (uncontrolled) |
isDisabled |
boolean |
false |
Whether the toggle button is disabled |
onChange |
(isSelected: boolean) => void |
- | Handler called when selection changes |
onPress |
(e: PressEvent) => void |
- | Handler called when the button is pressed |
children |
React.ReactNode | (values: ToggleButtonRenderProps) => React.ReactNode |
- | Button content or render prop |
렌더 프롭스 (Render Props)
렌더 프롭 패턴을 사용하면 다음 값이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isSelected |
boolean |
Whether the button is currently selected |
isPressed |
boolean |
Whether the button is currently pressed |
isHovered |
boolean |
Whether the button is hovered |
isFocused |
boolean |
Whether the button is focused |
isFocusVisible |
boolean |
Whether the button should show focus indicator |
isDisabled |
boolean |
Whether the button is disabled |