Code block
A code block with a copy button.
Example
<div class="full"> <focsd-code label="Install command" language="shell" >pnpm add @focsd-dev/components</focsd-code ></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-code.js';</script>
<focsd-code label="Install command" language="shell">pnpm add @focsd-dev/components</focsd-code>import { Code } from '@focsd-dev/react';
export function Example() { return <Code label="Install command" language="shell">pnpm add @focsd-dev/components</Code>;}<script setup>import '@focsd-dev/components/focsd-code.js';</script>
<template> <focsd-code label="Install command" language="shell">pnpm add @focsd-dev/components</focsd-code></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-code.js';
// template<focsd-code label="Install command" language="shell">pnpm add @focsd-dev/components</focsd-code><script> import '@focsd-dev/components/focsd-code.js';</script>
<focsd-code label="Install command" language="shell">pnpm add @focsd-dev/components</focsd-code>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
language | language | string | — |
Slots
| Slot | Description |
|---|---|
(default) | Default content. |
CSS parts
Style the inside of the component with ::part(), for example focsd-code::part(code).
| Part | Description |
|---|---|
code | The code element. |
copy | The copy button element. |
copy-button | The copy button's own button. |
header | The header element. |
label | The label element. |
viewport | The viewport element. |
Accessibility
- The
<pre>is a focusable, labeled region so long lines can be scrolled with the keyboard. - "Copy code" announces "Copied" in a polite live region. If copying fails, it announces "Copy failed" and selects the code so it can be copied by hand.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.