Skip to content

CodeDiff

What changed between two versions of a text: a file in a pull request, a policy, a migration. Removed lines with −, added lines with +, and long unchanged runs folded away.

import { CodeDiff, CodeDiffSkeleton } from '@sypnia/ui/react'

On this page
  1. A policy that changed
  2. A long file with one change
  3. Skeleton
  4. When to use it
  5. For everyone
  6. Props

A policy that changed

db/policies.sql+2−2
create policy "read own orders"
on orders for select
removed: using (customer_id = auth.uid());
added: using (customer_id = auth.uid() or auth.role() = 'staff');
create policy "insert own orders"
on orders for insert
removed: with check (customer_id = auth.uid());
added: with check (customer_id = auth.uid() and total > 0);
Code
import { CodeDiff } from '@sypnia/ui/react';

// A change to a policy: removed lines with −, added lines with +, both said in words to screen readers.
const before = `create policy "read own orders"
on orders for select
using (customer_id = auth.uid());

create policy "insert own orders"
on orders for insert
with check (customer_id = auth.uid());`;
const after = `create policy "read own orders"
on orders for select
using (customer_id = auth.uid() or auth.role() = 'staff');

create policy "insert own orders"
on orders for insert
with check (customer_id = auth.uid() and total > 0);`;
export default function Policy() {
  return <CodeDiff title="db/policies.sql" lang="sql" before={before} after={after} />;
}

A long file with one change

src/products.ts+1−1
{ id: 19, name: 'Product 19', price: 38 },
{ id: 20, name: 'Product 20', price: 40 },
removed: { id: 21, name: 'Product 21', price: 42 },
added: { id: 21, name: 'Rye loaf', price: 7 },
{ id: 22, name: 'Product 22', price: 44 },
{ id: 23, name: 'Product 23', price: 46 },
Code
import { CodeDiff } from '@sypnia/ui/react';

// A long file with one change: the unchanged lines fold away. Press a fold to open it.
const lines = Array.from({ length: 40 }, (_, i) => `  { id: ${i + 1}, name: 'Product ${i + 1}', price: ${(i + 1) * 2} },`);
const before = ['export const products = [', ...lines, '];'].join('\n');
const after = before.replace("name: 'Product 21', price: 42", "name: 'Rye loaf', price: 7");
export default function Folded() {
  return <CodeDiff title="src/products.ts" lang="ts" before={before} after={after} context={2} />;
}

Skeleton

Code
import { CodeDiffSkeleton } from '@sypnia/ui/react';

// While the two versions load.
export default function Skeleton() {
  return <CodeDiffSkeleton lines={5} />;
}

When to use it

Use it when

  • Reviewing a change before it is applied.
  • Showing what the agent changed in a fix.

Do not use it when

  • Text that is not line-based, like a paragraph: say what changed in words.

For everyone

  • Each changed line starts with «added» or «removed» for screen readers; the colour is never the only sign.
  • The counts in the bar say how many lines were added and removed.

Props

CodeDiffTypeDefault
afterrequired string
beforerequired string
context

Unchanged lines kept around each change; longer runs fold.

number 3
lang string text
title

The file: «db/policies.sql».

string
CodeDiffSkeletonTypeDefault
lines number 6
  1. A policy that changed
  2. A long file with one change
  3. Skeleton
  4. When to use it
  5. For everyone
  6. Props

    Search pages, sections and terms