Skip to content
Page

Code Block

Source listing with a title, actions, and optional line numbers.

app/api/chat/route.ts
export async function POST(request: Request) {  const { messages } = await request.json();   return Response.json({ messages });}
import {
CodeBlock,
CodeBlockActions,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockHeader,
CodeBlockLine,
CodeBlockTitle,
} from "@pelagornis/page/components";

The preview above renders this file. Copy it into src/components/ to start from it.

src/components/CodeBlockExample.tsx
import { useState } from "react";
import {
Button,
CodeBlock,
CodeBlockActions,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockHeader,
CodeBlockLine,
CodeBlockTitle,
WebIcon,
} from "@pelagornis/page/components";
const source = `export async function POST(request: Request) {
const { messages } = await request.json();
return Response.json({ messages });
}`;
function downloadSource() {
const url = URL.createObjectURL(new Blob([source], { type: "text/plain" }));
const link = document.createElement("a");
link.href = url;
link.download = "route.ts";
link.click();
URL.revokeObjectURL(url);
}
export default function CodeBlockDemo() {
const [wrap, setWrap] = useState(false);
return (
<CodeBlock className="w-full">
<CodeBlockHeader>
<CodeBlockTitle title="app/api/chat/route.ts">
<WebIcon name="document" size={16} color="currentColor" fallback="{}" aria-hidden />
app/api/chat/route.ts
</CodeBlockTitle>
<CodeBlockActions>
<Button
variant="ghost"
size="sm"
layout="icon"
aria-label="Wrap lines"
aria-pressed={wrap}
title="Wrap lines"
onClick={() => setWrap((current) => !current)}
>
<WebIcon name="text" size={16} color="currentColor" fallback="↔" aria-hidden />
</Button>
<Button
variant="ghost"
size="sm"
layout="icon"
aria-label="Download source"
title="Download source"
onClick={downloadSource}
>
<WebIcon name="save" size={16} color="currentColor" fallback="↓" aria-hidden />
</Button>
<CodeBlockCopyButton value={source} />
</CodeBlockActions>
</CodeBlockHeader>
<CodeBlockContent wrap={wrap} lineNumbers>
<CodeBlockLine>
<span className="text-refineui-alias-foreground-info">export async function</span>{" "}
<span className="text-refineui-alias-foreground-discovery">POST</span>
<span>(request: Request) {"{"}</span>
</CodeBlockLine>
<CodeBlockLine>
{" "}
<span className="text-refineui-alias-foreground-info">const</span>
{" { messages } = "}
<span className="text-refineui-alias-foreground-info">await</span>
{" request."}
<span className="text-refineui-alias-foreground-discovery">json</span>
<span>();</span>
</CodeBlockLine>
<CodeBlockLine> </CodeBlockLine>
<CodeBlockLine variant="highlighted">
{" "}
<span className="text-refineui-alias-foreground-info">return</span>
{" Response."}
<span className="text-refineui-alias-foreground-discovery">json</span>
<span>({"{"} messages {"}"});</span>
</CodeBlockLine>
<CodeBlockLine>{"}"}</CodeBlockLine>
</CodeBlockContent>
</CodeBlock>
);
}

Code Block is composed from parts that share React state. Children written in MDX are rendered as separate islands, so the parts can’t reach each other. Put the composition in a .tsx file, like the example above, and import that file instead.

src/content/docs/example.mdx
---
title: Example
---
import CodeBlockExample from "../../components/CodeBlockExample";
<CodeBlockExample client:load />

client:load hydrates the island on page load. Use client:visible for content far down the page.

Markdoc tags render Astro components, and this one needs client state. Wrap the example in an .astro file that hydrates it, then register that file as a tag.

src/components/markdoc/CodeBlockExample.astro
---
import CodeBlockExample from "../CodeBlockExample";
---
<CodeBlockExample client:load />
markdoc.config.mjs
import { component, defineMarkdocConfig } from "@astrojs/markdoc/config";
import starlightMarkdoc from "@astrojs/starlight-markdoc";
import pageMarkdoc from "@pelagornis/page/markdoc";
export default defineMarkdocConfig({
extends: [starlightMarkdoc(), pageMarkdoc()],
tags: {
"code-block-example": {
render: component("./src/components/markdoc/CodeBlockExample.astro"),
selfClosing: true,
},
},
});
src/content/docs/example.mdoc
{% code-block-example /%}

Values the component accepts for each variant prop.

Parts of the component and the data-refineui role each one renders.

PartRole
rootcode-block
actionscode-block-actions
contentcode-block-content
copycode-block-copy
headercode-block-header
linecode-block-line
titlecode-block-title

States the component styles. Component states are exposed as data attributes.

Keys the component handles when it has focus.