|$ curl https://forge-ai.dev/api/markdown?path=docs/components/context-menu
$cat docs/context-menu.md
updated Recently·25 min read·published
Context Menu
Introduction
Context menus appear on right-click or long-press, providing context-specific actions. They must handle positioning (stay within viewport), keyboard access (Shift+F10, Menu key), nested submenus, and focus management.
Smart Positioning
positioning.ts
TypeScript
| 1 | // Keep menu within viewport bounds |
| 2 | function getContextMenuPosition( |
| 3 | x: number, |
| 4 | y: number, |
| 5 | menuWidth: number, |
| 6 | menuHeight: number, |
| 7 | ) { |
| 8 | const padding = 8; |
| 9 | return { |
| 10 | x: Math.min(x, window.innerWidth - menuWidth - padding), |
| 11 | y: Math.min(y, window.innerHeight - menuHeight - padding), |
| 12 | }; |
| 13 | } |
| 14 | |
| 15 | // Keyboard trigger: Shift+F10 or Menu key |
| 16 | // Find target element and show menu at its center |
| 17 | function handleKeyboardTrigger(target: HTMLElement) { |
| 18 | const rect = target.getBoundingClientRect(); |
| 19 | const x = rect.left + rect.width / 2; |
| 20 | const y = rect.top + rect.height / 2; |
| 21 | showContextMenu(x, y); |
| 22 | } |
$Blueprint — Engineering Documentation·Section ID: UI-CM-01·Revision: 1.0