@cognitap/radiant 0.0.35 → 0.0.37
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 +18 -6
- 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 +88 -4
- package/dist/library/Menu.d.ts.map +1 -1
- package/dist/library/Menu.js +215 -27
- package/dist/library/Menu.js.map +1 -1
- package/dist/library/Menubar.d.ts +53 -0
- package/dist/library/Menubar.d.ts.map +1 -0
- package/dist/library/Menubar.js +246 -0
- package/dist/library/Menubar.js.map +1 -0
- 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 +4 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +6 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +3 -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/react/useRovingFocus.d.ts +3 -1
- package/dist/library/react/useRovingFocus.d.ts.map +1 -1
- package/dist/library/react/useRovingFocus.js +3 -2
- package/dist/library/react/useRovingFocus.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +9 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +2 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +36 -31
- package/package.json +1 -1
- package/src/ContextMenu.tsx +354 -0
- package/src/Menu.tsx +516 -43
- package/src/Menubar.tsx +496 -0
- package/src/Popover.tsx +46 -4
- package/src/cn.ts +4 -0
- package/src/index.ts +95 -0
- package/src/react/usePopoverSurface.ts +17 -4
- package/src/react/useRovingFocus.ts +5 -2
- package/src/themes/components.ts +2 -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
|
-
|
|
17
|
-
|
|
19
|
+
PopoverContentRoot,
|
|
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) {
|
|
65
87
|
return (
|
|
66
|
-
<
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
<MenuRoot
|
|
89
|
+
{...props}
|
|
90
|
+
anchorPoint={undefined}
|
|
91
|
+
family="menu"
|
|
92
|
+
parent={undefined}
|
|
93
|
+
focusIntent={undefined}
|
|
94
|
+
/>
|
|
69
95
|
)
|
|
70
96
|
}
|
|
71
|
-
|
|
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
|
+
}) {
|
|
111
|
+
return (
|
|
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>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
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}
|
|
@@ -138,21 +219,64 @@ export function MenuPortal(props: MenuPortalProps) {
|
|
|
138
219
|
useMenuContext()
|
|
139
220
|
return <PopoverPortal {...props} />
|
|
140
221
|
}
|
|
141
|
-
export function MenuContent(
|
|
222
|
+
export function MenuContent(props: MenuContentProps) {
|
|
223
|
+
return (
|
|
224
|
+
<MenuContentRoot
|
|
225
|
+
{...props}
|
|
226
|
+
onFocusOutside={undefined}
|
|
227
|
+
/>
|
|
228
|
+
)
|
|
229
|
+
}
|
|
230
|
+
/** Private bar focus boundary; public Menu retains shared outside dismissal. */
|
|
231
|
+
export function MenuContentRoot({
|
|
232
|
+
ref,
|
|
233
|
+
className,
|
|
234
|
+
onKeyDown,
|
|
235
|
+
onFocusOutside,
|
|
236
|
+
...props
|
|
237
|
+
}: MenuContentProps & { onFocusOutside?: () => void }) {
|
|
142
238
|
const menu = useMenuContext()
|
|
143
|
-
const
|
|
239
|
+
const nested = !!menu.parent || menu.family === 'menubar'
|
|
240
|
+
const triggerId = menu.triggerId
|
|
241
|
+
const capture = useCallback(
|
|
242
|
+
(node: HTMLDivElement | null) => {
|
|
243
|
+
const trigger = node?.ownerDocument.getElementById(triggerId)
|
|
244
|
+
// Establish the submenu's reading order before the native surface opens.
|
|
245
|
+
if (
|
|
246
|
+
nested &&
|
|
247
|
+
node &&
|
|
248
|
+
trigger &&
|
|
249
|
+
node.parentElement === trigger.parentElement &&
|
|
250
|
+
trigger.nextElementSibling !== node &&
|
|
251
|
+
!node.matches(':popover-open')
|
|
252
|
+
)
|
|
253
|
+
trigger.after(node)
|
|
254
|
+
},
|
|
255
|
+
[triggerId, nested]
|
|
256
|
+
)
|
|
257
|
+
const placed = useComposedRefs(menu.content, capture)
|
|
258
|
+
const host = useComposedRefs(placed, ref)
|
|
144
259
|
const search = useRef({ text: '', time: 0 })
|
|
260
|
+
const openIntent = useRef(menu.focusIntent)
|
|
261
|
+
useLayoutEffect(() => {
|
|
262
|
+
openIntent.current = menu.focusIntent
|
|
263
|
+
})
|
|
264
|
+
|
|
145
265
|
useLayoutEffect(() => {
|
|
146
266
|
search.current = { text: '', time: 0 }
|
|
147
267
|
const node = menu.content.current
|
|
148
268
|
if (!menu.open || !node) return
|
|
269
|
+
const edge = openIntent.current ?? menu.intent.current
|
|
270
|
+
if (edge === 'none') return
|
|
149
271
|
const options = items(node)
|
|
150
|
-
focusItem(node,
|
|
272
|
+
focusItem(node, edge === 'last' ? options.at(-1) : options[0])
|
|
151
273
|
}, [menu.open, menu.content, menu.intent])
|
|
152
274
|
return (
|
|
153
|
-
<
|
|
275
|
+
<PopoverContentRoot
|
|
154
276
|
{...props}
|
|
155
277
|
ref={host}
|
|
278
|
+
onFocusOutside={onFocusOutside}
|
|
279
|
+
initialFocusRef={undefined}
|
|
156
280
|
role="menu"
|
|
157
281
|
aria-orientation="vertical"
|
|
158
282
|
aria-labelledby={menu.triggerId}
|
|
@@ -172,8 +296,10 @@ export function MenuContent({ ref, className, onKeyDown, ...props }: MenuContent
|
|
|
172
296
|
if (target.closest('[role="menu"]') !== node) return
|
|
173
297
|
if (event.key === 'Tab') {
|
|
174
298
|
// Let the browser continue from the opener after closing, in either direction.
|
|
175
|
-
|
|
176
|
-
|
|
299
|
+
let root = menu
|
|
300
|
+
while (root.parent) root = root.parent
|
|
301
|
+
root.trigger.current?.focus({ preventScroll: true })
|
|
302
|
+
menu.closeTree()
|
|
177
303
|
return
|
|
178
304
|
}
|
|
179
305
|
if (event.altKey || event.ctrlKey || event.metaKey || event.nativeEvent.isComposing) return
|
|
@@ -206,7 +332,19 @@ export function MenuContent({ ref, className, onKeyDown, ...props }: MenuContent
|
|
|
206
332
|
/>
|
|
207
333
|
)
|
|
208
334
|
}
|
|
209
|
-
export function MenuItem({
|
|
335
|
+
export function MenuItem(props: MenuItemProps) {
|
|
336
|
+
return (
|
|
337
|
+
<MenuItemRoot
|
|
338
|
+
{...props}
|
|
339
|
+
itemRole="menuitem"
|
|
340
|
+
checkedValue={undefined}
|
|
341
|
+
/>
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
function MenuItemRoot({
|
|
345
|
+
itemRole = 'menuitem',
|
|
346
|
+
checkedValue,
|
|
347
|
+
closeOnSelect = true,
|
|
210
348
|
disabled = false,
|
|
211
349
|
textValue,
|
|
212
350
|
onSelect,
|
|
@@ -216,14 +354,20 @@ export function MenuItem({
|
|
|
216
354
|
className,
|
|
217
355
|
motion,
|
|
218
356
|
...props
|
|
219
|
-
}: MenuItemProps
|
|
357
|
+
}: MenuItemProps & {
|
|
358
|
+
itemRole?: 'menuitem' | 'menuitemcheckbox' | 'menuitemradio'
|
|
359
|
+
checkedValue?: boolean | 'mixed'
|
|
360
|
+
}) {
|
|
220
361
|
const menu = useMenuContext()
|
|
221
362
|
return (
|
|
222
|
-
<
|
|
363
|
+
<PopoverCloseRoot
|
|
364
|
+
requestClose={menu.closeTree}
|
|
365
|
+
closeOnClick={closeOnSelect}
|
|
223
366
|
{...props}
|
|
224
367
|
disabled={undefined}
|
|
225
368
|
aria-disabled={disabled || undefined}
|
|
226
|
-
role=
|
|
369
|
+
role={itemRole}
|
|
370
|
+
aria-checked={checkedValue ?? props['aria-checked']}
|
|
227
371
|
tabIndex={-1}
|
|
228
372
|
motion={disabled ? 'off' : motion}
|
|
229
373
|
data-slot={joinIds('menu-item', props['data-slot'])}
|
|
@@ -247,3 +391,332 @@ export function MenuItem({
|
|
|
247
391
|
/>
|
|
248
392
|
)
|
|
249
393
|
}
|
|
394
|
+
|
|
395
|
+
export type MenuGroupProps = Omit<ComponentProps<'div'>, 'style' | 'role'> & {
|
|
396
|
+
style?: ThemeStyle
|
|
397
|
+
'data-slot'?: string
|
|
398
|
+
}
|
|
399
|
+
export type MenuLabelProps = Omit<ComponentProps<'div'>, 'style' | 'role' | 'tabIndex'> & {
|
|
400
|
+
style?: ThemeStyle
|
|
401
|
+
'data-slot'?: string
|
|
402
|
+
}
|
|
403
|
+
export type MenuSeparatorProps = SeparatorProps
|
|
404
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Menu sections are ARIA groups, not native form fieldsets. */
|
|
405
|
+
export function MenuGroup({ className, ...props }: MenuGroupProps) {
|
|
406
|
+
useMenuContext()
|
|
407
|
+
return (
|
|
408
|
+
<div
|
|
409
|
+
{...props}
|
|
410
|
+
role="group"
|
|
411
|
+
data-slot={joinIds('menu-group', props['data-slot'])}
|
|
412
|
+
className={cn('grid gap-0', className)}
|
|
413
|
+
/>
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
/* oxlint-enable jsx-a11y/prefer-tag-over-role */
|
|
417
|
+
export function MenuLabel({ className, ...props }: MenuLabelProps) {
|
|
418
|
+
useMenuContext()
|
|
419
|
+
return (
|
|
420
|
+
<div
|
|
421
|
+
{...props}
|
|
422
|
+
role="presentation"
|
|
423
|
+
tabIndex={undefined}
|
|
424
|
+
data-slot={joinIds('menu-label', props['data-slot'])}
|
|
425
|
+
className={cn(
|
|
426
|
+
'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)]',
|
|
427
|
+
className
|
|
428
|
+
)}
|
|
429
|
+
/>
|
|
430
|
+
)
|
|
431
|
+
}
|
|
432
|
+
export function MenuSeparator(props: MenuSeparatorProps) {
|
|
433
|
+
useMenuContext()
|
|
434
|
+
return (
|
|
435
|
+
<Separator
|
|
436
|
+
{...props}
|
|
437
|
+
data-slot={joinIds('menu-separator', props['data-slot'])}
|
|
438
|
+
/>
|
|
439
|
+
)
|
|
440
|
+
}
|
|
441
|
+
type CheckableProps = DistributiveOmit<
|
|
442
|
+
MenuItemProps,
|
|
443
|
+
'pressed' | 'defaultPressed' | 'onPressedChange' | 'aria-pressed' | 'aria-checked' | 'type'
|
|
444
|
+
> & { parts?: { indicator?: PartStyle } }
|
|
445
|
+
export type MenuCheckboxItemProps = CheckableProps &
|
|
446
|
+
(
|
|
447
|
+
| { checked: boolean; defaultChecked?: never }
|
|
448
|
+
| { checked?: undefined; defaultChecked?: boolean }
|
|
449
|
+
) & { onCheckedChange?: (checked: boolean) => void; indeterminate?: boolean }
|
|
450
|
+
const plainText = (children: ReactNode) =>
|
|
451
|
+
typeof children === 'string' || typeof children === 'number' ? String(children) : undefined
|
|
452
|
+
function CheckMark({ children, part }: { children?: ReactNode; part?: PartStyle }) {
|
|
453
|
+
return (
|
|
454
|
+
<span
|
|
455
|
+
aria-hidden="true"
|
|
456
|
+
data-slot="menu-item-indicator"
|
|
457
|
+
className={cn('inline-flex w-4 shrink-0 items-center justify-center', part?.className)}
|
|
458
|
+
style={part?.style}
|
|
459
|
+
>
|
|
460
|
+
{children}
|
|
461
|
+
</span>
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
export function MenuCheckboxItem({
|
|
465
|
+
checked,
|
|
466
|
+
defaultChecked,
|
|
467
|
+
onCheckedChange,
|
|
468
|
+
indeterminate = false,
|
|
469
|
+
parts,
|
|
470
|
+
children,
|
|
471
|
+
textValue,
|
|
472
|
+
onSelect,
|
|
473
|
+
closeOnSelect = false,
|
|
474
|
+
...props
|
|
475
|
+
}: MenuCheckboxItemProps) {
|
|
476
|
+
const state = useControllableState({
|
|
477
|
+
name: 'MenuCheckboxItem',
|
|
478
|
+
value: checked,
|
|
479
|
+
defaultValue: defaultChecked,
|
|
480
|
+
initialValue: false,
|
|
481
|
+
onChange: onCheckedChange
|
|
482
|
+
})
|
|
483
|
+
return (
|
|
484
|
+
<MenuItemRoot
|
|
485
|
+
{...props}
|
|
486
|
+
type="button"
|
|
487
|
+
pressed={undefined}
|
|
488
|
+
defaultPressed={undefined}
|
|
489
|
+
onPressedChange={undefined}
|
|
490
|
+
aria-pressed={undefined}
|
|
491
|
+
itemRole="menuitemcheckbox"
|
|
492
|
+
checkedValue={indeterminate ? 'mixed' : state.value}
|
|
493
|
+
closeOnSelect={closeOnSelect}
|
|
494
|
+
textValue={textValue ?? plainText(children)}
|
|
495
|
+
data-slot={joinIds('menu-checkbox-item', props['data-slot'])}
|
|
496
|
+
onSelect={event => {
|
|
497
|
+
onSelect?.(event)
|
|
498
|
+
if (!event.defaultPrevented) state.request(indeterminate || !state.value)
|
|
499
|
+
}}
|
|
500
|
+
>
|
|
501
|
+
<CheckMark part={parts?.indicator}>
|
|
502
|
+
{indeterminate ? '−' : state.value ? '✓' : null}
|
|
503
|
+
</CheckMark>
|
|
504
|
+
{children}
|
|
505
|
+
</MenuItemRoot>
|
|
506
|
+
)
|
|
507
|
+
}
|
|
508
|
+
export type MenuRadioGroupProps = Omit<MenuGroupProps, 'defaultValue'> &
|
|
509
|
+
ValueOwner<string | null> & { onValueChange?: (value: string | null) => void }
|
|
510
|
+
type RadioContextValue = { value: string | null; request: (value: string) => void }
|
|
511
|
+
const RadioContext = createContext<RadioContextValue | null>(null)
|
|
512
|
+
export function MenuRadioGroup({
|
|
513
|
+
value,
|
|
514
|
+
defaultValue,
|
|
515
|
+
onValueChange,
|
|
516
|
+
...props
|
|
517
|
+
}: MenuRadioGroupProps) {
|
|
518
|
+
const state = useControllableState<string | null>({
|
|
519
|
+
name: 'MenuRadioGroup',
|
|
520
|
+
value,
|
|
521
|
+
defaultValue,
|
|
522
|
+
initialValue: null,
|
|
523
|
+
onChange: onValueChange
|
|
524
|
+
})
|
|
525
|
+
return (
|
|
526
|
+
<RadioContext.Provider value={{ value: state.value, request: state.request }}>
|
|
527
|
+
<MenuGroup
|
|
528
|
+
{...props}
|
|
529
|
+
data-slot={joinIds('menu-radio-group', props['data-slot'])}
|
|
530
|
+
/>
|
|
531
|
+
</RadioContext.Provider>
|
|
532
|
+
)
|
|
533
|
+
}
|
|
534
|
+
export type MenuRadioItemProps = DistributiveOmit<CheckableProps, 'value'> & { value: string }
|
|
535
|
+
export function MenuRadioItem({
|
|
536
|
+
value,
|
|
537
|
+
children,
|
|
538
|
+
textValue,
|
|
539
|
+
parts,
|
|
540
|
+
onSelect,
|
|
541
|
+
closeOnSelect = false,
|
|
542
|
+
...props
|
|
543
|
+
}: MenuRadioItemProps) {
|
|
544
|
+
const radio = useContext(RadioContext)
|
|
545
|
+
if (!radio) throw new Error('MenuRadioItem must render inside MenuRadioGroup.')
|
|
546
|
+
if (typeof value !== 'string') throw new TypeError('MenuRadioItem needs a string value.')
|
|
547
|
+
const checked = radio.value === value
|
|
548
|
+
return (
|
|
549
|
+
<MenuItemRoot
|
|
550
|
+
{...props}
|
|
551
|
+
type="button"
|
|
552
|
+
value={value}
|
|
553
|
+
pressed={undefined}
|
|
554
|
+
defaultPressed={undefined}
|
|
555
|
+
onPressedChange={undefined}
|
|
556
|
+
aria-pressed={undefined}
|
|
557
|
+
itemRole="menuitemradio"
|
|
558
|
+
checkedValue={checked}
|
|
559
|
+
closeOnSelect={closeOnSelect}
|
|
560
|
+
textValue={textValue ?? plainText(children)}
|
|
561
|
+
data-slot={joinIds('menu-radio-item', props['data-slot'])}
|
|
562
|
+
onSelect={event => {
|
|
563
|
+
onSelect?.(event)
|
|
564
|
+
if (!event.defaultPrevented) radio.request(value)
|
|
565
|
+
}}
|
|
566
|
+
>
|
|
567
|
+
<CheckMark part={parts?.indicator}>{checked ? '●' : null}</CheckMark>
|
|
568
|
+
{children}
|
|
569
|
+
</MenuItemRoot>
|
|
570
|
+
)
|
|
571
|
+
}
|
|
572
|
+
export type MenuSubProps = MenuProps
|
|
573
|
+
export type MenuSubTriggerProps = DistributiveOmit<
|
|
574
|
+
MenuItemProps,
|
|
575
|
+
'closeOnSelect' | 'id' | 'aria-haspopup' | 'aria-expanded' | 'aria-controls' | 'type'
|
|
576
|
+
> & { parts?: { indicator?: PartStyle } }
|
|
577
|
+
export type MenuSubContentProps = MenuContentProps
|
|
578
|
+
export function MenuSub(props: MenuSubProps) {
|
|
579
|
+
const parent = useMenuContext()
|
|
580
|
+
return (
|
|
581
|
+
<MenuRoot
|
|
582
|
+
{...props}
|
|
583
|
+
anchorPoint={undefined}
|
|
584
|
+
parent={parent}
|
|
585
|
+
family={parent.family}
|
|
586
|
+
focusIntent={undefined}
|
|
587
|
+
/>
|
|
588
|
+
)
|
|
589
|
+
}
|
|
590
|
+
function openMenu(menu: Context, edge: Intent) {
|
|
591
|
+
menu.setIntent(edge)
|
|
592
|
+
if (menu.open && menu.content.current) {
|
|
593
|
+
if (edge !== 'none') {
|
|
594
|
+
const options = items(menu.content.current)
|
|
595
|
+
focusItem(menu.content.current, edge === 'last' ? options.at(-1) : options[0])
|
|
596
|
+
}
|
|
597
|
+
} else menu.setOpen(true)
|
|
598
|
+
}
|
|
599
|
+
export function MenuSubTrigger({
|
|
600
|
+
ref,
|
|
601
|
+
children,
|
|
602
|
+
textValue,
|
|
603
|
+
parts,
|
|
604
|
+
className,
|
|
605
|
+
onSelect,
|
|
606
|
+
onKeyDown,
|
|
607
|
+
onPointerMove,
|
|
608
|
+
disabled,
|
|
609
|
+
pending,
|
|
610
|
+
...props
|
|
611
|
+
}: MenuSubTriggerProps) {
|
|
612
|
+
const menu = useMenuContext(),
|
|
613
|
+
popover = usePopoverContext(),
|
|
614
|
+
host = useComposedRefs<HTMLButtonElement>(menu.setTrigger, ref),
|
|
615
|
+
anchor = useComposedRefs<HTMLButtonElement>(popover.setTrigger, host)
|
|
616
|
+
if (!menu.parent) throw new Error('MenuSubTrigger must render inside MenuSub.')
|
|
617
|
+
return (
|
|
618
|
+
<MenuContext.Provider value={menu.parent}>
|
|
619
|
+
<MenuItemRoot
|
|
620
|
+
{...props}
|
|
621
|
+
textValue={textValue ?? plainText(children)}
|
|
622
|
+
disabled={disabled}
|
|
623
|
+
pending={pending}
|
|
624
|
+
ref={anchor}
|
|
625
|
+
type="button"
|
|
626
|
+
id={menu.triggerId}
|
|
627
|
+
aria-haspopup="menu"
|
|
628
|
+
aria-controls={menu.contentId}
|
|
629
|
+
aria-expanded={menu.open}
|
|
630
|
+
data-slot={joinIds('menu-sub-trigger', props['data-slot'])}
|
|
631
|
+
closeOnSelect={false}
|
|
632
|
+
className={cn(
|
|
633
|
+
'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)))]',
|
|
634
|
+
className
|
|
635
|
+
)}
|
|
636
|
+
onSelect={event => {
|
|
637
|
+
onSelect?.(event)
|
|
638
|
+
if (!event.defaultPrevented) openMenu(menu, 'first')
|
|
639
|
+
}}
|
|
640
|
+
onKeyDown={composeAction(onKeyDown, event => {
|
|
641
|
+
const rtl =
|
|
642
|
+
event.currentTarget.ownerDocument.defaultView!.getComputedStyle(event.currentTarget)
|
|
643
|
+
.direction === 'rtl'
|
|
644
|
+
if (
|
|
645
|
+
event.key !== (rtl ? 'ArrowLeft' : 'ArrowRight') ||
|
|
646
|
+
event.altKey ||
|
|
647
|
+
event.ctrlKey ||
|
|
648
|
+
event.metaKey ||
|
|
649
|
+
event.nativeEvent.isComposing ||
|
|
650
|
+
disabled ||
|
|
651
|
+
pending ||
|
|
652
|
+
event.currentTarget.matches(':disabled')
|
|
653
|
+
)
|
|
654
|
+
return
|
|
655
|
+
event.preventDefault()
|
|
656
|
+
openMenu(menu, 'first')
|
|
657
|
+
})}
|
|
658
|
+
onPointerMove={composeAction(onPointerMove, event => {
|
|
659
|
+
if (
|
|
660
|
+
event.pointerType !== 'touch' &&
|
|
661
|
+
!disabled &&
|
|
662
|
+
!pending &&
|
|
663
|
+
!event.currentTarget.matches(':disabled')
|
|
664
|
+
)
|
|
665
|
+
openMenu(menu, 'none')
|
|
666
|
+
})}
|
|
667
|
+
>
|
|
668
|
+
<span className="min-w-0 flex-1">{children}</span>
|
|
669
|
+
<CheckMark part={parts?.indicator}>
|
|
670
|
+
<span className="inline-block rtl:rotate-180">›</span>
|
|
671
|
+
</CheckMark>
|
|
672
|
+
</MenuItemRoot>
|
|
673
|
+
</MenuContext.Provider>
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
export function MenuSubContent({ side, onKeyDown, ...props }: MenuSubContentProps) {
|
|
677
|
+
const menu = useMenuContext(),
|
|
678
|
+
[inlineSide, setInlineSide] = useState<'right' | 'left'>('right')
|
|
679
|
+
if (!menu.parent) throw new Error('MenuSubContent must render inside MenuSub.')
|
|
680
|
+
useLayoutEffect(() => {
|
|
681
|
+
const trigger = menu.trigger.current
|
|
682
|
+
if (!trigger) return
|
|
683
|
+
const view = trigger.ownerDocument.defaultView!,
|
|
684
|
+
update = () =>
|
|
685
|
+
setInlineSide(view.getComputedStyle(trigger).direction === 'rtl' ? 'left' : 'right'),
|
|
686
|
+
observer = new view.MutationObserver(update)
|
|
687
|
+
update()
|
|
688
|
+
for (let node: HTMLElement | null = trigger; node; node = node.parentElement)
|
|
689
|
+
observer.observe(node, { attributes: true, attributeFilter: ['dir', 'class', 'style'] })
|
|
690
|
+
view.addEventListener('resize', update)
|
|
691
|
+
return () => {
|
|
692
|
+
observer.disconnect()
|
|
693
|
+
view.removeEventListener('resize', update)
|
|
694
|
+
}
|
|
695
|
+
}, [menu.trigger])
|
|
696
|
+
return (
|
|
697
|
+
<MenuContent
|
|
698
|
+
{...props}
|
|
699
|
+
side={side ?? inlineSide}
|
|
700
|
+
sideOffset={props.sideOffset ?? 4}
|
|
701
|
+
data-slot={joinIds('menu-sub-content', props['data-slot'])}
|
|
702
|
+
onKeyDown={composeAction(onKeyDown, event => {
|
|
703
|
+
const rtl =
|
|
704
|
+
event.currentTarget.ownerDocument.defaultView!.getComputedStyle(event.currentTarget)
|
|
705
|
+
.direction === 'rtl'
|
|
706
|
+
if (
|
|
707
|
+
event.key !== (rtl ? 'ArrowRight' : 'ArrowLeft') ||
|
|
708
|
+
event.altKey ||
|
|
709
|
+
event.ctrlKey ||
|
|
710
|
+
event.metaKey ||
|
|
711
|
+
event.nativeEvent.isComposing ||
|
|
712
|
+
(event.target as HTMLElement).closest('[role="menu"]') !== event.currentTarget
|
|
713
|
+
)
|
|
714
|
+
return
|
|
715
|
+
event.preventDefault()
|
|
716
|
+
event.stopPropagation()
|
|
717
|
+
menu.setOpen(false)
|
|
718
|
+
menu.trigger.current?.focus({ preventScroll: true })
|
|
719
|
+
})}
|
|
720
|
+
/>
|
|
721
|
+
)
|
|
722
|
+
}
|