Code block
Typography
Reading column with comfortable measure, paragraph rhythm, quotes without side bars and policy pages that render as plain prose.
실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.
내 앱에 넣기
아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.
git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이
src/components/taster에 들어갑니다.node ../taster/packages/engine/cli.mjs add content/code-block --from https://taster-six.vercel.app --dir src/components/taster부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고,
tokens.css를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.// tailwind.config.cjs module.exports = { presets: [require('./src/components/taster/tailwind-preset.cjs')], content: ['./index.html', './src/**/*.{ts,tsx}'], };Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는
src/의 스타일시트 맨 위에 이 두 줄을 넣습니다.@import "tailwindcss"; @config "../tailwind.config.cjs";tokens.css는:root에--bg,--text,--border,--accent를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.
언제 쓰나요
- Long-form proseReading column with comfortable measure, paragraph rhythm, quotes without side bars and policy pages that render as plain prose.
대신 없애 주는 AI 티
- Tight line height on Korean text (KO-03)
- Korean body text that is too small (KO-11)
- Long content as one image (LAY-07)
정보
코드 보기
import * as React from 'react'
import { cn } from '../blocks/_cn'
// Code block: preformatted code on the sunken surface with a header row that names the file or the
// language and holds one Copy button. The button says "Copied" for a moment after a copy and announces it,
// falls back to selecting the text when the clipboard is not available, and is a real 44px target on touch.
// Optional line numbers are drawn with CSS counters, so they are not copied with the code.
// The mono face is set on the code only; the header label stays in the interface face (AC-09). Long lines
// scroll inside the block by default, or wrap with `wrap`. No window dots, no fake terminal (CMP-07).
// Highlighting is out of scope: pass already highlighted children if you have them.
// Original code, MIT.
export interface CodeBlockProps extends Omit<React.HTMLAttributes<HTMLElement>, 'children'> {
code: string
/** File name or language, shown in the header, for example "tailwind.config.cjs". */
label?: string
/** Language for the code element class, for example "ts". */
language?: string
lineNumbers?: boolean
wrap?: boolean
copyLabel?: string
copiedLabel?: string
/** Highlighted markup to show instead of the plain `code`; `code` is still what gets copied. */
children?: React.ReactNode
}
export function CodeBlock({
code,
label,
language,
lineNumbers = false,
wrap = false,
copyLabel = 'Copy',
copiedLabel = 'Copied',
children,
className,
...props
}: CodeBlockProps) {
const [copied, setCopied] = React.useState(false)
const pre = React.useRef<HTMLPreElement>(null)
const timer = React.useRef<number>()
React.useEffect(() => () => window.clearTimeout(timer.current), [])
async function copy() {
try {
await navigator.clipboard.writeText(code)
setCopied(true)
window.clearTimeout(timer.current)
timer.current = window.setTimeout(() => setCopied(false), 1600)
} catch {
// No clipboard permission: select the code so the reader can copy it by hand.
const sel = window.getSelection()
if (pre.current && sel) {
const r = document.createRange()
r.selectNodeContents(pre.current)
sel.removeAllRanges()
sel.addRange(r)
}
}
}
const lines = code.replace(/\n$/, '').split('\n')
return (
<figure className={cn('m-0 min-w-0 overflow-hidden rounded-md bg-bg-sunken', className)} {...props}>
<figcaption className="flex min-h-11 items-center justify-between gap-3 border-b border-border-soft pl-4 pr-1">
<span className="min-w-0 truncate text-label text-text-muted">{label ?? language ?? ''}</span>
<button
type="button"
onClick={copy}
className="inline-flex h-11 shrink-0 items-center gap-2 rounded-sm px-3 text-label font-medium text-text-muted outline-none transition-colors duration-press ease-out hover:text-text focus-visible:ring-2 focus-visible:ring-ring desk:h-8"
>
<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
{copied ? (
<path d="M5 13l4 4 10-10" />
) : (
<>
<rect x="8" y="8" width="12" height="12" rx="2" />
<path d="M16 8V5a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h3" />
</>
)}
</svg>
<span aria-live="polite">{copied ? copiedLabel : copyLabel}</span>
</button>
</figcaption>
<pre
ref={pre}
tabIndex={0}
aria-label={label ? `Code: ${label}` : 'Code'}
className={cn(
'm-0 overflow-x-auto p-4 text-label leading-[var(--lh-normal)] text-text outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring',
wrap ? 'whitespace-pre-wrap [overflow-wrap:anywhere]' : 'whitespace-pre',
)}
>
<code className={cn(language && `language-${language}`, lineNumbers && !children && '[counter-reset:line]') || undefined}>
{children ??
(lineNumbers
? lines.map((l, i) => (
<span key={i} className="block before:mr-4 before:inline-block before:w-[2ch] before:select-none before:text-right before:text-text-subtle before:content-[counter(line)] [counter-increment:line]">
{l || ' '}
</span>
))
: code)}
</code>
</pre>
</figure>
)
}