# Diff Viewer URL: /docs/ui/diff-viewer Render code diffs with syntax highlighting for additions and deletions. import { PreviewCode } from "@/components/docs/preview-code.server"; import { DiffViewerSample, DiffViewerSplitSample, DiffViewerViewModesSample, DiffViewerVariantsSample, DiffViewerSizesSample, } from "@/components/docs/samples/diff-viewer"; This is a **standalone component** that does not depend on the assistant-ui runtime. Installation \[#installation] Usage \[#usage] ```tsx import { DiffViewer } from "@/components/assistant-ui/diff-viewer"; // With a unified diff patch // With file comparison ``` As Markdown Language Override \[#as-markdown-language-override] Integrate with `MarkdownTextPrimitive` to render diff code blocks: ```tsx title="/components/assistant-ui/markdown-text.tsx" import { DiffViewer } from "@/components/assistant-ui/diff-viewer"; // [!code ++] const MarkdownTextImpl = () => { return ( // [!code ++] }, // [!code ++] }} // [!code ++] /> ); }; export const MarkdownText = memo(MarkdownTextImpl); ``` Examples \[#examples] Unified View \[#unified-view] Shows all changes in a single column with `+`/`-` indicators. This is the default mode. ```tsx ``` Split View \[#split-view] Shows old content on the left, new content on the right side-by-side. ```tsx ``` Interactive Mode Toggle \[#interactive-mode-toggle] Variants \[#variants] Sizes \[#sizes] Theming \[#theming] DiffViewer uses CSS variables for colors. Override them in your CSS: ```css [data-slot="diff-viewer"] { --diff-add-bg: rgba(46, 160, 67, 0.15); --diff-add-text: #1a7f37; --diff-add-text-dark: #3fb950; --diff-del-bg: rgba(248, 81, 73, 0.15); --diff-del-text: #cf222e; --diff-del-text-dark: #f85149; } ``` | Variable | Description | | ---------------------- | ----------------------------------------- | | `--diff-add-bg` | Background for added lines | | `--diff-add-text` | Text color for added lines (light mode) | | `--diff-add-text-dark` | Text color for added lines (dark mode) | | `--diff-del-bg` | Background for deleted lines | | `--diff-del-text` | Text color for deleted lines (light mode) | | `--diff-del-text-dark` | Text color for deleted lines (dark mode) | API Reference \[#api-reference] DiffViewer \[#diffviewer] The main component for rendering diffs. Composable API \[#composable-api] | Component | Description | | --------------------- | ------------------------------------------ | | `DiffViewer` | Main component that renders the diff. | | `DiffViewerFile` | Wrapper for each file in multi-file diffs. | | `DiffViewerHeader` | File name header with icon and stats. | | `DiffViewerContent` | Scrollable content area. | | `DiffViewerLine` | Individual line in unified mode. | | `DiffViewerSplitLine` | Side-by-side line pair in split mode. | | `DiffViewerFileBadge` | File extension badge (e.g., "TS"). | | `DiffViewerStats` | Addition/deletion count display. | Style Variants (CVA) \[#style-variants-cva] | Export | Description | | ---------------------- | --------------------------------- | | `diffViewerVariants` | Styles for the root container. | | `diffLineVariants` | Background styles for diff lines. | | `diffLineTextVariants` | Text color styles for diff lines. | ```tsx import { diffViewerVariants, diffLineVariants, diffLineTextVariants, } from "@/components/assistant-ui/diff-viewer"; // Use variants directly
Custom diff container
``` Utilities \[#utilities] | Export | Description | | ----------------------- | ----------------------------------------------- | | `parsePatch(patch)` | Parse unified diff string into structured data. | | `computeDiff(old, new)` | Compute diff between two strings. | | `ParsedLine` | Type for a single diff line. | | `ParsedFile` | Type for a parsed file with lines and stats. | | `SplitLinePair` | Type for a side-by-side line pair. | Styling \[#styling] Data Attributes \[#data-attributes] Use data attributes for custom styling: | Attribute | Values | Description | | ---------------- | ------------------------------------------------------------------- | ------------------------ | | `data-slot` | `"diff-viewer"`, `"diff-viewer-header"`, `"diff-viewer-line"`, etc. | Component identification | | `data-type` | `"add"`, `"del"`, `"normal"`, `"empty"` | Line type | | `data-view-mode` | `"unified"`, `"split"` | Current view mode | | `data-variant` | `"default"`, `"ghost"`, `"muted"` | Current variant | Custom CSS Example \[#custom-css-example] ```css [data-slot="diff-viewer"][data-view-mode="split"] { /* Custom split view styles */ } [data-slot="diff-viewer-line"][data-type="add"] { /* Custom addition styles */ } [data-slot="diff-viewer-line"][data-type="del"] { /* Custom deletion styles */ } ``` Related Components \[#related-components] * [Markdown](/docs/ui/markdown) - Rich text rendering where diff viewer can be integrated * [Syntax Highlighting](/docs/ui/syntax-highlighting) - Code highlighting for other languages