Skip to main content
196

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Web docs
Overlays and menus

useContextMenu

Binds a trigger to a context menu that opens on right-click or Shift+F10 and supports Escape and roving item focus.

Live component preview · Astro

Explore ContextMenu

Try the component this hook connects to. The preview uses the Astro adapter; the React example below uses useContextMenu.

Right-click (or press Shift+F10) inside this area

React usage

Import from @santi020k/lumen-react and spread the controller props onto the matching components.

tsxuseContextMenu example
import { ContextMenu, useContextMenu } from '@santi020k/lumen-react'

export function ProjectActions() {
  const menu = useContextMenu({ id: 'project-actions' })

  return (
    <>
      <button {...menu.triggerProps}>Project</button>
      <ContextMenu {...menu.menuProps}>
        <button role="menuitem">Duplicate</button>
        <button role="menuitem">Delete</button>
      </ContextMenu>
    </>
  )
}
import { ContextMenu, useContextMenu } from '@santi020k/lumen-react'

export function ProjectActions() {
  const menu = useContextMenu({ id: 'project-actions' })

  return (
    <>
      <button {...menu.triggerProps}>Project</button>
      <ContextMenu {...menu.menuProps}>
        <button role="menuitem">Duplicate</button>
        <button role="menuitem">Delete</button>
      </ContextMenu>
    </>
  )
}

Load @santi020k/lumen-react/styles.css once in your application. With React Server Components, put 'use client' at the top of the module that calls the hook. Follow the React setup guide for the framework boundary.

Options

PropertyTypeDefaultDescription
defaultOpenbooleanfalseInitial open state.
openboolean-Controlled open state.
onOpenChange(open: boolean) => void-Callback fired when the context menu opens or closes.
idstringautoExplicit id for the menu.

Controller return values

PropertyTypeDescription
triggerPropsLumenProps<"button">Spread onto the trigger. Handles contextmenu and Shift+F10.
menuPropsLumenProps<"menu">Spread onto ContextMenu. Includes data-ui-context-menu, role, hidden state, and keyboard handlers.
triggerRefRefObject<HTMLElement>Ref to the trigger.
menuRefRefObject<HTMLElement>Ref to the menu.
openbooleanCurrent open state.
openAt(x: number, y: number) => voidOpen the menu at viewport coordinates.
close() => voidClose the menu.
Web docsFull catalog