Code Block
带标题、操作和可选行号的源代码块。
app/api/chat/route.ts
1export async function POST(request: Request) {2 const { messages } = await request.json();3 4 return Response.json({ messages });5}导入
“导入”部分import { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockLine, CodeBlockTitle,} from "@pelagornis/page/components";示例
“示例”部分上方预览渲染的就是这个文件。复制到 src/components/ 即可开始使用。
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> );}在 MDX 中使用
“在 MDX 中使用”部分Code Block 由共享 React 状态的多个部分组成。写在 MDX 中的子节点会分别成为独立岛屿,彼此无法互通。请像上方示例一样在 .tsx 文件中组装,再导入该文件。
---title: Example---
import CodeBlockExample from "../../components/CodeBlockExample";
<CodeBlockExample client:load />client:load 会在页面加载时水合该岛屿。页面靠下的内容可使用 client:visible。
在 Markdoc 中使用
“在 Markdoc 中使用”部分Markdoc 标签会渲染 Astro 组件,而该组件需要客户端状态。请用会水合示例的 .astro 文件包装,再把该文件注册为标签。
---import CodeBlockExample from "../CodeBlockExample";---
<CodeBlockExample client:load />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, }, },});{% code-block-example /%}变体
“变体”部分各 variant prop 可接受的值。
结构
“结构”部分组件各部分及其渲染的 data-refineui 角色。
| 部件 | 角色 |
|---|---|
root | code-block |
actions | code-block-actions |
content | code-block-content |
copy | code-block-copy |
header | code-block-header |
line | code-block-line |
title | code-block-title |
状态
“状态”部分组件会样式化的状态。组件状态通过 data 属性暴露。
键盘
“键盘”部分组件获得焦点时处理的按键。
参考
“参考”部分- 设计指南与令牌细节见 RefineUI · Code Block。
- 通用规则见 在 MDX 中使用 与 在 Markdoc 中使用。