GhostCodeBlock reference
Package: @ghostvue/vue. Status: experimental (stability policy).
Headless code block for documentation-style content: a header with a title, language label, consumer actions and a Copy button with a polite status announcement and a select-the-code fallback when the write fails; a <pre> viewport that scrolls horizontally and becomes a named tab stop only while it overflows; the consumer's highlighted markup in the default slot inside <code> (no v-html, no bundled highlighter); and an optional fold that clips long samples visually. useGhostCodeBlock exposes the same behavior for custom markup, and useGhostClipboard and writeClipboardText ship the clipboard contract for any other copy control.
Generated from the component manifest. Edit the manifest or Vue source, then rebuild the docs.
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
code | string | — | Yes | The raw source text. Copy writes it (unless `copyText` is set), the line count is taken from it, and it renders as escaped text when the default slot is empty. The slot must render the same lines. |
copiedDuration | number | 2000 | No | How long the copied state lasts, in ms. `0`, a negative number, or a non-finite number keeps it until the next copy. |
copyable | boolean | true | No | Renders the copy button. `false` removes it; the status region stays whenever the header renders. |
copyText | string | — | No | Text Copy writes instead of `code`, for example a shell sample without its `$` prompts. The failure fallback also supplies this text. |
defaultExpanded | boolean | false | No | Initial fold state in uncontrolled mode. Read once at setup. |
expanded | boolean | — | No | Controlled fold state (`v-model:expanded`). Ignored when the fold is not active. |
foldLines | number | — | No | Enables the fold when the sample has more lines than this. Must be a positive integer; any other value warns in development and disables the fold. |
language | string | — | No | Language label in the header (for example `ts`). Also `data-language` on the root and the class `language-<language>` on `<code>`. |
messages | Partial<GhostCodeBlockMessages> | "{}" | No | Text overrides, merged over the defaults. Read at render and when a write settles. |
title | string | — | No | Header title, usually a file name. Names the copy button and, through the title element, the code region. Replaces the native `title` attribute. |
Emits
| Name | Payload | Description |
|---|---|---|
copySettled | GhostCodeBlockCopySettledEvent | After a write settles, on success and failure, only for the latest request: { text, ok } where text is copyText ?? code at activation time. |
Models
| Prop | Event | Type | Description |
|---|---|---|---|
expanded | update:expanded | boolean | v-model:expanded for the fold state. Emitted on toggle activation, or when focus enters clipped content while collapsed. |
Slots
| Name | Description |
|---|---|
actions | Extra header controls before the copy button. Call `copy` directly from the event handler, not after an `await`. |
copy | Replaces the copy button's visible content, for example an icon. The button keeps its `aria-label`. |
default | Highlighted markup, rendered inside `<code data-part="code">`. Phrasing content only: no `<pre>` or `<code>` of its own. When empty (absent, or only comments and whitespace), `code` renders as escaped text. |
title | Replaces the title text. The region is named by this element; the copy button name still comes from the `title` prop, so pass both. |
Slot props: actions
| Name | Type | Description |
|---|---|---|
copy | () => Promise<boolean> | Slot prop `copy`. |
state | GhostClipboardState | Slot prop `state`. |
Slot props: copy
| Name | Type | Description |
|---|---|---|
state | GhostClipboardState | Slot prop `state`. |
Slot props: default
None declared.
Slot props: title
None declared.
Exposed members
| Name | Type | Description |
|---|---|---|
copy | () => Promise<boolean> | Runs the same copy as the button, including the failure fallback. |
element | HTMLElement | null | Root element; `null` before mount and after unmount. |
Parts and data attributes
| Attribute | Value | Description |
|---|---|---|
data-part | root | Block container (div, no role). Consumer class, style and other attributes land here. Carries data-language and data-fold. |
data-part | header | Rendered when title, the title slot, language, the actions slot or copyable applies. No role. |
data-part | title | Title text or the title slot. Names the viewport region through aria-labelledby. |
data-part | language | Language label, plain text exposed to assistive technology. |
data-part | copy | Native button. Visible text is messages.copy in every state; accessible name is messages.copyLabel(title). data-state carries idle, copied or failed. |
data-part | status | Polite role=status live region, in the server markup. Visually hidden while idle or copied; visible with the failure text while failed. data-state mirrors the copy state. |
data-part | viewport | The <pre>, dir=ltr, horizontal scroll container (overflow-x: auto). A named region and tab stop only after mount, while it overflows horizontally and holds no tabbable descendants. |
data-part | code | <code> with translate=no and class language-<language>; holds the default slot or the escaped code. While the fold is active it carries display: block and --ghost-code-block-fold-lines, plus the clip while collapsed. |
data-part | fold-toggle | Native button after the viewport, rendered only while the fold is active. No aria-expanded or aria-controls; its text says what activating it does. |
data-state | idle | data-state on copy and status before a copy, after copiedDuration, and after a new copy starts. |
data-state | copied | data-state on copy and status after a successful write, for copiedDuration. |
data-state | failed | data-state on copy and status after a failed write, until the next copy. The code is selected. |
data-state | collapsed | data-state on code and the fold toggle (and data-fold on the root) while the active fold clips the code. |
data-state | expanded | data-state on code and the fold toggle (and data-fold on the root) while the active fold shows every line. |
data-language | present | On the root: the language prop, when set. |
data-fold | collapsed, expanded | On the root while the fold is active; absent otherwise. |
data-overflow | none, horizontal | On the viewport: measured horizontal overflow, none on the server. |
CSS variables
| Name | Description |
|---|---|
--ghost-code-block-fold-lines | Set inline on the code part to the foldLines value while the fold is active; the collapsed clip is calc(var(--ghost-code-block-fold-lines) * 1lh). |
Accessibility
Role: generic. APG pattern: https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/.
Required ARIA attributes: aria-label, aria-labelledby.
Keyboard
Verification
Each category is reviewed separately. Automated keyboard and axe results do not attest manual or assistive-technology review.
| Category | Status | Evidence |
|---|---|---|
| Automated keyboard | Passed | Every declared key maps to a Chromium browser test that CI runs and requires to pass on every commit. |
| Manual interaction | Not reviewed | No review recorded. |
| Assistive technology | Not reviewed | No review recorded. |
Examples
code-block/headless
The unstyled GhostCodeBlock from @ghostvue/vue styled only through data-part and data-state hooks.