Snippet
Migration guide for Snippet from HeroUI v2 to v3
The Snippet component has been removed in HeroUI v3. Use native HTML elements with Tailwind CSS classes and implement copy functionality manually using the Clipboard API.
Key Changes
1. Component Removal
v2: <Snippet> component from @lenso/ui
v3: Native HTML elements (<pre>, <code>) with manual copy implementation
2. Features Mapping
The v2 Snippet component had the following features that need to be replaced:
| v2 Feature | v3 Equivalent | Notes |
|---|---|---|
| Copy button | Manual Button + Clipboard API | Use navigator.clipboard.writeText() |
| Copy tooltip | Tooltip component | Use v3 Tooltip component |
| Symbol prefix | Manual rendering | Add symbol as text content |
| Multi-line support | Array mapping | Map over array of strings |
Variants (flat, solid, bordered, shadow) | Tailwind classes | Use background/border utilities |
Colors (default, primary, etc.) | Tailwind classes | Use color utilities |
Sizes (sm, md, lg) | Tailwind text sizes | Use text-sm, text-base, text-lg |
| Radius | Tailwind border radius | Use rounded-* classes |
Structure Changes
In v2, Snippet was a component wrapper with built-in copy functionality:
import { Snippet } from "@lenso/ui";
export default function App() { return ( <Snippet symbol="$"> npm install @lenso/ui </Snippet> );}In v3, use native HTML elements with manual copy implementation:
import { Button, Tooltip } from "@lenso/ui";import { useState } from "react";
export default function App() { const [copied, setCopied] = useState(false);
const handleCopy = async () => { await navigator.clipboard.writeText("npm install @lenso/ui"); setCopied(true); setTimeout(() => setCopied(false), 2000); };
return ( <div className="flex items-center gap-2 rounded-lg bg-default-100 px-3 py-1.5"> <pre className="text-sm font-mono"> <span className="text-default-500">$ </span> npm install @lenso/ui </pre> <Tooltip> <Button isIconOnly aria-label="Copy" size="sm" variant="ghost" onPress={handleCopy} > {copied ? "✓" : "📋"} </Button> <Tooltip.Content>{copied ? "Copied!" : "Copy to clipboard"}</Tooltip.Content> </Tooltip> </div> );}Migration Examples
Multi-line Snippet
<Snippet symbol="$"> {[ "npm install @lenso/ui", "yarn add @lenso/ui", "pnpm add @lenso/ui" ]}</Snippet>Styling Options
{/* With variants */}<Snippet variant="bordered" color="primary"> npm install @lenso/ui</Snippet>
{/* Without symbol */}<Snippet hideSymbol> npm install @lenso/ui</Snippet>
{/* Without copy button */}<Snippet hideCopyButton> npm install @lenso/ui</Snippet>Creating a Reusable Snippet Component (Recommended)
Since Snippet functionality is commonly needed, here's a complete reusable component:
import { Snippet } from "@lenso/ui";
<Snippet symbol="$" variant="bordered" color="primary" size="md"> npm install @lenso/ui</Snippet>Summary
- Component Removed:
Snippetcomponent no longer exists in v3 - Import Change: Remove
import { Snippet } from "@lenso/ui" - Use Native Elements: Replace with native
<pre>and<code>elements - Manual Copy: Implement copy functionality using Clipboard API
- Styling: Apply Tailwind CSS classes directly for variants, colors, sizes
- Tooltip: Use v3 Tooltip component for copy button tooltips
- Button: Use v3 Button component for copy button
Migration Steps
- Remove Import: Remove
Snippetfrom@lenso/uiimports - Replace Component: Replace all
<Snippet>instances with native HTML elements - Add Copy Functionality: Implement copy using
navigator.clipboard.writeText() - Add Copy Button: Use v3 Button component with Tooltip
- Apply Styling: Use Tailwind CSS classes for variants, colors, sizes
- Handle Multi-line: Map over arrays if multi-line snippets are needed
- Optional: Create reusable Snippet component for your application
Clipboard API Notes
The Clipboard API requires:
- HTTPS (or localhost for development)
- User interaction (can't be called automatically)
- Browser support (modern browsers)
For fallback support:
const handleCopy = async (text: string) => { try { await navigator.clipboard.writeText(text); } catch (error) { // Fallback for older browsers const textArea = document.createElement("textarea"); textArea.value = text; textArea.style.position = "fixed"; textArea.style.opacity = "0"; document.body.appendChild(textArea); textArea.select(); document.execCommand("copy"); document.body.removeChild(textArea); }};