BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/tool-ui/code-block

Code Block

tool-ui/code-block
FreeBlock

Display syntax-highlighted code snippets.

See it running

Open the demo on tool-ui

www.tool-ui.com/components/code-block
Open
This registry sends X-Frame-Options, so its page cannot be embedded here. The link is the same page the frame would have shown.

Install it

npx shadcn@latest add https://www.tool-ui.com/r/code-block.json

Source

components/tool-ui/code-block/code-block.tsxwww.tool-ui.com/r/code-block.json · first 6 KB
1"use client";
2
3import {
4 useState,
5 useCallback,
6 useEffect,
7 createContext,
8 use,
9 type ReactNode,
10} from "react";
11import {
12 createHighlighter,
13 createJavaScriptRegexEngine,
14 type Highlighter,
15} from "shiki";
16import { Copy, Check, ChevronDown, ChevronUp } from "lucide-react";
17import pierreDarkTheme from "../shared/pierre-dark-theme.js";
18import pierreLightTheme from "../shared/pierre-light-theme.js";
19import type { CodeBlockLineNumbersMode, CodeBlockProps } from "./schema";
20import { useCopyToClipboard } from "../shared/use-copy-to-clipboard";
21
22import { Button, cn, Collapsible, CollapsibleTrigger } from "./_adapter";
23
24const COPY_ID = "codeblock-code";
25const MAX_HTML_CACHE_ENTRIES = 64;
26
27let highlighterPromise: Promise<Highlighter> | null = null;
28
29function getHighlighter(): Promise<Highlighter> {
30 if (!highlighterPromise) {
31 highlighterPromise = createHighlighter({
32 themes: [pierreDarkTheme as never, pierreLightTheme as never],
33 langs: [],
34 engine: createJavaScriptRegexEngine(),
35 });
36 }
37 return highlighterPromise;
38}
39
40const htmlCache = new Map<string, string>();
41
42function getCacheKey(
43 code: string,
44 language: string,
45 theme: string,
46 lineNumbers: CodeBlockLineNumbersMode,
47 highlightLines?: number[],
48): string {
49 return JSON.stringify({
50 code,
51 language,
52 theme,
53 lineNumbers,
54 highlightLines: highlightLines ?? null,
55 });
56}
57
58function setCachedHtml(cacheKey: string, html: string): void {
59 if (htmlCache.has(cacheKey)) {
60 htmlCache.set(cacheKey, html);
61 return;
62 }
63
64 if (htmlCache.size >= MAX_HTML_CACHE_ENTRIES) {
65 const oldestKey = htmlCache.keys().next().value;
66 if (typeof oldestKey === "string") {
67 htmlCache.delete(oldestKey);
68 }
69 }
70
71 htmlCache.set(cacheKey, html);
72}
73
74const LANGUAGE_DISPLAY_NAMES: Record<string, string> = {
75 typescript: "TypeScript",
76 javascript: "JavaScript",
77 python: "Python",
78 tsx: "TSX",
79 jsx: "JSX",
80 json: "JSON",
81 bash: "Bash",
82 shell: "Shell",
83 css: "CSS",
84 html: "HTML",
85 markdown: "Markdown",
86 sql: "SQL",
87 yaml: "YAML",
88 go: "Go",
89 rust: "Rust",
90 text: "Plain Text",
91};
92
93function getLanguageDisplayName(lang: string): string {
94 return LANGUAGE_DISPLAY_NAMES[lang.toLowerCase()] || lang.toUpperCase();
95}
96
97function getSystemTheme(): "light" | "dark" {
98 if (typeof window === "undefined") return "light";
99 return window.matchMedia?.("(prefers-color-scheme: dark)").matches
100 ? "dark"
101 : "light";
102}
103
104// … truncated

What it pulls in

npm packages

  • lucide-react
  • shiki
  • zod

Other registry items

  • button
  • collapsible

Files it writes

  • components/tool-ui/code-block/_adapter.tsx
  • components/tool-ui/code-block/code-block.tsx
  • components/tool-ui/code-block/index.tsx
  • components/tool-ui/code-block/README.md
  • components/tool-ui/code-block/schema.ts
  • components/tool-ui/shared/contract.ts
  • components/tool-ui/shared/parse.ts
  • components/tool-ui/shared/pierre-dark-theme.js
  • components/tool-ui/shared/pierre-light-theme.js
  • components/tool-ui/shared/schema.ts
  • components/tool-ui/shared/use-copy-to-clipboard.ts

Facts

Registry
tool-ui
Type
registry:block
Access
Free
Files written
11
Licence
the repository states none
First indexed
2026-08-02
Last confirmed
yesterday

Go to the source

Docs on tool-ui www.tool-ui.com Raw registry item This item as JSON

More from tool-ui

All 27 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Approval Cardapproval-cardtool-uiBlocksFree2 deps · 11 files
Audioaudiotool-uiBlocksFree2 deps · 9 files
Chartcharttool-uiBlocksFree2 deps · 8 files
Citationcitationtool-uiBlocksFree2 deps · 15 files
Code Diffcode-difftool-uiBlocksFree3 deps · 10 files
Data Tabledata-tabletool-uiBlocksFree1 dep · 17 files
Geo Mapgeo-maptool-uiBlocksFree4 deps · 12 files
Imageimagetool-uiBlocksFree1 dep · 14 files
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex