Keyframery

Diff Viewer

Review the changes an agent proposes to a file: a line diff computed in-house (or read from a patch), unified or side by side, changed words marked, long unchanged runs folded, and Accept or Reject on every hunk.

See it as an AI agent does.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/diff-viewer

This adds components/keyframery/diff-viewer.tsx and its primitive to your project, and installs UI SDK, along with code-block, which it builds on. The motion tokens are added to your global CSS the first time you install a component.

Usage

import { DiffViewer } from "@/components/keyframery/diff-viewer"

export default function Usage() {
  return (
    <DiffViewer
      filename="src/config.ts"
      before={`export const config = {\n  retries: 2,\n  timeout: 5000,\n}\n`}
      after={`export const config = {\n  retries: 3,\n  timeout: 8000,\n}\n`}
    />
  )
}

Examples

Review an agent's edit

Pass onAccept and onReject and every hunk gets the two buttons. Each call carries the hunk's index, so you can apply or drop that part of the edit.

From a patch

Give it the output of git diff instead of two texts. The file name comes from the patch; folds between hunks show how many lines they hide.

Side by side

Start in the split view with defaultView="split". Below splitMinWidth (640px by default) it shows as unified, so it still reads on a phone.

Motion

MoveWhat you see
DirectionSwitching unified and split, the new layout fades in from the side of its button while the switch's key slides across.
HeightAn opened fold grows from its row to its lines, which fade in.
CascadeOn mount, hunks rise in one after another (flourish).
RollThe +added and −removed counts roll up to their values (flourish).
DrawAccepting a hunk swaps its buttons for a tick that draws itself; a rejected hunk steps back.
PressThe view switch, review buttons and copy key sink under the press.

Timing and springiness follow the motion style you set. To change how fast this diff viewer moves, pass speed:

<DiffViewer speed="fast">
  …
</DiffViewer>

"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.

Theming

Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.

VariableWhat it changesDefault
--diff-viewer-faceSheetbg-card
--diff-viewer-inkCode texttext-card-foreground
--diff-viewer-addedAdded lines and wordsoklch(0.62 0.16 150)
--diff-viewer-removedRemoved lines and wordsvar(--destructive)
--diff-viewer-radiusSheet cornersrounded-[14px]
--diff-viewer-fontCode sizetext-[12.5px]
--diff-viewer-max-heightBody height before it scrollsnone

API reference

Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.

DiffViewer

Renders a <div>, and takes its props too.

Prop

Type

DiffViewerBlocks

Prop

Type

DiffViewerLines

Prop

Type

DiffViewerViewOption

Renders a <button>, and takes its props too.

Prop

Type

Other parts

These take the props of the element they render, and nothing else:

  • DiffViewerActions: a <div>
  • DiffViewerContent: a <div>
  • DiffViewerCopyButton: a <button>
  • DiffViewerHeader: a <div>
  • DiffViewerStats: a <div>
  • DiffViewerTitle: a <div>
  • DiffViewerViewSwitch: a <div>
  • DiffViewerAccept: a <button>
  • DiffViewerExpanded: a <div>
  • DiffViewerFold: a <button>
  • DiffViewerGap: a <div>
  • DiffViewerHunk: a <div>
  • DiffViewerHunkHeader: a <div>
  • DiffViewerReject: a <button>
  • DiffViewerStatus: a <span>
  • DiffViewerUndo: a <button>

Keyboard

KeyWhat it does
TabMoves to the view switch, the copy button, the body (so it can scroll), then each fold and review button.
← → ↑ ↓Scroll the diff while the body has focus.
Enter or SpacePresses the focused button: switch view, open a fold, accept, reject, undo or copy.

Accessibility

Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.

Built on UI SDK's core

UI SDK doesn't ship a diff viewer, so this one is defined with its core: one definePrimitive in components/keyframery/core/diff-viewer-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/diff-viewer.

Questions

How does the animated Diff Viewer for UI SDK move?

Direction: Switching unified and split, the new layout fades in from the side of its button while the switch's key slides across. Height: An opened fold grows from its row to its lines, which fade in. Cascade: On mount, hunks rise in one after another (flourish). Roll: The +added and −removed counts roll up to their values (flourish). Draw: Accepting a hunk swaps its buttons for a tick that draws itself; a rejected hunk steps back. Press: The view switch, review buttons and copy key sink under the press.

Do I need Motion or Framer Motion for the animated Diff Viewer?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

UI SDK has no diff viewer. How is this one built on UI SDK?

Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/diff-viewer-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.

What happens to the Diff Viewer when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.

All components

On this page