Skip to content

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.

All component references

Props ​

NameTypeDefaultRequiredDescription
codestring—YesThe 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.
copiedDurationnumber2000NoHow long the copied state lasts, in ms. `0`, a negative number, or a non-finite number keeps it until the next copy.
copyablebooleantrueNoRenders the copy button. `false` removes it; the status region stays whenever the header renders.
copyTextstring—NoText Copy writes instead of `code`, for example a shell sample without its `$` prompts. The failure fallback also supplies this text.
defaultExpandedbooleanfalseNoInitial fold state in uncontrolled mode. Read once at setup.
expandedboolean—NoControlled fold state (`v-model:expanded`). Ignored when the fold is not active.
foldLinesnumber—NoEnables 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.
languagestring—NoLanguage label in the header (for example `ts`). Also `data-language` on the root and the class `language-<language>` on `<code>`.
messagesPartial<GhostCodeBlockMessages>"{}"NoText overrides, merged over the defaults. Read at render and when a write settles.
titlestring—NoHeader title, usually a file name. Names the copy button and, through the title element, the code region. Replaces the native `title` attribute.

Emits ​

NamePayloadDescription
copySettledGhostCodeBlockCopySettledEventAfter a write settles, on success and failure, only for the latest request: { text, ok } where text is copyText ?? code at activation time.

Models ​

PropEventTypeDescription
expandedupdate:expandedbooleanv-model:expanded for the fold state. Emitted on toggle activation, or when focus enters clipped content while collapsed.

Slots ​

NameDescription
actionsExtra header controls before the copy button. Call `copy` directly from the event handler, not after an `await`.
copyReplaces the copy button's visible content, for example an icon. The button keeps its `aria-label`.
defaultHighlighted 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.
titleReplaces 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 ​

NameTypeDescription
copy() => Promise<boolean>Slot prop `copy`.
stateGhostClipboardStateSlot prop `state`.

Slot props: copy ​

NameTypeDescription
stateGhostClipboardStateSlot prop `state`.

Slot props: default ​

None declared.

Slot props: title ​

None declared.

Exposed members ​

NameTypeDescription
copy() => Promise<boolean>Runs the same copy as the button, including the failure fallback.
elementHTMLElement | nullRoot element; `null` before mount and after unmount.

Parts and data attributes ​

AttributeValueDescription
data-partrootBlock container (div, no role). Consumer class, style and other attributes land here. Carries data-language and data-fold.
data-partheaderRendered when title, the title slot, language, the actions slot or copyable applies. No role.
data-parttitleTitle text or the title slot. Names the viewport region through aria-labelledby.
data-partlanguageLanguage label, plain text exposed to assistive technology.
data-partcopyNative button. Visible text is messages.copy in every state; accessible name is messages.copyLabel(title). data-state carries idle, copied or failed.
data-partstatusPolite 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-partviewportThe <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-partcode<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-partfold-toggleNative 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-stateidledata-state on copy and status before a copy, after copiedDuration, and after a new copy starts.
data-statecopieddata-state on copy and status after a successful write, for copiedDuration.
data-statefaileddata-state on copy and status after a failed write, until the next copy. The code is selected.
data-statecollapseddata-state on code and the fold toggle (and data-fold on the root) while the active fold clips the code.
data-stateexpandeddata-state on code and the fold toggle (and data-fold on the root) while the active fold shows every line.
data-languagepresentOn the root: the language prop, when set.
data-foldcollapsed, expandedOn the root while the fold is active; absent otherwise.
data-overflownone, horizontalOn the viewport: measured horizontal overflow, none on the server.

CSS variables ​

NameDescription
--ghost-code-block-fold-linesSet 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 ​

KeyActionAutomated browser test
TabMoves through header actions, Copy, the viewport (when it is a tab stop) or tabbables inside the code, then the fold toggle. Focus entering clipped content expands the fold.GhostCodeBlock @component:GhostCodeBlock › keyboard: Tab and Shift+Tab visit actions, Copy, the viewport and the fold toggle in order (@ghostvue/vue)
GhostCodeBlock @component:GhostCodeBlock › long line: the viewport is a named tab stop, arrows scroll it, and the page never scrolls sideways at 320px (@ghostvue/vue)
GhostCodeBlock @component:GhostCodeBlock › a link on a clipped line: no viewport tab stop, and tabbing to it expands the fold (@ghostvue/vue)
Shift+TabMoves through the same stops in reverse.GhostCodeBlock @component:GhostCodeBlock › keyboard: Tab and Shift+Tab visit actions, Copy, the viewport and the fold toggle in order (@ghostvue/vue)
EnterCopy button: copies without moving focus. Fold toggle: toggles the fold; focus stays on the toggle.GhostCodeBlock @component:GhostCodeBlock › keyboard: Enter and Space on Copy write inside the key handler @cross-browser (@ghostvue/vue)
GhostCodeBlock @component:GhostCodeBlock › fold: collapsed height is foldLines lines; Enter and Space toggle; lines stay in the DOM (@ghostvue/vue)
GhostCodeBlock @component:GhostCodeBlock › without the Clipboard API: failure, selection, focus stays, and Ctrl+C copies copyText (@ghostvue/vue)
SpaceSame as Enter.GhostCodeBlock @component:GhostCodeBlock › keyboard: Enter and Space on Copy write inside the key handler @cross-browser (@ghostvue/vue)
GhostCodeBlock @component:GhostCodeBlock › fold: collapsed height is foldLines lines; Enter and Space toggle; lines stay in the DOM (@ghostvue/vue)
ArrowLeftNative scrolling of the focused viewport.GhostCodeBlock @component:GhostCodeBlock › long line: the viewport is a named tab stop, arrows scroll it, and the page never scrolls sideways at 320px (@ghostvue/vue)
ArrowRightNative scrolling of the focused viewport.GhostCodeBlock @component:GhostCodeBlock › long line: the viewport is a named tab stop, arrows scroll it, and the page never scrolls sideways at 320px (@ghostvue/vue)
HomeNative scrolling of the focused viewport.GhostCodeBlock @component:GhostCodeBlock › long line: the viewport is a named tab stop, arrows scroll it, and the page never scrolls sideways at 320px (@ghostvue/vue)
EndNative scrolling of the focused viewport.GhostCodeBlock @component:GhostCodeBlock › long line: the viewport is a named tab stop, arrows scroll it, and the page never scrolls sideways at 320px (@ghostvue/vue)
Ctrl+CNative copy of the current selection; after a failed write the code is selected and the copy receives copyText ?? code.GhostCodeBlock @component:GhostCodeBlock › without the Clipboard API: failure, selection, focus stays, and Ctrl+C copies copyText (@ghostvue/vue)

Verification ​

Each category is reviewed separately. Automated keyboard and axe results do not attest manual or assistive-technology review.

CategoryStatusEvidence
Automated keyboardPassedEvery declared key maps to a Chromium browser test that CI runs and requires to pass on every commit.
Manual interactionNot reviewedNo review recorded.
Assistive technologyNot reviewedNo review recorded.

Examples ​

code-block/headless ​

Example source

Headless with consumer CSS Headless

The unstyled GhostCodeBlock from @ghostvue/vue styled only through data-part and data-state hooks.

package.jsonjson
{
  "name": "release-notes",
  "private": true,
  "scripts": {
    "build": "vite build",
    "preview": "vite preview --port 4173 --strictPort --host 127.0.0.1 --open /changelog"
  },
  "dependencies": {
    "@ghostvue/vue": "^0.4.0"
  }
}