apps/docs/content/docs/en/react/migration/(components)/code.mdx
v2: <Code> component from @heroui/react
v3: Native HTML <code> element with Tailwind classes
The v2 Code component had the following variants that need to be replaced:
| v2 Variant | v3 Equivalent | Notes |
|---|---|---|
color="default" | bg-default/40 text-default-700 | Use Tailwind opacity utilities |
color="primary" | bg-accent/20 text-accent-600 | primary renamed to accent |
color="secondary" | bg-default/40 text-default-700 | Similar to default |
color="success" | bg-success/20 text-success-700 | Same color name |
color="warning" | bg-warning/20 text-warning-700 | Same color name |
color="danger" | bg-danger/20 text-danger-600 | Same color name |
size="sm" | text-sm | Tailwind text size |
size="md" | text-base | Tailwind text size |
size="lg" | text-lg | Tailwind text size |
radius="none" | rounded-none | Tailwind border radius |
radius="sm" | rounded-sm | Tailwind border radius |
radius="md" | rounded-md | Tailwind border radius |
radius="lg" | rounded-lg | Tailwind border radius |
radius="full" | rounded-full | Tailwind border radius |
In v2, Code was a component wrapper around the native <code> element:
import { Code } from "@heroui/react";
export default function App() {
return <Code>npm install @heroui/react</Code>;
}
In v3, use the native <code> element directly with Tailwind CSS classes:
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>
);
}
<Tabs items={["v2", "v3"]}>
<Tab value="v2">
tsx <Code color="primary">Primary code</Code> <Code color="success">Success code</Code> <Code size="md">Medium code</Code>
</Tab>
<Tab value="v3">
tsx <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> <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>
</Tab>
</Tabs>
<Tabs items={["v2", "v3"]}>
<Tab value="v2">
tsx <Code color="primary" size="md" radius="lg"> npm install @heroui/react </Code>
</Tab>
<Tab value="v3">
tsx <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>
</Tab>
</Tabs>
If you use inline code frequently, you can create a simple wrapper component:
<Tabs items={["v2", "v3"]}> <Tab value="v2"> ```tsx import { Code } from "@heroui/react";
<Code color="primary" size="md">Code snippet</Code>
```
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>
```
<Tabs items={["v2", "v3"]}> <Tab value="v2"> ```tsx 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>
);
}
```
The v2 Code component used these base styles that you should include:
px-2 - Horizontal paddingpy-1 - Vertical paddingh-fit - Height fits contentfont-mono - Monospace font familyfont-normal - Normal font weightinline-block - Inline block displaywhitespace-nowrap - Prevent text wrappingCode component no longer exists in v3import { Code } from "@heroui/react"<code> HTML elementprimary → accent in v3Code from @heroui/react imports<Code> instances with <code> elementscolor="primary" to use accent color classes