Navigation

Context Menu

A right-click menu on the HTML popover, with a duplicate button: without it the menu is out of reach for keyboard users.

  • context-menu
  • popover
  • right-click
  • a11y

Preview

1440pxHost theme

Use it with AI

  1. 1. Copy the link.
  2. 2. Write to your agent in your own words and drop the link into the sentence.
  3. 3. The agent opens the link and installs the component from the registry.

put this in the header: https://vibeui.ru/c/contextmenu-001?lang=en

Right-click the area — or use the button
Show the full instructions

What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.

# Install and place "contextmenu-001" (Context Menu) from VibeUI

## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/contextmenu-001.json

Registry item: https://vibeui.ru/r/contextmenu-001.json
Installs to: components/vibeui/contextmenu-001.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.

Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.

## 2. What it is
A right-click menu on the HTML popover, with a duplicate button: without it the menu is out of reach for keyboard users.

A right-click menu: opens at the cursor, closes on Escape and outside clicks, the destructive item is coloured. Zero dependencies, one file.

## 3. How to use it
import { Contextmenu001 } from "@/components/vibeui/contextmenu-001"

<Contextmenu001 items={[{ label: "Rename", keys: "F2" }]} />

Read the installed file for the full prop list.

## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.

Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)

## 5. Keep exactly as installed
- the local --vibeui-contextmenu-001-* palette — do not swap it for your theme tokens
- the duplicate button: right-click is unavailable without a mouse
- the popover: Escape and outside clicks come from the browser
- the coordinates in variables — they are known only at event time
- colouring the destructive item and keeping it last
- preventDefault on contextmenu only inside your own area, never page-wide

## 6. You may change
- the items array and their handlers
- hint — the area's caption
- the accent through the accent prop
- the area size

## 7. Rules
- Hijacking right-click takes away the system menu: do it only where your menu is more useful.
- Near a screen edge the menu can overflow: clamp the coordinates for your layout.
- The component does not handle arrow keys between items — Tab moves between them.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.

## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.

For developers

npx shadcn@latest add https://vibeui.ru/r/contextmenu-001.json
https://vibeui.ru/r/contextmenu-001.json

Компонент самодостаточен: один файл, без зависимостей, собственная палитра в локальных переменных --vibeui-contextmenu-001-*. Клиентский: "use client" ради координат курсора. Меню — popover, поэтому закрытие по Escape и клику мимо достаётся от браузера, а положение подставляется переменными --vibeui-contextmenu-001-x/y. Кнопка «Действия» открывает то же меню без мыши.

Component source

The same file your agent installs. Here in case you would rather copy it by hand.

"use client"

import { useRef, useState } from "react"
import type { ComponentPropsWithoutRef, CSSProperties, MouseEvent } from "react"

export type Contextmenu001Item = {
  label: string
  keys?: string
  danger?: boolean
}

export type Contextmenu001Props = Omit<
  ComponentPropsWithoutRef<"div">,
  "children"
> & {
  items?: Contextmenu001Item[]
  hint?: string
  accent?: string
}

// Идея компонента: меню по правому клику. Само меню — HTML popover, поэтому
// закрытие по Escape и клику мимо достаётся от браузера, а координаты курсора
// подставляются переменными: они известны только в момент события. Меню
// доступно и с клавиатуры: у области есть кнопка-дублёр, иначе правый клик
// остаётся единственным способом, а он не работает без мыши.
const STYLES = `
:where([data-vibeui-block="contextmenu-001"]){
--vibeui-contextmenu-001-bg:oklch(1 0 0);
--vibeui-contextmenu-001-fg:oklch(0.24 0.014 265);
--vibeui-contextmenu-001-muted:oklch(0.56 0.014 265);
--vibeui-contextmenu-001-border:oklch(0.9 0.006 265);
--vibeui-contextmenu-001-hover:oklch(0.55 0.02 265 / 9%);
--vibeui-contextmenu-001-accent:oklch(0.55 0.2 262);
--vibeui-contextmenu-001-danger:oklch(0.56 0.19 25);
--vibeui-contextmenu-001-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
--vibeui-contextmenu-001-x:50%;
--vibeui-contextmenu-001-y:50%;
}
[data-vibeui-block="contextmenu-001"]{
width:100%;max-width:22rem;box-sizing:border-box;
font-family:var(--vibeui-contextmenu-001-font);color:var(--vibeui-contextmenu-001-fg);
}
[data-vibeui-block="contextmenu-001"] [data-part="area"]{
display:flex;flex-direction:column;align-items:center;justify-content:center;gap:0.5rem;
min-height:8rem;padding:1rem;box-sizing:border-box;
background:var(--vibeui-contextmenu-001-bg);
border:1px dashed var(--vibeui-contextmenu-001-border);border-radius:0.875rem;
font-size:0.8125rem;color:var(--vibeui-contextmenu-001-muted);text-align:center;
}
[data-vibeui-block="contextmenu-001"] [data-part="fallback"]{
appearance:none;cursor:pointer;
height:1.875rem;padding:0 0.75rem;border-radius:0.5rem;
border:1px solid var(--vibeui-contextmenu-001-border);
background:none;color:var(--vibeui-contextmenu-001-fg);font:inherit;font-size:0.75rem;
}
[data-vibeui-block="contextmenu-001"] [data-part="fallback"]:focus-visible{outline:2px solid var(--vibeui-contextmenu-001-accent);outline-offset:2px}
/* Координаты курсора известны только в момент клика — отсюда переменные. */
[data-vibeui-block="contextmenu-001"] [data-part="menu"]{
position:fixed;margin:0;padding:0.25rem;
top:var(--vibeui-contextmenu-001-y);left:var(--vibeui-contextmenu-001-x);
min-width:11rem;box-sizing:border-box;
background:var(--vibeui-contextmenu-001-bg);color:var(--vibeui-contextmenu-001-fg);
border:1px solid var(--vibeui-contextmenu-001-border);border-radius:0.625rem;
box-shadow:0 16px 36px -18px oklch(0.2 0.03 265 / 45%);
font-family:var(--vibeui-contextmenu-001-font);
}
[data-vibeui-block="contextmenu-001"] [data-part="item"]{
display:flex;align-items:center;justify-content:space-between;gap:1.5rem;
width:100%;min-height:1.875rem;padding:0 0.5rem;box-sizing:border-box;
appearance:none;border:0;background:none;cursor:pointer;border-radius:0.4375rem;
font:inherit;font-size:0.8125rem;color:inherit;text-align:left;
}
[data-vibeui-block="contextmenu-001"] [data-part="item"]:hover{background:var(--vibeui-contextmenu-001-hover)}
[data-vibeui-block="contextmenu-001"] [data-part="item"]:focus-visible{outline:2px solid var(--vibeui-contextmenu-001-accent);outline-offset:-2px}
[data-vibeui-block="contextmenu-001"] [data-part="item"][data-danger="true"]{color:var(--vibeui-contextmenu-001-danger)}
[data-vibeui-block="contextmenu-001"] [data-part="keys"]{font-size:0.75rem;color:var(--vibeui-contextmenu-001-muted)}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="contextmenu-001"] *{animation:none!important;transition:none!important}}
`

const DEFAULT_ITEMS: Contextmenu001Item[] = [
  { label: "Открыть", keys: "↵" },
  { label: "Переименовать", keys: "F2" },
  { label: "Дублировать", keys: "⌘D" },
  { label: "Удалить", keys: "⌫", danger: true },
]

/**
 * Меню по правому клику на HTML popover, с кнопкой-дублёром для клавиатуры.
 * Один файл, ноль зависимостей, собственная палитра.
 */
export function Contextmenu001({
  items = DEFAULT_ITEMS,
  hint = "Правый клик по области — или кнопка ниже",
  accent,
  className,
  style,
  ...props
}: Contextmenu001Props) {
  const menu = useRef<HTMLDivElement>(null)
  const [spot, setSpot] = useState<{ x: string; y: string } | null>(null)

  const openAt = (x: number, y: number) => {
    setSpot({ x: `${Math.round(x)}px`, y: `${Math.round(y)}px` })
    menu.current?.showPopover()
  }

  const onContextMenu = (event: MouseEvent<HTMLDivElement>) => {
    event.preventDefault()
    openAt(event.clientX, event.clientY)
  }

  const palette = {
    ...(accent ? { "--vibeui-contextmenu-001-accent": accent } : null),
    ...(spot
      ? {
          "--vibeui-contextmenu-001-x": spot.x,
          "--vibeui-contextmenu-001-y": spot.y,
        }
      : null),
    ...style,
  } as CSSProperties

  return (
    <>
      <style href="vibeui-contextmenu-001" precedence="medium">
        {STYLES}
      </style>
      <div
        {...props}
        data-vibeui-block="contextmenu-001"
        className={className}
        style={palette}
      >
        <div data-part="area" onContextMenu={onContextMenu}>
          {hint}
          <button
            type="button"
            data-part="fallback"
            onClick={(event) => {
              const box = event.currentTarget.getBoundingClientRect()
              openAt(box.left, box.bottom + 6)
            }}
          >
            Действия
          </button>
        </div>
        <div data-part="menu" popover="auto" role="menu" ref={menu}>
          {items.map((item) => (
            <button
              key={item.label}
              type="button"
              data-part="item"
              data-danger={item.danger}
              role="menuitem"
              onClick={() => menu.current?.hidePopover()}
            >
              {item.label}
              {item.keys ? <span data-part="keys">{item.keys}</span> : null}
            </button>
          ))}
        </div>
      </div>
    </>
  )
}