JGuang

Code Block

Code Block · Chakra UI

Preview
npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0

Code Block

Used to display and highlight dynamic code blocks

Copy source without decorative line numbers, handle long lines, and load only required highlighting languages. Displayed code is not executable page content.

Included icon notices

The source viewer includes ICON_ATTRIBUTION.md and original notices for the icon collections used by these examples. Preserve them when redistributing examples; react-icons' wrapper license does not replace individual collection terms.

  • Font Awesome 5: CC BY 4.0 License
  • Ionicons 5: MIT
  • Lucide: ISC

Installation and initialization

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0

Initialize ChakraProvider with defaultSystem and Emotion before rendering. Preserve the host React version and inspect peer compatibility. Official npm installation supplies components, not these example source files. Use the resource setup guide for locale, next-themes and framework-specific SSR; copy the selected example source separately from npm installation.

Complete native usage example

This is the actual pinned example, not a generic API mock. Copy its source closure and retain the named export. Additional imports are listed per example below.

"use client"

import { CodeBlock, createShikiAdapter } from "@chakra-ui/react"
import type { HighlighterGeneric } from "shiki"

const file = {
  code: `
<div class="container">
  <h1>Hello, world!</h1>
</div>
`,
  language: "html",
  title: "index.html",
}

export const CodeBlockBasic = () => {
  return (
    <CodeBlock.AdapterProvider value={shikiAdapter}>
      <CodeBlock.Root code={file.code} language={file.language}>
        <CodeBlock.Content>
          <CodeBlock.Code>
            <CodeBlock.CodeText />
          </CodeBlock.Code>
        </CodeBlock.Content>
      </CodeBlock.Root>
    </CodeBlock.AdapterProvider>
  )
}

const shikiAdapter = createShikiAdapter<HighlighterGeneric<any, any>>({
  async load() {
    const { createHighlighter } = await import("shiki")
    return createHighlighter({
      langs: ["tsx", "scss", "html", "bash", "json"],
      themes: ["github-dark"],
    })
  },
  theme: "github-dark",
})

All examples and reproduction requirements

Names identify demos, not component variant props. Copy every sourceFiles entry for the selected example. The compositions alias denotes local files, not a package. Preserve relative imports or rewrite the alias to your project's actual path. React peers are inspected separately; editor examples retain the coherent pinned Tiptap extension family.

Basic

code-block-basic · Named export CodeBlockBasic · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-basic.tsx

With Sizes

code-block-with-sizes · Named export CodeBlockWithSizes · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-sizes.tsx

With Title

code-block-with-title · Named export CodeBlockWithTitle · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-title.tsx

With Copy Button

code-block-with-copy-button · Named export CodeBlockWithCopyButton · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-copy-button.tsx

With Line Numbers

code-block-with-line-numbers · Named export CodeBlockWithLineNumbers · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-line-numbers.tsx

With Line Highlight

code-block-with-line-highlight · Named export CodeBlockWithLineHighlight · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-line-highlight.tsx

With Line Focus

code-block-with-line-focus · Named export CodeBlockWithLineFocus · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-line-focus.tsx

With Diff

code-block-with-diff · Named export CodeBlockWithDiff · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-diff.tsx

With Max Lines

code-block-with-max-lines · Named export CodeBlockWithMaxLines · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-max-lines.tsx

With Language Switcher

code-block-with-language-switcher · Named export CodeBlockWithLanguageSwitcher · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 react-icons@5.6.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-language-switcher.tsx

With Floating Copy Button

code-block-with-floating-copy-button · Named export CodeBlockWithFloatingCopyButton · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-floating-copy-button.tsx

With Tabs

code-block-with-tabs · Named export CodeBlockWithTabs · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-tabs.tsx

With Tabs Sync

code-block-with-tabs-sync · Named export CodeBlockWithTabsSync · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-tabs-sync.tsx

With Themes

code-block-with-themes · Named export CodeBlockWithThemes · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 next-themes@0.4.6 react-icons@5.6.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-themes.tsx
  • vendor/apps/compositions/src/ui/color-mode.tsx

With Word Wrap

code-block-with-word-wrap · Named export CodeBlockWithWordWrap · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-word-wrap.tsx

With Line Numbers Word Wrap

code-block-with-line-numbers-word-wrap · Named export CodeBlockWithLineNumbersWordWrap · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 shiki@3.23.0
  • vendor/apps/compositions/src/examples/code-block-with-line-numbers-word-wrap.tsx

With Highlight Js

code-block-with-highlight-js · Named export CodeBlockWithHighlightJs · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0 highlight.js@11.11.1
  • vendor/apps/compositions/src/examples/code-block-with-highlight-js.tsx

Plain Text

code-block-plain-text · Named export CodeBlockPlainText · original upstream example

npm install @chakra-ui/react@3.37.0 @emotion/react@11.14.0
  • vendor/apps/compositions/src/examples/code-block-plain-text.tsx

Native API and source references

The source viewer retains original declarations, imports and inheritance. Ark UI event contracts, Chakra style props and recipe types stay in their native packages; this guide does not flatten or invent their defaults. Code comments remain original source, not translated prose.

  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/adapters.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/code-block-adapter-context.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/code-block-adapter-provider.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/code-block-context.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/code-block.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/index.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/namespace.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/types.d.ts
  • npm/@chakra-ui/react@3.37.0/dist/types/components/code-block/use-code-highlight.d.ts

State and installation checks

Run strict types, the target production build and real interactions. Test labels/focus/keyboard, both themes, narrow layout, empty/loading/error states where supported and cleanup after unmount. Remote demo endpoints, uploads and downloads are constrained in the preview; configure your own services. Copying a command or reaching preview-ready does not prove installation or backend success.

Provenance and license boundaries

https://github.com/chakra-ui/chakra-ui/tree/2e7517745cff2fcde0b8012f136cf99611ff262a

These are JGuang practical bilingual notes, not a full translation of upstream MDX. Original documentation and example bytes remain in vendor. Preserve repository MIT, compositions ISC metadata and separate dependency notices. The original metadata discrepancy and any missing full notices remain explicit review items; paid Chakra Pro is excluded.