Button (버튼)
클릭 가능한 버튼 컴포넌트예요. 여러 변형(variant)과 상태를 지원해서, 폼 제출부터 주요 동작까지 다양한 곳에 사용할 수 있어요.
출처: HeroUI Button
본문
사용법 (Usage)
import { Button } from '@heroui/react';
{"demo": "button-basic"}
예제 (Examples)
변형 (Variants)
{"demo": "button-variants"}
크기 (Sizes)
{"demo": "button-sizes"}
아이콘과 함께 (With Icons)
{"demo": "button-with-icons"}
아이콘만 (Icon Only)
{"demo": "button-icon-only"}
로딩 (Loading)
{"demo": "button-loading"}
로딩 상태 (Loading State)
{"demo": "button-loading-state"}
전체 너비 (Full Width)
{"demo": "button-full-width"}
비활성 상태 (Disabled State)
{"demo": "button-disabled"}
소셜 버튼 (Social Buttons)
{"demo": "button-social"}
렌더 함수 (Render Function)
{"demo": "button-render-function"}
커스텀 변형 추가하기 (Adding custom variants)
HeroUI 컴포넌트를 감싸고 나만의 커스텀 변형을 추가해서 확장할 수 있어요.
{"demo": "button-custom-variants"}
리플 효과 추가하기 (Adding Ripple Effect)
Button 컴포넌트는 합성(composition)을 통해 리플 효과를 지원해요. 리플 컴포넌트를 자식으로 중첩해서 사용할 수 있고, 이 예제는 m3-ripple을 사용해요.
{"demo": "button-ripple-effect"}
커스터마이징 (Customization)
Tailwind CSS
{"demo": "button-custom-styles"}
Global CSS
Button 컴포넌트의 클래스를 커스터마이징하려면 @layer components 지시어를 사용할 수 있어요.
자세히 알아보기.
@layer components {
.button {
@apply bg-purple-500 text-white hover:bg-purple-600;
}
.button--icon-only {
@apply rounded-lg bg-blue-500;
}
}
스타일링 참조 (Styling Reference)
HeroUI는 BEM 방법론을 따라 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽게 만들어요.
CSS 클래스 (CSS Classes)
Button 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):
기본 및 크기 클래스 (Base & Size Classes) [!toc]
.button- 버튼 기본 스타일.button--sm- 작은 크기 변형.button--md- 중간 크기 변형.button--lg- 큰 크기 변형
변형 클래스 (Variant Classes) [!toc]
.button--primary.button--secondary.button--tertiary.button--outline.button--ghost.button--danger
수식 클래스 (Modifier Classes) [!toc]
.button--icon-only.button--icon-only.button--sm.button--icon-only.button--lg
상호작용 상태 (Interactive States)
버튼은 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:
- 호버 (Hover):
:hover또는[data-hovered="true"] - 활성/누름 (Active/Pressed):
:active또는[data-pressed="true"](크기 변환 포함) - 포커스 (Focus):
:focus-visible또는[data-focus-visible="true"](포커스 링 표시) - 비활성 (Disabled):
:disabled또는[aria-disabled="true"](불투명도 감소, 포인터 이벤트 없음) - 대기 (Pending):
[data-pending](로딩 중 포인터 이벤트 없음)
API 참조 (API Reference)
Button
| Prop | Type | Default | Description |
|---|---|---|---|
variant |
'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger' |
'primary' |
Visual style variant |
size |
'sm' | 'md' | 'lg' |
'md' |
Size of the button |
fullWidth |
boolean |
false |
Whether the button should take full width of its container |
isDisabled |
boolean |
false |
Whether the button is disabled |
isPending |
boolean |
false |
Whether the button is in a loading state |
isIconOnly |
boolean |
false |
Whether the button contains only an icon |
onPress |
(e: PressEvent) => void |
- | Handler called when the button is pressed |
children |
React.ReactNode | (values: ButtonRenderProps) => React.ReactNode |
- | Button content or render prop |
render |
DOMRenderFunction<keyof React.JSX.IntrinsicElements, ButtonRenderProps> |
- | Overrides the default DOM element with a custom render function. |
렌더 프로퍼티 (Render Props)
렌더 프로퍼티 패턴을 사용하면 다음 값이 제공돼요:
| Prop | Type | Description |
|---|---|---|
isPending |
boolean |
Whether the button is in a loading state |
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 |