Code Comparison
magicui/code-comparisonFreeComponent
A component which compares two code snippets.
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/code-comparison.jsonSource
1"use client"23import { useEffect, useMemo, useState } from "react"4import {5 transformerNotationDiff,6 transformerNotationFocus,7} from "@shikijs/transformers"8import { FileIcon } from "lucide-react"9import { useTheme } from "next-themes"1011import { cn } from "@/lib/utils"1213interface CodeComparisonProps {14 beforeCode: string15 afterCode: string16 language: string17 filename: string18 lightTheme: string19 darkTheme: string20 highlightColor?: string21}2223export function CodeComparison({24 beforeCode,25 afterCode,26 language,27 filename,28 lightTheme,29 darkTheme,30 highlightColor = "#ff3333",31}: CodeComparisonProps) {32 const { theme, systemTheme } = useTheme()33 const [highlightedBefore, setHighlightedBefore] = useState("")34 const [highlightedAfter, setHighlightedAfter] = useState("")35 const [hasLeftFocus, setHasLeftFocus] = useState(false)36 const [hasRightFocus, setHasRightFocus] = useState(false)3738 const selectedTheme = useMemo(() => {39 const currentTheme = theme === "system" ? systemTheme : theme40 return currentTheme === "dark" ? darkTheme : lightTheme41 }, [theme, systemTheme, darkTheme, lightTheme])4243 useEffect(() => {44 if (highlightedBefore || highlightedAfter) {45 setHasLeftFocus(highlightedBefore.includes('class="line focused"'))46 setHasRightFocus(highlightedAfter.includes('class="line focused"'))47 }48 }, [highlightedBefore, highlightedAfter])4950 useEffect(() => {51 async function highlightCode() {52 try {53 const { codeToHtml } = await import("shiki")54 const { transformerNotationHighlight } =55 await import("@shikijs/transformers")5657 const before = await codeToHtml(beforeCode, {58 lang: language,59 theme: selectedTheme,60 transformers: [61 transformerNotationHighlight({ matchAlgorithm: "v3" }),62 transformerNotationDiff({ matchAlgorithm: "v3" }),63 transformerNotationFocus({ matchAlgorithm: "v3" }),64 ],65 })66 const after = await codeToHtml(afterCode, {67 lang: language,68 theme: selectedTheme,69 transformers: [70 transformerNotationHighlight({ matchAlgorithm: "v3" }),71 transformerNotationDiff({ matchAlgorithm: "v3" }),72 transformerNotationFocus({ matchAlgorithm: "v3" }),73 ],74 })75 setHighlightedBefore(before)76 setHighlightedAfter(after)77 } catch (error) {78 console.error("Error highlighting code:", error)79// … truncated
What it pulls in
npm packages
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
