콘텐츠로 이동
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 속성으로 노출됩니다.

포커스가 있을 때 컴포넌트가 처리하는 키입니다.