torch-glare 2.1.7 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,458 @@
1
+ ---
2
+ title: ContextMenu
3
+ description: Right-click (or long-press) menu that opens at the pointer, with submenus, checkboxes, radio groups, and keyboard navigation
4
+ group: Overlays & Dialogs
5
+ keywords: [context-menu, right-click, menu, radix-ui, submenu, checkbox, contextmenu]
6
+ ---
7
+
8
+ # ContextMenu
9
+
10
+ > A right-click / long-press menu that opens at the pointer. Wrap any zone in a `ContextMenuTrigger` and the menu appears where the user clicks — same surface as DropdownMenu (items, groups, the boxed look, auto-grouping), built on `@radix-ui/react-context-menu`.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @radix-ui/react-context-menu
16
+ ```
17
+
18
+ ## Import
19
+
20
+ ```typescript
21
+ import {
22
+ ContextMenu,
23
+ ContextMenuTrigger,
24
+ ContextMenuContent,
25
+ ContextMenuItem,
26
+ ContextMenuGroup,
27
+ ContextMenuRadioGroup,
28
+ ContextMenuCheckboxItem,
29
+ ContextMenuRadioItem,
30
+ ContextMenuLabel,
31
+ ContextMenuShortcut,
32
+ ContextMenuSub,
33
+ ContextMenuSubTrigger,
34
+ ContextMenuSubContent,
35
+ ContextMenuPortal,
36
+ } from '@torch-ui/components'
37
+ ```
38
+
39
+ ## Quick Examples
40
+
41
+ ### Basic Menu
42
+
43
+ Wrap the right-click zone in `ContextMenuTrigger`. The menu opens at the pointer.
44
+
45
+ ```typescript
46
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem } from '@torch-ui/components'
47
+
48
+ function Example() {
49
+ return (
50
+ <ContextMenu>
51
+ <ContextMenuTrigger asChild>
52
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
53
+ Right-click here
54
+ </div>
55
+ </ContextMenuTrigger>
56
+ <ContextMenuContent>
57
+ <ContextMenuItem>Profile</ContextMenuItem>
58
+ <ContextMenuItem>Settings</ContextMenuItem>
59
+ <ContextMenuItem>Logout</ContextMenuItem>
60
+ </ContextMenuContent>
61
+ </ContextMenu>
62
+ )
63
+ }
64
+ ```
65
+
66
+ ### With Icons, Shortcuts, and a Negative Item
67
+
68
+ ```typescript
69
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem, ContextMenuShortcut } from '@torch-ui/components'
70
+
71
+ function ActionsMenu() {
72
+ return (
73
+ <ContextMenu>
74
+ <ContextMenuTrigger asChild>
75
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
76
+ Right-click the canvas
77
+ </div>
78
+ </ContextMenuTrigger>
79
+ <ContextMenuContent>
80
+ <ContextMenuItem>
81
+ <i className="ri-edit-line" />
82
+ <span>Edit</span>
83
+ <ContextMenuShortcut>⌘E</ContextMenuShortcut>
84
+ </ContextMenuItem>
85
+ <ContextMenuItem>
86
+ <i className="ri-share-line" />
87
+ <span>Share</span>
88
+ <ContextMenuShortcut>⌘⇧S</ContextMenuShortcut>
89
+ </ContextMenuItem>
90
+ <ContextMenuItem variant="Negative">
91
+ <i className="ri-delete-bin-line" />
92
+ <span>Delete</span>
93
+ <ContextMenuShortcut>⌫</ContextMenuShortcut>
94
+ </ContextMenuItem>
95
+ </ContextMenuContent>
96
+ </ContextMenu>
97
+ )
98
+ }
99
+ ```
100
+
101
+ ### With Checkboxes
102
+
103
+ > Clicking a checkbox item keeps the menu open — `onSelect` calls `preventDefault()` internally so Radix does not auto-close. Toggle several options without the menu dismissing.
104
+
105
+ ```typescript
106
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuCheckboxItem } from '@torch-ui/components'
107
+ import { useState } from 'react'
108
+
109
+ function CheckboxMenu() {
110
+ const [showStatusBar, setShowStatusBar] = useState(true)
111
+ const [showActivityBar, setShowActivityBar] = useState(false)
112
+ const [showPanel, setShowPanel] = useState(false)
113
+
114
+ return (
115
+ <ContextMenu>
116
+ <ContextMenuTrigger asChild>
117
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
118
+ Right-click to toggle view
119
+ </div>
120
+ </ContextMenuTrigger>
121
+ <ContextMenuContent>
122
+ <ContextMenuCheckboxItem
123
+ checked={showStatusBar}
124
+ onCheckedChange={setShowStatusBar}
125
+ >
126
+ Status Bar
127
+ </ContextMenuCheckboxItem>
128
+ <ContextMenuCheckboxItem
129
+ checked={showActivityBar}
130
+ onCheckedChange={setShowActivityBar}
131
+ >
132
+ Activity Bar
133
+ </ContextMenuCheckboxItem>
134
+ <ContextMenuCheckboxItem
135
+ checked={showPanel}
136
+ onCheckedChange={setShowPanel}
137
+ >
138
+ Panel
139
+ </ContextMenuCheckboxItem>
140
+ </ContextMenuContent>
141
+ </ContextMenu>
142
+ )
143
+ }
144
+ ```
145
+
146
+ ### With Radio Group
147
+
148
+ > Like checkboxes, selecting a radio item keeps the menu open (`onSelect` `preventDefault` is built in).
149
+
150
+ ```typescript
151
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuRadioGroup, ContextMenuRadioItem } from '@torch-ui/components'
152
+ import { useState } from 'react'
153
+
154
+ function RadioMenu() {
155
+ const [position, setPosition] = useState('bottom')
156
+
157
+ return (
158
+ <ContextMenu>
159
+ <ContextMenuTrigger asChild>
160
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
161
+ Right-click to pick a position
162
+ </div>
163
+ </ContextMenuTrigger>
164
+ <ContextMenuContent>
165
+ <ContextMenuRadioGroup value={position} onValueChange={setPosition}>
166
+ <ContextMenuRadioItem value="top">Top</ContextMenuRadioItem>
167
+ <ContextMenuRadioItem value="bottom">Bottom</ContextMenuRadioItem>
168
+ <ContextMenuRadioItem value="right">Right</ContextMenuRadioItem>
169
+ </ContextMenuRadioGroup>
170
+ </ContextMenuContent>
171
+ </ContextMenu>
172
+ )
173
+ }
174
+ ```
175
+
176
+ ### With Submenu
177
+
178
+ ```typescript
179
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem, ContextMenuSub, ContextMenuSubTrigger, ContextMenuSubContent } from '@torch-ui/components'
180
+
181
+ function SubmenuExample() {
182
+ return (
183
+ <ContextMenu>
184
+ <ContextMenuTrigger asChild>
185
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
186
+ Right-click for more
187
+ </div>
188
+ </ContextMenuTrigger>
189
+ <ContextMenuContent>
190
+ <ContextMenuItem>New Tab</ContextMenuItem>
191
+ <ContextMenuItem>New Window</ContextMenuItem>
192
+
193
+ <ContextMenuSub>
194
+ <ContextMenuSubTrigger>More Tools</ContextMenuSubTrigger>
195
+ <ContextMenuSubContent>
196
+ <ContextMenuItem>Developer Tools</ContextMenuItem>
197
+ <ContextMenuItem>Task Manager</ContextMenuItem>
198
+ <ContextMenuItem>Extensions</ContextMenuItem>
199
+ </ContextMenuSubContent>
200
+ </ContextMenuSub>
201
+
202
+ <ContextMenuItem>Print</ContextMenuItem>
203
+ </ContextMenuContent>
204
+ </ContextMenu>
205
+ )
206
+ }
207
+ ```
208
+
209
+ ### RTL
210
+
211
+ Set `dir="rtl"` on the Root and the menu, items, and submenu arrows mirror automatically.
212
+
213
+ ```typescript
214
+ import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem, ContextMenuShortcut } from '@torch-ui/components'
215
+
216
+ function RtlMenu() {
217
+ return (
218
+ <ContextMenu dir="rtl">
219
+ <ContextMenuTrigger asChild>
220
+ <div className="flex h-40 w-72 items-center justify-center rounded-md border border-dashed">
221
+ انقر بزر الفأرة الأيمن
222
+ </div>
223
+ </ContextMenuTrigger>
224
+ <ContextMenuContent>
225
+ <ContextMenuItem>
226
+ تحرير
227
+ <ContextMenuShortcut>⌘E</ContextMenuShortcut>
228
+ </ContextMenuItem>
229
+ <ContextMenuItem variant="Negative">حذف</ContextMenuItem>
230
+ </ContextMenuContent>
231
+ </ContextMenu>
232
+ )
233
+ }
234
+ ```
235
+
236
+ ## API Reference
237
+
238
+ ### ContextMenu (Root)
239
+
240
+ A controlled wrapper around the Radix root. It tracks the open state internally (so a second right-click can dismiss the menu) while still forwarding `open` / `onOpenChange` when you control it.
241
+
242
+ | Prop | Type | Default | Description |
243
+ |------|------|---------|-------------|
244
+ | `open` | `boolean` | - | Controlled open state |
245
+ | `onOpenChange` | `(open: boolean) => void` | - | Callback when state changes |
246
+ | `dir` | `'ltr' \| 'rtl'` | - | Reading direction; mirrors layout and arrows |
247
+ | `modal` | `boolean` | `true` | Whether to block outside interactions |
248
+ | `children` | `React.ReactNode` | - | Trigger and content |
249
+
250
+ ### ContextMenuTrigger
251
+
252
+ The right-click zone. Wrap it around the element the menu should open from.
253
+
254
+ | Prop | Type | Default | Description |
255
+ |------|------|---------|-------------|
256
+ | `asChild` | `boolean` | `false` | Merge props onto the child element instead of rendering a wrapper |
257
+ | `disabled` | `boolean` | `false` | Disables opening on right-click |
258
+
259
+ ### ContextMenuContent
260
+
261
+ | Prop | Type | Default | Description |
262
+ |------|------|---------|-------------|
263
+ | `variant` | `'PresentationStyle'` | `'PresentationStyle'` | Visual style variant |
264
+ | `theme` | `'dark' \| 'light' \| 'default'` | - | Theme variant (applied as `data-theme`) |
265
+ | `className` | `string` | - | Additional CSS classes |
266
+ | `collisionPadding` | `number` | `8` | Min distance kept from the viewport edge |
267
+ | `autoGroup` | `boolean` | `true` | Auto-wrap loose items in a Boxed group (see Behavior notes) |
268
+
269
+ ### ContextMenuItem
270
+
271
+ | Prop | Type | Default | Description |
272
+ |------|------|---------|-------------|
273
+ | `variant` | `'Default' \| 'info' \| 'Negative'` | `'Default'` | Item style variant |
274
+ | `size` | `'S' \| 'M'` | `'M'` | Item size |
275
+ | `inset` | `boolean` | `false` | Add left padding to align with items that have icons |
276
+ | `active` | `boolean` | `false` | Active (selected) state |
277
+ | `disabled` | `boolean` | `false` | Disabled state (still shows but is not selectable) |
278
+ | `onSelect` | `(event: Event) => void` | - | Select handler; closes the menu by default |
279
+
280
+ ### ContextMenuCheckboxItem
281
+
282
+ | Prop | Type | Default | Description |
283
+ |------|------|---------|-------------|
284
+ | `checked` | `boolean \| 'indeterminate'` | `false` | Checked state |
285
+ | `onCheckedChange` | `(checked: boolean) => void` | - | Change handler |
286
+ | `variant` | `'Default' \| 'info' \| 'Negative'` | `'Default'` | Style variant |
287
+ | `size` | `'S' \| 'M'` | `'M'` | Item size |
288
+
289
+ > Selecting a checkbox item keeps the menu open — `onSelect` `preventDefault` is built in.
290
+
291
+ ### ContextMenuRadioGroup
292
+
293
+ | Prop | Type | Default | Description |
294
+ |------|------|---------|-------------|
295
+ | `value` | `string` | - | Selected radio value |
296
+ | `onValueChange` | `(value: string) => void` | - | Change handler |
297
+ | `variant` | `'Boxed' \| 'Plain'` | `'Boxed'` | `Boxed` renders a bordered container; `Plain` is semantic grouping only |
298
+
299
+ ### ContextMenuRadioItem
300
+
301
+ | Prop | Type | Default | Description |
302
+ |------|------|---------|-------------|
303
+ | `value` | `string` | Required | Radio option value |
304
+ | `variant` | `'Default' \| 'info' \| 'Negative'` | `'Default'` | Style variant |
305
+ | `size` | `'S' \| 'M'` | `'M'` | Item size |
306
+
307
+ > Selecting a radio item keeps the menu open — `onSelect` `preventDefault` is built in.
308
+
309
+ ### ContextMenuSubTrigger
310
+
311
+ | Prop | Type | Default | Description |
312
+ |------|------|---------|-------------|
313
+ | `variant` | `'Default' \| 'info' \| 'Negative'` | `'Default'` | Style variant |
314
+ | `size` | `'S' \| 'M'` | `'M'` | Item size |
315
+ | `inset` | `boolean` | `false` | Add left padding to align with items that have icons |
316
+ | `className` | `string` | - | Additional CSS classes |
317
+
318
+ Renders a trailing chevron (`ri-arrow-right-s-line`) that mirrors in RTL.
319
+
320
+ ### ContextMenuLabel
321
+
322
+ | Prop | Type | Default | Description |
323
+ |------|------|---------|-------------|
324
+ | `inset` | `boolean` | `false` | Add left padding to align with items that have icons |
325
+ | `className` | `string` | - | Additional CSS classes |
326
+
327
+ A non-interactive section heading. Acts as a boundary for auto-grouping.
328
+
329
+ ### ContextMenuShortcut
330
+
331
+ | Prop | Type | Default | Description |
332
+ |------|------|---------|-------------|
333
+ | `className` | `string` | - | Additional CSS classes |
334
+
335
+ A right-aligned (RTL-aware) span for keyboard hints inside an item.
336
+
337
+ ## TypeScript
338
+
339
+ ### Key Interfaces
340
+
341
+ ```typescript
342
+ import * as ContextMenuPrimitive from '@radix-ui/react-context-menu'
343
+
344
+ // Root — controlled wrapper
345
+ type ContextMenuProps = React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Root>
346
+ // { open?, onOpenChange?, dir?, modal?, children, ... }
347
+
348
+ export const ContextMenu: React.FC<ContextMenuProps>
349
+
350
+ // Content
351
+ interface ContextMenuContentProps {
352
+ variant?: 'PresentationStyle'
353
+ theme?: 'dark' | 'light' | 'default'
354
+ className?: string
355
+ collisionPadding?: number // default 8
356
+ autoGroup?: boolean // default true
357
+ }
358
+
359
+ export const ContextMenuContent: React.ForwardRefExoticComponent<ContextMenuContentProps>
360
+
361
+ // Item
362
+ interface ContextMenuItemProps {
363
+ variant?: 'Default' | 'info' | 'Negative'
364
+ size?: 'S' | 'M'
365
+ active?: boolean
366
+ disabled?: boolean
367
+ onSelect?: (event: Event) => void
368
+ }
369
+
370
+ export const ContextMenuItem: React.ForwardRefExoticComponent<ContextMenuItemProps>
371
+
372
+ // CheckboxItem
373
+ interface ContextMenuCheckboxItemProps {
374
+ checked?: boolean | 'indeterminate'
375
+ onCheckedChange?: (checked: boolean) => void
376
+ variant?: 'Default' | 'info' | 'Negative'
377
+ size?: 'S' | 'M'
378
+ }
379
+
380
+ export const ContextMenuCheckboxItem: React.ForwardRefExoticComponent<ContextMenuCheckboxItemProps>
381
+
382
+ // RadioGroup
383
+ interface ContextMenuRadioGroupProps {
384
+ value?: string
385
+ onValueChange?: (value: string) => void
386
+ variant?: 'Boxed' | 'Plain'
387
+ }
388
+
389
+ export const ContextMenuRadioGroup: React.ForwardRefExoticComponent<ContextMenuRadioGroupProps>
390
+
391
+ // RadioItem
392
+ interface ContextMenuRadioItemProps {
393
+ value: string
394
+ variant?: 'Default' | 'info' | 'Negative'
395
+ size?: 'S' | 'M'
396
+ }
397
+
398
+ export const ContextMenuRadioItem: React.ForwardRefExoticComponent<ContextMenuRadioItemProps>
399
+ ```
400
+
401
+ ## Behavior Notes
402
+
403
+ - **Opens at the pointer**: the menu opens on right-click (`contextmenu`) at the exact cursor position, not anchored to a fixed trigger button.
404
+ - **Second right-click closes it**: the Root is made controlled and tracks `open` in context. The Trigger listens in the capture phase, and when the menu is already open it `preventDefault()` / `stopPropagation()` and closes — so a second right-click dismisses instead of re-anchoring (which Radix handles unreliably).
405
+ - **Auto-grouping**: by default (`autoGroup` on `ContextMenuContent`, default `true`) consecutive loose items (`ContextMenuItem`, `ContextMenuCheckboxItem`, `ContextMenuRadioItem`, and `ContextMenuSub`) are automatically wrapped in a `Boxed` `ContextMenuGroup`, so they render inside a boxed container like DropdownMenu even when you do not write a group. Labels and explicit groups act as boundaries and pass through unchanged. Set `autoGroup={false}` to render children verbatim.
406
+ - **Checkbox / radio keep the menu open**: `ContextMenuCheckboxItem` and `ContextMenuRadioItem` call `event.preventDefault()` inside `onSelect`, stopping Radix's default auto-close so users can toggle multiple options in one pass.
407
+ - **Open-only animation**: only the open (enter) state animates (`fade-in`). There is intentionally no exit animation — holding the old DOM node during close breaks close/reposition on a second right-click, so it is omitted to keep repositioning reliable.
408
+ - **Submenus and RTL**: nested `ContextMenuSub` / `ContextMenuSubTrigger` / `ContextMenuSubContent` are supported, and `dir="rtl"` on the Root mirrors the layout (including the submenu chevron).
409
+
410
+ ## Accessibility
411
+
412
+ - **Keyboard Support**:
413
+ - Shift+F10 or the Menu (context) key: open the menu from the focused trigger
414
+ - Arrow Down / Arrow Up: move between items
415
+ - Arrow Right: open submenu (Arrow Left to close) — mirrored in RTL
416
+ - Enter / Space: select item
417
+ - Escape: close menu
418
+ - **Touch**: long-press on the trigger opens the menu on touch devices.
419
+ - **ARIA Attributes**: roles and states are applied automatically by Radix UI.
420
+ - **Focus Management**: focus is trapped within the open menu and restored on close.
421
+ - **Screen Readers**: menu structure, checked/selected states, and submenus are announced.
422
+
423
+ ## Best Practices
424
+
425
+ 1. **Use a clear right-click zone**
426
+ ```typescript
427
+ <ContextMenuTrigger asChild>
428
+ <div className="rounded-md border border-dashed">Right-click here</div>
429
+ </ContextMenuTrigger>
430
+ ```
431
+
432
+ 2. **Use labels to separate sections** — they also break auto-grouping into distinct boxed runs
433
+ ```typescript
434
+ <ContextMenuLabel>Edit</ContextMenuLabel>
435
+ ```
436
+
437
+ 3. **Show keyboard shortcuts**
438
+ ```typescript
439
+ <ContextMenuItem>
440
+ Save
441
+ <ContextMenuShortcut>⌘S</ContextMenuShortcut>
442
+ </ContextMenuItem>
443
+ ```
444
+
445
+ 4. **Use the Negative variant for destructive actions**
446
+ ```typescript
447
+ <ContextMenuItem variant="Negative">Delete</ContextMenuItem>
448
+ ```
449
+
450
+ 5. **Let checkbox/radio toggles stay open**: rely on the built-in behavior so users can adjust several settings without reopening.
451
+ 6. **Avoid deeply nested submenus**: 2 levels max.
452
+ 7. **Keep item labels concise**: short, action-oriented text.
453
+
454
+ ## Related Components
455
+
456
+ - [DropdownMenu](./dropdown-menu.md) - Button-anchored menu
457
+ - [Popover](./popover.md) - Non-menu popover
458
+ - [Select](./select.md) - Form select field