Code Block, animated React component
A syntax-highlighted code block with a title bar, language badge, line numbers and a copy button. A soft bar glides between lines as the pointer moves, highlightLines marks lines with an accent bar, and copying turns the icon into a tick that draws itself.
- Free
- Feature Sections
- Hover
- Spring
Chimes softly when you press Copy. Sound is off on this site until you switch it on.
Dark with marked lines
Follows the page theme
Like Code Block? 256 more are in Pro.
Pro unlocks all 256 Pro components, every template and block, and everything we release next. Code Block stays free.
Play with it
Move the controls and watch it change. Copy code gives you a snippet with only the props you changed.
Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| code | string | — | The source to show. |
| language | "plain" | "js" | "ts" | "jsx" | "tsx" | "css" | "json" | "html" | "tsx" | Highlighting rules and the badge in the title bar. |
| theme | "dark" | "light" | "auto" | "custom" | "dark" | Built-in palettes; auto follows the page's dark class; custom reads colours from header, codeArea and border. |
| header | HeaderProps | { enabled, showDots, title, copy } | Title bar: enabled, showDots, title, font, and copy { enabled, label, copiedLabel, copiedDurationMs }. |
| codeArea | CodeAreaProps | { showLineNumbers: true, wrap: false, padding: 18 } | Line numbers, wrapping, padding, font and custom colours. |
| border | BorderProps | { width: 0, radius: "22px" } | Border colour, width and radius. |
| highlightLines | number[] | — | 1-based lines to mark with an accent bar. |
| hoverHighlight | boolean | true | A soft bar glides to the line under the pointer. |
| autoplay | boolean | false | Walks the glide bar through highlightLines (or every line) until the pointer comes over the code. |
| className | string | — | Extra classes for the root. |
| style | React.CSSProperties | — | Inline styles for the root. |
| enableSound | boolean | true | Plays interface sounds on interaction. They follow the sound switch (setSoundsEnabled, on by default) and stay silent on hover. Autoplay makes the same sounds once the visitor has used the page. Pass false to silence this component. |
Add it to your project
- 1
Set up your project once
You need a Next.js project with Tailwind CSS and the
@/import alias. In its root folder, run this once. It createscomponents.jsonand thecn()helper insrc/lib/utils.ts:npx shadcn@latest init -d
Starting from nothing? The Quick Start walks through the whole thing.
- 2
Connect the registry
Run this once per project. It writes the Nexus UI entry into
components.jsonfor you:npx shadcn@latest registry add "@nexus-labs=https://www.nexus-ui.com/api/registry/{name}"Type it exactly as shown.
{name}is filled in by the CLI with the component you ask for, so you never replace it.
- 3
Add the component
Run this from your project root. It adds the component, the helpers it imports and its npm packages.
npx shadcn@latest add @nexus-labs/code-block
- 4
Check what was added
With a project made as in the Quick Start (a
src/folder), these files land in it:components/code-block/code-block.tsxindex.ts - 5
Copying by hand? Skip the registry
Instead of steps 2 and 3 you can copy the files yourself, with no registry and no API key. Step 1 still applies, so that
cn()exists. Install the packages:npm install framer-motion
Then show the source and create each file at the path it gives:
- 6
Use it in a page
Installing only adds the files. Import the component in a page and render it. The Usage tab on the preview shows a fuller example with props:
// src/app/page.tsx import { CodeBlock } from "@/components/code-block"; export default function Page() { return ( <main> <CodeBlock /> </main> ); }
Like Code Block? 256 more are in Pro.
Pro unlocks all 256 Pro components, every template and block, and everything we release next. Code Block stays free.
About Code Block
The Code Block is a syntax-highlighted code display component with a configurable header, line numbers, copy button with feedback animation, and a trailing fade gradient. It supports dark and light themes and multiple programming languages via Prism or Shiki tokenization. A copy confirmation checkmark animates in on click and resets after a configurable duration. Use it in documentation pages, installation guides, and component detail pages to display code samples.
Good for
- Code documentation and API reference pages
- Installation and setup guide steps
- Component usage example displays
- Tutorial and course content pages
Code Block questions
How do I add the code block to my project?
Copy the CodeBlock source from the Code tab into your project. It is a single client component whose only dependency is framer-motion.
Which languages does it highlight?
Its built-in tokenizer handles `js`, `ts`, `jsx`, `tsx`, `css`, `json` and `html`, plus `plain` for no highlighting. Set the language with the `language` prop (default "jsx").
How do I customize its look?
`theme` takes "dark", "light" or "custom". The `header` prop controls the title, window dots and copy button, `codeArea` controls line numbers, wrapping, padding and colors, and `border` sets color, width and radius.
How does the copy button work?
It uses the Clipboard API, falling back to a hidden textarea in older browsers. The label switches to the copied label for `header.copy.copiedDurationMs` (default 1200 ms).
Does it make sound, and how do I turn it off?
Chimes softly when you press Copy. Sounds are synthesised with the Web Audio API, so there are no audio files, and they only play after a click or key press. Pass `enableSound={false}` to silence this component, or call `setSoundsEnabled(false)` from `@/lib/ui-sounds` to switch every sound off at once. The CLI installs `@/lib/ui-sounds` with the component.