Code Block
Display a command with a copy action.
Examples
Display a command with a copy action
npm install @accelerate/ui
React code
import {
CodeBlock,
CodeBlockContent,
CodeBlockCopy,
} from "@accelerate/react/components";
export function Example() {
return (
<>
<CodeBlock>
<CodeBlockContent>
<code>npm install @accelerate/ui</code>
</CodeBlockContent>
<CodeBlockCopy>Copy command</CodeBlockCopy>
<span role="status" data-acc-copy-status=""></span>
</CodeBlock>
</>
);
}Angular code
import { Component } from "@angular/core";
import {
AccCodeBlock,
AccCodeBlockContent,
AccCodeBlockCopy,
} from "@accelerate/angular/components";
@Component({
selector: "app-example",
imports: [
AccCodeBlock,
AccCodeBlockContent,
AccCodeBlockCopy,
],
template: `
<div accCodeBlock>
<pre accCodeBlockContent>
<code>npm install @accelerate/ui</code>
</pre>
<button accCodeBlockCopy>Copy command</button>
<span role="status" data-acc-copy-status=""></span>
</div>
`,
})
export class Example {}Installation
Source preview. The expanded component catalog is ahead of the public npm releases. To run these examples in your own application, use packages built from the SDK source used for the previews or a release that includes the APIs below.
- React
@accelerate/ui@0.4.0and@accelerate/react@0.4.0- Angular
@accelerate/ui@0.4.0and@accelerate/angular@0.2.0
Load @accelerate/ui/theme.css and put acc-theme on the app or the region using Accelerator components. See the framework setup guide for the theme and adapter conventions.
Usage
Add CodeBlockContent containing a code element. Style authored syntax spans with data-token="keyword", comment, or string. Optional CodeBlockCopy copies text and updates a role="status" element with data-acc-copy-status.
Import the React component CodeBlock from @accelerate/react/components. For Angular, add AccCodeBlock to the standalone component’s imports and use <div accCodeBlock>.
Examples use these React parts: CodeBlock, CodeBlockContent, CodeBlockCopy. The Angular examples show the corresponding directives and template hosts.
API reference
- CodeBlockContent
- Wrap a code element in the preformatted part.
- data-acc-copy-status
- Receives success or failure feedback through the status region.