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.See this component the way an AI agent does.
Installation
npx shadcn@latest add @keyframery/diff-viewerThis 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
| Move | What you see |
|---|---|
| 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. |
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.
| Variable | What it changes | Default |
|---|---|---|
--diff-viewer-face | Sheet | bg-card |
--diff-viewer-ink | Code text | text-card-foreground |
--diff-viewer-added | Added lines and words | oklch(0.62 0.16 150) |
--diff-viewer-removed | Removed lines and words | var(--destructive) |
--diff-viewer-radius | Sheet corners | rounded-[14px] |
--diff-viewer-font | Code size | text-[12.5px] |
--diff-viewer-max-height | Body height before it scrolls | none |
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
| Key | What it does |
|---|---|
| Tab | Moves 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 Space | Presses 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.
More in AI
All componentsBubble
Chat bubbles that grow from their sender’s corner, with grouped messages, reply actions and delayed reactions, built on UI SDK core.
Markdown
Renders an AI answer's markdown as it streams: finished blocks stay put while only the last one grows, each new block rises in, unfinished syntax reads as it will once closed, and a slim caret marks where the text is arriving.