Files
supabase/apps/design-system/content/docs/fragments/code-block.mdx
Anthony Lio 8bcbe3dcc7 feat(docs): extracts code block component (#51661)
## Problem

new surfaces display code blocks their own way while docs has the full
implementation but it lives inside `apps/docs`


## Solution

- moves the docs code block parts to `ui-patterns/CodeBlock` as separate
imports
- kb code blocks now show line numbers, which matches docs

| state | preview |
| -------|------|
| before | <img width="913" height="509" alt="image"
src="https://github.com/user-attachments/assets/76ff0096-433a-4b06-9eca-1d79c4940e24"
/> |
| after | <img width="913" height="509" alt="image"
src="https://github.com/user-attachments/assets/4f5d91d9-13df-44a0-9e65-edf605773c5f"
/> |

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. visit /[docs/guides/database/tables#enabling-row-level-security
](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/database/tables#enabling-row-level-security)code
blocks, line numbers, colors in light and dark mode, copy, and word wrap
match
2. visit
[/docs/guides/auth/passwords](https://docs-git-feat-shared-code-block-parts-supabase.vercel.app/docs/guides/auth/passwords)
hover over a typed identifier tooltip should still show
3. visit a kb guide with code, for example
[/kb/guides/migrate-from-mysql](https://kb-git-feat-shared-code-block-parts-supabase.vercel.app/kb/guides/migrate-from-mysql)
4. visit the [design system code block
page](https://design-system-git-feat-shared-code-block-parts-supabase.vercel.app/design-system/docs/fragments/code-block)

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-22 11:46:38 +03:00

94 lines
3.3 KiB
Plaintext

---
title: Code Block
description: Shows highlighted code with line numbers, a copy button, and a wrap button.
fragment: true
---
Code Block is the frame for code samples in the documentation and in knowledge base articles. The frame and the highlighted code render as static HTML. Users can see the code before JavaScript loads. The copy button and the wrap button are optional. They operate in the browser.
<ComponentPreview name="code-block-demo" peekCode wide />
## Without line numbers
For short code samples, such as a single command, set `lineNumbers={false}`.
Add `controls` only for code that users copy.
<ComponentPreview name="code-block-plain" peekCode />
## Annotations
A token can have annotations that show when you hold the pointer over it. For example, Twoslash supplies type information. Put the annotations in the third item of the token: `[content, className, annotations]`.
## Setup
### Highlight the code
```tsx
import { highlightCode } from 'ui-patterns/CodeBlock/CodeBlock.highlight'
import { getTokenClassName } from 'ui-patterns/CodeBlock/CodeBlock.tokens'
import { CodeBlockControls } from 'ui-patterns/CodeBlock/CodeBlockControls'
import { CodeBlockFrame, CodeBlockTokens } from 'ui-patterns/CodeBlock/CodeBlockFrame'
```
```tsx
const { tokens } = await highlightCode(code, 'ts')
<CodeBlockFrame
lang="ts"
lineCount={tokens.length}
controls={<CodeBlockControls content={code} />}
>
<CodeBlockTokens
lineNumbers
lines={tokens.map((line) =>
line.map(({ content, color, fontStyle }) => [content, getTokenClassName(color, fontStyle)])
)}
/>
</CodeBlockFrame>
```
Use `highlightCode` in a server component or during the build. It loads Shiki one time and uses it again for all other blocks. Do not import `highlightCode` into a client component.
`getTokenClassName` changes each Shiki color into a short class name, for example `s-k` for keywords. The token colors are CSS variables. When the theme changes between light and dark, the colors also change.
### Add the styles
Import the Code Block stylesheet one time. Put the import after the Tailwind config:
```css
@import 'config/tailwind.config.css';
@import 'ui-patterns/CodeBlock/code-block.css';
```
This stylesheet contains these styles:
- The line number gutter
- The token colors for the light theme and the dark theme
- The word wrap mode
- The space around code blocks in `prose` content
### Give the copy text
`CodeBlockControls` copies the `content` string. Give the source code that users can run. Do not give the rendered text, because it contains the line numbers and the annotations.
The button shows a check mark only after the clipboard write is successful. Then, screen readers announce "Code copied".
## Guidelines
### When to use
Use Code Block for code that users copy or read line by line, for example a setup step or a function.
Use inline `code` for a single name or value in a sentence.
For Studio and other app screens, use the `CodeBlock` component from `ui-patterns/CodeBlock`.
### Always set the language
Always set `lang` to the language of the code. `highlightCode` uses the language to color the tokens. The frame uses it to name the block for screen readers, for example "JavaScript, 3 lines".
### Keep the code runnable
Give code that users can run without changes. Do not use pseudo-code, and do not put a `$` prompt before shell commands.