@cognitap/radiant 0.0.35 → 0.0.36
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.
- package/CHANGELOG.md +13 -5
- package/dist/library/ContextMenu.d.ts +38 -0
- package/dist/library/ContextMenu.d.ts.map +1 -0
- package/dist/library/ContextMenu.js +169 -0
- package/dist/library/ContextMenu.js.map +1 -0
- package/dist/library/Menu.d.ts +83 -3
- package/dist/library/Menu.d.ts.map +1 -1
- package/dist/library/Menu.js +209 -25
- package/dist/library/Menu.js.map +1 -1
- package/dist/library/Popover.d.ts +18 -2
- package/dist/library/Popover.d.ts.map +1 -1
- package/dist/library/Popover.js +24 -4
- package/dist/library/Popover.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +1 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +4 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +2 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/react/usePopoverSurface.d.ts +4 -0
- package/dist/library/react/usePopoverSurface.d.ts.map +1 -1
- package/dist/library/react/usePopoverSurface.js +13 -4
- package/dist/library/react/usePopoverSurface.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +3 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +1 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +35 -31
- package/package.json +1 -1
- package/src/ContextMenu.tsx +354 -0
- package/src/Menu.tsx +496 -40
- package/src/Popover.tsx +46 -4
- package/src/cn.ts +1 -0
- package/src/index.ts +57 -0
- package/src/react/usePopoverSurface.ts +17 -4
- package/src/themes/components.ts +1 -0
package/src/Menu.tsx
CHANGED
|
@@ -2,27 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
createContext,
|
|
5
|
+
useCallback,
|
|
5
6
|
useContext,
|
|
6
7
|
useId,
|
|
7
8
|
useLayoutEffect,
|
|
8
9
|
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
type ComponentProps,
|
|
9
12
|
type MouseEvent,
|
|
10
13
|
type ReactNode,
|
|
11
14
|
type RefObject
|
|
12
15
|
} from 'react'
|
|
13
16
|
import {
|
|
14
|
-
|
|
17
|
+
PopoverRoot,
|
|
15
18
|
PopoverTrigger,
|
|
16
19
|
PopoverContent,
|
|
17
|
-
|
|
20
|
+
PopoverCloseRoot,
|
|
18
21
|
PopoverPortal,
|
|
19
22
|
usePopover,
|
|
23
|
+
usePopoverContext,
|
|
20
24
|
type PopoverProps,
|
|
21
25
|
type PopoverState,
|
|
22
26
|
type PopoverContentProps
|
|
23
27
|
} from './Popover.js'
|
|
24
28
|
import type { ButtonProps, ButtonRenderProps } from './Button.js'
|
|
25
29
|
import type { DistributiveOmit } from './internal/types.js'
|
|
30
|
+
import { Separator, type SeparatorProps } from './Separator.js'
|
|
31
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
32
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
33
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
34
|
+
import type { PartStyle } from './presentation.js'
|
|
26
35
|
import { cn } from './cn.js'
|
|
27
36
|
import { composeAction } from './internal/events.js'
|
|
28
37
|
import { joinIds } from './internal/ids.js'
|
|
@@ -43,58 +52,130 @@ export type MenuItemProps = DistributiveOmit<
|
|
|
43
52
|
'role' | 'tabIndex' | 'aria-disabled' | 'onSelect'
|
|
44
53
|
> & {
|
|
45
54
|
textValue?: string
|
|
55
|
+
closeOnSelect?: boolean
|
|
46
56
|
onSelect?: (event: MouseEvent<HTMLButtonElement>) => void
|
|
47
57
|
}
|
|
58
|
+
type Intent = 'first' | 'last' | 'none'
|
|
59
|
+
type MenuFamily = 'menu' | 'context-menu' | 'menubar'
|
|
48
60
|
type Context = MenuState & {
|
|
61
|
+
parent?: Context
|
|
62
|
+
family: MenuFamily
|
|
63
|
+
focusIntent?: Intent
|
|
64
|
+
closeTree(): void
|
|
49
65
|
triggerId: string
|
|
50
|
-
trigger: RefObject<
|
|
66
|
+
trigger: RefObject<HTMLElement | null>
|
|
67
|
+
setTrigger: (node: HTMLElement | null) => void
|
|
51
68
|
content: RefObject<HTMLDivElement | null>
|
|
52
|
-
intent: RefObject<
|
|
53
|
-
setIntent: (edge:
|
|
69
|
+
intent: RefObject<Intent>
|
|
70
|
+
setIntent: (edge: Intent) => void
|
|
54
71
|
}
|
|
55
72
|
const MenuContext = createContext<Context | null>(null)
|
|
56
73
|
export function useMenu(): MenuState | null {
|
|
57
74
|
return useContext(MenuContext)
|
|
58
75
|
}
|
|
59
|
-
|
|
76
|
+
/** Private family guard for composed menus. */
|
|
77
|
+
export function useMenuFamily() {
|
|
78
|
+
return useContext(MenuContext)?.family
|
|
79
|
+
}
|
|
80
|
+
/** Private composition seam; not exported from the package. */
|
|
81
|
+
export function useMenuContext() {
|
|
60
82
|
const menu = useContext(MenuContext)
|
|
61
83
|
if (!menu) throw new Error('Menu parts must render inside Menu.')
|
|
62
84
|
return menu
|
|
63
85
|
}
|
|
64
|
-
export function Menu(
|
|
86
|
+
export function Menu(props: MenuProps) {
|
|
87
|
+
return (
|
|
88
|
+
<MenuRoot
|
|
89
|
+
{...props}
|
|
90
|
+
anchorPoint={undefined}
|
|
91
|
+
family="menu"
|
|
92
|
+
parent={undefined}
|
|
93
|
+
focusIntent={undefined}
|
|
94
|
+
/>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
/** Composed families share one owner and native surface tree. */
|
|
98
|
+
export function MenuRoot({
|
|
99
|
+
children,
|
|
100
|
+
parent,
|
|
101
|
+
family = 'menu',
|
|
102
|
+
anchorPoint,
|
|
103
|
+
focusIntent,
|
|
104
|
+
...props
|
|
105
|
+
}: MenuProps & {
|
|
106
|
+
parent?: Context
|
|
107
|
+
family?: MenuFamily
|
|
108
|
+
focusIntent?: Intent
|
|
109
|
+
anchorPoint?: { x: number; y: number }
|
|
110
|
+
}) {
|
|
65
111
|
return (
|
|
66
|
-
<
|
|
67
|
-
|
|
68
|
-
|
|
112
|
+
<PopoverRoot
|
|
113
|
+
{...props}
|
|
114
|
+
anchorPoint={anchorPoint}
|
|
115
|
+
>
|
|
116
|
+
<MenuOwner
|
|
117
|
+
parent={parent}
|
|
118
|
+
family={family}
|
|
119
|
+
focusIntent={focusIntent}
|
|
120
|
+
>
|
|
121
|
+
{children}
|
|
122
|
+
</MenuOwner>
|
|
123
|
+
</PopoverRoot>
|
|
69
124
|
)
|
|
70
125
|
}
|
|
71
|
-
function MenuOwner({
|
|
126
|
+
function MenuOwner({
|
|
127
|
+
children,
|
|
128
|
+
parent,
|
|
129
|
+
family,
|
|
130
|
+
focusIntent
|
|
131
|
+
}: {
|
|
132
|
+
children?: ReactNode
|
|
133
|
+
parent?: Context
|
|
134
|
+
family: MenuFamily
|
|
135
|
+
focusIntent?: Intent
|
|
136
|
+
}) {
|
|
72
137
|
const state = usePopover()!
|
|
73
138
|
const triggerId = useId()
|
|
74
|
-
const trigger = useRef<
|
|
139
|
+
const trigger = useRef<HTMLElement>(null)
|
|
140
|
+
const setTrigger = useCallback((node: HTMLElement | null) => {
|
|
141
|
+
trigger.current = node
|
|
142
|
+
}, [])
|
|
75
143
|
const content = useRef<HTMLDivElement>(null)
|
|
76
|
-
const intent = useRef<
|
|
77
|
-
return (
|
|
78
|
-
<
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
intent
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
144
|
+
const intent = useRef<Intent>('first')
|
|
145
|
+
return (
|
|
146
|
+
<RadioContext.Provider value={null}>
|
|
147
|
+
<MenuContext.Provider
|
|
148
|
+
value={{
|
|
149
|
+
...state,
|
|
150
|
+
triggerId,
|
|
151
|
+
trigger,
|
|
152
|
+
setTrigger,
|
|
153
|
+
content,
|
|
154
|
+
intent,
|
|
155
|
+
parent,
|
|
156
|
+
family,
|
|
157
|
+
focusIntent,
|
|
158
|
+
setIntent: edge => {
|
|
159
|
+
intent.current = edge
|
|
160
|
+
},
|
|
161
|
+
closeTree: () => {
|
|
162
|
+
state.setOpen(false)
|
|
163
|
+
parent?.closeTree()
|
|
164
|
+
}
|
|
165
|
+
}}
|
|
166
|
+
>
|
|
167
|
+
{children}
|
|
168
|
+
</MenuContext.Provider>
|
|
169
|
+
</RadioContext.Provider>
|
|
92
170
|
)
|
|
93
171
|
}
|
|
172
|
+
|
|
94
173
|
function items(node: HTMLElement) {
|
|
95
|
-
return Array.from(
|
|
96
|
-
|
|
97
|
-
|
|
174
|
+
return Array.from(
|
|
175
|
+
node.querySelectorAll<HTMLButtonElement>(
|
|
176
|
+
'button[role="menuitem"],button[role="menuitemcheckbox"],button[role="menuitemradio"]'
|
|
177
|
+
)
|
|
178
|
+
).filter(item => item.closest('[role="menu"]') === node)
|
|
98
179
|
}
|
|
99
180
|
function focusItem(node: HTMLElement, item?: HTMLButtonElement) {
|
|
100
181
|
for (const candidate of items(node)) candidate.tabIndex = candidate === item ? 0 : -1
|
|
@@ -102,7 +183,7 @@ function focusItem(node: HTMLElement, item?: HTMLButtonElement) {
|
|
|
102
183
|
}
|
|
103
184
|
export function MenuTrigger({ ref, onKeyDown, onClick, ...props }: MenuTriggerProps) {
|
|
104
185
|
const menu = useMenuContext()
|
|
105
|
-
const host = useComposedRefs(menu.
|
|
186
|
+
const host = useComposedRefs<HTMLButtonElement>(menu.setTrigger, ref)
|
|
106
187
|
return (
|
|
107
188
|
<PopoverTrigger
|
|
108
189
|
{...props}
|
|
@@ -140,14 +221,40 @@ export function MenuPortal(props: MenuPortalProps) {
|
|
|
140
221
|
}
|
|
141
222
|
export function MenuContent({ ref, className, onKeyDown, ...props }: MenuContentProps) {
|
|
142
223
|
const menu = useMenuContext()
|
|
143
|
-
const
|
|
224
|
+
const nested = !!menu.parent || menu.family === 'menubar'
|
|
225
|
+
const triggerId = menu.triggerId
|
|
226
|
+
const capture = useCallback(
|
|
227
|
+
(node: HTMLDivElement | null) => {
|
|
228
|
+
const trigger = node?.ownerDocument.getElementById(triggerId)
|
|
229
|
+
// Establish the submenu's reading order before the native surface opens.
|
|
230
|
+
if (
|
|
231
|
+
nested &&
|
|
232
|
+
node &&
|
|
233
|
+
trigger &&
|
|
234
|
+
node.parentElement === trigger.parentElement &&
|
|
235
|
+
trigger.nextElementSibling !== node &&
|
|
236
|
+
!node.matches(':popover-open')
|
|
237
|
+
)
|
|
238
|
+
trigger.after(node)
|
|
239
|
+
},
|
|
240
|
+
[triggerId, nested]
|
|
241
|
+
)
|
|
242
|
+
const placed = useComposedRefs(menu.content, capture)
|
|
243
|
+
const host = useComposedRefs(placed, ref)
|
|
144
244
|
const search = useRef({ text: '', time: 0 })
|
|
245
|
+
const openIntent = useRef(menu.focusIntent)
|
|
246
|
+
useLayoutEffect(() => {
|
|
247
|
+
openIntent.current = menu.focusIntent
|
|
248
|
+
})
|
|
249
|
+
|
|
145
250
|
useLayoutEffect(() => {
|
|
146
251
|
search.current = { text: '', time: 0 }
|
|
147
252
|
const node = menu.content.current
|
|
148
253
|
if (!menu.open || !node) return
|
|
254
|
+
const edge = openIntent.current ?? menu.intent.current
|
|
255
|
+
if (edge === 'none') return
|
|
149
256
|
const options = items(node)
|
|
150
|
-
focusItem(node,
|
|
257
|
+
focusItem(node, edge === 'last' ? options.at(-1) : options[0])
|
|
151
258
|
}, [menu.open, menu.content, menu.intent])
|
|
152
259
|
return (
|
|
153
260
|
<PopoverContent
|
|
@@ -172,8 +279,10 @@ export function MenuContent({ ref, className, onKeyDown, ...props }: MenuContent
|
|
|
172
279
|
if (target.closest('[role="menu"]') !== node) return
|
|
173
280
|
if (event.key === 'Tab') {
|
|
174
281
|
// Let the browser continue from the opener after closing, in either direction.
|
|
175
|
-
|
|
176
|
-
|
|
282
|
+
let root = menu
|
|
283
|
+
while (root.parent) root = root.parent
|
|
284
|
+
root.trigger.current?.focus({ preventScroll: true })
|
|
285
|
+
menu.closeTree()
|
|
177
286
|
return
|
|
178
287
|
}
|
|
179
288
|
if (event.altKey || event.ctrlKey || event.metaKey || event.nativeEvent.isComposing) return
|
|
@@ -206,7 +315,19 @@ export function MenuContent({ ref, className, onKeyDown, ...props }: MenuContent
|
|
|
206
315
|
/>
|
|
207
316
|
)
|
|
208
317
|
}
|
|
209
|
-
export function MenuItem({
|
|
318
|
+
export function MenuItem(props: MenuItemProps) {
|
|
319
|
+
return (
|
|
320
|
+
<MenuItemRoot
|
|
321
|
+
{...props}
|
|
322
|
+
itemRole="menuitem"
|
|
323
|
+
checkedValue={undefined}
|
|
324
|
+
/>
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
function MenuItemRoot({
|
|
328
|
+
itemRole = 'menuitem',
|
|
329
|
+
checkedValue,
|
|
330
|
+
closeOnSelect = true,
|
|
210
331
|
disabled = false,
|
|
211
332
|
textValue,
|
|
212
333
|
onSelect,
|
|
@@ -216,14 +337,20 @@ export function MenuItem({
|
|
|
216
337
|
className,
|
|
217
338
|
motion,
|
|
218
339
|
...props
|
|
219
|
-
}: MenuItemProps
|
|
340
|
+
}: MenuItemProps & {
|
|
341
|
+
itemRole?: 'menuitem' | 'menuitemcheckbox' | 'menuitemradio'
|
|
342
|
+
checkedValue?: boolean | 'mixed'
|
|
343
|
+
}) {
|
|
220
344
|
const menu = useMenuContext()
|
|
221
345
|
return (
|
|
222
|
-
<
|
|
346
|
+
<PopoverCloseRoot
|
|
347
|
+
requestClose={menu.closeTree}
|
|
348
|
+
closeOnClick={closeOnSelect}
|
|
223
349
|
{...props}
|
|
224
350
|
disabled={undefined}
|
|
225
351
|
aria-disabled={disabled || undefined}
|
|
226
|
-
role=
|
|
352
|
+
role={itemRole}
|
|
353
|
+
aria-checked={checkedValue ?? props['aria-checked']}
|
|
227
354
|
tabIndex={-1}
|
|
228
355
|
motion={disabled ? 'off' : motion}
|
|
229
356
|
data-slot={joinIds('menu-item', props['data-slot'])}
|
|
@@ -247,3 +374,332 @@ export function MenuItem({
|
|
|
247
374
|
/>
|
|
248
375
|
)
|
|
249
376
|
}
|
|
377
|
+
|
|
378
|
+
export type MenuGroupProps = Omit<ComponentProps<'div'>, 'style' | 'role'> & {
|
|
379
|
+
style?: ThemeStyle
|
|
380
|
+
'data-slot'?: string
|
|
381
|
+
}
|
|
382
|
+
export type MenuLabelProps = Omit<ComponentProps<'div'>, 'style' | 'role' | 'tabIndex'> & {
|
|
383
|
+
style?: ThemeStyle
|
|
384
|
+
'data-slot'?: string
|
|
385
|
+
}
|
|
386
|
+
export type MenuSeparatorProps = SeparatorProps
|
|
387
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Menu sections are ARIA groups, not native form fieldsets. */
|
|
388
|
+
export function MenuGroup({ className, ...props }: MenuGroupProps) {
|
|
389
|
+
useMenuContext()
|
|
390
|
+
return (
|
|
391
|
+
<div
|
|
392
|
+
{...props}
|
|
393
|
+
role="group"
|
|
394
|
+
data-slot={joinIds('menu-group', props['data-slot'])}
|
|
395
|
+
className={cn('grid gap-0', className)}
|
|
396
|
+
/>
|
|
397
|
+
)
|
|
398
|
+
}
|
|
399
|
+
/* oxlint-enable jsx-a11y/prefer-tag-over-role */
|
|
400
|
+
export function MenuLabel({ className, ...props }: MenuLabelProps) {
|
|
401
|
+
useMenuContext()
|
|
402
|
+
return (
|
|
403
|
+
<div
|
|
404
|
+
{...props}
|
|
405
|
+
role="presentation"
|
|
406
|
+
tabIndex={undefined}
|
|
407
|
+
data-slot={joinIds('menu-label', props['data-slot'])}
|
|
408
|
+
className={cn(
|
|
409
|
+
'px-[var(--rad-menu-item-padding,0.625rem)] py-2 [color:var(--rad-color-muted-foreground)] text-[length:var(--rad-typography-roles-ui-label-size,0.8125rem)] font-[number:var(--rad-typography-roles-ui-label-weight,600)]',
|
|
410
|
+
className
|
|
411
|
+
)}
|
|
412
|
+
/>
|
|
413
|
+
)
|
|
414
|
+
}
|
|
415
|
+
export function MenuSeparator(props: MenuSeparatorProps) {
|
|
416
|
+
useMenuContext()
|
|
417
|
+
return (
|
|
418
|
+
<Separator
|
|
419
|
+
{...props}
|
|
420
|
+
data-slot={joinIds('menu-separator', props['data-slot'])}
|
|
421
|
+
/>
|
|
422
|
+
)
|
|
423
|
+
}
|
|
424
|
+
type CheckableProps = DistributiveOmit<
|
|
425
|
+
MenuItemProps,
|
|
426
|
+
'pressed' | 'defaultPressed' | 'onPressedChange' | 'aria-pressed' | 'aria-checked' | 'type'
|
|
427
|
+
> & { parts?: { indicator?: PartStyle } }
|
|
428
|
+
export type MenuCheckboxItemProps = CheckableProps &
|
|
429
|
+
(
|
|
430
|
+
| { checked: boolean; defaultChecked?: never }
|
|
431
|
+
| { checked?: undefined; defaultChecked?: boolean }
|
|
432
|
+
) & { onCheckedChange?: (checked: boolean) => void; indeterminate?: boolean }
|
|
433
|
+
const plainText = (children: ReactNode) =>
|
|
434
|
+
typeof children === 'string' || typeof children === 'number' ? String(children) : undefined
|
|
435
|
+
function CheckMark({ children, part }: { children?: ReactNode; part?: PartStyle }) {
|
|
436
|
+
return (
|
|
437
|
+
<span
|
|
438
|
+
aria-hidden="true"
|
|
439
|
+
data-slot="menu-item-indicator"
|
|
440
|
+
className={cn('inline-flex w-4 shrink-0 items-center justify-center', part?.className)}
|
|
441
|
+
style={part?.style}
|
|
442
|
+
>
|
|
443
|
+
{children}
|
|
444
|
+
</span>
|
|
445
|
+
)
|
|
446
|
+
}
|
|
447
|
+
export function MenuCheckboxItem({
|
|
448
|
+
checked,
|
|
449
|
+
defaultChecked,
|
|
450
|
+
onCheckedChange,
|
|
451
|
+
indeterminate = false,
|
|
452
|
+
parts,
|
|
453
|
+
children,
|
|
454
|
+
textValue,
|
|
455
|
+
onSelect,
|
|
456
|
+
closeOnSelect = false,
|
|
457
|
+
...props
|
|
458
|
+
}: MenuCheckboxItemProps) {
|
|
459
|
+
const state = useControllableState({
|
|
460
|
+
name: 'MenuCheckboxItem',
|
|
461
|
+
value: checked,
|
|
462
|
+
defaultValue: defaultChecked,
|
|
463
|
+
initialValue: false,
|
|
464
|
+
onChange: onCheckedChange
|
|
465
|
+
})
|
|
466
|
+
return (
|
|
467
|
+
<MenuItemRoot
|
|
468
|
+
{...props}
|
|
469
|
+
type="button"
|
|
470
|
+
pressed={undefined}
|
|
471
|
+
defaultPressed={undefined}
|
|
472
|
+
onPressedChange={undefined}
|
|
473
|
+
aria-pressed={undefined}
|
|
474
|
+
itemRole="menuitemcheckbox"
|
|
475
|
+
checkedValue={indeterminate ? 'mixed' : state.value}
|
|
476
|
+
closeOnSelect={closeOnSelect}
|
|
477
|
+
textValue={textValue ?? plainText(children)}
|
|
478
|
+
data-slot={joinIds('menu-checkbox-item', props['data-slot'])}
|
|
479
|
+
onSelect={event => {
|
|
480
|
+
onSelect?.(event)
|
|
481
|
+
if (!event.defaultPrevented) state.request(indeterminate || !state.value)
|
|
482
|
+
}}
|
|
483
|
+
>
|
|
484
|
+
<CheckMark part={parts?.indicator}>
|
|
485
|
+
{indeterminate ? '−' : state.value ? '✓' : null}
|
|
486
|
+
</CheckMark>
|
|
487
|
+
{children}
|
|
488
|
+
</MenuItemRoot>
|
|
489
|
+
)
|
|
490
|
+
}
|
|
491
|
+
export type MenuRadioGroupProps = Omit<MenuGroupProps, 'defaultValue'> &
|
|
492
|
+
ValueOwner<string | null> & { onValueChange?: (value: string | null) => void }
|
|
493
|
+
type RadioContextValue = { value: string | null; request: (value: string) => void }
|
|
494
|
+
const RadioContext = createContext<RadioContextValue | null>(null)
|
|
495
|
+
export function MenuRadioGroup({
|
|
496
|
+
value,
|
|
497
|
+
defaultValue,
|
|
498
|
+
onValueChange,
|
|
499
|
+
...props
|
|
500
|
+
}: MenuRadioGroupProps) {
|
|
501
|
+
const state = useControllableState<string | null>({
|
|
502
|
+
name: 'MenuRadioGroup',
|
|
503
|
+
value,
|
|
504
|
+
defaultValue,
|
|
505
|
+
initialValue: null,
|
|
506
|
+
onChange: onValueChange
|
|
507
|
+
})
|
|
508
|
+
return (
|
|
509
|
+
<RadioContext.Provider value={{ value: state.value, request: state.request }}>
|
|
510
|
+
<MenuGroup
|
|
511
|
+
{...props}
|
|
512
|
+
data-slot={joinIds('menu-radio-group', props['data-slot'])}
|
|
513
|
+
/>
|
|
514
|
+
</RadioContext.Provider>
|
|
515
|
+
)
|
|
516
|
+
}
|
|
517
|
+
export type MenuRadioItemProps = DistributiveOmit<CheckableProps, 'value'> & { value: string }
|
|
518
|
+
export function MenuRadioItem({
|
|
519
|
+
value,
|
|
520
|
+
children,
|
|
521
|
+
textValue,
|
|
522
|
+
parts,
|
|
523
|
+
onSelect,
|
|
524
|
+
closeOnSelect = false,
|
|
525
|
+
...props
|
|
526
|
+
}: MenuRadioItemProps) {
|
|
527
|
+
const radio = useContext(RadioContext)
|
|
528
|
+
if (!radio) throw new Error('MenuRadioItem must render inside MenuRadioGroup.')
|
|
529
|
+
if (typeof value !== 'string') throw new TypeError('MenuRadioItem needs a string value.')
|
|
530
|
+
const checked = radio.value === value
|
|
531
|
+
return (
|
|
532
|
+
<MenuItemRoot
|
|
533
|
+
{...props}
|
|
534
|
+
type="button"
|
|
535
|
+
value={value}
|
|
536
|
+
pressed={undefined}
|
|
537
|
+
defaultPressed={undefined}
|
|
538
|
+
onPressedChange={undefined}
|
|
539
|
+
aria-pressed={undefined}
|
|
540
|
+
itemRole="menuitemradio"
|
|
541
|
+
checkedValue={checked}
|
|
542
|
+
closeOnSelect={closeOnSelect}
|
|
543
|
+
textValue={textValue ?? plainText(children)}
|
|
544
|
+
data-slot={joinIds('menu-radio-item', props['data-slot'])}
|
|
545
|
+
onSelect={event => {
|
|
546
|
+
onSelect?.(event)
|
|
547
|
+
if (!event.defaultPrevented) radio.request(value)
|
|
548
|
+
}}
|
|
549
|
+
>
|
|
550
|
+
<CheckMark part={parts?.indicator}>{checked ? '●' : null}</CheckMark>
|
|
551
|
+
{children}
|
|
552
|
+
</MenuItemRoot>
|
|
553
|
+
)
|
|
554
|
+
}
|
|
555
|
+
export type MenuSubProps = MenuProps
|
|
556
|
+
export type MenuSubTriggerProps = DistributiveOmit<
|
|
557
|
+
MenuItemProps,
|
|
558
|
+
'closeOnSelect' | 'id' | 'aria-haspopup' | 'aria-expanded' | 'aria-controls' | 'type'
|
|
559
|
+
> & { parts?: { indicator?: PartStyle } }
|
|
560
|
+
export type MenuSubContentProps = MenuContentProps
|
|
561
|
+
export function MenuSub(props: MenuSubProps) {
|
|
562
|
+
const parent = useMenuContext()
|
|
563
|
+
return (
|
|
564
|
+
<MenuRoot
|
|
565
|
+
{...props}
|
|
566
|
+
anchorPoint={undefined}
|
|
567
|
+
parent={parent}
|
|
568
|
+
family={parent.family}
|
|
569
|
+
focusIntent={undefined}
|
|
570
|
+
/>
|
|
571
|
+
)
|
|
572
|
+
}
|
|
573
|
+
function openMenu(menu: Context, edge: Intent) {
|
|
574
|
+
menu.setIntent(edge)
|
|
575
|
+
if (menu.open && menu.content.current) {
|
|
576
|
+
if (edge !== 'none') {
|
|
577
|
+
const options = items(menu.content.current)
|
|
578
|
+
focusItem(menu.content.current, edge === 'last' ? options.at(-1) : options[0])
|
|
579
|
+
}
|
|
580
|
+
} else menu.setOpen(true)
|
|
581
|
+
}
|
|
582
|
+
export function MenuSubTrigger({
|
|
583
|
+
ref,
|
|
584
|
+
children,
|
|
585
|
+
textValue,
|
|
586
|
+
parts,
|
|
587
|
+
className,
|
|
588
|
+
onSelect,
|
|
589
|
+
onKeyDown,
|
|
590
|
+
onPointerMove,
|
|
591
|
+
disabled,
|
|
592
|
+
pending,
|
|
593
|
+
...props
|
|
594
|
+
}: MenuSubTriggerProps) {
|
|
595
|
+
const menu = useMenuContext(),
|
|
596
|
+
popover = usePopoverContext(),
|
|
597
|
+
host = useComposedRefs<HTMLButtonElement>(menu.setTrigger, ref),
|
|
598
|
+
anchor = useComposedRefs<HTMLButtonElement>(popover.setTrigger, host)
|
|
599
|
+
if (!menu.parent) throw new Error('MenuSubTrigger must render inside MenuSub.')
|
|
600
|
+
return (
|
|
601
|
+
<MenuContext.Provider value={menu.parent}>
|
|
602
|
+
<MenuItemRoot
|
|
603
|
+
{...props}
|
|
604
|
+
textValue={textValue ?? plainText(children)}
|
|
605
|
+
disabled={disabled}
|
|
606
|
+
pending={pending}
|
|
607
|
+
ref={anchor}
|
|
608
|
+
type="button"
|
|
609
|
+
id={menu.triggerId}
|
|
610
|
+
aria-haspopup="menu"
|
|
611
|
+
aria-controls={menu.contentId}
|
|
612
|
+
aria-expanded={menu.open}
|
|
613
|
+
data-slot={joinIds('menu-sub-trigger', props['data-slot'])}
|
|
614
|
+
closeOnSelect={false}
|
|
615
|
+
className={cn(
|
|
616
|
+
'aria-expanded:bg-[var(--rad-menu-item-focus-background,color-mix(in_oklab,var(--rad-color-accent)_12%,var(--rad-color-surface)))] aria-expanded:[color:var(--rad-menu-item-focus-color,color-mix(in_oklab,var(--rad-color-accent)_25%,var(--rad-color-foreground)))]',
|
|
617
|
+
className
|
|
618
|
+
)}
|
|
619
|
+
onSelect={event => {
|
|
620
|
+
onSelect?.(event)
|
|
621
|
+
if (!event.defaultPrevented) openMenu(menu, 'first')
|
|
622
|
+
}}
|
|
623
|
+
onKeyDown={composeAction(onKeyDown, event => {
|
|
624
|
+
const rtl =
|
|
625
|
+
event.currentTarget.ownerDocument.defaultView!.getComputedStyle(event.currentTarget)
|
|
626
|
+
.direction === 'rtl'
|
|
627
|
+
if (
|
|
628
|
+
event.key !== (rtl ? 'ArrowLeft' : 'ArrowRight') ||
|
|
629
|
+
event.altKey ||
|
|
630
|
+
event.ctrlKey ||
|
|
631
|
+
event.metaKey ||
|
|
632
|
+
event.nativeEvent.isComposing ||
|
|
633
|
+
disabled ||
|
|
634
|
+
pending ||
|
|
635
|
+
event.currentTarget.matches(':disabled')
|
|
636
|
+
)
|
|
637
|
+
return
|
|
638
|
+
event.preventDefault()
|
|
639
|
+
openMenu(menu, 'first')
|
|
640
|
+
})}
|
|
641
|
+
onPointerMove={composeAction(onPointerMove, event => {
|
|
642
|
+
if (
|
|
643
|
+
event.pointerType !== 'touch' &&
|
|
644
|
+
!disabled &&
|
|
645
|
+
!pending &&
|
|
646
|
+
!event.currentTarget.matches(':disabled')
|
|
647
|
+
)
|
|
648
|
+
openMenu(menu, 'none')
|
|
649
|
+
})}
|
|
650
|
+
>
|
|
651
|
+
<span className="min-w-0 flex-1">{children}</span>
|
|
652
|
+
<CheckMark part={parts?.indicator}>
|
|
653
|
+
<span className="inline-block rtl:rotate-180">›</span>
|
|
654
|
+
</CheckMark>
|
|
655
|
+
</MenuItemRoot>
|
|
656
|
+
</MenuContext.Provider>
|
|
657
|
+
)
|
|
658
|
+
}
|
|
659
|
+
export function MenuSubContent({ side, onKeyDown, ...props }: MenuSubContentProps) {
|
|
660
|
+
const menu = useMenuContext(),
|
|
661
|
+
[inlineSide, setInlineSide] = useState<'right' | 'left'>('right')
|
|
662
|
+
if (!menu.parent) throw new Error('MenuSubContent must render inside MenuSub.')
|
|
663
|
+
useLayoutEffect(() => {
|
|
664
|
+
const trigger = menu.trigger.current
|
|
665
|
+
if (!trigger) return
|
|
666
|
+
const view = trigger.ownerDocument.defaultView!,
|
|
667
|
+
update = () =>
|
|
668
|
+
setInlineSide(view.getComputedStyle(trigger).direction === 'rtl' ? 'left' : 'right'),
|
|
669
|
+
observer = new view.MutationObserver(update)
|
|
670
|
+
update()
|
|
671
|
+
for (let node: HTMLElement | null = trigger; node; node = node.parentElement)
|
|
672
|
+
observer.observe(node, { attributes: true, attributeFilter: ['dir', 'class', 'style'] })
|
|
673
|
+
view.addEventListener('resize', update)
|
|
674
|
+
return () => {
|
|
675
|
+
observer.disconnect()
|
|
676
|
+
view.removeEventListener('resize', update)
|
|
677
|
+
}
|
|
678
|
+
}, [menu.trigger])
|
|
679
|
+
return (
|
|
680
|
+
<MenuContent
|
|
681
|
+
{...props}
|
|
682
|
+
side={side ?? inlineSide}
|
|
683
|
+
sideOffset={props.sideOffset ?? 4}
|
|
684
|
+
data-slot={joinIds('menu-sub-content', props['data-slot'])}
|
|
685
|
+
onKeyDown={composeAction(onKeyDown, event => {
|
|
686
|
+
const rtl =
|
|
687
|
+
event.currentTarget.ownerDocument.defaultView!.getComputedStyle(event.currentTarget)
|
|
688
|
+
.direction === 'rtl'
|
|
689
|
+
if (
|
|
690
|
+
event.key !== (rtl ? 'ArrowRight' : 'ArrowLeft') ||
|
|
691
|
+
event.altKey ||
|
|
692
|
+
event.ctrlKey ||
|
|
693
|
+
event.metaKey ||
|
|
694
|
+
event.nativeEvent.isComposing ||
|
|
695
|
+
(event.target as HTMLElement).closest('[role="menu"]') !== event.currentTarget
|
|
696
|
+
)
|
|
697
|
+
return
|
|
698
|
+
event.preventDefault()
|
|
699
|
+
event.stopPropagation()
|
|
700
|
+
menu.setOpen(false)
|
|
701
|
+
menu.trigger.current?.focus({ preventScroll: true })
|
|
702
|
+
})}
|
|
703
|
+
/>
|
|
704
|
+
)
|
|
705
|
+
}
|