React Code Block With Copy Button
A glass React code block with a small built-in syntax highlighter, line numbers, highlighted and diff lines, a file tab and a copy button.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/code-block.jsonCodeBlockCodeBlockTabsparseLineSpecdefaultCodeBlockLabels- +2 more
A file with highlighted lines
highlightLines takes "10-11, 16" or an array. Highlighted lines are <mark>; numbers are left out of copy and of screen readers.
import { CodeBlock } from "@/components/sheenui/code-block/code-block";
// Package build (after the first npm release, or a packed tarball): import { CodeBlock } from "@sheenui/theme";
// The file's text: fetched, or imported with Vite's ?raw suffix.
declare const source: string;
export function Example() {
return (
<CodeBlock
filename="components/counter.tsx"
language="tsx"
highlightLines="10-11, 16"
code={source}
/>
);
}Files to copy
8 folders · 13 files · 4 packages
- From
theme/src/ components/code-block/components/button/components/spinner/components/status-icons/hooks/{use-controllable.ts, use-direction.ts, use-latest.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts}motion/{gate.ts, presets.ts, tokens.ts}lib/{cn.ts, form.ts, state-light.tsx, warn-once.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/code-block.jsonRegistry item: code-block. The styles need the Tailwind v4 glass UI setup.- The examples also use
components/glass-card/motion/light-follower.tslib/heading.ts
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/glass-card.json- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Diff
addedLines and removedLines render as <ins> / <del> with + and − markers, so color is never the only signal.
import { CodeBlock } from "@/components/sheenui/code-block/code-block";
// Package build (after the first npm release, or a packed tarball): import { CodeBlock } from "@sheenui/theme";
// The file's text: fetched, or imported with Vite's ?raw suffix.
declare const source: string;
export function Example() {
return (
<CodeBlock
filename="motion/tokens.ts"
language="ts"
startLineNumber={28}
removedLines={[31]}
addedLines={[32]}
code={source}
/>
);
}Tabs: commands and languages
CodeBlockTabs keeps several snippets in one block. The underline slides between tabs (transform only); copy and wrap act on the active one. When the tabs don't fit, the hidden edge fades and the list scrolls sideways.
import { CodeBlockTabs } from "@/components/sheenui/code-block/code-block";
// Package build (after the first npm release, or a packed tarball): import { CodeBlockTabs } from "@sheenui/theme";
// File contents for the second group of tabs.
declare const css: string, json: string, bash: string, python: string, html: string;
export function Example() {
return (
<>
<CodeBlockTabs
labels={{ tabs: "Package manager" }}
files={[
{ label: "pnpm", language: "bash", code: "pnpm add motion @base-ui/react" },
{ label: "npm", language: "bash", code: "npm install motion @base-ui/react" },
{ label: "yarn", language: "bash", code: "yarn add motion @base-ui/react" },
{ label: "bun", language: "bash", code: "bun add motion @base-ui/react" },
]}
/>
<CodeBlockTabs
files={[
{ label: "toast.css", language: "css", code: css, highlightLines: "7" },
{ label: "package.json", language: "json", code: json },
{ label: "dev.sh", language: "bash", code: bash },
{ label: "swatch.py", language: "python", code: python },
{ label: "index.html", language: "html", code: html },
]}
/>
</>
);
}Long lines and no header
Unwrapped, a long line scrolls inside the block (it becomes keyboard-focusable only when it actually scrolls): the line numbers stay pinned and a fade at the end edge shows there is more. Toggle wrap in the header, or start wrapped with defaultWrap. Without a header, copy floats in the corner.
import { CodeBlock } from "@/components/sheenui/code-block/code-block";
// Package build (after the first npm release, or a packed tarball): import { CodeBlock } from "@sheenui/theme";
export function Example() {
return (
<>
<CodeBlock
language="tsx"
filename="hero.tsx"
code={`<Button className="…a very long class list…">Get started</Button>`}
/>
<CodeBlock header={false} language="bash" code="npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/carousel.json" />
</>
);
}CodeBlock
CodeBlockLabels keys and English defaults (10)
CodeBlockTabs
Theming tokens
Keyboard
Accessibility
- The code area is a named region ("Code: button.tsx"). Line numbers and diff markers are not read or copied; diff lines also carry +/- text.
- Copy announces "Code copied" politely; if copying fails, the code is selected for manual copy.
- Code is always left to right and translate="no". Syntax colors keep 4.5:1 on glass in light and dark.
All exports on this page
CodeBlock— Syntax-highlighted code on glass, with no highlighting dependency (a small built-in tokenizer for ts, tsx, js, jsx, json, css, bash, python and html).CodeBlockTabs— Several files (or one command for several package managers) in one block.defaultCodeBlockLabels— The English strings of CodeBlock and CodeBlockTabs.parseLineSpec— Turn a LineSpec ("1, 4-6" or [1, 4, 5, 6]) into a set of 1-based line numbers.
When to use the code block
- Use it as a syntax highlighter in documentation and blog posts to show a file with a filename tab, line numbers and highlightLines for the two or three lines the paragraph is explaining.
- Use addedLines and removedLines to show a migration or a diff, since the lines render as ins and del elements with plus and minus markers, so color is never the only signal.
- Use CodeBlockTabs for an install command in npm, pnpm and yarn, or the same snippet in several languages, with one copy and wrap control acting on the active tab.
- Skip it when users need to edit or run the code; it is a read-only display, so a real editor or sandbox fits, and Kbd is the right tool for a single key or short inline command.
- Pair it with Tabs-style CodeBlockTabs for variants, Toast for your own copy confirmation through onCopy, and GlassCard or Accordion to group long examples.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .