Context menu

A right-click menu. Fill it with the dropdown menu's items.

Install

If this is the first component in the project, set up once. This writes ermine.json and copies the theme stylesheet. The installation guide covers Vue, Nuxt and Astro.

npx ermineui init
pnpm dlx ermineui init
yarn dlx ermineui init
bunx ermineui init

Then add the component:

npx ermineui add context-menu
pnpm dlx ermineui add context-menu
yarn dlx ermineui add context-menu
bunx ermineui add context-menu

That copies 5 files into src/components/ui/context-menu/, 2 helpers into src/lib/ermine/, and the 1 component it is built from. Files you already have are kept.

Use

import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from '@/components/ui/context-menu'
<ContextMenu>
  <ContextMenuTrigger>Right-click here</ContextMenuTrigger>
  <ContextMenuContent>
    <DropdownMenuItem @select="rename">Rename</DropdownMenuItem>
  </ContextMenuContent>
</ContextMenu>

In Nuxt, with the auto-import setting, the tags work in templates without the import. In Astro, use it inside a Vue island.

What gets copied

FileWhere it goes
ContextMenu.vuesrc/components/ui/context-menu/ContextMenu.vue
ContextMenuContent.vuesrc/components/ui/context-menu/ContextMenuContent.vue
ContextMenuTrigger.vuesrc/components/ui/context-menu/ContextMenuTrigger.vue
context.tssrc/components/ui/context-menu/context.ts
index.tssrc/components/ui/context-menu/index.ts
context.ts helpersrc/lib/ermine/context.ts
primitive.ts helpersrc/lib/ermine/primitive.ts

It is built from: Dropdown menu. These are added with it.

The folders are the suggested ones. ermine.json says where yours are.

Source

This is exactly what the command copies. To install by hand, create each file below and the helpers listed above.

ContextMenu.vue 53 lines
<script setup lang="ts">
import { nextTick, ref, useId } from 'vue'
import { Portal } from '../../lib/primitive'
import { provideDropdownMenu } from '../dropdown-menu/context'
import { provideContextMenu } from './context'

const open = defineModel<boolean>('open', { default: false })

/** An invisible point placed where the pointer was. The menu positions itself against it like any trigger. */
const anchor = ref<HTMLElement | null>(null)
const point = ref({ x: 0, y: 0 })
const focusFirst = ref(false)

function setOpen(next: boolean) {
  open.value = next
}

// The same state the dropdown menu's parts expect, so its items, groups and submenus all work inside.
provideDropdownMenu({ open, setOpen, anchor, focusFirst, triggerId: useId(), contentId: useId() })

provideContextMenu({
  openAt: async (x, y, fromKeyboard) => {
    // A second right-click while open moves the menu: close first so it reopens at the new point.
    if (open.value) {
      open.value = false
      await nextTick()
    }
    point.value = { x, y }
    focusFirst.value = fromKeyboard
    await nextTick()
    open.value = true
  },
})
</script>

<template>
  <slot :open="open" />
  <Portal>
    <span ref="anchor" class="er-context-menu-point" aria-hidden="true" :style="{ left: `${point.x}px`, top: `${point.y}px` }" />
  </Portal>
</template>

<style>
@layer components {
  .er-context-menu-point {
    position: fixed;
    width: 0;
    height: 0;
    pointer-events: none;
  }
}
</style>
ContextMenuContent.vue 11 lines
<script setup lang="ts">
import { DropdownMenuContent } from '../dropdown-menu'
</script>

<template>
  <!-- The dropdown menu's own panel, opening down and to the right of the pointer. -->
  <DropdownMenuContent side="bottom" align="start" :side-offset="2">
    <slot />
  </DropdownMenuContent>
</template>
ContextMenuTrigger.vue 37 lines
<script setup lang="ts">
import type { Component } from 'vue'
import { Primitive } from '../../lib/primitive'
import { useContextMenu } from './context'

const props = withDefaults(
  defineProps<{
    as?: string | Component
    /** Use the single child as the right-click area instead of wrapping it in an element. */
    asChild?: boolean
    disabled?: boolean
  }>(),
  { as: 'div' },
)

const menu = useContextMenu()

function onContextMenu(event: MouseEvent) {
  if (props.disabled) return
  event.preventDefault()
  // The Menu key and Shift+F10 also fire this event, without a real pointer position.
  const fromKeyboard = event.button !== 2 && event.detail === 0
  if (fromKeyboard) {
    const box = (event.currentTarget as HTMLElement).getBoundingClientRect()
    menu.openAt(box.left + Math.min(box.width / 2, 24), box.top + Math.min(box.height / 2, 24), true)
  } else {
    menu.openAt(event.clientX, event.clientY, false)
  }
}
</script>

<template>
  <Primitive :as="as" :as-child="asChild" @contextmenu="onContextMenu">
    <slot />
  </Primitive>
</template>
context.ts 9 lines
import { createContext } from '../../lib/context'

export interface ContextMenuContext {
  /** Open the menu at a point in the window. `fromKeyboard` puts focus on the first item. */
  openAt: (x: number, y: number, fromKeyboard: boolean) => void
}

export const [provideContextMenu, useContextMenu] = createContext<ContextMenuContext>('ContextMenu')
index.ts 5 lines
/** Context menu — a right-click menu. Fill it with the dropdown menu's items. */
export { default as ContextMenu } from './ContextMenu.vue'
export { default as ContextMenuContent } from './ContextMenuContent.vue'
export { default as ContextMenuTrigger } from './ContextMenuTrigger.vue'