Fumadocs

Code Block (Dynamic)

A codeblock that also highlights code

console.log("This is pre-rendered")

Usage

Client Component

import { DynamicCodeBlock } from 'fumadocs-ui/components/dynamic-codeblock';
 
<DynamicCodeBlock lang="ts" code='console.log("Hello World")' />;

Unlike the MDX CodeBlock component, this is a client component that can be used without MDX. It highlights the code with Shiki and use the default component to render it.

Features:

  • Can be pre-rendered on server
  • load languages and themes on browser lazily

Options

import { DynamicCodeBlock } from 'fumadocs-ui/components/dynamic-codeblock';
 
<DynamicCodeBlock
  lang="ts"
  code='console.log("Hello World")'
  options={{
    themes: {
      light: 'github-light',
      dark: 'github-dark',
    },
    components: {
      // override components (e.g. `pre` and `code`)
    },
    // other Shiki options
  }}
/>;

Server Component

For a server component equivalent, you can use the built-in utility from core:

import * as Base from 'fumadocs-ui/components/codeblock';
import { highlight } from 'fumadocs-core/highlight';
import { type HTMLAttributes } from 'react';
 
export async function CodeBlock({
  code,
  lang,
  ...rest
}: HTMLAttributes<HTMLElement> & {
  code: string;
  lang: string;
}) {
  const rendered = await highlight(code, {
    lang,
    components: {
      pre: (props) => <Base.Pre {...props} />,
    },
    // other Shiki options
  });
 
  return <Base.CodeBlock {...rest}>{rendered}</Base.CodeBlock>;
}

How is this guide?

On this page