组件 · 数据展示
CopyableCode
One command people copy and run, on a code surface with a copy button that confirms with a check.
也叫 Command line
import { CopyableCode } from "@botiverse/raft-ui";One line that people copy and run elsewhere: the command that connects a computer, installs the CLI or signs in. The command sits on a code surface with a copy button beside it; after a copy, the button shows a check for a moment.
When to use
| Situation | Use |
|---|---|
| One command people copy and run | CopyableCode |
| A name from code inside a sentence | InlineCode |
| Several lines of code, or a log | CodeBlock |
Anatomy
CopyableCodeRoot: holds the command and the button, and handles the copy. Required.CopyableCode: the command. Its text is what gets copied. Required.CopyableCodeAction: the copy button, an icon Button. Required.
Variants
| Variant | Use for |
|---|---|
size="md" | The main step of a dialog or a page, such as setting up a computer |
size="sm" | A command among other details, such as in a panel or a list |
States
| State | Look |
|---|---|
| Copied | The copy icon turns into a check, then back after a moment |
| Copy failed | Nothing changes in the component; onError runs so you can tell the person |
Behavior
- The button copies the text content of
CopyableCode.onCopyruns only after the copy succeeded. - Without
truncate, a long command wraps; with it, the command stays on one line and scrolls sideways.
Content
- Show the command exactly as people run it.
- When a copy fails, say so and say what to do: "Couldn't copy the command. Copy the text manually, or check the browser's clipboard permission."
Accessibility
CopyableCodeActionis labeled "Copy code" and "Copied code" by default, and announces the change (aria-live="polite"). Give it anaria-labelthat names what is copied: "Copy login command".- Tell the person about a failed copy in words, through
onError(a Toast or a status line). The icon alone doesn't say it failed. - The command is a
codeelement, so screen readers read it as text.
Rules
- Set the command in Space Mono,
text-mono-code, onlayer-codewith aline-hairlineborder. - Keep a command on one line: use
truncate, don't let it wrap. - Don't copy the
$prompt. Keep the prompt out ofCopyableCode, because everything in it is copied.
API
CopyableCode
| Prop | 类型 | 默认值 |
|---|---|---|
| boolean |
Also takes the attributes of its element.
CopyableCodeAction
| Prop | 类型 | 默认值 |
|---|---|---|
| string | ||
| ReactNode | ((state: { copied: boolean; }) => ReactNode) | ||
| boolean | ||
| string | ||
| `md` (32px) and `icon-md` for rows, cards, banners, toolbars and panel headers; `lg` and `icon-lg` (the control height) for dialog footers, forms and beside a field; `xl` for a full-width call to action. The size follows the place: see Button › Size by place. | "md" | "lg" | "xl" | "icon-md" | "icon-lg" | DeprecatedButtonSize | "md" |
| "primary" | "accent" | "warning" | "danger" | "default" | "outline" | "information" | "muted" | "success" | "danger-secondary" | "danger-outline" | "ghost" | "on-frame" | ||
| Whether the button should be focusable when disabled. | boolean | false |
| Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | boolean | true |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) |
Also takes the attributes of its element.
CopyableCodeRoot
| Prop | 类型 | 默认值 |
|---|---|---|
| boolean | ||
| Called with the copied text only after a write that actually landed. | (value: string) => void | Promise<void> | |
| Called when every copy strategy failed, so the consumer can tell the user. | (error: Error) => void | |
| "sm" | "md" | "md" |
Also takes the attributes of its element.
相关组件
改动
- 0.4.0 · Breaking Changes
CopyableCodeActionlabel props removed.ariaLabelandcopiedAriaLabelare gone; use the standardaria-labelprop instead. The default label still swaps between "Copy code" and "Copied code". ([b3786da](https://github.com/botiverse/rui/commit/b3786da)) - 0.1.0 · Breaking ChangesFlattened
CopyableCode: the preset andCopyableCode.Codecompound API are removed. ComposeCopyableCodeRoot,CopyableCode, andCopyableCodeAction;CopyableCodeCodePropsis gone and data-slots were renamed.