Dynamic code block
A code block highlighted in the browser, for code that's only known at runtime.
const greeting = "Hello, world!";Installation
npx shadcn@latest add @docscn/dynamic-codeblockUsage
import { DynamicCodeBlock } from "@/components/docs/components/dynamic-codeblock";
<DynamicCodeBlock lang="ts" code={code} />;It highlights the code with Shiki and the GitHub light and dark themes, using the same code block as your content. It's highlighted on the server when it renders there, and in the browser after Shiki has loaded otherwise, showing the code without colours until then.
Shiki's full bundle is large. To load less, use DynamicCodeBlock from components/dynamic-codeblock.core with your own Shiki highlighter, built with only the languages and themes you need, or create one with createDynamicCodeBlock:
"use client";
import { createDynamicCodeBlock } from "@/components/docs/components/dynamic-codeblock.core";
import { highlighter } from "@/lib/highlighter";
export const DynamicCodeBlock = createDynamicCodeBlock({ highlighter });highlighter is a Highlighter from Fumadocs Core, created like the one for createServerCodeBlock.
For code that's known on the server, ServerCodeBlock sends no JavaScript to the browser.
Props
Prop
Type