Code 마이그레이션 가이드
이 문서는 HeroUI v2에서 v3로 옮길 때 Code 컴포넌트를 어떻게 처리해야 하는지 설명해요.
참고: Code 컴포넌트는 HeroUI v3에서 제거됐어요. 대신 네이티브 HTML
<code>요소와 Tailwind CSS 클래스를 사용해요.
출처: HeroUI
본문
주요 변경 사항
1. 컴포넌트 제거
v2: @heroui/react의 <Code> 컴포넌트
v3: Tailwind 클래스를 적용한 네이티브 HTML <code> 요소
2. 변형 매핑
v2 Code 컴포넌트에는 교체가 필요한 다음 변형들이 있었어요:
| v2 변형 | v3 대체 | 비고 |
|---|---|---|
color="default" |
bg-default/40 text-default-700 |
Tailwind 불투명도 유틸리티 사용 |
color="primary" |
bg-accent/20 text-accent-600 |
primary가 accent로 이름 변경됨 |
color="secondary" |
bg-default/40 text-default-700 |
default와 비슷함 |
color="success" |
bg-success/20 text-success-700 |
색상 이름 동일 |
color="warning" |
bg-warning/20 text-warning-700 |
색상 이름 동일 |
color="danger" |
bg-danger/20 text-danger-600 |
색상 이름 동일 |
size="sm" |
text-sm |
Tailwind 텍스트 크기 |
size="md" |
text-base |
Tailwind 텍스트 크기 |
size="lg" |
text-lg |
Tailwind 텍스트 크기 |
radius="none" |
rounded-none |
Tailwind 테두리 반경 |
radius="sm" |
rounded-sm |
Tailwind 테두리 반경 |
radius="md" |
rounded-md |
Tailwind 테두리 반경 |
radius="lg" |
rounded-lg |
Tailwind 테두리 반경 |
radius="full" |
rounded-full |
Tailwind 테두리 반경 |
구조 변경
v2에서 Code는 네이티브 <code> 요소를 감싸는 컴포넌트 래퍼였어요:
import { Code } from "@heroui/react";
export default function App() {
return <Code>npm install @heroui/react</Code>;
}
v3에서는 네이티브 <code> 요소에 Tailwind CSS 클래스를 직접 적용해요:
export default function App() {
return (
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-sm">
npm install @heroui/react
</code>
);
}
마이그레이션 예제
변형
{/* Colors */}
<Code color="primary">Primary code</Code>
<Code color="success">Success code</Code>
{/* Sizes */}
<Code size="md">Medium code</Code>
{/* Colors */}
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-accent/20 text-accent-600 text-sm">
Primary code
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-success/20 text-success-700 text-sm">
Success code
</code>
{/* Sizes */}
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-base">
Medium code
</code>
결합 변형
<Code color="primary" size="md" radius="lg">
npm install @heroui/react
</Code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-lg bg-accent/20 text-accent-600 text-base">
npm install @heroui/react
</code>
재사용 가능한 Code 컴포넌트 만들기 (선택 사항)
인라인 코드를 자주 쓴다면 간단한 래퍼 컴포넌트를 만들 수 있어요:
import { Code } from "@heroui/react";
<Code color="primary" size="md">Code snippet</Code>
// components/Code.tsx
import { cn } from "@/lib/utils"; // or your cn utility
interface CodeProps extends React.HTMLAttributes<HTMLElement> {
color?: "default" | "accent" | "success" | "warning" | "danger";
size?: "sm" | "md" | "lg";
radius?: "none" | "sm" | "md" | "lg" | "full";
}
const colorClasses = {
default: "bg-default/40 text-default-700",
accent: "bg-accent/20 text-accent-600",
success: "bg-success/20 text-success-700",
warning: "bg-warning/20 text-warning-700",
danger: "bg-danger/20 text-danger-600",
};
const sizeClasses = {
sm: "text-sm",
md: "text-base",
lg: "text-lg",
};
const radiusClasses = {
none: "rounded-none",
sm: "rounded-sm",
md: "rounded-md",
lg: "rounded-lg",
full: "rounded-full",
};
export function Code({
children,
className,
color = "default",
size = "sm",
radius = "sm",
...props
}: CodeProps) {
return (
<code
className={cn(
"px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap",
colorClasses[color],
sizeClasses[size],
radiusClasses[radius],
className
)}
{...props}
>
{children}
</code>
);
}
// Usage
<Code color="accent" size="md">Code snippet</Code>
완전한 예제
import { Code } from "@heroui/react";
export default function App() {
return (
<div className="space-y-4">
<p>
Install HeroUI with <Code>npm install @heroui/react</Code>
</p>
<div className="flex gap-2">
<Code color="primary">Primary</Code>
<Code color="success">Success</Code>
<Code color="warning">Warning</Code>
<Code color="danger">Danger</Code>
</div>
<div className="flex gap-2 items-center">
<Code size="sm">Small</Code>
<Code size="md">Medium</Code>
<Code size="lg">Large</Code>
</div>
</div>
);
}
export default function App() {
return (
<div className="space-y-4">
<p>
Install HeroUI with{" "}
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-sm">
npm install @heroui/react
</code>
</p>
<div className="flex gap-2">
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-accent/20 text-accent-600 text-sm">
Primary
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-success/20 text-success-700 text-sm">
Success
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-warning/20 text-warning-700 text-sm">
Warning
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-danger/20 text-danger-600 text-sm">
Danger
</code>
</div>
<div className="flex gap-2 items-center">
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-sm">
Small
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-base">
Medium
</code>
<code className="px-2 py-1 h-fit font-mono font-normal inline-block whitespace-nowrap rounded-sm bg-default/40 text-default-700 text-lg">
Large
</code>
</div>
</div>
);
}
기본 스타일 레퍼런스
v2 Code 컴포넌트는 포함해야 하는 다음 기본 스타일을 사용했어요:
px-2- 가로 패딩py-1- 세로 패딩h-fit- 콘텐츠에 맞는 높이font-mono- 모노스페이스 폰트 패밀리font-normal- 보통 폰트 두께inline-block- 인라인 블록 표시whitespace-nowrap- 텍스트 줄바꿈 방지
요약
- 컴포넌트 제거:
Code컴포넌트는 v3에 더 이상 없어요. - import 변경:
import { Code } from "@heroui/react"를 제거해요. - 네이티브 요소 사용: 네이티브 HTML
<code>요소로 교체해요. - 스타일링: Tailwind CSS 클래스를 직접 적용해요.
- 색상 매핑: v3에서는
primary→accent로 바뀌어요.
마이그레이션 단계
- import 제거:
@heroui/reactimport에서Code를 제거해요. - 컴포넌트 교체: 모든
<Code>인스턴스를<code>요소로 바꿔요. - Tailwind 클래스 추가: 스타일링에 해당하는 Tailwind 클래스를 적용해요.
- 색상 업데이트:
color="primary"를accent색상 클래스로 바꿔요. - 선택 사항: 인라인 코드를 자주 쓴다면 재사용 가능한 래퍼 컴포넌트를 만들어요.