Diff
ProAn agent's edit to one file, waiting in its answer: the row reads Edited policy.ts +5 −2 with Undo and Keep, over the path and the line diff. Keep and Undo turn the diff into the file in place, and each change can be kept or undone on its own.
npx shadcn@latest add @pixilui-pro/diffPreview
import { useRef } from "react";
import { Button } from "../../../registry/ui/button/button";
import { Diff, type DiffHandle } from "@/components/pixilui/diff";
import { policyAfter, policyBefore } from "@/components/pixilui/diff.examples";
// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
state: ["review", "kept", "undone", "editing"],
context: ["3 lines", "1 line"],
} as const;
const ANSWER = { fontSize: "var(--pixilui-text-answer)", lineHeight: "var(--pixilui-text-answer-leading)", letterSpacing: "var(--pixilui-text-answer-tracking)" };
/** The agent's edit to one file, waiting: Keep or Undo it whole, or hover a change for its own. Replay shows the agent writing it. */
export default function DiffDemo({ state = "review", context = "3 lines" }: KnobValues<typeof knobs>) {
const ref = useRef<DiffHandle>(null);
return (
<div className="grid w-full max-w-xl gap-3 rounded-panel bg-surface p-5 elevation-raised">
<p className="m-0 text-foreground" style={ANSWER}>
Raised the auto-approve limit to $500. Anything above it now goes to finance leads instead of being denied.
</p>
<Diff key={`${state}${context}`} ref={ref} path="src/refunds/policy.ts" before={policyBefore} after={policyAfter} state={state} context={context === "3 lines" ? 3 : 1} />
<div className="flex gap-1">
<Button variant="secondary-2" size="sm" onClick={() => ref.current?.write()}>
Replay the edit
</Button>
<Button variant="secondary-2" size="sm" onClick={() => ref.current?.reset()}>
Reset
</Button>
</div>
</div>
);
}When to use
Use it when
- An edit the agent made to a file that the person keeps or undoes, inside the chat.
- A file with several changes, where some are right and some are not: each change has its own Undo and Keep.
- A replay of the agent writing the edit (a ref's write()).
Use something else when
- An edit across several files under one decision: use File edits.
- Code with nothing to decide: use Code block.
- A side by side review of a whole pull request: a code host does that.
Installation
Join the waitlist and we'll email you when PixilUI launches. The preview, props and docs stay open until then.
Pro installs with the same CLI as the free items. One command adds diff.tsx and the free button.tsx, the free kbd.tsx, the free tooltip.tsx and the free code-block.tsx it uses; your license key goes in once, as in Installation.
{
"registries": {
"@pixilui-pro": {
"url": "https://pixilui.com/r/pro/{name}.json",
"headers": { "Authorization": "Bearer ${PIXILUI_TOKEN}" }
}
}
}npx shadcn@latest add @pixilui-pro/diffAdds diff.tsx with button.tsx, kbd.tsx, tooltip.tsx and code-block.tsx, and installs clsx and tailwind-merge. The CLI reads PIXILUI_TOKEN from .env.local; if this computer has not logged in yet, run npx pixilui@latest login once.
components/pixilui/diff.tsxthe item, 733 linescomponents/pixilui/button.tsxused inside, freecomponents/pixilui/kbd.tsxused inside, freecomponents/pixilui/tooltip.tsxused inside, freecomponents/pixilui/code-block.tsxused inside, freeIt uses Button, Kbd, Tooltip and Code block: add them the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { Diff } from "@/components/pixilui/diff";
<Diff
path={path}
before={before}
after={after}
onOpenChange={onOpenChange}
onStateChange={onStateChange}
/>Fills its container. Long lines wrap under their own column; under 330 wide Undo keeps only its icon; nothing scrolls sideways at 390.
API reference
pathRequiredstring–The file: "src/refunds/policy.ts". The folders go quiet, the file name leads.beforeRequiredstring–The file as it was (an empty string: a new file, all added).afterRequiredstring–The file as the agent left it.contextnumber3Unchanged lines kept around each change (3, as git); longer runs fold into a band you can open.state"review" | "kept" | "undone" | "editing""review"How it starts: review (waiting, the default), kept, undone or editing.defaultOpenbooleantrueOpen (default) or folded, uncontrolled.openboolean–Open or folded, controlled.onOpenChange(open: boolean) => void–The row was pressed.onStateChange(state: DiffFileState, changes: DiffState[]) => void–The file's state changed (each change's state, in order, too).changeActions"float" | "icons" | "under""float"Where a change's own Undo and Keep sit: float (the lock's pick), icons or under.wordsPartial<DiffWords>–Every word it says.iconsDiffIcons{}The icons it draws (the locked disclosure, undo, redo and done).Also takes every native <section> attribute except onToggle, and a ref.
Keyboard
Accessibility
- After Undo focus moves to Redo, after Redo to Undo, after a change's Keep to the next waiting change; each decision is said in the row's status line.
- Signs carry the change as well as color, and each changed line says Added or Removed to a screen reader.
- A change's Undo and Keep are a group named Change 1 of 2; on touch they take a 44 row of their own inside the green block; Keep reaches 44 on touch.
Motion
- Keep: the removed rows fold shut and the tints drain left to right. Undo: the new words erase from the right, then their rows fold, and the numbers below roll back.
- Redo and the replay: rows open and the words write in across the line (560ms, even), a row apart; the counts roll up from nothing.
- A change's buttons wait 80ms for a hover, then grow from their right edge out of a 2px blur; they leave faster. The header's words swap with the quiet blur swap.
- Reduced motion: every fold, wipe, write and swap at once.