组件

Code block

A typed slip taped to the page: the file name in a typewriter face, the code on a two-color ribbon (red keywords and numbers, black for the rest), line numbers in the margin and a diff mode where removed lines are struck through in red pencil and added ones ticked in green. Ships its own small lexer for tsx, ts, jsx, js, css, json and bash.

安装

terminal
npx shadcn@latest add @wobko/code-block

第一次使用?先把 registry 加进 components.json。 如何安装

示例

A typed slip

Pass `code`, a `filename` and a language: the header, the copy button and the lines come with it. Keywords are typed in red, functions struck twice, strings underscored.

A diff

Removed lines are struck through in red pencil, added lines are ticked in green, and the header counts them. Copy puts the result on the clipboard.

css, json and bash

The small lexer knows tsx, ts, jsx, js, css, json and bash. The language comes from the file name when you do not say. Long lines wrap with `wrap`.

In a reply

A code slip inside a message: it takes the width of the column and scrolls sideways when a line is long.

API 参考

CodeBlock

A `figure` on a slip of paper, taped at the top. With `code` and no children it assembles a header (file name, `+/-` counts, copy button) and the content itself; with children you compose the parts.

属性类型默认值说明
codestring-The source. For a diff: unified diff lines (`+added`, `-removed`, ` context`).
language"tsx" | "ts" | "jsx" | "js" | "css" | "json" | "bash" | "diff" | "text"-Default: from the file name, else plain text. Aliases (`typescript`, `sh`, `javascript`...) work.
filenamestring-Typed at the top of the slip.
lineNumbersbooleanfalseNumber the lines in the margin (generated content: never selected, never copied).
diffboolean-Read the code as a diff. `language="diff"` turns it on too; give the code's own language with `filename`.
wrapbooleanfalseWrap long lines instead of scrolling sideways.
startLinenumber1The number of the first line.
copyablebooleantrueShow the copy button of the default header.
stock / tape / edge / tilt / fastener / seedPaperProps-It is a Paper: default `slip` stock with `tape="top"`. Any Paper prop works (a torn `receipt`, a kraft `stock`).

CodeBlockContent

The lines, in a scrolling `pre` (focusable, so the keyboard can scroll it) inside a host that draws the focus ring. Takes its own `code` and `language` to override the block's.

属性类型默认值说明
codestring-Show this instead of the block's code.
languagestring-Language of `code`.

无障碍

  • The code is real text in a `pre` > `code`, one `span` per line, each ending in a real newline (the last one does not): select, copy, find-in-page and screen readers see the source, blank lines included. Line numbers are CSS generated content and are never selected. The visually hidden 'Added:' / 'Removed:' are `user-select: none`, so they stay out of a copied selection. The figure is named by the file name, or by the language.
  • The scrolling body is a tab stop (`tabindex=0`) so a keyboard user can scroll long lines; on :focus-visible a blue ring is drawn round it.
  • Diff lines are `ins` and `del` elements with a visually hidden 'Added:' / 'Removed:'; the tick and the strike are aria-hidden and never the only signal. Git's `\ No newline at end of file` is read as a note, not as code.
  • Contrast: ink on the slip 14:1, red ribbon 4.8:1, graphite comments and line numbers 5.9:1. Reduced motion changes nothing (there is no animation but the copied tick).
  • Forced colors: the drawn rule and the ring step aside for real outlines; the strike and tick keep the text color.

源码

CLI 会复制进你项目的文件。

正在加载源码