Specification
- Catalogue
- No. 24 of 110
- Section
- Text and code
- Interaction
- Press
- Element
- Code Block
- Preview
- Live component
- Imports
- react 19.3.1, next 16.5.0
- Added
Notes
Two versions of the same code side by side, with every changed line marked by a sign as well as a colour.
A developer sees exactly which lines change. Removed and added lines carry a minus or plus and are named for screen readers, and on a phone the two stack behind a toggle.
Real example: Next.js useRouter (Pages Router) to useSearchParams (App Router). Under 600px the panes stack behind a Before and After toggle. Copy button with a written result, including when copying fails. Mono only inside the code.
Code
React and plain CSS"use client";
import { useEffect, useId, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "./code-comparison.css";
type CodeLine = { text: string; mark?: "add" | "del" };
const BEFORE: CodeLine[] = [
{ text: 'import { useRouter } from "next/router";', mark: "del" },
{ text: "" },
{ text: "export default function Search() {" },
{ text: " const router = useRouter();", mark: "del" },
{ text: " const term = router.query.q;", mark: "del" },
{ text: " return <p>Results for {term}</p>;" },
{ text: "}" },
];
const AFTER: CodeLine[] = [
{ text: '"use client";', mark: "add" },
{ text: 'import { useSearchParams } from "next/navigation";', mark: "add" },
{ text: "" },
{ text: "export default function Search() {" },
{ text: " const params = useSearchParams();", mark: "add" },
{ text: ' const term = params.get("q");', mark: "add" },
{ text: " return <p>Results for {term}</p>;" },
{ text: "}" },
];
function Pane({ title, sub, lines, id }: { title: string; sub: string; lines: CodeLine[]; id: string }) {
const [copied, setCopied] = useState("");
useEffect(() => {
if (!copied) return;
const t = window.setTimeout(() => setCopied(""), 2000);
return () => window.clearTimeout(t);
}, [copied]);
const copy = async () => {
try {
await navigator.clipboard.writeText(lines.map((l) => l.text).join("\n"));
setCopied("Copied");
} catch (err) {
console.error("Copy failed", err);
setCopied("Copy failed: select the code instead");
}
};
return (
<figure className="ccode-pane" aria-labelledby={id}>
<figcaption className="ccode-head">
<span id={id}>
<strong>{title}</strong> {sub}
</span>
<button type="button" className="ccode-copy" onClick={() => void copy()}>
{copied || "Copy"}
</button>
</figcaption>
<pre className="ccode-code" tabIndex={0}>
<code>
{lines.map((l, i) => (
<span key={i} className="ccode-line" data-mark={l.mark}>
<span className="ccode-sign" aria-hidden="true">
{l.mark === "add" ? "+" : l.mark === "del" ? "−" : " "}
</span>
{l.mark && <span className="sr-only">{l.mark === "add" ? "Added: " : "Removed: "}</span>}
{l.text || " "}
{"\n"}
</span>
))}
</code>
</pre>
</figure>
);
}
function CodeComparisonDemo() {
const [tab, setTab] = useState<"before" | "after">("after");
const uid = useId();
return (
<div className="dm">
<div className="ccode" data-tab={tab}>
<div className="ccode-tabs" role="group" aria-label="Show">
{(["before", "after"] as const).map((t) => (
<button key={t} type="button" aria-pressed={tab === t} onClick={() => setTab(t)}>
{t === "before" ? "Before" : "After"}
</button>
))}
</div>
<div className="ccode-panes">
<Pane id={`${uid}-b`} title="Before" sub="Pages Router, next/router" lines={BEFORE} />
<Pane id={`${uid}-a`} title="After" sub="App Router, next/navigation" lines={AFTER} />
</div>
</div>
</div>
);
}
export default function CodeComparisonEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Before and after of code">
<CodeComparisonDemo />
</PieceStage>
);
}