aiellie

Search

Find a component, template or page

GitHub
Components

Code Diff

What a change did, read from a unified diff: added and removed lines tinted and marked, both sides numbered, and a +N −M count for the header.

lib/format.ts
Added +3Removed −2
@@ -1,8 +1,9 @@export function formatDuration(seconds: number) {Removed:   const whole = Math.round(seconds)Added:   const whole = Math.max(1, Math.round(seconds))  if (whole < 60) return `${whole}s`  const minutes = Math.floor(whole / 60)Removed:   return `${minutes}m ${whole % 60}s`Added:   const rest = whole % 60Added:   return rest > 0 ? `${minutes}m ${rest}s` : `${minutes}m`}
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/code-diff"

shadcn needs the @aiellie registry in components.json first. npx aiellie add code-diff adds it on its first run, then installs the same way.

Code Diff shows what a change did, read straight from the unified diff that git diff or an agent's edit tool writes. Added and removed lines are tinted and marked, both sides of the change are numbered, and each hunk keeps its header. CodeDiffStat counts the lines for the header.

It goes inside a CodeBlock, so a diff has the same frame, title and actions as any other code.

Getting started

  1. Install the code block too

    The diff installs code-block with it, for its frame and its colouring.

  2. Show a diff

    components/change.tsx
    import {
      CodeBlock,
      CodeBlockHeader,
      CodeBlockTitle,
    } from "@/components/aiellie/code-block"
    import { CodeDiffContent, CodeDiffStat } from "@/components/aiellie/code-diff"
    
    export function Change({ file, diff }: { file: string; diff: string }) {
      return (
        <CodeBlock>
          <CodeBlockHeader>
            <CodeBlockTitle>{file}</CodeBlockTitle>
            <CodeDiffStat diff={diff} className="ms-auto" />
          </CodeBlockHeader>
          <CodeDiffContent diff={diff} language="ts" />
        </CodeBlock>
      )
    }

    Lines before the first hunk, like diff --git and the --- and +++ names, are left out, since the header already names the file.

Anatomy

<pre data-slot="code-diff-content" dir="ltr">
  <span data-slot="code-diff-hunk" /> {/* @@ -1,8 +1,9 @@ */}
  <span data-slot="code-diff-line" data-kind="context | added | removed" />
</pre>
<span data-slot="code-diff-stat" /> {/* +3 −2 */}

Each line's gutter holds its old number, its new number and a + or −, and stays put while the code scrolls. A screen reader hears "Added" or "Removed" before a changed line, so the change doesn't rest on colour alone.

The tints are mixed into the ground rather than laid over it, so the pinned gutter can share its line's colour. Added lines use emerald, since a shadcn theme has no success colour.

Examples

A new file

A diff against /dev/null is all additions. Leave out lineNumbers when the numbers say nothing, and leave out language to keep the lines plain.

.env.example
Added +3Removed −0
@@ -0,0 +1,3 @@Added: DATABASE_URL=Added: OPENAI_API_KEY=Added: NEXT_PUBLIC_APP_URL=http://localhost:3000

Count what changed

diffStat gives the same counts as numbers, for a summary of several files.

import { diffStat } from "@/components/aiellie/code-diff"

const { added, removed } = diffStat(diff)

API reference

CodeDiffContent

PropTypeDefaultDescription
diffstringrequiredA unified diff.
languagestringAny language Shiki knows. Left out, the lines stay plain.
lineNumbersbooleantrueNumbers both sides of each line.

All other pre props are forwarded.

CodeDiffStat

PropTypeDefaultDescription
diffstringrequiredThe diff to count.

All other span props are forwarded.

parseDiff

ArgumentTypeDefaultDescription
diffstringrequiredA unified diff.

Returns its rows as DiffLines: hunk headers, and context, added and removed lines with their numbers.