Skip to content

Copy Code Snippet

A dark code block with a one-click copy button that confirms itself.

Freeminimaleditorial
Category
Patterns
Added
2026-08-23
Deps
1
Updated
2026-09-01
Preview
bash
npm install viberdy
npx viberdy add magnetic-button

Props

Requires lucide-react
"use client";

import { useState } from "react";
import { Check, Copy } from "lucide-react";

export function CopyCodeSnippet({
  code = "npm install viberdy",
  language = "bash",
}: {
  code?: string;
  language?: string;
}) {
  const [copied, setCopied] = useState(false);

  function handleCopy() {
    navigator.clipboard?.writeText(code).catch(() => {});
    setCopied(true);
    setTimeout(() => setCopied(false), 1600);
  }

  return (
    <div className="w-full max-w-md overflow-hidden rounded-xl border border-neutral-800 bg-neutral-900">
      <div className="flex items-center justify-between border-b border-white/10 px-4 py-2">
        <span className="rounded-full bg-white/10 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wide text-neutral-400">
          {language}
        </span>
        <button
          onClick={handleCopy}
          className="flex items-center gap-1.5 text-xs font-medium text-neutral-400 hover:text-red-400"
        >
          {copied ? (
            <>
              <Check size={12} className="text-red-400" />
              Copied
            </>
          ) : (
            <>
              <Copy size={12} />
              Copy
            </>
          )}
        </button>
      </div>
      <pre className="overflow-x-auto p-4 font-mono text-xs leading-relaxed text-neutral-100">{code}</pre>
    </div>
  );
}
Notes

About this pattern

The interesting part of a copy button is not the icon swap, it is making the confirmation state trustworthy: this one wraps navigator.clipboard.writeText in a .catch that silently swallows a rejected or unsupported clipboard call, so the button still flips to Copied even where the clipboard API is unavailable or permission was denied, which is generous but means the confirmation is not proof the copy succeeded. Reach for this over a raw pre block with no controls when the snippet is meant to be run, not just read, an install command or a config example, where whether that actually copied is a real question users ask. The code renders through a plain pre with a monospace font and no syntax highlighting; colored tokens are a separate integration, Shiki or Prism, layered on top. The language prop is just a label pill, not a highlighting hint; it does not parse or affect rendering. Colors are the same fixed dark-background pairing used in the docs site's own code viewer, so the block keeps correct contrast regardless of the page's theme.

Curator’s note

Uses the same fixed bg-ink/text-paper inverted pair as the detail page's own code viewer — both tokens flip together per theme, so the block always has correct contrast, unlike using either token alone directly against the canvas.

Related

Pairs well with

Matched on shared tags and category — the entries most likely to be used alongside this one.

Cards that pin and stack on top of each other as you scroll past.

scrollstickystack

A drag-handle-driven list that reorders with a smooth layout swap.

dragsortablelist

A Cmd+K style searchable command menu with a filtered live list.

commandpalettesearch