Code Block
A clean code block that builds its entire theme from a single accent color. Pass code and a hex, it does the rest.
Loading component source...
Installation
pnpm dlx shadcn@latest add mox/mox-ui/code-blockRun this command from your project directory to add the component and its dependencies.
- 1
Install the packages
pnpm add motion prism-react-renderer lucide-react clsx tailwind-merge - 2
Add the utility file
lib/utils.tsimport { clsx, type ClassValue } from "clsx";import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs));} - 3
Copy and paste the code into your project
Loading source⦠- 4
Update the import paths to match your project
Point
@/lib/utilsto your utility file if your path aliases differ.
Usage
app/page.tsx
import CodeBlock from "@/components/ui/code-block"
export function Demo() { return ( <CodeBlock code={`const greet = (name: string) => \`Hello, \${name}!\``} language="ts" accent="#F75001" filename="greet.ts" /> )}Options
Pick a value to see the prop you would pass. Defaults are left out.
accent
snippet.tsx
<CodeBlock />Props
| Prop | Type | Default | Description |
|---|---|---|---|
code* | string | Default: - | The source code to render. |
language | string | Default: "tsx" | Prism language id, e.g. "tsx", "css", "json", "bash". Also shown as the tag in the header. |
accent | string | Default: "#F75001" | Any hex color. The whole theme is shades of it: the darkest shade is the background, tokens are tints of the accent, and the lightest text is always white. |
mode | "auto" | "dark" | "light" | Default: "auto" | Color scheme. Auto follows the page theme (html dark/light class, data-theme, or OS preference). Pass dark or light to pin a palette: dark puts light tints of the accent on a dark surface, light flips the ramp. |
filename | string | Default: - | Filename or path shown on the left of the header. Falls back to the language id when omitted. |
showFrame | boolean | Default: true | Toggles the outer layout: background, border, rounded corners, and header. Turn off to render nothing but the highlighted code. |
showHeader | boolean | Default: true | Toggles the header bar. When hidden, the copy button floats over the top-right corner instead. Ignored when showFrame is off. |
showLineNumbers | boolean | Default: true | Toggles the line-number gutter. |
showCopyButton | boolean | Default: true | Toggles the copy-to-clipboard button. |
highlightLines | number[] | Default: - | Optional 1-based line numbers to highlight with a soft accent wash. Off when omitted. |
className | string | Default: - | Extra classes merged onto the root element (data-slot="code-block"). Use it for width and max-height. |
Pick an accent swatch to re-shade the whole block from that color. Hit the copy button to see it spring into a check.
Credits and license
Most components here are recreations of great work from around the web. I don't claim to be the original creator - this is my attempt to reverse-engineer, replicate, and often add a few extra features. I've tried to credit everyone; if I missed someone, let me know.
- Free to use and modify in both personal and commercial projects.
- Attribution to Mox UI is appreciated when using a component.
- Please don't resell the components as your own kit.