GhostContextMenu
GhostContextMenu implements the WAI-ARIA APG context menu pattern for GHO-92: right-click, long-press, and Shift+F10 / ContextMenu keyboard shortcuts open a portaled role="menu" at the pointer via a virtual useFloating anchor.
Import the parts from @ghostvue/themes for the default elevated menu surface (the same item tokens as GhostDropdownMenu). To style the menu yourself, see Advanced: headless composition.
Act on a file
Right-click a file row or press Shift+F10 to duplicate, pin, open, or change row density, with each outcome announced.
<script setup lang="ts">
import { ref } from 'vue'
import {
GhostBadge,
GhostCard,
GhostContextMenu,
GhostContextMenuCheckboxItem,
GhostContextMenuContent,
GhostContextMenuItem,
GhostContextMenuLabel,
GhostContextMenuRadioGroup,
GhostContextMenuRadioItem,
GhostContextMenuSeparator,
GhostContextMenuTrigger,
GhostSubmenu,
GhostSubmenuContent,
GhostSubmenuTrigger,
} from '@ghostvue/themes'
interface ProjectFile {
id: string
name: string
kind: string
updated: string
pinned: boolean
}
const files = ref<ProjectFile[]>([
{
id: 'launch-brief',
name: 'Q3 launch brief.md',
kind: 'Markdown · 14 KB',
updated: 'Updated 2h ago',
pinned: true,
},
{
id: 'token-audit',
name: 'Token audit.csv',
kind: 'Spreadsheet · 48 KB',
updated: 'Updated yesterday',
pinned: false,
},
{
id: 'release-checklist',
name: 'Release checklist.md',
kind: 'Markdown · 8 KB',
updated: 'Updated Nov 12',
pinned: false,
},
])
const density = ref<'compact' | 'comfortable'>('comfortable')
const statusMessage = ref(
'Right-click any file row or press Shift+F10 for actions.',
)
function openFile(file: ProjectFile, app: string): void {
statusMessage.value = `Opened "${file.name}" in ${app}.`
}
// Name the copy after its source ("Token audit copy.csv", then "copy 2"),
// skipping names already in the list so every row stays distinct.
function copyName(name: string): string {
const dot = name.lastIndexOf('.')
const base = dot > 0 ? name.slice(0, dot) : name
const extension = dot > 0 ? name.slice(dot) : ''
const taken = new Set(files.value.map((file) => file.name))
let candidate = `${base} copy${extension}`
for (let count = 2; taken.has(candidate); count += 1) {
candidate = `${base} copy ${String(count)}${extension}`
}
return candidate
}
let copies = 0
// The copy lands right below its source. Focus still returns to the row that
// opened the menu, so the next Shift+F10 acts on the same file.
function duplicateFile(file: ProjectFile): void {
copies += 1
const copy: ProjectFile = {
...file,
id: `${file.id}-copy-${String(copies)}`,
name: copyName(file.name),
updated: 'Updated just now',
pinned: false,
}
files.value.splice(files.value.indexOf(file) + 1, 0, copy)
statusMessage.value = `Duplicated "${file.name}" as "${copy.name}".`
}
function setPinned(file: ProjectFile, pinned: boolean): void {
file.pinned = pinned
statusMessage.value = pinned
? `Pinned "${file.name}".`
: `Unpinned "${file.name}".`
}
function setDensity(value: string): void {
if (value === 'compact' || value === 'comfortable') {
density.value = value
statusMessage.value = `Row density set to ${value}.`
}
}
</script>
<template>
<GhostCard class="file-browser" :data-density="density">
<template #header>
<div class="file-browser__header">
<span class="file-browser__title">Project files</span>
<GhostBadge tone="neutral" variant="soft">
{{ files.filter((file) => file.pinned).length }} pinned
</GhostBadge>
</div>
</template>
<ul class="file-browser__list" aria-label="Project files">
<li v-for="file in files" :key="file.id" class="file-browser__item">
<GhostContextMenu>
<GhostContextMenuTrigger
class="file-browser__row"
data-testid="context-themed-trigger"
tabindex="0"
>
<span class="file-browser__name">
<span>{{ file.name }}</span>
<GhostBadge v-if="file.pinned" tone="accent" variant="soft">
Pinned
</GhostBadge>
</span>
<span class="file-browser__meta">
{{ file.kind }} · {{ file.updated }}
</span>
</GhostContextMenuTrigger>
<GhostContextMenuContent
:aria-label="`Actions for ${file.name}`"
data-testid="context-themed-content"
>
<GhostContextMenuLabel>{{ file.name }}</GhostContextMenuLabel>
<GhostContextMenuSeparator decorative />
<GhostSubmenu>
<GhostSubmenuTrigger>Open with</GhostSubmenuTrigger>
<GhostSubmenuContent>
<GhostContextMenuItem @select="openFile(file, 'Preview')">
Preview
</GhostContextMenuItem>
<GhostContextMenuItem @select="openFile(file, 'Editor')">
Editor
</GhostContextMenuItem>
</GhostSubmenuContent>
</GhostSubmenu>
<GhostContextMenuItem @select="duplicateFile(file)">
<span class="context-menu__indicator" aria-hidden="true" />
Duplicate
</GhostContextMenuItem>
<GhostContextMenuSeparator decorative />
<GhostContextMenuCheckboxItem
:checked="file.pinned"
@update:checked="setPinned(file, $event)"
>
<span class="context-menu__indicator" aria-hidden="true">
{{ file.pinned ? '✓' : '' }}
</span>
Pin to top
</GhostContextMenuCheckboxItem>
<GhostContextMenuSeparator decorative />
<GhostSubmenu>
<GhostSubmenuTrigger>View</GhostSubmenuTrigger>
<GhostSubmenuContent portal>
<GhostSubmenu>
<GhostSubmenuTrigger>Row density</GhostSubmenuTrigger>
<GhostSubmenuContent>
<GhostContextMenuRadioGroup
:model-value="density"
@update:model-value="setDensity"
>
<GhostContextMenuRadioItem value="compact">
<span
class="context-menu__indicator"
aria-hidden="true"
>
{{ density === 'compact' ? '●' : '' }}
</span>
Compact
</GhostContextMenuRadioItem>
<GhostContextMenuRadioItem value="comfortable">
<span
class="context-menu__indicator"
aria-hidden="true"
>
{{ density === 'comfortable' ? '●' : '' }}
</span>
Comfortable
</GhostContextMenuRadioItem>
</GhostContextMenuRadioGroup>
</GhostSubmenuContent>
</GhostSubmenu>
</GhostSubmenuContent>
</GhostSubmenu>
</GhostContextMenuContent>
</GhostContextMenu>
</li>
</ul>
<template #footer>
<div class="file-browser__status" role="status" aria-live="polite">
{{ statusMessage }}
</div>
</template>
</GhostCard>
</template>
<style scoped>
.file-browser {
inline-size: 100%;
max-inline-size: 34rem;
}
.file-browser__header {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm);
justify-content: space-between;
}
.file-browser__title {
color: var(--ghost-surface-fg);
font-size: var(--ghost-type-size-md);
font-weight: var(--ghost-type-weight-semibold);
}
.file-browser__list {
display: grid;
gap: var(--ghost-space-gap-xs);
list-style: none;
margin: 0;
padding: 0;
}
.file-browser__item {
margin: 0;
}
.file-browser__row {
align-items: center;
background-color: var(--ghost-surface-subtle-bg);
border: var(--ghost-border-hairline-width) solid var(--ghost-border-subtle);
border-radius: var(--ghost-radius-md);
color: var(--ghost-surface-fg);
cursor: context-menu;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-xs) var(--ghost-space-gap-md);
justify-content: space-between;
padding: var(--ghost-space-inset-sm) var(--ghost-space-inset-md);
}
.file-browser[data-density='compact'] .file-browser__row {
padding: var(--ghost-space-inset-xs) var(--ghost-space-inset-sm);
}
.file-browser__row:hover {
background-color: var(--ghost-elevation-2-bg);
}
.file-browser__row:focus-visible {
outline: var(--ghost-focus-ring-width) solid var(--ghost-focus-ring-color);
outline-offset: var(--ghost-focus-ring-offset);
}
.file-browser__name {
align-items: center;
display: inline-flex;
flex-wrap: wrap;
font-size: var(--ghost-type-size-sm);
font-weight: var(--ghost-type-weight-medium);
gap: var(--ghost-space-gap-xs);
}
.file-browser__meta {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-xs);
}
.file-browser__status {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-xs);
}
.context-menu__indicator {
display: inline-block;
inline-size: 1em;
text-align: center;
}
</style>
Every part in this example comes from @ghostvue/themes. Right-click a file row, or press Tab to focus it and press Shift+F10 (or the ContextMenu key). The menu opens at the row with focus on its first item. Move to Duplicate with the arrow keys and press Enter: a copy appears below the file, the role="status" footer names it, and focus returns to the row that opened the menu. Pin to top toggles without closing the menu and updates the pinned badge. The themed GhostContextMenuContent sets data-themed so portaled panels receive elevation-3 tokens outside a .ghost-theme host.
GhostContextMenuTrigger renders a <span> from both @ghostvue/themes and @ghostvue/vue. It is a context target, not a menu button, so the theme adds no trigger styles; the row's own class gives it layout. Pass as to render another element, such as as="div" around a block of content.
Checkbox item model
GhostContextMenuCheckboxItem binds its state with v-model:checked or plain v-model. Both names stay in sync; if you bind both, checked decides what the item shows.
Nested commands
Choose Open with → Preview to act on the invoking file, or View → Row density → Compact to change the list. Enter, Space, and the forward arrow open a child. The back arrow and Escape return to its trigger. Escape at the root returns to the file row. Tab and Shift+Tab continue from that row even when a child is portaled. Forward is Right in LTR and Left in RTL.
Use GhostContextMenuItem, checkbox items, or radio items inside GhostSubmenuContent. The shared parts also support controlled open state and custom portal targets. Shared submenu API
Menus on list rows
When the rows are items of a composite widget whose items hold focus, such as GhostListbox, set focus-target="descendant" on the trigger. The trigger becomes a passive wrapper with no tab stop and no aria-haspopup. The menu opens for the option under the pointer, or for the focused option on Shift+F10 and the ContextMenu key, and focus returns to that option on close. A listbox selects the option first when it was not selected, so menu items act on the selection. If the action removes the option, focus moves to the list's next option. Keep every menu action available in a visible control outside the list as well. See the mail list.
<GhostContextMenu>
<GhostContextMenuTrigger as="div" focus-target="descendant">
<GhostListbox v-model="selected" aria-label="Inbox">…</GhostListbox>
</GhostContextMenuTrigger>
<GhostContextMenuContent>…</GhostContextMenuContent>
</GhostContextMenu>Advanced: headless composition
Teams that bring their own design system can import the same parts from @ghostvue/vue. They keep the menu behavior and paint nothing, so the example brings its own consumer CSS for the panel and item states.
Basic use
Headless context menu on a row target with items, checkbox, radio group, label, separator, and disabled item.
<script setup lang="ts">
import { ref } from 'vue'
import {
GhostContextMenu,
GhostContextMenuCheckboxItem,
GhostContextMenuContent,
GhostContextMenuItem,
GhostContextMenuLabel,
GhostContextMenuRadioGroup,
GhostContextMenuRadioItem,
GhostContextMenuSeparator,
GhostContextMenuTrigger,
} from '@ghostvue/vue'
const showHidden = ref(false)
const sort = ref('name')
</script>
<template>
<GhostContextMenu>
<GhostContextMenuTrigger
data-testid="context-basic-trigger"
class="context-menu-e2e-target"
>
Right-click this row for actions
</GhostContextMenuTrigger>
<GhostContextMenuContent
class="row-actions-menu"
data-testid="context-basic-content"
:offset="4"
>
<GhostContextMenuLabel>Row actions</GhostContextMenuLabel>
<GhostContextMenuSeparator decorative />
<GhostContextMenuItem data-testid="context-basic-item-edit">
Edit
</GhostContextMenuItem>
<GhostContextMenuItem disabled data-testid="context-basic-item-disabled">
Archive (disabled)
</GhostContextMenuItem>
<GhostContextMenuSeparator decorative />
<GhostContextMenuCheckboxItem
v-model:checked="showHidden"
data-testid="context-checkbox-item"
>
Show hidden rows
</GhostContextMenuCheckboxItem>
<GhostContextMenuSeparator decorative />
<GhostContextMenuRadioGroup v-model="sort">
<GhostContextMenuRadioItem
value="name"
data-testid="context-radio-name"
>
Sort by name
</GhostContextMenuRadioItem>
<GhostContextMenuRadioItem
value="date"
data-testid="context-radio-date"
>
Sort by date
</GhostContextMenuRadioItem>
</GhostContextMenuRadioGroup>
</GhostContextMenuContent>
</GhostContextMenu>
</template>
<style scoped>
.context-menu-e2e-target {
display: block;
min-height: 12rem;
width: 100%;
}
</style>
<style>
/*
* Headless GhostContextMenu ships behavior and positioning only, so this
* sample brings its own consumer CSS. It is unscoped on purpose so the block
* can be copied into an app stylesheet as is, and the prefix keeps the
* classes from colliding with page styles.
*
* Colors read GhostVue semantic tokens when a theme is loaded and fall back
* to CSS system colors, which follow light and dark on their own. Items keep
* fixed geometry: highlight, hover, checked and disabled states only change
* color or a mark's visibility, so the menu never shifts. The highlight
* follows focus, which roves through the items. Nothing animates, so reduced
* motion needs no override.
*/
/* An opaque, layered panel above page content. */
.row-actions-menu {
background-color: var(--ghost-elevation-3-bg, Canvas);
border: 1px solid var(--ghost-elevation-3-border, GrayText);
border-radius: var(--ghost-radius-md, 0.375rem);
box-shadow: var(--ghost-elevation-3-shadow, 0 0.5rem 1.5rem rgb(0 0 0 / 20%));
box-sizing: border-box;
color: var(--ghost-surface-fg, CanvasText);
max-block-size: min(18rem, calc(100dvh - 2rem));
max-inline-size: calc(100vw - 2rem);
min-inline-size: 11rem;
overflow: auto;
padding: 0.25rem;
z-index: 50;
}
.row-actions-menu
:is([role='menuitem'], [role='menuitemcheckbox'], [role='menuitemradio']) {
border-radius: var(--ghost-radius-sm, 0.25rem);
cursor: default;
/* A transparent outline turns into a visible ring under forced colors. */
outline: 2px solid transparent;
outline-offset: -2px;
padding: 0.375rem 0.75rem 0.375rem 1.75rem;
position: relative;
}
/* Check mark slot, shown only on the checked row. */
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)::before {
block-size: 0.55rem;
border-block-end: 2px solid currentColor;
border-inline-end: 2px solid currentColor;
content: '';
inline-size: 0.3rem;
inset-block-start: 50%;
inset-inline-start: 0.7rem;
position: absolute;
transform: translateY(-65%) rotate(45deg);
visibility: hidden;
}
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)[aria-checked='true'] {
font-weight: 600;
}
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)[aria-checked='true']::before {
visibility: visible;
}
/* Radio rows show a dot instead of a check. */
.row-actions-menu [role='menuitemradio']::before {
block-size: 0;
border: 0.2rem solid currentColor;
border-radius: 50%;
inline-size: 0;
transform: translateY(-50%);
}
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
):hover:not(
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)[aria-disabled='true']
) {
background-color: var(--ghost-surface-muted-bg, Canvas);
}
/* Keyboard highlight. */
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
):focus {
background-color: var(--ghost-action-primary-bg, Highlight);
color: var(--ghost-action-primary-fg, HighlightText);
}
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)[aria-disabled='true'] {
color: var(--ghost-text-muted, GrayText);
cursor: not-allowed;
}
.row-actions-menu [data-part='label'] {
color: var(--ghost-text-muted, GrayText);
font-size: 0.8125rem;
padding: 0.375rem 0.75rem 0.25rem 1.75rem;
}
.row-actions-menu [data-part='separator'] {
border-block-start: 1px solid var(--ghost-border-default, GrayText);
margin-block: 0.25rem;
}
@media (forced-colors: active) {
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
):focus {
background-color: Highlight;
color: HighlightText;
forced-color-adjust: none;
}
.row-actions-menu
:is(
[role='menuitem'],
[role='menuitemcheckbox'],
[role='menuitemradio']
)[aria-disabled='true'] {
color: GrayText;
}
}
</style>
Right-click or focus the target and press Shift+F10 to open. Item keyboard behavior comes from useGhostMenuCollection (shared with GhostDropdownMenu). The native contextmenu event is suppressed only when this menu actually opens.
API
See the generated component references for props, emits, slots, and styling hooks.
| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | false | Declared prop defaultOpen. |
open | boolean | — | Declared prop open. |
| Slot | Slot props | Description |
|---|---|---|
default | close: () => void, open: boolean | Container content. |
@ghostvue/themes adds no props, events, or slots to the headless GhostContextMenu API, which it forwards.
Specification
See GhostContextMenu specification and ADR 0011.