Skip to content
Focsd homeFocsd homeFocsd
Forms

Code block

A code block with a copy button.

  • Tag: focsd-code
  • Pattern: Pre/code + copy button
  • Category: Display

Example

pnpm add @focsd-dev/components
HTML
<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>

API

Attributes

AttributePropertyTypeDefault
labellabelstring—
languagelanguagestring—

Slots

SlotDescription
(default)Default content.

CSS parts

Style the inside of the component with ::part(), for example focsd-code::part(code).

PartDescription
codeThe code element.
copyThe copy button element.
copy-buttonThe copy button's own button.
headerThe header element.
labelThe label element.
viewportThe 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.