Code Diff
tool-ui/code-diffFreeBlock
Code Diff component for AI interfaces.
See it running
Open the demo on tool-ui
www.tool-ui.com/components/code-diffInstall it
npx shadcn@latest add https://www.tool-ui.com/r/code-diff.jsonSource
1"use client";23import {4 useState,5 useCallback,6 useEffect,7 useMemo,8 createContext,9 use,10 type ReactNode,11} from "react";12import {13 FileDiff as PierreFileDiff,14 PatchDiff as PierrePatchDiff,15} from "@pierre/diffs/react";16import { parseDiffFromFile, RegisteredCustomThemes } from "@pierre/diffs";17import type { FileDiffMetadata, ThemesType } from "@pierre/diffs";18import { Copy, Check, ChevronDown, ChevronUp } from "lucide-react";19import type { CodeDiffProps } from "./schema";20import { useCopyToClipboard } from "../shared/use-copy-to-clipboard";21import { Button, cn, Collapsible, CollapsibleTrigger } from "./_adapter";2223/*24 * Pierre's shared_highlighter registers custom themes with dynamic imports25 * (`import("../themes/pierre-dark.js")`) that fail under Turbopack because the26 * package `exports` field doesn't include those subpaths. We override the27 * RegisteredCustomThemes map entries with loaders that point to local vendored28 * theme files in `components/tool-ui/shared`, which Turbopack can resolve.29 */30RegisteredCustomThemes.set("pierre-dark", () =>31 import("../shared/pierre-dark-theme.js").then((m) => m.default as never),32);33RegisteredCustomThemes.set("pierre-light", () =>34 import("../shared/pierre-light-theme.js").then((m) => m.default as never),35);3637const COPY_ID = "codediff-code";3839/* ── Theme detection (mirrors CodeBlock) ────────────────────────── */4041function getSystemTheme(): "light" | "dark" {42 if (typeof window === "undefined") return "light";43 return window.matchMedia?.("(prefers-color-scheme: dark)").matches44 ? "dark"45 : "light";46}4748function getDocumentTheme(): "light" | "dark" | null {49 if (typeof document === "undefined") return null;50 const root = document.documentElement;51 const dataTheme = root.getAttribute("data-theme")?.toLowerCase();52 if (dataTheme === "dark") return "dark";53 if (dataTheme === "light") return "light";54 if (root.classList.contains("dark")) return "dark";55 if (root.classList.contains("light")) return "light";56 return null;57}5859function useResolvedTheme(): "light" | "dark" {60 const [theme, setTheme] = useState<"light" | "dark">(() => {61 return getDocumentTheme() ?? getSystemTheme();62 });6364 useEffect(() => {65 if (typeof window === "undefined" || typeof document === "undefined") {66 return;67 }6869 const update = () => setTheme(getDocumentTheme() ?? getSystemTheme());7071// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from tool-ui
All 27 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Approval Cardapproval-card | tool-ui | Blocks | Free | 2 deps · 11 files | |
| Audioaudio | tool-ui | Blocks | Free | 2 deps · 9 files | |
| Chartchart | tool-ui | Blocks | Free | 2 deps · 8 files | |
| Citationcitation | tool-ui | Blocks | Free | 2 deps · 15 files | |
| Code Blockcode-block | tool-ui | Blocks | Free | 3 deps · 11 files | |
| Data Tabledata-table | tool-ui | Blocks | Free | 1 dep · 17 files | |
| Geo Mapgeo-map | tool-ui | Blocks | Free | 4 deps · 12 files | |
| Imageimage | tool-ui | Blocks | Free | 1 dep · 14 files |
