terra vidhal

Code Snippet Library

Component

About

A searchable code snippet library with language and tag filters, favourites, sorting, numbered code preview, one-click copy, an inline create and edit form with validation and deletion with undo.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Code Snippet Library to your project and fits it to your theme.

Code

How Code Snippet Library is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

/**
 * PrequelUI Animate — MIT — https://prequeluianimate.terravidhal.me
 */

import { CodeSnippetLibrary, type CodeSnippet } from "@/components/ui/code-snippet-library";

const SNIPPETS: CodeSnippet[] = [
  {
    id: "debounce",
    title: "useDebouncedValue",
    description: "Delay a fast-changing value so filters and searches stay calm.",
    language: "typescript",
    tags: ["react", "hooks", "input"],
    updatedAt: "Today",
    favorite: true,
    code: `export function useDebouncedValue<T>(value: T, delay = 250) {
  const [debounced, setDebounced] = React.useState(value);

  React.useEffect(() => {
    const timer = window.setTimeout(() => setDebounced(value), delay);
    return () => window.clearTimeout(timer);
  }, [value, delay]);

  return debounced;
}`,
  },
  {
    id: "fetch-json",
    title: "fetchJson",
    description: "A tiny fetch wrapper that throws on non-2xx responses.",
    language: "typescript",
    tags: ["api", "fetch", "errors"],
    updatedAt: "2 days ago",
    favorite: true,
    code: `export async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
  const response = await fetch(url, {
    ...init,
    headers: { "Content-Type": "application/json", ...init?.headers },
  });

  if (!response.ok) {
    throw new Error("Request failed with status " + response.status);
  }

  return (await response.json()) as T;
}`,
  },
  {
    id: "focus-ring",
    title: "Accessible focus ring",
    description: "A focus style that survives dark backgrounds without a halo.",
    language: "css",
    tags: ["accessibility", "focus", "css"],
    updatedAt: "Last week",
    favorite: false,
    code: `:where(button, a, input, select, textarea):focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 6px;
}

:where(button, a):focus:not(:focus-visible) {
  outline: none;
}`,
  },
  {
    id: "soft-delete",
    title: "Soft delete with a grace period",
    description: "Mark a row deleted, let the visitor undo, then clean it up.",
    language: "sql",
    tags: ["database", "delete", "undo"],
    updatedAt: "Last week",
    favorite: false,
    code: `update documents
set deleted_at = now()
where id = $1
returning id, deleted_at;

-- A scheduled job removes rows older than 30 days:
delete from documents
where deleted_at < now() - interval '30 days';`,
  },
  {
    id: "backup",
    title: "Nightly backup",
    description: "Copy the database and keep the last fourteen archives.",
    language: "bash",
    tags: ["backup", "ops", "cron"],
    updatedAt: "2 weeks ago",
    favorite: true,
    code: `set -euo pipefail

stamp=$(date +%Y-%m-%d)
target="/srv/backups/db-$stamp.sql.gz"

pg_dump "$DATABASE_URL" | gzip -9 > "$target"
find /srv/backups -name 'db-*.sql.gz' -mtime +14 -delete

echo "Backup written to $target"`,
  },
];

export default function CodeSnippetLibraryDemo() {
  return (
    <main className="flex min-h-screen w-full items-center justify-center bg-background px-4 py-12 sm:px-6">
      <CodeSnippetLibrary
        snippets={SNIPPETS}
        title="Engineering snippets"
        description="Five small pieces the team reaches for every week. Search by tag, copy in one click and keep the favourites close."
      />
    </main>
  );
}

Dependencies

More by terra vidhal

View all
  • Gravity Stars Background

    Gravity Stars Background

    16
  • Audio Waveform

    Audio Waveform

    13
  • Topographic Lines

    Topographic Lines

    12
  • Notification Center

    Notification Center

    11
  • Booking Calendar

    Booking Calendar

    10
  • Spotlight Card

    Spotlight Card

    10
  • Command Palette

    Command Palette

    9
  • Kpi Counter

    Kpi Counter

    9

Explore more

Part of PrequelUI Animate — browse the full library on 21st.dev.