Skip to content
Lenso UI

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 Featurev3 EquivalentNotes
Copy buttonManual Button + Clipboard APIUse navigator.clipboard.writeText()
Copy tooltipTooltip componentUse v3 Tooltip component
Symbol prefixManual renderingAdd symbol as text content
Multi-line supportArray mappingMap over array of strings
Variants (flat, solid, bordered, shadow)Tailwind classesUse background/border utilities
Colors (default, primary, etc.)Tailwind classesUse color utilities
Sizes (sm, md, lg)Tailwind text sizesUse text-sm, text-base, text-lg
RadiusTailwind border radiusUse 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>

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

  1. Component Removed: Snippet component no longer exists in v3
  2. Import Change: Remove import { Snippet } from "@lenso/ui"
  3. Use Native Elements: Replace with native <pre> and <code> elements
  4. Manual Copy: Implement copy functionality using Clipboard API
  5. Styling: Apply Tailwind CSS classes directly for variants, colors, sizes
  6. Tooltip: Use v3 Tooltip component for copy button tooltips
  7. Button: Use v3 Button component for copy button

Migration Steps

  1. Remove Import: Remove Snippet from @lenso/ui imports
  2. Replace Component: Replace all <Snippet> instances with native HTML elements
  3. Add Copy Functionality: Implement copy using navigator.clipboard.writeText()
  4. Add Copy Button: Use v3 Button component with Tooltip
  5. Apply Styling: Use Tailwind CSS classes for variants, colors, sizes
  6. Handle Multi-line: Map over arrays if multi-line snippets are needed
  7. 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);  }};