shadimage
mischief
examplediff-view-demosource: ok · refreshed

Diff View Demo

A worked example of Diff View: A proposed change shown as a unified or side-by-side diff, with optional accept and reject controls.

npx shadcn add "https://ui.tinkererslabs.com/r/diff-view-demo.json"
free

No selection tags yet — empty means no evidence, not lack of support.

Dependencies

Bundle weight · light
lucide-react@^1.31.0

Registry dependencies

https://ui.tinkererslabs.com/r/diff-view.json

Files3 · source cached 10/8/2026, 5:24:08 AM

components/demos/diff-view-demo.tsx
"use client"

import * as React from "react"

import { DemoVariants } from "@/components/demos/demo-variants"
import { RestartButton } from "@/components/demos/restart-button"
import { DiffView } from "@/registry/default/diff-view/diff-view"

const before = `export function total(items) {
  let sum = 0

  for (const item of items) {
    sum += item.price
  }

  return sum
}`

const after = `export function total(items, { taxRate = 0 } = {}) {
  let sum = 0

  for (const item of items) {
    sum += item.price * item.quantity
  }

  return sum * (1 + taxRate)
}`

function Decision() {
  const [status, setStatus] = React.useState<
    "pending" | "accepted" | "rejected"
  >("pending")

  return (
    <div className="grid gap-4">
      <DiffView
        filename="lib/total.ts"
        before={before}
        after={after}
        status={status}
        onAccept={() => setStatus("accepted")}
        onReject={() => setStatus("rejected")}
      />

      {status === "pending" ? null : (
        <RestartButton onClick={() => setStatus("pending")} />
      )}
    </div>
  )
}

export function DiffViewDemo() {
  return (
    <DemoVariants
      label="Diff layout"
      variants={[
        { id: "decide", label: "To review", render: () => <Decision /> },
        {
          id: "unified",
          label: "Unified",
          render: () => (
            <DiffView filename="lib/total.ts" before={before} after={after} />
          ),
        },
        {
          id: "split",
          label: "Split",
          render: () => (
            <DiffView
              filename="lib/total.ts"
              before={before}
              after={after}
              view="split"
            />
          ),
        },
      ]}
    />
  )
}
components/demos/demo-variants.tsx
"use client"

import * as React from "react"

export type DemoVariant = {
  id: string
  label: string
  render: () => React.ReactNode
}

export function DemoVariants({
  variants,
  label = "Demo variant",
}: {
  variants: [DemoVariant, ...DemoVariant[]]
  label?: string
}) {
  const [activeId, setActiveId] = React.useState(variants[0].id)
  const active =
    variants.find((variant) => variant.id === activeId) ?? variants[0]

  return (
    <div className="grid w-full justify-items-center gap-6">
      {/* Centres whatever the variant renders. Without this a demo that caps
          its own width sits against the left edge while the switch below it
          is centred, which reads as a mistake. */}
      <div className="flex w-full justify-center">{active.render()}</div>

      <div
        role="group"
        aria-label={label}
        className="border-border bg-background/70 inline-flex flex-wrap justify-center gap-1 rounded-full border p-1"
      >
        {variants.map((variant) => {
          const isActive = variant.id === active.id

          return (
            <button
              key={variant.id}
              type="button"
              aria-pressed={isActive}
              className={
                isActive
                  ? "bg-foreground text-background rounded-full px-3 py-1.5 text-xs font-semibold"
                  : "text-muted-foreground hover:text-foreground rounded-full px-3 py-1.5 text-xs font-semibold"
              }
              onClick={() => setActiveId(variant.id)}
            >
              {variant.label}
            </button>
          )
        })}
      </div>
    </div>
  )
}
components/demos/restart-button.tsx
"use client"

import { RotateCcw } from "lucide-react"

export function RestartButton({ onClick }: { onClick: () => void }) {
  return (
    <button className="restore-button" type="button" onClick={onClick}>
      <RotateCcw aria-hidden="true" size={15} /> Replay
    </button>
  )
}

More from mischief

Browse all components in this library →