code-block
aceternity/code-blockUnverifiedComponent
aceternity publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/rickhallett/becoming-diamond-nextjs/main/code-block.jsonSource
1"use client";2import React from "react";3import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";4import { atomDark } from "react-syntax-highlighter/dist/cjs/styles/prism";5import { IconCheck, IconCopy } from "@tabler/icons-react";67type CodeBlockProps = {8 language: string;9 filename: string;10 highlightLines?: number[];11} & (12 | {13 code: string;14 tabs?: never;15 }16 | {17 code?: never;18 tabs: Array<{19 name: string;20 code: string;21 language?: string;22 highlightLines?: number[];23 }>;24 }25);2627export const CodeBlock = ({28 language,29 filename,30 code,31 highlightLines = [],32 tabs = [],33}: CodeBlockProps) => {34 const [copied, setCopied] = React.useState(false);35 const [activeTab, setActiveTab] = React.useState(0);3637 const tabsExist = tabs.length > 0;3839 const copyToClipboard = async () => {40 const textToCopy = tabsExist ? tabs[activeTab].code : code;41 if (textToCopy) {42 await navigator.clipboard.writeText(textToCopy);43 setCopied(true);44 setTimeout(() => setCopied(false), 2000);45 }46 };4748 const activeCode = tabsExist ? tabs[activeTab].code : code;49 const activeLanguage = tabsExist50 ? tabs[activeTab].language || language51 : language;52 const activeHighlightLines = tabsExist53 ? tabs[activeTab].highlightLines || []54 : highlightLines;5556 return (57 <div className="relative w-full rounded-lg bg-slate-900 p-4 font-mono text-sm">58 <div className="flex flex-col gap-2">59 {tabsExist && (60 <div className="flex overflow-x-auto">61 {tabs.map((tab, index) => (62 <button63 key={index}64 onClick={() => setActiveTab(index)}65 className={`px-3 !py-2 text-xs transition-colors font-sans ${66 activeTab === index67 ? "text-white"68 : "text-zinc-400 hover:text-zinc-200"69 }`}70 >71 {tab.name}72 </button>73 ))}74 </div>75 )}76 {!tabsExist && filename && (77 <div className="flex justify-between items-center py-2">78 <div className="text-xs text-zinc-400">{filename}</div>79 <button80 onClick={copyToClipboard}81 className="flex items-center gap-1 text-xs text-zinc-400 hover:text-zinc-200 transition-colors font-sans"82 >83 {copied ? <IconCheck size={14} /> : <IconCopy size={14} />}84// … truncated
What it pulls in
npm packages
Files it writes
More from aceternity
All 89 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3d-card3d-card | aceternity | Components | Unverified | no deps | |
| 3d Globe3d-globe | aceternity | Components | Free | 3 deps | |
| 3d-marquee3d-marquee | aceternity | Components | Unverified | no deps | |
| 3d-pin3d-pin | aceternity | Components | Unverified | 1 dep | |
| animated-modalanimated-modal | aceternity | Components | Unverified | 1 dep | |
| animated-testimonialsanimated-testimonials | aceternity | Components | Unverified | 2 deps | |
| animated-tooltipanimated-tooltip | aceternity | Components | Unverified | 1 dep | |
| apple-cards-carouselapple-cards-carousel | aceternity | Components | Unverified | 2 deps |
