배지(Badge) v2 → v3 마이그레이션
HeroUI v2의 Badge를 v3로 옮길 때 필요한 구조·props·변형(variant) 변경을 정리한 가이드예요. v2에서 자식 요소를 감싸던 방식이 v3에서는 합성 컴포넌트(compound component)로 바뀌었으니, 구조가 어떻게 달라지는지 차근차근 살펴볼게요.
출처: HeroUI
본문
참고: 완전한 API 레퍼런스, 스타일 가이드, 고급 예제는 v3 Badge 문서를 참고하세요. 이 가이드는 HeroUI v2에서 마이그레이션하는 부분에만 집중합니다.
구조 변화
v2에서는 Badge가 자신의 자식 요소를 기준으로 콘텐츠를 배치하는 래퍼(wrapper) 컴포넌트였어요:
import { Badge, Avatar } from "@heroui/react";
export default function App() {
return (
<Badge content="5" color="primary">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
);
}
v3에서는 Badge가 Badge.Anchor로 위치를 잡는 합성 컴포넌트 패턴을 사용해요:
import { Badge, Avatar } from "@heroui/react";
export default function App() {
return (
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
<Badge color="accent">5</Badge>
</Badge.Anchor>
);
}
주요 변경 사항
1. 컴포넌트 구조
v2: 하나의 Badge 컴포넌트가 자식 요소를 감싸고, content prop으로 배지 텍스트를 전달
v3: 합성 컴포넌트 — Badge.Anchor(위치 래퍼), Badge(배지 자체), Badge.Label(텍스트 슬롯, 문자열이면 자동으로 감싸줌)
2. 변형(variant)
v2: solid, flat, faded, shadow
v3: primary, secondary, soft
3. 색상
v2: default, primary, secondary, success, warning, danger
v3: default, accent, success, warning, danger
4. Prop 변경
| v2 Prop | v3 Location | Notes |
|---|---|---|
content |
Badge children |
Content is now passed as children |
children |
Badge.Anchor |
The anchored element goes inside Badge.Anchor |
variant |
Badge |
Values changed (see variant mapping) |
color |
Badge |
primary → accent, secondary removed |
size |
Badge |
Same (sm, md, lg) |
placement |
Badge |
Same (top-right, top-left, bottom-right, bottom-left) |
shape |
- | Removed (use Tailwind CSS) |
showOutline |
- | Removed (use Tailwind CSS e.g. border-2) |
disableOutline |
- | Removed |
disableAnimation |
- | Removed |
isInvisible |
- | Removed (use conditional rendering) |
isOneChar |
- | Removed (use Tailwind CSS) |
isDot |
- | Omit children to render as dot |
classNames |
- | Use className on individual components |
5. 변형 매핑
| v2 Variant | v3 Equivalent | Notes |
|---|---|---|
solid |
primary |
Filled background |
flat |
soft |
Light background |
faded |
secondary |
Border with background |
shadow |
primary |
Use Tailwind shadow-* classes |
6. 색상 매핑
| v2 Color | v3 Equivalent | Notes |
|---|---|---|
default |
default |
Same |
primary |
accent |
Renamed |
secondary |
default or accent |
Depends on context |
success |
success |
Same |
warning |
warning |
Same |
danger |
danger |
Same |
마이그레이션 예제
기본 Badge
v2:
import { Badge, Avatar } from "@heroui/react";
<Badge content="5" color="primary">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
v3:
import { Badge, Avatar } from "@heroui/react";
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
<Badge color="accent">5</Badge>
</Badge.Anchor>
Dot Badge
v2:
<Badge content="" isDot color="success">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
v3:
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
<Badge color="success" />
</Badge.Anchor>
배치(Placement)
v2:
<Badge content="5" placement="bottom-right" color="danger">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
v3:
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
<Badge placement="bottom-right" color="danger">5</Badge>
</Badge.Anchor>
표시 여부 토글
v2:
const [isInvisible, setIsInvisible] = useState(false);
<Badge content="5" isInvisible={isInvisible} color="danger">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
v3:
const [isVisible, setIsVisible] = useState(true);
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
{isVisible && <Badge color="danger">5</Badge>}
</Badge.Anchor>
아이콘 콘텐츠와 함께
v2:
import { Icon } from "@iconify/react";
<Badge content={<Icon icon="gravity-ui:check" />} color="success">
<Avatar src="https://i.pravatar.cc/300" />
</Badge>
v3:
import { Icon } from "@iconify/react";
<Badge.Anchor>
<Avatar>
<Avatar.Image src="https://i.pravatar.cc/300" alt="User" />
<Avatar.Fallback>U</Avatar.Fallback>
</Avatar>
<Badge color="success">
<Icon icon="gravity-ui:check" />
</Badge>
</Badge.Anchor>
스타일링 변경
v2: classNames Prop
<Badge
classNames={{
base: "custom-base",
badge: "custom-badge"
}}
/>
v3: 직접 className Props 사용
<Badge.Anchor className="custom-anchor">
<Avatar />
<Badge className="custom-badge">
<Badge.Label className="custom-label">5</Badge.Label>
</Badge>
</Badge.Anchor>
컴포넌트 구조(Anatomy)
v3 Badge의 구조는 다음과 같아요:
Badge.Anchor (positioning wrapper)
├── [Anchored element: Avatar, Button, etc.]
└── Badge (the badge indicator)
└── Badge.Label (auto-wrapped for string/number children)
요약
- 컴포넌트 구조: 자식 요소를 감싸던
Badge→ 형제 관계인Badge.Anchor+Badge로 변경 - content Prop 제거:
contentprop →Badge의 자식 요소로 전달 - Dot Badge:
isDotprop → 자식 요소 생략 - 표시 여부:
isInvisibleprop → 조건부 렌더링 - 변형 축소: 4개에서 3개로 축소 (
primary,secondary,soft) - 색상 변경:
primary→accent,secondary제거 - 제거된 Props:
shape,showOutline,disableAnimation,isOneChar— Tailwind CSS 사용 - classNames 제거: 개별 합성 컴포넌트에
className사용