Code Block

呈现带高亮的代码,并支持行控件、文件标签与复制操作。

适用场景

  • 当界面需要以下行为时使用 Code Block:呈现带高亮的代码,并支持行控件、文件标签与复制操作。

安装

选择安装方式,用法示例将同步更新导入路径。

$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/code-block.json

首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。

示例

预览

summarize.tstypescriptWriting
1import { generateText } from "ai";

组合结构

使用公开部件 CodeBlock, AgentCodeLine 组合 Code Block,并从该组件的公开入口导入。

StreamingResponse
└── CodeBlock

说明: File Diff 通过对齐行号与可展开上下文检查新增及删除内容。 Tool Result 在同一表面呈现工具调用状态、摘要与可展开结果。 Streaming Response 组合持续变化的助手回复,包括文字、附件、引用和操作。

工作方式

呈现带高亮的代码,并支持行控件、文件标签与复制操作。

状态与事件

此示例组合没有声明成对的受控属性与 update 事件。使用已列出的属性、原生事件和插槽组合内容;不要为它添加未公开的 v-model。

API 参考

CodeBlock

code
string
—
language?
"text" | "bash" | "diff" | "json" | "tsx" | "typescript" | "javascript" | "vue" | "css" | "html"
"typescript"
filename?
boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>
—
status?
"streaming" | "complete"
"complete"
showLineNumbers?
boolean
true
highlightLines?
number[]
() => []
maxHeight?
number
280
wrap?
boolean
false
copyable?
boolean
true
onCopy?
() => void | Promise<void>
—

AgentCodeLine

code
string
—
tokens?
AgentCodeToken[]
—

无障碍

为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。

常见问题

如何配置 Code Block?
通过 code、language、filename、status、showLineNumbers、highlightLines、maxHeight、wrap、copyable、onCopy 配置 Code Block;API 表列出其类型与默认值。
如何连接到应用状态?
通过文档中的组合方式提供内容,应用副作用由调用方管理。
可以在服务端渲染吗?
可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。

更新于