UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: code-comparison line-highlight (#553)
Arghya Dasc81f17d4a281
diff --git a/content/docs/components/code-comparison.mdx b/content/docs/components/code-comparison.mdxindex a5562b66..7ca4ab30 100644--- a/content/docs/components/code-comparison.mdx+++ b/content/docs/components/code-comparison.mdx@@ -40,12 +40,13 @@ npx shadcn@latest add "https://magicui.design/r/code-comparison"## Props-| Prop | Type | Default | Description |-| ------------ | -------- | -------------- | ------------------------------------------------------ |-| `className` | `string` | `-` | The class name to be applied to the component |-| `beforeCode` | `string` | `-` | The code snippet to display in the "before" section |-| `afterCode` | `string` | `-` | The code snippet to display in the "after" section |-| `language` | `string` | `-` | The language of the code snippets (e.g., "typescript") |-| `filename` | `string` | `-` | The filename to display for the code snippets |-| `lightTheme` | `string` | `github-light` | The theme to use for light mode |-| `darkTheme` | `string` | `github-dark` | The theme to use for dark mode |+| Prop | Type | Default | Description |+| ---------------- | -------- | --------------------------- | ------------------------------------------------------ |+| `className` | `string` | `-` | The class name to be applied to the component |+| `beforeCode` | `string` | `-` | The code snippet to display in the "before" section |+| `afterCode` | `string` | `-` | The code snippet to display in the "after" section |+| `language` | `string` | `-` | The language of the code snippets (e.g., "typescript") |+| `filename` | `string` | `-` | The filename to display for the code snippets |+| `lightTheme` | `string` | `github-light` | The theme to use for light mode |+| `darkTheme` | `string` | `github-dark` | The theme to use for dark mode |+| `highlightColor` | `string` | `rgba(101, 117, 133, 0.16)` | The color to use for highlighting the code snippets |diff --git a/package.json b/package.jsonindex 9989a028..c385ea02 100644--- a/package.json+++ b/package.json@@ -88,6 +88,7 @@"@content-collections/mdx": "^0.1.5","@content-collections/next": "^0.2.2","@ianvs/prettier-plugin-sort-imports": "^4.4.1",+ "@shikijs/transformers": "^2.3.2","@tailwindcss/typography": "^0.5.9","@types/canvas-confetti": "^1.6.4","@types/node": "^20.11.22",@@ -113,7 +114,7 @@"remark-code-import": "^1.2.0","remark-gfm": "^4.0.0","rimraf": "^5.0.7",- "shiki": "^2.3.1",+ "shiki": "^2.3.2","tailwindcss": "3.4.1","tailwindcss-animate": "^1.0.7","tsx": "^4.11.0",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 8656541e..531a3358 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -195,6 +195,9 @@ importers:'@ianvs/prettier-plugin-sort-imports':specifier: ^4.4.1version: 4.4.1([email protected])+ '@shikijs/transformers':+ specifier: ^2.3.2+ version: 2.4.2'@tailwindcss/typography':specifier: ^0.5.9@@ -254,7 +257,7 @@ importers:version: 7.1.0rehype-pretty-code:specifier: ^0.13.2- version: 0.13.2([email protected])+ version: 0.13.2([email protected])rehype-slug:specifier: 6.0.0version: 6.0.0@@ -271,8 +274,8 @@ importers:specifier: ^5.0.7version: 5.0.7shiki:- specifier: ^2.3.1- version: 2.3.1+ specifier: ^2.3.2+ version: 2.5.0tailwindcss:specifier: 3.4.1@@ -2248,26 +2251,41 @@ packages:'@rushstack/[email protected]':resolution: {integrity: sha512-kkKUDVlII2DQiKy7UstOR1ErJP8kUKAQ4oa+SQtM0K+lPdmmjj0YnnxBgtTVYH7mUKtbsxeFC9y0AmK7Yb78/A==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-u9WTI0CgQUicTJjkHoJbZosxLP2AlBPr8RV3cuh4SQDsXYqMomjnAoo4lZSqVq8a8kpMwyv/LqoSrg69dH0ZeA==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-V0kXYB/70xA3CO+b2Pz9kcSThaOUfObOEkGeHsKSFqV6rultaWPfeyZPpBlKHMUXO9Bt1ZGINDCctN90pQvnTg==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-sZLM4utrD1D28ENLtVS1+b7TIf1OIr3Gt0gLejMIG69lmFQI8mY0eGBdvbuvvM3Ys2M0kNYJF6BaWct27PggHw==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-uu/8RExTKtavlpH7XqnVYBrfBkUc20ngXiX9NSrBhOVZYv/7XQRKUyhtkeflY5QsxC0GbJThCerruZfsUaSldg==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-UKJEMht1gkF2ROigCgb3FE2ssmbR8CJEwUneImJ2QoZqayH/96Vp88p2N+RmyqJEHo1rsOivlJKeU9shhKpfSA==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-WRg63Lfta+5RJ0y0/ns1e1NqSxo+jSQclMf9kBHvtchLhR/x3R/E3PSNFiCM+t7oo+d9/VCCp1kURqsSVTHWJg==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-3csAX8RGm2EQCbpCb1Eq+r4DSpkku6gxb4jiHnOxlV4D36VYZsmunUiDo/4NZvpFA0CW33v/JoYmFJ3yQ2TvSw==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-VjnOpnQf8WuCEZtNUdjjwGUbtAVKuZkVQ/5cHy/tojVVRIRtlWMYVjyWhxOmIq05AlSOv72z7hRNRGVBgQOl0w==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-QtkIM4Vz166+m4KED7/U5iVpgAdhfsHqMbBbjIzdTyTM1GIk2XQLcaB9b/LQY0y83Zl4lg7A7Hg+FT8+vAGL5A==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-YmvW7XcvT2f2pf1r1IvKd48fFYcsZRMMISRr2nY1fE2uOF4xcm+84R7+yg4jNAblrFcXU9tDrkllJKH2uD3mBQ==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-1BQV6R4zF4pDPpPTbML8mPFX6RsNYtROfhgPT2YX+KW4B99a2UNtwuvmNj03BRy/sDz9GeAx9gAmnv8NroS/2w==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-pGd1wRATzbo/uatrCIILlAdFVKdxImWJGQ5rFiB5VZi2ve5xj3Ax9jny8QvkaV93btQEwR/rSz5ERFpC5mKNIw==}- '@shikijs/[email protected]':- resolution: {integrity: sha512-fTIQwLF+Qhuws31iw7Ncl1R3HUDtGwIipiJ9iU+UsDUwMhegFcQKQHd51nZjb7CArq0MvON8rbgCGQYWHUKAdg==}+ '@shikijs/[email protected]':+ resolution: {integrity: sha512-Qfrrt5OsNH5R+5tJ/3uYBBZv3SuGmnRPejV9IlIbFH3HTGLDlkqgHymAlzklVmKBjAaVmkPkyikAV/sQ1wSL+w==}++ '@shikijs/[email protected]':+ resolution: {integrity: sha512-wGrk+R8tJnO0VMzmUExHR+QdSaPUl/NKs+a4cQQRWyoc3YFbUzuLEi/KWK1hj+8BfHRKm2jNhhJck1dfstJpiw==}++ '@shikijs/[email protected]':+ resolution: {integrity: sha512-vDoOsJM1IfYTY7xneAnMGPds9PgKSAJSmwkV9F+g8srf9/IdelRdB/Asz8btrXQWUpqaCjP4LDAihqNlL2sr/A==}++ '@shikijs/[email protected]':+ resolution: {integrity: sha512-e28aFDPwVgK8H2nPrEA5CexLa5yumBvb5aF6nN4SlmqaBFOuGQdxX/Cfh8rwRFALepJtlj0P3wvJ4oL+ndxgSA==}++ '@shikijs/[email protected]':+ resolution: {integrity: sha512-ygl5yhxki9ZLNuNpPitBWvcy9fsSKKaRuO4BAlMyagszQidxcpLAr0qiW/q43DtSIDxO6hEbtYLiFZNXO/hdGw==}++ '@shikijs/[email protected]':+ resolution: {integrity: sha512-83yeghZ2xxin3Nj8z1NMd/NCuca+gsYXswywDy5bHvwlWL8tpTQmzGeUuHd9FC3E/SBEMvzJRwWEOz5gGes9Qg==}'@swc/[email protected]':resolution: {integrity: sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==}@@ -5445,8 +5463,8 @@ packages:resolution: {integrity: sha512-6j1W9l1iAs/4xYBI1SYOVZyFcCis9b4KCLQ8fgAGG07QvzaRLVVRQvAy85yNmmZSjYjg4MWh4gNvlPujU/5LpA==}- resolution: {integrity: sha512-bD1XuVAyZBVxHiPlO/m2nM2F5g8G5MwSZHNYx+ArpcOW52+fCN6peGP5gG61O0gZpzUVbImeR3ar8cF+Z5WM8g==}+ resolution: {integrity: sha512-mI//trrsaiCIPsja5CNfsyNOqgAZUb6VpJA+340toL42UpzQlXpwRV9nch69X6gaUxrr9kaOOa6e3y3uAkGFxQ==}resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}@@ -8126,40 +8144,70 @@ snapshots:'@rushstack/[email protected]': {}- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/engine-javascript': 2.3.1- '@shikijs/engine-oniguruma': 2.3.1- '@shikijs/types': 2.3.1- '@shikijs/vscode-textmate': 10.0.1+ '@shikijs/engine-javascript': 2.4.2+ '@shikijs/engine-oniguruma': 2.4.2+ '@shikijs/types': 2.4.2+ '@shikijs/vscode-textmate': 10.0.2'@types/hast': 3.0.4hast-util-to-html: 9.0.4- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/types': 2.3.1- '@shikijs/vscode-textmate': 10.0.1+ '@shikijs/engine-javascript': 2.5.0+ '@shikijs/engine-oniguruma': 2.5.0+ '@shikijs/types': 2.5.0+ '@shikijs/vscode-textmate': 10.0.2+ '@types/hast': 3.0.4+ hast-util-to-html: 9.0.4++ '@shikijs/[email protected]':+ dependencies:+ '@shikijs/types': 2.4.2+ '@shikijs/vscode-textmate': 10.0.2oniguruma-to-es: 3.1.0- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/types': 2.3.1- '@shikijs/vscode-textmate': 10.0.1+ '@shikijs/types': 2.5.0+ '@shikijs/vscode-textmate': 10.0.2+ oniguruma-to-es: 3.1.0- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/types': 2.3.1+ '@shikijs/types': 2.4.2+ '@shikijs/vscode-textmate': 10.0.2- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/types': 2.3.1+ '@shikijs/types': 2.5.0+ '@shikijs/vscode-textmate': 10.0.2++ '@shikijs/[email protected]':+ dependencies:+ '@shikijs/types': 2.5.0++ '@shikijs/[email protected]':+ dependencies:+ '@shikijs/types': 2.5.0++ '@shikijs/[email protected]':+ dependencies:+ '@shikijs/core': 2.4.2+ '@shikijs/types': 2.4.2++ '@shikijs/[email protected]':+ dependencies:+ '@shikijs/vscode-textmate': 10.0.2+ '@types/hast': 3.0.4- '@shikijs/[email protected]':+ '@shikijs/[email protected]':dependencies:- '@shikijs/vscode-textmate': 10.0.1+ '@shikijs/vscode-textmate': 10.0.2'@types/hast': 3.0.4- '@shikijs/[email protected]': {}+ '@shikijs/[email protected]': {}'@swc/[email protected]': {}@@ -11924,13 +11972,13 @@ snapshots:hast-util-from-html: 2.0.3unified: 11.0.5dependencies:'@types/hast': 3.0.4hast-util-to-string: 3.0.0parse-numeric-range: 1.3.0rehype-parse: 9.0.0- shiki: 2.3.1+ shiki: 2.5.0unified: 11.0.5unist-util-visit: 5.0.0@@ -12226,15 +12274,15 @@ snapshots:dependencies:- '@shikijs/core': 2.3.1- '@shikijs/engine-javascript': 2.3.1- '@shikijs/engine-oniguruma': 2.3.1- '@shikijs/langs': 2.3.1- '@shikijs/themes': 2.3.1- '@shikijs/types': 2.3.1- '@shikijs/vscode-textmate': 10.0.1+ '@shikijs/core': 2.5.0+ '@shikijs/engine-javascript': 2.5.0+ '@shikijs/engine-oniguruma': 2.5.0+ '@shikijs/langs': 2.5.0+ '@shikijs/themes': 2.5.0+ '@shikijs/types': 2.5.0+ '@shikijs/vscode-textmate': 10.0.2'@types/hast': 3.0.4diff --git a/public/r/code-comparison-demo.json b/public/r/code-comparison-demo.jsonindex b767f758..dbcd63ca 100644--- a/public/r/code-comparison-demo.json+++ b/public/r/code-comparison-demo.json@@ -10,7 +10,7 @@"files": [{"path": "registry/example/code-comparison-demo.tsx",- "content": "import { CodeComparison } from \"@/components/magicui/code-comparison\";\n\nconst beforeCode = `import { NextRequest } from 'next/server';\n\nexport const middleware = async (req: NextRequest) => {\n let user = undefined;\n let team = undefined;\n const token = req.headers.get('token');\n\n if(req.nextUrl.pathname.startsWith('/auth')) {\n user = await getUserByToken(token);\n\n if(!user) {\n return NextResponse.redirect('/login');\n }\n }\n\n if(req.nextUrl.pathname.startsWith('/team')) {\n user = await getUserByToken(token);\n\n if(!user) {\n return NextResponse.redirect('/login');\n }\n\n const slug = req.nextUrl.query.slug;\n team = await getTeamBySlug(slug);\n\n if(!team) {\n return NextResponse.redirect('/');\n }\n }\n\n return NextResponse.next();\n}\n\nexport const config = {\n matcher: ['/((?!_next/|_static|_vercel|[\\\\w-]+\\\\.\\\\w+).*)'],\n};`;\n\nconst afterCode = `import { createMiddleware, type MiddlewareFunctionProps } from '@app/(auth)/auth/_middleware';\nimport { auth } from '@app/(auth)/auth/_middleware';\nimport { team } from '@app/(team)/team/_middleware';\n\nconst middlewares = {\n '/auth{/:path?}': auth,\n '/team{/:slug?}': [ auth, team ],\n};\n\nexport const middleware = createMiddleware(middlewares);\n\nexport const config = {\n matcher: ['/((?!_next/|_static|_vercel|[\\\\w-]+\\\\.\\\\w+).*)'],\n};`;\n\nexport default function CodeComparisonDemo() {\n return (\n <CodeComparison\n beforeCode={beforeCode}\n afterCode={afterCode}\n language=\"typescript\"\n filename=\"middleware.ts\"\n lightTheme=\"github-light\"\n darkTheme=\"github-dark\"\n />\n );\n}\n",+ "content": "import { CodeComparison } from \"@/components/magicui/code-comparison\";\n\nconst beforeCode = `import { NextRequest } from 'next/server';\n\nexport const middleware = async (req: NextRequest) => {\n let user = undefined;\n let team = undefined;\n const token = req.headers.get('token'); \n\n if(req.nextUrl.pathname.startsWith('/auth')) {\n user = await getUserByToken(token);\n\n if(!user) {\n return NextResponse.redirect('/login');\n }\n }\n\n if(req.nextUrl.pathname.startsWith('/team')) {\n user = await getUserByToken(token);\n\n if(!user) {\n return NextResponse.redirect('/login');\n }\n\n const slug = req.nextUrl.query.slug;\n team = await getTeamBySlug(slug); // [!code highlight]\n\n if(!team) { // [!code highlight]\n return NextResponse.redirect('/'); // [!code highlight]\n } // [!code highlight]\n } // [!code highlight]\n\n return NextResponse.next(); // [!code highlight]\n}\n\nexport const config = {\n matcher: ['/((?!_next/|_static|_vercel|[\\\\w-]+\\\\.\\\\w+).*)'], // [!code highlight]\n};`;\n\nconst afterCode = `import { createMiddleware, type MiddlewareFunctionProps } from '@app/(auth)/auth/_middleware';\nimport { auth } from '@/app/(auth)/auth/_middleware'; // [!code --]\nimport { auth } from '@/app/(auth)/auth/_middleware'; // [!code ++]\nimport { team } from '@/app/(team)/team/_middleware';\n\nconst middlewares = {\n '/auth{/:path?}': auth,\n '/team{/:slug?}': [ auth, team ],\n};\n\nexport const middleware = createMiddleware(middlewares); // [!code focus]\n\nexport const config = {\n matcher: ['/((?!_next/|_static|_vercel|[\\\\w-]+\\\\.\\\\w+).*)'],\n};`;\n\nexport default function CodeComparisonDemo() {\n return (\n <CodeComparison\n beforeCode={beforeCode}\n afterCode={afterCode}\n language=\"typescript\"\n filename=\"middleware.ts\"\n lightTheme=\"github-light\"\n darkTheme=\"github-dark\"\n highlightColor=\"rgba(101, 117, 133, 0.16)\"\n />\n );\n}\n","type": "registry:example","target": "components/code-comparison-demo.tsx"}diff --git a/public/r/code-comparison.json b/public/r/code-comparison.jsonindex 658eabb2..f41d894f 100644--- a/public/r/code-comparison.json+++ b/public/r/code-comparison.json@@ -11,7 +11,7 @@"files": [{"path": "registry/magicui/code-comparison.tsx",- "content": "\"use client\";\n\nimport { FileIcon } from \"lucide-react\";\nimport { useTheme } from \"next-themes\";\nimport { useEffect, useState } from \"react\";\n\ninterface CodeComparisonProps {\n beforeCode: string;\n afterCode: string;\n language: string;\n filename: string;\n lightTheme: string;\n darkTheme: string;\n}\n\nexport function CodeComparison({\n beforeCode,\n afterCode,\n language,\n filename,\n lightTheme,\n darkTheme,\n}: CodeComparisonProps) {\n const { theme, systemTheme } = useTheme();\n const [highlightedBefore, setHighlightedBefore] = useState(\"\");\n const [highlightedAfter, setHighlightedAfter] = useState(\"\");\n\n useEffect(() => {\n const currentTheme = theme === \"system\" ? systemTheme : theme;\n const selectedTheme = currentTheme === \"dark\" ? darkTheme : lightTheme;\n\n async function highlightCode() {\n try {\n const { codeToHtml } = await import(\"shiki\");\n const before = await codeToHtml(beforeCode, {\n lang: language,\n theme: selectedTheme,\n });\n const after = await codeToHtml(afterCode, {\n lang: language,\n theme: selectedTheme,\n });\n setHighlightedBefore(before);\n setHighlightedAfter(after);\n } catch (error) {\n console.error(\"Error highlighting code:\", error);\n setHighlightedBefore(`<pre>${beforeCode}</pre>`);\n setHighlightedAfter(`<pre>${afterCode}</pre>`);\n }\n }\n highlightCode();\n }, [\n theme,\n systemTheme,\n beforeCode,\n afterCode,\n language,\n lightTheme,\n darkTheme,\n ]);\n\n const renderCode = (code: string, highlighted: string) => {\n if (highlighted) {\n return (\n <div\n className=\"h-full overflow-auto bg-background font-mono text-xs [&>pre]:h-full [&>pre]:!bg-transparent [&>pre]:p-4 [&_code]:break-all\"\n dangerouslySetInnerHTML={{ __html: highlighted }}\n />\n );\n } else {\n return (\n <pre className=\"h-full overflow-auto break-all bg-background p-4 font-mono text-xs text-foreground\">\n {code}\n </pre>\n );\n }\n };\n return (\n <div className=\"mx-auto w-full max-w-5xl\">\n <div className=\"relative w-full overflow-hidden rounded-md border border-border\">\n <div className=\"relative grid md:grid-cols-2 md:divide-x md:divide-border\">\n <div>\n <div className=\"flex items-center bg-accent p-2 text-sm text-foreground\">\n <FileIcon className=\"mr-2 h-4 w-4\" />\n {filename}\n <span className=\"ml-auto\">before</span>\n </div>\n {renderCode(beforeCode, highlightedBefore)}\n </div>\n <div>\n <div className=\"flex items-center bg-accent p-2 text-sm text-foreground\">\n <FileIcon className=\"mr-2 h-4 w-4\" />\n {filename}\n <span className=\"ml-auto\">after</span>\n </div>\n {renderCode(afterCode, highlightedAfter)}\n </div>\n </div>\n <div className=\"absolute left-1/2 top-1/2 flex h-8 w-8 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-md bg-accent text-xs text-foreground\">\n VS\n </div>\n </div>\n </div>\n );\n}\n",+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n transformerNotationDiff,\n transformerNotationFocus,\n} from \"@shikijs/transformers\";\nimport { FileIcon } from \"lucide-react\";\nimport { useTheme } from \"next-themes\";\nimport { useEffect, useState } from \"react\";\n\ninterface CodeComparisonProps {\n beforeCode: string;\n afterCode: string;\n language: string;\n filename: string;\n lightTheme: string;\n darkTheme: string;\n\n highlightColor?: string;\n}\n\nexport function CodeComparison({\n beforeCode,\n afterCode,\n language,\n filename,\n lightTheme,\n darkTheme,\n highlightColor = \"#ff3333\",\n}: CodeComparisonProps) {\n const { theme, systemTheme } = useTheme();\n const [highlightedBefore, setHighlightedBefore] = useState(\"\");\n const [highlightedAfter, setHighlightedAfter] = useState(\"\");\n\n const [hasLeftFocus, setHasLeftFocus] = useState(false);\n const [hasRightFocus, setHasRightFocus] = useState(false);\n\n useEffect(() => {\n // Check if highlighted code contains any focused elements after it's set\n if (highlightedBefore || highlightedAfter) {\n const hasBeforeFocus = highlightedBefore.includes('class=\"line focused\"');\n const hasAfterFocus = highlightedAfter.includes('class=\"line focused\"');\n setHasLeftFocus(hasBeforeFocus);\n setHasRightFocus(hasAfterFocus);\n }\n }, [highlightedBefore, highlightedAfter]);\n\n useEffect(() => {\n const currentTheme = theme === \"system\" ? systemTheme : theme;\n const selectedTheme = currentTheme === \"dark\" ? darkTheme : lightTheme;\n\n async function highlightCode() {\n try {\n const { codeToHtml } = await import(\"shiki\");\n const { transformerNotationHighlight } = await import(\n \"@shikijs/transformers\"\n );\n\n const before = await codeToHtml(beforeCode, {\n lang: language,\n theme: selectedTheme,\n transformers: [\n transformerNotationHighlight({ matchAlgorithm: \"v3\" }),\n transformerNotationDiff({ matchAlgorithm: \"v3\" }),\n transformerNotationFocus({ matchAlgorithm: \"v3\" }),\n ],\n });\n const after = await codeToHtml(afterCode, {\n lang: language,\n theme: selectedTheme,\n transformers: [\n transformerNotationHighlight({ matchAlgorithm: \"v3\" }),\n transformerNotationDiff({ matchAlgorithm: \"v3\" }),\n transformerNotationFocus({ matchAlgorithm: \"v3\" }),\n ],\n });\n setHighlightedBefore(before);\n setHighlightedAfter(after);\n } catch (error) {\n console.error(\"Error highlighting code:\", error);\n setHighlightedBefore(`<pre>${beforeCode}</pre>`);\n setHighlightedAfter(`<pre>${afterCode}</pre>`);\n }\n }\n highlightCode();\n }, [\n theme,\n systemTheme,\n beforeCode,\n afterCode,\n language,\n lightTheme,\n darkTheme,\n highlightColor,\n ]);\n\n const renderCode = (code: string, highlighted: string) => {\n if (highlighted) {\n return (\n <div\n style={\n {\n \"--highlight-color\": highlightColor,\n } as React.CSSProperties\n }\n className={cn(\n \"h-full w-full overflow-auto bg-background font-mono text-xs\",\n \"[&>pre]:h-full [&>pre]:!w-screen [&>pre]:py-2\",\n \"[&>pre>code]:!inline-block [&>pre>code]:!w-full\",\n \"[&>pre>code>span]:!inline-block [&>pre>code>span]:w-full [&>pre>code>span]:px-4 [&>pre>code>span]:py-0.5\",\n \"[&>pre>code>.highlighted]:inline-block [&>pre>code>.highlighted]:w-full [&>pre>code>.highlighted]:!bg-[var(--highlight-color)]\",\n \"group-hover/left:[&>pre>code>:not(.focused)]:!opacity-100 group-hover/left:[&>pre>code>:not(.focused)]:!blur-none\",\n \"group-hover/right:[&>pre>code>:not(.focused)]:!opacity-100 group-hover/right:[&>pre>code>:not(.focused)]:!blur-none\",\n \"[&>pre>code>.add]:bg-[rgba(16,185,129,.16)] [&>pre>code>.remove]:bg-[rgba(244,63,94,.16)]\",\n \"group-hover/left:[&>pre>code>:not(.focused)]:transition-all group-hover/left:[&>pre>code>:not(.focused)]:duration-300\",\n \"group-hover/right:[&>pre>code>:not(.focused)]:transition-all group-hover/right:[&>pre>code>:not(.focused)]:duration-300\"\n )}\n dangerouslySetInnerHTML={{ __html: highlighted }}\n />\n );\n } else {\n return (\n <pre className=\"h-full overflow-auto break-all bg-background p-4 font-mono text-xs text-foreground\">\n {code}\n </pre>\n );\n }\n };\n return (\n <div className=\"mx-auto w-full max-w-5xl\">\n <div className=\"group relative w-full overflow-hidden rounded-md border border-border\">\n <div className=\"relative grid md:grid-cols-2\">\n <div\n className={cn(\n \"leftside group/left border-primary/20 md:border-r\",\n hasLeftFocus &&\n \"[&>div>pre>code>:not(.focused)]:!opacity-50 [&>div>pre>code>:not(.focused)]:!blur-[0.095rem]\",\n \"[&>div>pre>code>:not(.focused)]:transition-all [&>div>pre>code>:not(.focused)]:duration-300\"\n )}\n >\n <div className=\"flex items-center border-b border-primary/20 bg-accent p-2 text-sm text-foreground\">\n <FileIcon className=\"mr-2 h-4 w-4\" />\n {filename}\n <span className=\"ml-auto hidden md:block\">before</span>\n </div>\n {renderCode(beforeCode, highlightedBefore)}\n </div>\n <div\n className={cn(\n \"rightside group/right border-t border-primary/20 md:border-t-0\",\n hasRightFocus &&\n \"[&>div>pre>code>:not(.focused)]:!opacity-50 [&>div>pre>code>:not(.focused)]:!blur-[0.095rem]\",\n \"[&>div>pre>code>:not(.focused)]:transition-all [&>div>pre>code>:not(.focused)]:duration-300\"\n )}\n >\n <div className=\"flex items-center border-b border-primary/20 bg-accent p-2 text-sm text-foreground\">\n <FileIcon className=\"mr-2 h-4 w-4\" />\n {filename}\n <span className=\"ml-auto hidden md:block\">after</span>\n </div>\n {renderCode(afterCode, highlightedAfter)}\n </div>\n </div>\n <div className=\"absolute left-1/2 top-1/2 flex h-8 w-8 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-md border border-primary/20 bg-accent text-xs text-foreground\">\n VS\n </div>\n </div>\n </div>\n );\n}\n","type": "registry:ui","target": "components/magicui/code-comparison.tsx"}diff --git a/public/r/text-animate.json b/public/r/text-animate.jsonindex c5ee4b52..099fc6a4 100644--- a/public/r/text-animate.json+++ b/public/r/text-animate.json@@ -4,9 +4,7 @@"type": "registry:ui","title": "Text Animate","description": "A text animation component that animates text using a variety of different animations.",- "dependencies": [- "motion"- ],+ "dependencies": ["motion"],"files": [{"path": "registry/magicui/text-animate.tsx",@@ -15,4 +13,4 @@"target": "components/magicui/text-animate.tsx"}]-}\ No newline at end of file+}diff --git a/registry/example/code-comparison-demo.tsx b/registry/example/code-comparison-demo.tsxindex e984964f..20e97d45 100644--- a/registry/example/code-comparison-demo.tsx+++ b/registry/example/code-comparison-demo.tsx@@ -5,7 +5,7 @@ const beforeCode = `import { NextRequest } from 'next/server';export const middleware = async (req: NextRequest) => {let user = undefined;let team = undefined;- const token = req.headers.get('token');+ const token = req.headers.get('token');if(req.nextUrl.pathname.startsWith('/auth')) {user = await getUserByToken(token);@@ -23,30 +23,31 @@ export const middleware = async (req: NextRequest) => {}const slug = req.nextUrl.query.slug;- team = await getTeamBySlug(slug);+ team = await getTeamBySlug(slug); // [!code highlight]- if(!team) {- return NextResponse.redirect('/');- }- }+ if(!team) { // [!code highlight]+ return NextResponse.redirect('/'); // [!code highlight]+ } // [!code highlight]+ } // [!code highlight]- return NextResponse.next();+ return NextResponse.next(); // [!code highlight]}export const config = {- matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'],+ matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'], // [!code highlight]};`;const afterCode = `import { createMiddleware, type MiddlewareFunctionProps } from '@app/(auth)/auth/_middleware';-import { auth } from '@app/(auth)/auth/_middleware';-import { team } from '@app/(team)/team/_middleware';+import { auth } from '@/app/(auth)/auth/_middleware'; // [!code --]+import { auth } from '@/app/(auth)/auth/_middleware'; // [!code ++]+import { team } from '@/app/(team)/team/_middleware';const middlewares = {'/auth{/:path?}': auth,'/team{/:slug?}': [ auth, team ],};-export const middleware = createMiddleware(middlewares);+export const middleware = createMiddleware(middlewares); // [!code focus]export const config = {matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'],@@ -61,6 +62,7 @@ export default function CodeComparisonDemo() {filename="middleware.ts"lightTheme="github-light"darkTheme="github-dark"+ highlightColor="rgba(101, 117, 133, 0.16)"/>);}diff --git a/registry/magicui/code-comparison.tsx b/registry/magicui/code-comparison.tsxindex 9fa34fdd..138c96bc 100644--- a/registry/magicui/code-comparison.tsx+++ b/registry/magicui/code-comparison.tsx@@ -1,8 +1,13 @@"use client";+import { cn } from "@/lib/utils";+import {+ transformerNotationDiff,+ transformerNotationFocus,+} from "@shikijs/transformers";import { FileIcon } from "lucide-react";import { useTheme } from "next-themes";-import { useEffect, useState } from "react";+import { useEffect, useState, useMemo } from "react";interface CodeComparisonProps {beforeCode: string;@@ -11,6 +16,7 @@ interface CodeComparisonProps {filename: string;lightTheme: string;darkTheme: string;+ highlightColor?: string;}export function CodeComparison({@@ -20,25 +26,51 @@ export function CodeComparison({filename,lightTheme,darkTheme,+ highlightColor = "#ff3333",}: CodeComparisonProps) {const { theme, systemTheme } = useTheme();const [highlightedBefore, setHighlightedBefore] = useState("");const [highlightedAfter, setHighlightedAfter] = useState("");+ const [hasLeftFocus, setHasLeftFocus] = useState(false);+ const [hasRightFocus, setHasRightFocus] = useState(false);- useEffect(() => {+ const selectedTheme = useMemo(() => {const currentTheme = theme === "system" ? systemTheme : theme;- const selectedTheme = currentTheme === "dark" ? darkTheme : lightTheme;+ return currentTheme === "dark" ? darkTheme : lightTheme;+ }, [theme, systemTheme, darkTheme, lightTheme]);++ useEffect(() => {+ if (highlightedBefore || highlightedAfter) {+ setHasLeftFocus(highlightedBefore.includes('class="line focused"'));+ setHasRightFocus(highlightedAfter.includes('class="line focused"'));+ }+ }, [highlightedBefore, highlightedAfter]);+ useEffect(() => {async function highlightCode() {try {const { codeToHtml } = await import("shiki");+ const { transformerNotationHighlight } = await import(+ "@shikijs/transformers"+ );+const before = await codeToHtml(beforeCode, {lang: language,theme: selectedTheme,+ transformers: [+ transformerNotationHighlight({ matchAlgorithm: "v3" }),+ transformerNotationDiff({ matchAlgorithm: "v3" }),+ transformerNotationFocus({ matchAlgorithm: "v3" }),+ ],});const after = await codeToHtml(afterCode, {lang: language,theme: selectedTheme,+ transformers: [+ transformerNotationHighlight({ matchAlgorithm: "v3" }),+ transformerNotationDiff({ matchAlgorithm: "v3" }),+ transformerNotationFocus({ matchAlgorithm: "v3" }),+ ],});setHighlightedBefore(before);setHighlightedAfter(after);@@ -49,21 +81,25 @@ export function CodeComparison({}}highlightCode();- }, [- theme,- systemTheme,- beforeCode,- afterCode,- language,- lightTheme,- darkTheme,- ]);+ }, [beforeCode, afterCode, language, selectedTheme]);const renderCode = (code: string, highlighted: string) => {if (highlighted) {return (<div- className="h-full overflow-auto bg-background font-mono text-xs [&>pre]:h-full [&>pre]:!bg-transparent [&>pre]:p-4 [&_code]:break-all"+ style={{ "--highlight-color": highlightColor } as React.CSSProperties}+ className={cn(+ "h-full w-full overflow-auto bg-background font-mono text-xs",+ "[&>pre]:h-full [&>pre]:!w-screen [&>pre]:py-2",+ "[&>pre>code]:!inline-block [&>pre>code]:!w-full",+ "[&>pre>code>span]:!inline-block [&>pre>code>span]:w-full [&>pre>code>span]:px-4 [&>pre>code>span]:py-0.5",+ "[&>pre>code>.highlighted]:inline-block [&>pre>code>.highlighted]:w-full [&>pre>code>.highlighted]:!bg-[var(--highlight-color)]",+ "group-hover/left:[&>pre>code>:not(.focused)]:!opacity-100 group-hover/left:[&>pre>code>:not(.focused)]:!blur-none",+ "group-hover/right:[&>pre>code>:not(.focused)]:!opacity-100 group-hover/right:[&>pre>code>:not(.focused)]:!blur-none",+ "[&>pre>code>.add]:bg-[rgba(16,185,129,.16)] [&>pre>code>.remove]:bg-[rgba(244,63,94,.16)]",+ "group-hover/left:[&>pre>code>:not(.focused)]:transition-all group-hover/left:[&>pre>code>:not(.focused)]:duration-300",+ "group-hover/right:[&>pre>code>:not(.focused)]:transition-all group-hover/right:[&>pre>code>:not(.focused)]:duration-300",+ )}dangerouslySetInnerHTML={{ __html: highlighted }}/>);@@ -75,28 +111,43 @@ export function CodeComparison({);}};+return (<div className="mx-auto w-full max-w-5xl">- <div className="relative w-full overflow-hidden rounded-md border border-border">- <div className="relative grid md:grid-cols-2 md:divide-x md:divide-border">- <div>- <div className="flex items-center bg-accent p-2 text-sm text-foreground">+ <div className="group relative w-full overflow-hidden rounded-md border border-border">+ <div className="relative grid md:grid-cols-2">+ <div+ className={cn(+ "leftside group/left border-primary/20 md:border-r",+ hasLeftFocus &&+ "[&>div>pre>code>:not(.focused)]:!opacity-50 [&>div>pre>code>:not(.focused)]:!blur-[0.095rem]",+ "[&>div>pre>code>:not(.focused)]:transition-all [&>div>pre>code>:not(.focused)]:duration-300",+ )}+ >+ <div className="flex items-center border-b border-primary/20 bg-accent p-2 text-sm text-foreground"><FileIcon className="mr-2 h-4 w-4" />{filename}- <span className="ml-auto">before</span>+ <span className="ml-auto hidden md:block">before</span></div>{renderCode(beforeCode, highlightedBefore)}</div>- <div>- <div className="flex items-center bg-accent p-2 text-sm text-foreground">+ <div+ className={cn(+ "rightside group/right border-t border-primary/20 md:border-t-0",+ hasRightFocus &&+ "[&>div>pre>code>:not(.focused)]:!opacity-50 [&>div>pre>code>:not(.focused)]:!blur-[0.095rem]",+ "[&>div>pre>code>:not(.focused)]:transition-all [&>div>pre>code>:not(.focused)]:duration-300",+ )}+ >+ <div className="flex items-center border-b border-primary/20 bg-accent p-2 text-sm text-foreground"><FileIcon className="mr-2 h-4 w-4" />{filename}- <span className="ml-auto">after</span>+ <span className="ml-auto hidden md:block">after</span></div>{renderCode(afterCode, highlightedAfter)}</div></div>- <div className="absolute left-1/2 top-1/2 flex h-8 w-8 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-md bg-accent text-xs text-foreground">+ <div className="absolute left-1/2 top-1/2 hidden h-8 w-8 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-md border border-primary/20 bg-accent text-xs text-foreground md:flex">VS</div></div>diff --git a/styles/globals.css b/styles/globals.cssindex 68997f8c..db5928d2 100644--- a/styles/globals.css+++ b/styles/globals.css@@ -159,3 +159,21 @@-webkit-mask-composite: source-in, xor;mask-composite: intersect;}++++/* .shiki {++ // focus+++ // diff++ // add++ // remove++ // highlight+++} */\ No newline at end of file