コンテンツにスキップ
Page

Code Block

タイトル、アクション、任意の行番号を持つソース一覧です。

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";

上のプレビューはこのファイルを描画したものです。src/components/ にコピーして使い始めてください。

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 は React の状態を共有する複数のパーツで構成されます。MDX に書いた子要素はそれぞれ別のアイランドとして描画されるため、パーツ同士がつながりません。上の例のように .tsx ファイルで組み立て、そのファイルをインポートしてください。

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

client:load はページの読み込み時にアイランドをハイドレートします。ページの下の方にあるコンテンツには client:visible を使ってください。

Markdoc タグは Astro コンポーネントを描画し、このコンポーネントはクライアント状態が必要です。例をハイドレートする .astro ファイルで包み、そのファイルをタグとして登録してください。

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 /%}

各 variant prop に渡せる値です。

コンポーネントの各パーツと、それぞれが描画する data-refineui の役割です。

パーツロール
rootcode-block
actionscode-block-actions
contentcode-block-content
copycode-block-copy
headercode-block-header
linecode-block-line
titlecode-block-title

コンポーネントがスタイルする状態です。コンポーネント状態は data 属性として公開されます。

フォーカスがあるときにコンポーネントが扱うキーです。