@luxalgo/vela-ui 0.2.0 → 0.4.0
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 +153 -91
- package/LICENSE +201 -201
- package/NOTICE +28 -28
- package/README.md +86 -86
- package/cli/engine.mjs +282 -282
- package/cli/index.mjs +154 -154
- package/dist/badge.js +1 -1
- package/dist/bar-chart.d.ts +1 -1
- package/dist/bar-chart.js +1 -1
- package/dist/button.d.ts +1 -1
- package/dist/button.js +4 -4
- package/dist/callout-bubble.d.ts +1 -1
- package/dist/callout-bubble.js +4 -4
- package/dist/card.d.ts +52 -0
- package/dist/card.js +179 -0
- package/dist/checkbox.js +5 -5
- package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
- package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
- package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
- package/dist/chunk-GUIYFQ27.js +30 -0
- package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
- package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
- package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
- package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
- package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
- package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
- package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
- package/dist/collapsible.d.ts +1 -1
- package/dist/collapsible.js +3 -3
- package/dist/color-picker.js +5 -5
- package/dist/context-menu.js +5 -5
- package/dist/data-table.d.ts +1 -1
- package/dist/data-table.js +1 -1
- package/dist/date-picker.d.ts +1 -1
- package/dist/date-picker.js +2 -2
- package/dist/dialog.d.ts +9 -1
- package/dist/dialog.js +15 -5
- package/dist/donut.d.ts +1 -1
- package/dist/donut.js +1 -1
- package/dist/drawer.d.ts +1 -1
- package/dist/drawer.js +2 -2
- package/dist/dropdown-menu.d.ts +1 -1
- package/dist/dropdown-menu.js +5 -5
- package/dist/empty.d.ts +1 -1
- package/dist/empty.js +2 -2
- package/dist/field.js +13 -13
- package/dist/{stat.d.ts → figure.d.ts} +9 -9
- package/dist/figure.js +41 -0
- package/dist/glyph-select.js +5 -5
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.js +3 -3
- package/dist/icons.js +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -3
- package/dist/input-group.d.ts +1 -1
- package/dist/input-group.js +4 -4
- package/dist/input.js +2 -2
- package/dist/item.d.ts +1 -1
- package/dist/item.js +2 -2
- package/dist/kbd.d.ts +1 -1
- package/dist/kbd.js +2 -2
- package/dist/line-chart.d.ts +1 -1
- package/dist/line-chart.js +1 -1
- package/dist/manifest.json +691 -74
- package/dist/month-grid.js +1 -1
- package/dist/number-input.d.ts +1 -1
- package/dist/number-input.js +5 -5
- package/dist/popover.js +3 -3
- package/dist/scatter-chart.d.ts +1 -1
- package/dist/scatter-chart.js +1 -1
- package/dist/select.d.ts +1 -1
- package/dist/select.js +7 -7
- package/dist/slider.d.ts +1 -1
- package/dist/slider.js +2 -2
- package/dist/sort-button.js +4 -4
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.js +1 -1
- package/dist/switch.d.ts +1 -1
- package/dist/switch.js +3 -3
- package/dist/tabs.d.ts +1 -1
- package/dist/tabs.js +4 -4
- package/dist/text-area.d.ts +1 -1
- package/dist/text-area.js +2 -2
- package/dist/theme-TefB8vtl.d.ts +341 -0
- package/dist/theme.d.ts +42 -192
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +321 -30
- package/dist/toast.d.ts +49 -0
- package/dist/toast.js +463 -0
- package/dist/toggle-group.d.ts +1 -1
- package/dist/toggle-group.js +4 -4
- package/dist/tooltip.js +2 -2
- package/dist/treemap.d.ts +80 -0
- package/dist/treemap.js +470 -0
- package/dist/zag.d.ts +11 -2
- package/dist/zag.js +1 -1
- package/docs/theming.md +217 -217
- package/docs/what-to-change.md +20 -20
- package/docs/writing-a-component.md +250 -250
- package/llms.txt +6 -3
- package/package.json +90 -90
- package/registry/badge.ts +119 -119
- package/registry/bar-chart.ts +215 -215
- package/registry/button.ts +387 -379
- package/registry/callout-bubble.ts +347 -347
- package/registry/card.ts +379 -0
- package/registry/checkbox.ts +234 -234
- package/registry/collapsible.ts +271 -271
- package/registry/color-picker.ts +582 -582
- package/registry/context-menu.ts +208 -208
- package/registry/data-table.ts +215 -215
- package/registry/date-picker.ts +483 -483
- package/registry/dialog.ts +551 -535
- package/registry/donut.ts +145 -145
- package/registry/drawer.ts +636 -636
- package/registry/dropdown-menu.ts +762 -762
- package/registry/empty.ts +91 -91
- package/registry/field.ts +584 -584
- package/registry/{stat.ts → figure.ts} +96 -96
- package/registry/glyph-select.ts +268 -268
- package/registry/hover-card.ts +222 -222
- package/registry/input-group.ts +229 -229
- package/registry/input.ts +181 -181
- package/registry/item.ts +376 -376
- package/registry/kbd.ts +64 -64
- package/registry/line-chart.ts +218 -218
- package/registry/month-grid.ts +328 -328
- package/registry/number-input.ts +443 -443
- package/registry/popover.ts +368 -368
- package/registry/scatter-chart.ts +221 -221
- package/registry/select.ts +620 -620
- package/registry/slider.ts +259 -259
- package/registry/sort-button.ts +189 -189
- package/registry/sparkline.ts +114 -114
- package/registry/switch.ts +205 -205
- package/registry/tabs.ts +426 -426
- package/registry/text-area.ts +186 -186
- package/registry/toast.ts +965 -0
- package/registry/toggle-group.ts +295 -295
- package/registry/tooltip.ts +156 -156
- package/registry/treemap.ts +847 -0
- package/skills/theming-vela-ui/SKILL.md +137 -137
- package/skills/using-vela-ui-components/SKILL.md +139 -137
- package/skills/writing-vela-ui-components/SKILL.md +163 -163
- package/src/components/badge.ts +119 -119
- package/src/components/bar-chart.ts +215 -215
- package/src/components/button.ts +387 -379
- package/src/components/callout-bubble.ts +347 -347
- package/src/components/card.ts +379 -0
- package/src/components/checkbox.ts +234 -234
- package/src/components/collapsible.ts +271 -271
- package/src/components/color-picker.ts +582 -582
- package/src/components/context-menu.ts +208 -208
- package/src/components/data-table.ts +215 -215
- package/src/components/date-picker.ts +483 -483
- package/src/components/dialog.ts +551 -535
- package/src/components/donut.ts +145 -145
- package/src/components/drawer.ts +636 -636
- package/src/components/dropdown-menu.ts +762 -762
- package/src/components/empty.ts +91 -91
- package/src/components/field.ts +584 -584
- package/src/components/{stat.ts → figure.ts} +96 -96
- package/src/components/glyph-select.ts +268 -268
- package/src/components/hover-card.ts +222 -222
- package/src/components/input-group.ts +229 -229
- package/src/components/input.ts +181 -181
- package/src/components/item.ts +376 -376
- package/src/components/kbd.ts +64 -64
- package/src/components/line-chart.ts +218 -218
- package/src/components/month-grid.ts +328 -328
- package/src/components/number-input.ts +443 -443
- package/src/components/popover.ts +368 -368
- package/src/components/scatter-chart.ts +221 -221
- package/src/components/select.ts +620 -620
- package/src/components/slider.ts +259 -259
- package/src/components/sort-button.ts +189 -189
- package/src/components/sparkline.ts +114 -114
- package/src/components/switch.ts +205 -205
- package/src/components/tabs.ts +426 -426
- package/src/components/text-area.ts +186 -186
- package/src/components/toast.ts +965 -0
- package/src/components/toggle-group.ts +295 -295
- package/src/components/tooltip.ts +156 -156
- package/src/components/treemap.ts +847 -0
- package/src/index.ts +24 -24
- package/src/lib/calendar.ts +117 -117
- package/src/lib/chart.ts +72 -72
- package/src/lib/color.ts +89 -89
- package/src/lib/component-index.ts +163 -151
- package/src/lib/css.ts +48 -48
- package/src/lib/define.ts +45 -45
- package/src/lib/dispose.ts +36 -36
- package/src/lib/h.ts +55 -55
- package/src/lib/headless.ts +16 -16
- package/src/lib/i18n.ts +30 -30
- package/src/lib/icons.ts +244 -237
- package/src/lib/kit-text.ts +26 -26
- package/src/lib/page.ts +24 -24
- package/src/lib/palette.ts +81 -81
- package/src/lib/placement.ts +188 -188
- package/src/lib/scope.ts +135 -135
- package/src/lib/styles.ts +53 -53
- package/src/lib/surface-events.ts +31 -31
- package/src/lib/surface-exit.ts +87 -87
- package/src/lib/t.ts +68 -68
- package/src/lib/theme.ts +684 -664
- package/src/lib/tokens.ts +110 -110
- package/src/lib/variants.ts +32 -32
- package/src/lib/zag.ts +61 -46
- package/stylelint/index.mjs +60 -60
- package/stylelint/lint.mjs +81 -81
- package/stylelint/plugin.mjs +115 -115
- package/dist/chunk-P4MTEMT7.js +0 -23
- package/dist/stat.js +0 -41
- package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
- package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
- package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
|
@@ -1,762 +1,762 @@
|
|
|
1
|
-
// DropdownMenu: MenuItemDescriptor[] projected into a floating list over Zag's menu machine
|
|
2
|
-
// (keyboard navigation, typeahead and dismissal belong to the machine). Items are data
|
|
3
|
-
// descriptors, the plugin contribution currency. An item carrying `submenu` becomes a
|
|
4
|
-
// trigger row owning a nested machine and list, registered as this machine's child: Zag
|
|
5
|
-
// drives hover intent, arrow-key traversal and the dismiss chain across levels.
|
|
6
|
-
import * as zagMenu from '@zag-js/menu';
|
|
7
|
-
import { msg } from '@luxalgo/vela-ui/i18n';
|
|
8
|
-
import { textOf } from '@luxalgo/vela-ui/i18n';
|
|
9
|
-
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
10
|
-
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
11
|
-
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
12
|
-
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
13
|
-
import { defineComponent } from '@luxalgo/vela-ui';
|
|
14
|
-
import { addSlot } from '@luxalgo/vela-ui';
|
|
15
|
-
import { t } from '@luxalgo/vela-ui';
|
|
16
|
-
import { announceSurface } from '@luxalgo/vela-ui/surface-events';
|
|
17
|
-
import { holdForExit, type SurfaceExit } from '@luxalgo/vela-ui/surface-exit';
|
|
18
|
-
import { floatingLayerHost } from '@luxalgo/vela-ui/theme';
|
|
19
|
-
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
|
|
20
|
-
import { splitPlacement, zagPositioning, type PlacementProps } from '@luxalgo/vela-ui/placement';
|
|
21
|
-
|
|
22
|
-
/** One menu entry. `id` is the selection value reported to `onSelect`. */
|
|
23
|
-
export interface MenuItemDescriptor {
|
|
24
|
-
id: string;
|
|
25
|
-
label: string;
|
|
26
|
-
disabled?: boolean;
|
|
27
|
-
/** Draw a separator line above this item. */
|
|
28
|
-
separatorBefore?: boolean;
|
|
29
|
-
/** Right-aligned hint (e.g. a shortcut display from KeymapManager). */
|
|
30
|
-
hint?: string;
|
|
31
|
-
/** Selected state (undefined = plain item). A plain checked item renders as a
|
|
32
|
-
* highlighted row (brighter surface + bright ink) — or, in a menu built with
|
|
33
|
-
* `checkmarks`, as a leading ✓ with the row surface left free for hover. */
|
|
34
|
-
checked?: boolean;
|
|
35
|
-
/** Render as a SWITCH row (a right-aligned toggle pill reflecting `checked`)
|
|
36
|
-
* instead of the selected-row highlight, and keep the menu OPEN on selection —
|
|
37
|
-
* the shape for boolean settings living inside a dropdown. */
|
|
38
|
-
toggle?: boolean;
|
|
39
|
-
/** Icon id (see the `vela/ui` icon registry) rendered before the label. A level where
|
|
40
|
-
* any row has one gives its iconless rows an empty slot, so the labels align. */
|
|
41
|
-
icon?: string;
|
|
42
|
-
/** Favorite-star affordance at the row's right edge: `false` renders an outline
|
|
43
|
-
* star revealed on row hover, `true` a filled star that stays visible. Clicking
|
|
44
|
-
* it reports through the menu's `onFavorite` WITHOUT selecting (or closing) the
|
|
45
|
-
* row — undefined rows carry no star at all. */
|
|
46
|
-
favorite?: boolean;
|
|
47
|
-
/** Nested entries — the row becomes a submenu trigger opening its own list to the side.
|
|
48
|
-
* A branch is not selectable itself: `onSelect` only ever reports leaf ids. */
|
|
49
|
-
submenu?: readonly MenuItemDescriptor[];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface DropdownMenuControllerOptions extends PlacementProps {
|
|
53
|
-
items: readonly MenuItemDescriptor[];
|
|
54
|
-
onSelect?: (id: string) => void;
|
|
55
|
-
/** @deprecated Use `side` and `align`. */
|
|
56
|
-
placement?: zagMenu.Props['positioning'] extends infer P | undefined ? (P extends { placement?: infer PL } ? PL : never) : never;
|
|
57
|
-
onOpenChange?: (open: boolean) => void;
|
|
58
|
-
/** Pin the floating list to this rect (viewport coords). Used to keep an open
|
|
59
|
-
* menu still when its trigger moves — starring a timeframe adds a chip and
|
|
60
|
-
* would otherwise drag the list with the caret. `null` falls through to the
|
|
61
|
-
* live trigger. */
|
|
62
|
-
getAnchorRect?: () => { x: number; y: number; width: number; height: number } | null;
|
|
63
|
-
/** Share the trigger's DOM id with other machines composed on the same element. */
|
|
64
|
-
triggerId?: string;
|
|
65
|
-
/** Pin the machine's own id instead of taking a fresh one. A parent registers its
|
|
66
|
-
* submenus under this key, so rebuilding a branch replaces its entry rather than
|
|
67
|
-
* piling a new one on top. */
|
|
68
|
-
id?: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export type DropdownMenuService = zagMenu.Service;
|
|
72
|
-
export type DropdownMenuApi = zagMenu.Api;
|
|
73
|
-
|
|
74
|
-
export interface DropdownMenuController {
|
|
75
|
-
machine: typeof zagMenu.machine;
|
|
76
|
-
props: Partial<zagMenu.Props>;
|
|
77
|
-
connect(service: DropdownMenuService): DropdownMenuApi;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Zag's menu machine with the options mapped onto it, for any view layer. */
|
|
81
|
-
export function dropdownMenuController(opts: DropdownMenuControllerOptions): DropdownMenuController {
|
|
82
|
-
return {
|
|
83
|
-
machine: zagMenu.machine,
|
|
84
|
-
props: {
|
|
85
|
-
id: opts.id ?? nextUid('vela-menu'),
|
|
86
|
-
ids: opts.triggerId ? { trigger: opts.triggerId } : undefined,
|
|
87
|
-
positioning: {
|
|
88
|
-
...zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: 'bottom', align: 'start' }),
|
|
89
|
-
...(opts.getAnchorRect ? { getAnchorRect: () => opts.getAnchorRect!() } : {}),
|
|
90
|
-
},
|
|
91
|
-
onSelect: (d: zagMenu.SelectionDetails) => opts.onSelect?.(d.value),
|
|
92
|
-
onOpenChange: (d: zagMenu.OpenChangeDetails) => opts.onOpenChange?.(d.open),
|
|
93
|
-
} satisfies Partial<zagMenu.Props>,
|
|
94
|
-
connect: (service: DropdownMenuService): DropdownMenuApi => zagMenu.connect(service, normalizeProps),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export const dropdownMenuDef = defineComponent({
|
|
99
|
-
name: 'dropdown-menu',
|
|
100
|
-
parts: ['content', 'item', 'icon', 'label', 'hint', 'mark', 'badge', 'star', 'switch', 'arrow', 'separator'],
|
|
101
|
-
settings: { radius: t.radius.md, 'switch-radius': '9px' },
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const injectStyles = defineStyles(
|
|
105
|
-
'vela-ui-menu',
|
|
106
|
-
css`
|
|
107
|
-
@layer vela.ui {
|
|
108
|
-
[data-slot~='dropdown-menu-content'] {
|
|
109
|
-
/* The list is a <ul>: without this, block rows (the separator) paint a ::marker dot. */
|
|
110
|
-
list-style: none;
|
|
111
|
-
margin: 0;
|
|
112
|
-
background: ${t.surface};
|
|
113
|
-
color: ${t.fg};
|
|
114
|
-
border: 1px solid ${t.border.strong};
|
|
115
|
-
border-radius: ${dropdownMenuDef.vars.radius};
|
|
116
|
-
box-shadow: ${t.shadow};
|
|
117
|
-
padding: 6px;
|
|
118
|
-
min-width: 180px;
|
|
119
|
-
/* Never past the room its clipping container leaves on the side it opened on
|
|
120
|
-
(Zag measures it onto the positioner): a menu in a short chart scrolls inside
|
|
121
|
-
itself instead of overflowing the chart and scrolling the page around it. */
|
|
122
|
-
max-height: min(60vh, var(--available-height, 60vh));
|
|
123
|
-
overflow-y: auto;
|
|
124
|
-
font-size: 13px;
|
|
125
|
-
z-index: ${t.z.menu};
|
|
126
|
-
outline: 2px solid transparent;
|
|
127
|
-
|
|
128
|
-
&[data-state='open'] {
|
|
129
|
-
animation: vela-menu-in ${t.dur.fast} ${t.ease};
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
@keyframes vela-menu-in {
|
|
133
|
-
from {
|
|
134
|
-
opacity: 0;
|
|
135
|
-
transform: translateY(-3px);
|
|
136
|
-
}
|
|
137
|
-
to {
|
|
138
|
-
opacity: 1;
|
|
139
|
-
transform: translateY(0);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
[data-slot~='dropdown-menu-item'] {
|
|
143
|
-
display: flex;
|
|
144
|
-
align-items: center;
|
|
145
|
-
gap: 10px;
|
|
146
|
-
padding: 7px 10px;
|
|
147
|
-
border-radius: ${t.radius.sm};
|
|
148
|
-
cursor: pointer;
|
|
149
|
-
user-select: none;
|
|
150
|
-
white-space: nowrap;
|
|
151
|
-
|
|
152
|
-
&[data-highlighted] {
|
|
153
|
-
background: ${t.hover};
|
|
154
|
-
}
|
|
155
|
-
&[data-disabled] {
|
|
156
|
-
opacity: 0.4;
|
|
157
|
-
cursor: default;
|
|
158
|
-
}
|
|
159
|
-
& [data-slot~='dropdown-menu-icon'] {
|
|
160
|
-
width: 16px;
|
|
161
|
-
height: 16px;
|
|
162
|
-
font-size: 16px;
|
|
163
|
-
justify-content: center;
|
|
164
|
-
color: ${t.fg.muted};
|
|
165
|
-
}
|
|
166
|
-
& [data-slot~='dropdown-menu-label'] {
|
|
167
|
-
flex: 1 1 auto;
|
|
168
|
-
}
|
|
169
|
-
& [data-slot~='dropdown-menu-hint'] {
|
|
170
|
-
color: ${t.fg.faint};
|
|
171
|
-
font-size: ${t.fontSize.sm};
|
|
172
|
-
}
|
|
173
|
-
/* The active entry: the row surface carries the selection, a stronger wash
|
|
174
|
-
than the hover one plus bright ink. After the hover rule, so a selected row
|
|
175
|
-
stays visibly selected while highlighted. */
|
|
176
|
-
&[data-checked] {
|
|
177
|
-
background: ${t.hover.strong};
|
|
178
|
-
color: ${t.fg.bright};
|
|
179
|
-
}
|
|
180
|
-
/* Checkmark mode (context and action menus): every row reserves the leading
|
|
181
|
-
mark column so labels align; a checked row fills it with a ✓ and brightens
|
|
182
|
-
its ink, leaving the row surface free for the hover wash. */
|
|
183
|
-
& [data-slot~='dropdown-menu-mark'] {
|
|
184
|
-
width: 14px;
|
|
185
|
-
flex: none;
|
|
186
|
-
display: inline-flex;
|
|
187
|
-
align-items: center;
|
|
188
|
-
justify-content: center;
|
|
189
|
-
}
|
|
190
|
-
& [data-slot~='dropdown-menu-mark'] [data-slot~='dropdown-menu-icon'] {
|
|
191
|
-
width: 14px;
|
|
192
|
-
height: 14px;
|
|
193
|
-
font-size: 14px;
|
|
194
|
-
color: ${t.fg.bright};
|
|
195
|
-
}
|
|
196
|
-
&[data-checkmark] {
|
|
197
|
-
color: ${t.fg.bright};
|
|
198
|
-
}
|
|
199
|
-
/* Badge mode: the row icon sits in a rounded tile and the row grows to fit it.
|
|
200
|
-
An icon-less row of the same level keeps an invisible slot, so labels align. */
|
|
201
|
-
& [data-slot~='dropdown-menu-badge'] {
|
|
202
|
-
width: 24px;
|
|
203
|
-
height: 24px;
|
|
204
|
-
padding: 3px;
|
|
205
|
-
flex: none;
|
|
206
|
-
box-sizing: border-box;
|
|
207
|
-
display: inline-flex;
|
|
208
|
-
align-items: center;
|
|
209
|
-
justify-content: center;
|
|
210
|
-
border-radius: ${t.radius.md};
|
|
211
|
-
background: ${t.hover};
|
|
212
|
-
border: 1px solid ${t.border};
|
|
213
|
-
color: ${t.fg.muted};
|
|
214
|
-
transition:
|
|
215
|
-
background ${t.dur.fast} ${t.ease},
|
|
216
|
-
border-color ${t.dur.fast} ${t.ease},
|
|
217
|
-
color ${t.dur.fast} ${t.ease};
|
|
218
|
-
}
|
|
219
|
-
& [data-slot~='dropdown-menu-badge']:empty {
|
|
220
|
-
background: transparent;
|
|
221
|
-
border-color: transparent;
|
|
222
|
-
}
|
|
223
|
-
& [data-slot~='dropdown-menu-badge'] [data-slot~='dropdown-menu-icon'] {
|
|
224
|
-
color: inherit;
|
|
225
|
-
}
|
|
226
|
-
&[data-highlighted] [data-slot~='dropdown-menu-badge']:not(:empty) {
|
|
227
|
-
color: ${t.fg.bright};
|
|
228
|
-
}
|
|
229
|
-
/* The selected tile inverts: a bright fill under the surface-colored glyph.
|
|
230
|
-
After the hover rule, so a selected row keeps its tile while highlighted. */
|
|
231
|
-
&[data-checked] [data-slot~='dropdown-menu-badge']:not(:empty),
|
|
232
|
-
&[data-checkmark] [data-slot~='dropdown-menu-badge']:not(:empty) {
|
|
233
|
-
background: color-mix(in srgb, ${t.fg.bright} 83%, ${t.surface});
|
|
234
|
-
border-color: color-mix(in srgb, ${t.fg.bright} 40%, transparent);
|
|
235
|
-
color: ${t.surface};
|
|
236
|
-
}
|
|
237
|
-
/* The favorite star: reserved space always (it fades in on row hover instead
|
|
238
|
-
of shifting the layout), and a starred row keeps its filled star visible. */
|
|
239
|
-
& [data-slot~='dropdown-menu-star'] {
|
|
240
|
-
order: 98;
|
|
241
|
-
/* Pad the 16px icon slot into a bigger hit target without moving the row's
|
|
242
|
-
layout: the box is the icon plus its padding. */
|
|
243
|
-
width: 26px;
|
|
244
|
-
height: 22px;
|
|
245
|
-
margin-block: -3px;
|
|
246
|
-
margin-inline: 0 -5px;
|
|
247
|
-
padding: 3px 5px;
|
|
248
|
-
border-radius: ${t.radius.sm};
|
|
249
|
-
opacity: 0;
|
|
250
|
-
transition:
|
|
251
|
-
opacity ${t.dur.fast} ease,
|
|
252
|
-
background ${t.dur.fast} ease;
|
|
253
|
-
}
|
|
254
|
-
&[data-highlighted] [data-slot~='dropdown-menu-star'] {
|
|
255
|
-
opacity: 0.55;
|
|
256
|
-
}
|
|
257
|
-
& [data-slot~='dropdown-menu-star']:hover {
|
|
258
|
-
opacity: 1;
|
|
259
|
-
background: ${t.hover.strong};
|
|
260
|
-
}
|
|
261
|
-
& [data-slot~='dropdown-menu-star'][data-fav] {
|
|
262
|
-
opacity: 0.95;
|
|
263
|
-
color: ${t.highlight};
|
|
264
|
-
}
|
|
265
|
-
/* A submenu trigger row: a chevron at the end, and the row stays highlighted
|
|
266
|
-
while its own list is open, so the path you came down remains readable. */
|
|
267
|
-
& [data-slot~='dropdown-menu-arrow'] {
|
|
268
|
-
order: 99;
|
|
269
|
-
margin-inline-start: auto;
|
|
270
|
-
width: 12px;
|
|
271
|
-
flex: none;
|
|
272
|
-
font-size: 11px;
|
|
273
|
-
color: ${t.fg.faint};
|
|
274
|
-
}
|
|
275
|
-
&[data-state='open'] {
|
|
276
|
-
background: ${t.hover};
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
[data-slot~='dropdown-menu-content'][data-badges] > [data-slot~='dropdown-menu-item'] {
|
|
280
|
-
padding-block: 5px;
|
|
281
|
-
padding-inline: 6px 10px;
|
|
282
|
-
}
|
|
283
|
-
/* Switch rows (boolean settings in a dropdown): a toggle pill at the row's end,
|
|
284
|
-
the same control language as the settings dialog's toggles. */
|
|
285
|
-
[data-slot~='dropdown-menu-switch'] {
|
|
286
|
-
order: 99;
|
|
287
|
-
margin-inline-start: auto;
|
|
288
|
-
position: relative;
|
|
289
|
-
flex: none;
|
|
290
|
-
width: 34px;
|
|
291
|
-
height: 18px;
|
|
292
|
-
border-radius: ${dropdownMenuDef.vars['switch-radius']};
|
|
293
|
-
background: ${t.surface.overlay};
|
|
294
|
-
border: 1px solid ${t.border.soft};
|
|
295
|
-
transition:
|
|
296
|
-
background ${t.dur.med} ease,
|
|
297
|
-
border-color ${t.dur.med} ease;
|
|
298
|
-
|
|
299
|
-
&::after {
|
|
300
|
-
content: '';
|
|
301
|
-
position: absolute;
|
|
302
|
-
top: 2px;
|
|
303
|
-
inset-inline-start: 2px;
|
|
304
|
-
width: 12px;
|
|
305
|
-
height: 12px;
|
|
306
|
-
border-radius: 50%;
|
|
307
|
-
background: ${t.fg.muted};
|
|
308
|
-
transition:
|
|
309
|
-
transform ${t.dur.med} ease,
|
|
310
|
-
background ${t.dur.med} ease;
|
|
311
|
-
}
|
|
312
|
-
&[data-on] {
|
|
313
|
-
background: ${t.selected.bg};
|
|
314
|
-
border-color: ${t.selected.bg};
|
|
315
|
-
}
|
|
316
|
-
&[data-on]::after {
|
|
317
|
-
transform: translateX(16px);
|
|
318
|
-
background: ${t.selected.fg};
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
[data-slot~='dropdown-menu-separator'] {
|
|
322
|
-
height: 1px;
|
|
323
|
-
margin: 4px 6px;
|
|
324
|
-
background: ${t.border};
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
`,
|
|
328
|
-
);
|
|
329
|
-
|
|
330
|
-
/** An icon in a menu row, carrying the row icon part. */
|
|
331
|
-
function menuIcon(id: string, doc: Document): HTMLElement {
|
|
332
|
-
const el = iconEl(id, doc);
|
|
333
|
-
el.dataset.slot = dropdownMenuDef.slot('icon');
|
|
334
|
-
return el;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const TEXT = {
|
|
338
|
-
addFavorite: msg('ui.menu.addFavorite', 'Add to favorites'),
|
|
339
|
-
removeFavorite: msg('ui.menu.removeFavorite', 'Remove from favorites'),
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
export type DropdownMenuProps = DropdownMenuOptions;
|
|
343
|
-
|
|
344
|
-
export interface DropdownMenuOptions extends DropdownMenuControllerOptions {
|
|
345
|
-
/** Element that opens the menu on click (gets the machine's trigger props). */
|
|
346
|
-
trigger?: HTMLElement;
|
|
347
|
-
host?: HTMLElement;
|
|
348
|
-
/** Override the root list's min-width (the stylesheet default suits full-word labels;
|
|
349
|
-
* compact lists like the timeframe dropdown pass something snug). Submenus keep the
|
|
350
|
-
* default. */
|
|
351
|
-
minWidth?: string;
|
|
352
|
-
/** Star-toggle reports from items carrying `favorite` (the menu stays open — starring
|
|
353
|
-
* is a side action on a row, never a selection). */
|
|
354
|
-
onFavorite?: (id: string, on: boolean) => void;
|
|
355
|
-
/** Mark checked items with a leading ✓ instead of the selected-row background wash —
|
|
356
|
-
* the shape for pointer-anchored action menus (right-click context menus), where a
|
|
357
|
-
* washed row reads as hover state. A level holding checkable rows reserves the mark
|
|
358
|
-
* column on all its rows so labels align; an all-action level keeps its natural left
|
|
359
|
-
* edge. Submenus inherit the mode. Dropdown menus (a trigger button opening a
|
|
360
|
-
* picker) keep the default wash. */
|
|
361
|
-
checkmarks?: boolean;
|
|
362
|
-
/** Sit each row's icon in a squared badge (a 24px rounded tile with a faint fill and a soft
|
|
363
|
-
* border) instead of a bare glyph: its glyph brightens on a hovered row, and the tile
|
|
364
|
-
* inverts on the selected one. A
|
|
365
|
-
* level where some rows carry an icon reserves the badge column on all its rows so
|
|
366
|
-
* labels align; a level with no icon keeps its natural left edge. With `checkmarks`,
|
|
367
|
-
* the mark column leads and the badge follows. Submenus inherit the mode. */
|
|
368
|
-
iconBadges?: boolean;
|
|
369
|
-
/** The translator of the menu's own words (a star's name). Default: the kit build's, else English. */
|
|
370
|
-
i18n?: Translator | null;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/** Where a menu level opens: the shadcn props, or the deprecated `placement`. */
|
|
374
|
-
type MenuPlacement = PlacementProps & Pick<DropdownMenuControllerOptions, 'placement'>;
|
|
375
|
-
|
|
376
|
-
interface SurfaceOptions {
|
|
377
|
-
host: HTMLElement;
|
|
378
|
-
placement: MenuPlacement;
|
|
379
|
-
onSelect: (id: string) => void;
|
|
380
|
-
onOpenChange?: (open: boolean) => void;
|
|
381
|
-
trigger?: HTMLElement;
|
|
382
|
-
/** A submenu's branch row — announced as its opener (it takes trigger-ITEM props, not `trigger`'s). */
|
|
383
|
-
opener?: HTMLElement;
|
|
384
|
-
triggerId?: string;
|
|
385
|
-
id?: string;
|
|
386
|
-
minWidth?: string;
|
|
387
|
-
onFavorite?: (id: string, on: boolean) => void;
|
|
388
|
-
checkmarks?: boolean;
|
|
389
|
-
iconBadges?: boolean;
|
|
390
|
-
i18n: Translator | null;
|
|
391
|
-
/** A branch's level, opened beside its row. */
|
|
392
|
-
submenu?: boolean;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/** How far a row sits inside its list: the list's padding (6px) plus its border (1px). */
|
|
396
|
-
const ROW_INSET_PX = 7;
|
|
397
|
-
/** The space between a submenu and the list it opened from. */
|
|
398
|
-
const SUBMENU_GAP_PX = 4;
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* One menu level: a Zag machine plus the floating list it projects onto. The root menu is a
|
|
402
|
-
* surface and every branch spawns another, so nesting is depth-agnostic. Private — `Menu`
|
|
403
|
-
* is the public face.
|
|
404
|
-
*/
|
|
405
|
-
class Surface {
|
|
406
|
-
readonly list: HTMLElement;
|
|
407
|
-
readonly handle: HandleOf<typeof zagMenu.machine>;
|
|
408
|
-
private readonly positioner: HTMLElement;
|
|
409
|
-
private readonly ctrl: DropdownMenuController;
|
|
410
|
-
private readonly mid: string;
|
|
411
|
-
private readonly doc: Document;
|
|
412
|
-
private readonly host: HTMLElement;
|
|
413
|
-
private readonly onSelect: (id: string) => void;
|
|
414
|
-
private readonly onFavorite?: (id: string, on: boolean) => void;
|
|
415
|
-
private readonly i18n: Translator | null;
|
|
416
|
-
private readonly checkmarks: boolean;
|
|
417
|
-
private readonly iconBadges: boolean;
|
|
418
|
-
private items: readonly MenuItemDescriptor[] = [];
|
|
419
|
-
/** Branch item id → the surface it opens. */
|
|
420
|
-
private readonly subs = new Map<string, Surface>();
|
|
421
|
-
/** Trigger rect captured when this level opened — the list stays put if the
|
|
422
|
-
* trigger then moves (favorite chips shifting the caret). */
|
|
423
|
-
private pinnedAnchor: { x: number; y: number; width: number; height: number } | null = null;
|
|
424
|
-
/** The open state last announced — see {@link announce}. */
|
|
425
|
-
private shown = false;
|
|
426
|
-
/** The list's exit animation after a close; a reopen cancels it. */
|
|
427
|
-
private exit: SurfaceExit | null = null;
|
|
428
|
-
private readonly opener: HTMLElement | null;
|
|
429
|
-
|
|
430
|
-
constructor(doc: Document, opts: SurfaceOptions) {
|
|
431
|
-
this.doc = doc;
|
|
432
|
-
this.host = opts.host;
|
|
433
|
-
this.onSelect = opts.onSelect;
|
|
434
|
-
this.onFavorite = opts.onFavorite;
|
|
435
|
-
this.i18n = opts.i18n;
|
|
436
|
-
this.checkmarks = opts.checkmarks === true;
|
|
437
|
-
this.iconBadges = opts.iconBadges === true;
|
|
438
|
-
|
|
439
|
-
this.positioner = doc.createElement('div');
|
|
440
|
-
this.positioner.className = 'vela-ui-layer';
|
|
441
|
-
this.list = doc.createElement('ul');
|
|
442
|
-
this.list.className = 'vela-menu';
|
|
443
|
-
this.list.dataset.slot = dropdownMenuDef.slot('content');
|
|
444
|
-
if (opts.minWidth) this.list.style.minWidth = opts.minWidth;
|
|
445
|
-
this.positioner.appendChild(this.list);
|
|
446
|
-
this.host.appendChild(this.positioner);
|
|
447
|
-
|
|
448
|
-
const trigger = opts.trigger;
|
|
449
|
-
this.opener = trigger ?? opts.opener ?? null;
|
|
450
|
-
this.ctrl = dropdownMenuController({
|
|
451
|
-
items: [],
|
|
452
|
-
id: opts.id,
|
|
453
|
-
...opts.placement,
|
|
454
|
-
onSelect: (id) => this.onSelect(id),
|
|
455
|
-
getAnchorRect: () => this.pinnedAnchor,
|
|
456
|
-
onOpenChange: (open) => {
|
|
457
|
-
if (open && trigger) {
|
|
458
|
-
const r = trigger.getBoundingClientRect();
|
|
459
|
-
this.pinnedAnchor = { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
460
|
-
} else {
|
|
461
|
-
this.pinnedAnchor = null;
|
|
462
|
-
}
|
|
463
|
-
opts.onOpenChange?.(open);
|
|
464
|
-
},
|
|
465
|
-
triggerId: opts.triggerId,
|
|
466
|
-
});
|
|
467
|
-
if (opts.submenu) {
|
|
468
|
-
// Zag lays a submenu against its row, which sits inside the parent list: push it out
|
|
469
|
-
// past the list's edge (on whichever side it opens) so it never covers the parent,
|
|
470
|
-
// and up by the same inset so its first row lines up with the row it opened from.
|
|
471
|
-
// Zag sets a submenu's gutter itself; an explicit offset outranks it.
|
|
472
|
-
this.ctrl.props.positioning = {
|
|
473
|
-
...this.ctrl.props.positioning,
|
|
474
|
-
offset: { mainAxis: ROW_INSET_PX + SUBMENU_GAP_PX },
|
|
475
|
-
shift: -ROW_INSET_PX,
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
this.mid = String(this.ctrl.props.id);
|
|
479
|
-
if (opts.triggerId && trigger) trigger.id = opts.triggerId;
|
|
480
|
-
this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
|
|
481
|
-
const api = this.ctrl.connect(service);
|
|
482
|
-
// Close announces while the list still shows, open once it does.
|
|
483
|
-
const closing = this.shown && !api.open;
|
|
484
|
-
if (closing) this.announce(false);
|
|
485
|
-
if (api.open && this.exit) {
|
|
486
|
-
this.exit.cancel();
|
|
487
|
-
this.exit = null;
|
|
488
|
-
}
|
|
489
|
-
if (trigger) spreadProps(trigger, api.getTriggerProps(), this.mid);
|
|
490
|
-
spreadProps(this.positioner, api.getPositionerProps(), this.mid);
|
|
491
|
-
spreadProps(this.list, api.getContentProps(), this.mid);
|
|
492
|
-
this.project(api);
|
|
493
|
-
if (closing) this.holdExit();
|
|
494
|
-
if (api.open && !this.shown) this.announce(true);
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/** Keep the just-closed list up through its exit animation. `spreadProps` re-applies
|
|
499
|
-
* `hidden` only when the machine's value for it changes, so the list stays shown until
|
|
500
|
-
* the exit ends — or until a reopen, which projects `hidden: false` itself. */
|
|
501
|
-
private holdExit(): void {
|
|
502
|
-
this.list.hidden = false;
|
|
503
|
-
this.exit = holdForExit(this.list, () => {
|
|
504
|
-
this.exit = null;
|
|
505
|
-
if (!this.shown) this.list.hidden = true;
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
get api(): zagMenu.Api {
|
|
510
|
-
return this.ctrl.connect(this.handle.service);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
private announce(open: boolean): void {
|
|
514
|
-
this.shown = open;
|
|
515
|
-
announceSurface(this.list, open, 'menu', this.opener);
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
setItems(items: readonly MenuItemDescriptor[]): void {
|
|
519
|
-
this.items = items;
|
|
520
|
-
for (const sub of this.subs.values()) sub.destroy();
|
|
521
|
-
this.subs.clear();
|
|
522
|
-
this.build();
|
|
523
|
-
const api = this.api;
|
|
524
|
-
for (const sub of this.subs.values()) {
|
|
525
|
-
api.setChild(sub.handle.service);
|
|
526
|
-
sub.api.setParent(this.handle.service);
|
|
527
|
-
}
|
|
528
|
-
this.handle.flush();
|
|
529
|
-
// Those two are queued sends: a child only learns it IS a submenu on the next
|
|
530
|
-
// microtask, and a branch row's props (hover intent, the trigger-item part) are
|
|
531
|
-
// derived from that. Re-project once the queue has drained.
|
|
532
|
-
if (this.subs.size > 0) queueMicrotask(() => this.handle.flush());
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
destroy(): void {
|
|
536
|
-
for (const sub of this.subs.values()) sub.destroy();
|
|
537
|
-
this.subs.clear();
|
|
538
|
-
this.exit?.finish();
|
|
539
|
-
this.handle.stop();
|
|
540
|
-
// Torn down while open: the stopped machine never projects the close.
|
|
541
|
-
if (this.shown) this.announce(false);
|
|
542
|
-
this.positioner.remove();
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
private byId(id: string): MenuItemDescriptor | undefined {
|
|
546
|
-
return this.items.find((i) => i.id === id);
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
/** Push the machine's item props onto the rendered rows. A branch takes the submenu
|
|
550
|
-
* trigger props, which carry the child's ids and hover handlers; a leaf takes plain ones. */
|
|
551
|
-
private project(api: zagMenu.Api): void {
|
|
552
|
-
for (const li of this.list.children) {
|
|
553
|
-
const id = (li as HTMLElement).dataset.veiId;
|
|
554
|
-
if (!id) continue;
|
|
555
|
-
const sub = this.subs.get(id);
|
|
556
|
-
if (sub) {
|
|
557
|
-
spreadProps(li as HTMLElement, api.getTriggerItemProps(sub.api), this.mid);
|
|
558
|
-
continue;
|
|
559
|
-
}
|
|
560
|
-
const item = this.byId(id);
|
|
561
|
-
// Switch rows keep the menu OPEN on selection — flip, see, flip again.
|
|
562
|
-
spreadProps(li as HTMLElement, api.getItemProps({ value: id, disabled: item?.disabled, closeOnSelect: item?.toggle ? false : undefined }), this.mid);
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
private build(): void {
|
|
567
|
-
const doc = this.doc;
|
|
568
|
-
this.list.replaceChildren();
|
|
569
|
-
// Reserve the leading mark column only when THIS level actually holds checkable
|
|
570
|
-
// rows — an all-action list (e.g. the chart-body context menu) keeps its natural
|
|
571
|
-
// left edge instead of carrying an empty gutter.
|
|
572
|
-
const markable = this.checkmarks
|
|
573
|
-
&& this.items.some((i) => !(i.submenu && i.submenu.length > 0) && !i.toggle && i.checked !== undefined);
|
|
574
|
-
// Same for icons: once a row of this level carries one, the others keep an empty
|
|
575
|
-
// icon slot (an empty badge in badge mode) so every label shares one left edge.
|
|
576
|
-
const iconic = this.items.some((i) => !!i.icon);
|
|
577
|
-
const badged = this.iconBadges && iconic;
|
|
578
|
-
if (badged) this.list.dataset.badges = '1';
|
|
579
|
-
else delete this.list.dataset.badges;
|
|
580
|
-
for (const item of this.items) {
|
|
581
|
-
if (item.separatorBefore) {
|
|
582
|
-
const sep = doc.createElement('li');
|
|
583
|
-
sep.className = 'vela-menu-sep';
|
|
584
|
-
sep.dataset.slot = dropdownMenuDef.slot('separator');
|
|
585
|
-
sep.setAttribute('role', 'separator');
|
|
586
|
-
this.list.appendChild(sep);
|
|
587
|
-
}
|
|
588
|
-
const branch = item.submenu !== undefined && item.submenu.length > 0;
|
|
589
|
-
const li = doc.createElement('li');
|
|
590
|
-
li.className = 'vela-menu-item';
|
|
591
|
-
li.dataset.slot = dropdownMenuDef.slot('item');
|
|
592
|
-
li.dataset.veiId = item.id;
|
|
593
|
-
if (item.toggle) {
|
|
594
|
-
// Switch row: the pill carries the state.
|
|
595
|
-
// (Zag owns the item's ARIA props; the pill below is decorative.)
|
|
596
|
-
li.dataset.toggle = '1';
|
|
597
|
-
}
|
|
598
|
-
if (markable) {
|
|
599
|
-
// Mark mode: a leading ✓ slot on every row of this level (empty when
|
|
600
|
-
// unchecked, so labels share a left edge); the row surface stays free
|
|
601
|
-
// for hover.
|
|
602
|
-
const mark = doc.createElement('span');
|
|
603
|
-
mark.className = 'vela-menu-mark';
|
|
604
|
-
mark.dataset.slot = dropdownMenuDef.slot('mark');
|
|
605
|
-
if (!branch && !item.toggle && item.checked) {
|
|
606
|
-
mark.appendChild(menuIcon('check', doc));
|
|
607
|
-
li.dataset.checkmark = '1';
|
|
608
|
-
}
|
|
609
|
-
li.appendChild(mark);
|
|
610
|
-
} else if (!branch && !item.toggle && item.checked) {
|
|
611
|
-
// Selection reads from the row surface: a stronger background wash
|
|
612
|
-
// marks the active entry (hover stays the lighter wash).
|
|
613
|
-
li.dataset.checked = '1';
|
|
614
|
-
}
|
|
615
|
-
if (badged) {
|
|
616
|
-
const badge = doc.createElement('span');
|
|
617
|
-
badge.className = 'vela-menu-badge';
|
|
618
|
-
badge.dataset.slot = dropdownMenuDef.slot('badge');
|
|
619
|
-
if (item.icon) badge.appendChild(menuIcon(item.icon, doc));
|
|
620
|
-
li.appendChild(badge);
|
|
621
|
-
} else if (item.icon) {
|
|
622
|
-
li.appendChild(menuIcon(item.icon, doc));
|
|
623
|
-
} else if (iconic) {
|
|
624
|
-
li.appendChild(menuIcon('', doc));
|
|
625
|
-
}
|
|
626
|
-
const label = doc.createElement('span');
|
|
627
|
-
label.className = 'vela-menu-label';
|
|
628
|
-
label.dataset.slot = dropdownMenuDef.slot('label');
|
|
629
|
-
label.textContent = item.label;
|
|
630
|
-
li.appendChild(label);
|
|
631
|
-
if (item.hint) {
|
|
632
|
-
const hint = doc.createElement('span');
|
|
633
|
-
hint.className = 'vela-menu-hint';
|
|
634
|
-
hint.dataset.slot = dropdownMenuDef.slot('hint');
|
|
635
|
-
hint.textContent = item.hint;
|
|
636
|
-
li.appendChild(hint);
|
|
637
|
-
}
|
|
638
|
-
if (item.favorite !== undefined && this.onFavorite) {
|
|
639
|
-
const on = item.favorite;
|
|
640
|
-
const star = menuIcon(on ? 'star-filled' : 'star', doc);
|
|
641
|
-
star.classList.add('vela-menu-star');
|
|
642
|
-
addSlot(star, dropdownMenuDef.slot('star'));
|
|
643
|
-
if (on) {
|
|
644
|
-
star.classList.add('vela-fav');
|
|
645
|
-
star.dataset.fav = '';
|
|
646
|
-
}
|
|
647
|
-
star.removeAttribute('aria-hidden'); // iconEl hides decorative icons; this one is a control
|
|
648
|
-
star.setAttribute('role', 'button');
|
|
649
|
-
star.setAttribute('aria-label', textOf(this.i18n, on ? TEXT.removeFavorite : TEXT.addFavorite));
|
|
650
|
-
star.setAttribute('aria-pressed', on ? 'true' : 'false');
|
|
651
|
-
// Swallow the whole pointer sequence: the machine selects rows on click,
|
|
652
|
-
// and a press that starts on the star must never reach the row.
|
|
653
|
-
for (const ev of ['pointerdown', 'pointerup', 'mousedown', 'mouseup'] as const) {
|
|
654
|
-
star.addEventListener(ev, (e) => e.stopPropagation());
|
|
655
|
-
}
|
|
656
|
-
star.addEventListener('click', (e) => {
|
|
657
|
-
e.stopPropagation();
|
|
658
|
-
e.preventDefault();
|
|
659
|
-
this.onFavorite?.(item.id, !on);
|
|
660
|
-
});
|
|
661
|
-
li.appendChild(star);
|
|
662
|
-
}
|
|
663
|
-
if (item.toggle) {
|
|
664
|
-
const sw = doc.createElement('span');
|
|
665
|
-
sw.className = 'vela-menu-switch' + (item.checked ? ' on' : '');
|
|
666
|
-
sw.dataset.slot = dropdownMenuDef.slot('switch');
|
|
667
|
-
if (item.checked) sw.dataset.on = '';
|
|
668
|
-
sw.setAttribute('aria-hidden', 'true');
|
|
669
|
-
li.appendChild(sw);
|
|
670
|
-
}
|
|
671
|
-
if (branch) {
|
|
672
|
-
li.dataset.branch = '1';
|
|
673
|
-
const arrow = menuIcon('chevron-right', doc);
|
|
674
|
-
arrow.classList.add('vela-menu-arrow');
|
|
675
|
-
addSlot(arrow, dropdownMenuDef.slot('arrow'));
|
|
676
|
-
li.appendChild(arrow);
|
|
677
|
-
const sub = new Surface(doc, {
|
|
678
|
-
host: this.host,
|
|
679
|
-
placement: { placement: 'right-start' },
|
|
680
|
-
submenu: true,
|
|
681
|
-
opener: li,
|
|
682
|
-
onSelect: this.onSelect,
|
|
683
|
-
onFavorite: this.onFavorite,
|
|
684
|
-
i18n: this.i18n,
|
|
685
|
-
checkmarks: this.checkmarks,
|
|
686
|
-
iconBadges: this.iconBadges,
|
|
687
|
-
id: `${this.mid}--${item.id}`,
|
|
688
|
-
});
|
|
689
|
-
sub.setItems(item.submenu ?? []);
|
|
690
|
-
this.subs.set(item.id, sub);
|
|
691
|
-
}
|
|
692
|
-
this.list.appendChild(li);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
export class DropdownMenu {
|
|
698
|
-
private readonly root: Surface;
|
|
699
|
-
|
|
700
|
-
constructor(opts: DropdownMenuOptions) {
|
|
701
|
-
const anchor = opts.trigger ?? opts.host ?? document.body;
|
|
702
|
-
const doc = anchor.ownerDocument;
|
|
703
|
-
injectStyles(doc);
|
|
704
|
-
const host = floatingLayerHost(opts.host ?? anchor, doc.body);
|
|
705
|
-
this.root = new Surface(doc, {
|
|
706
|
-
host,
|
|
707
|
-
placement: {
|
|
708
|
-
placement: opts.placement,
|
|
709
|
-
side: opts.side,
|
|
710
|
-
align: opts.align,
|
|
711
|
-
sideOffset: opts.sideOffset,
|
|
712
|
-
alignOffset: opts.alignOffset,
|
|
713
|
-
avoidCollisions: opts.avoidCollisions,
|
|
714
|
-
collisionPadding: opts.collisionPadding,
|
|
715
|
-
collisionBoundary: opts.collisionBoundary,
|
|
716
|
-
},
|
|
717
|
-
onSelect: (id) => opts.onSelect?.(id),
|
|
718
|
-
onOpenChange: opts.onOpenChange,
|
|
719
|
-
trigger: opts.trigger,
|
|
720
|
-
triggerId: opts.triggerId,
|
|
721
|
-
id: opts.id,
|
|
722
|
-
minWidth: opts.minWidth,
|
|
723
|
-
onFavorite: opts.onFavorite,
|
|
724
|
-
i18n: opts.i18n ?? kitTranslator(),
|
|
725
|
-
checkmarks: opts.checkmarks,
|
|
726
|
-
iconBadges: opts.iconBadges,
|
|
727
|
-
});
|
|
728
|
-
this.root.setItems(opts.items);
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
get api(): zagMenu.Api {
|
|
732
|
-
return this.root.api;
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
open(): void {
|
|
736
|
-
this.api.setOpen(true);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/** Open anchored to a viewport point (context menus). */
|
|
740
|
-
openAt(clientX: number, clientY: number): void {
|
|
741
|
-
const api = this.api;
|
|
742
|
-
api.setOpen(true);
|
|
743
|
-
api.reposition({ getAnchorRect: () => ({ x: clientX, y: clientY, width: 0, height: 0 }) });
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
close(): void {
|
|
747
|
-
this.api.setOpen(false);
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
/** Swap the item descriptors (e.g. checked states) and re-project. */
|
|
751
|
-
setItems(items: readonly MenuItemDescriptor[]): void {
|
|
752
|
-
this.root.setItems(items);
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
destroy(): void {
|
|
756
|
-
this.root.destroy();
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
export function createDropdownMenu(props: DropdownMenuProps): DropdownMenu {
|
|
761
|
-
return new DropdownMenu(props);
|
|
762
|
-
}
|
|
1
|
+
// DropdownMenu: MenuItemDescriptor[] projected into a floating list over Zag's menu machine
|
|
2
|
+
// (keyboard navigation, typeahead and dismissal belong to the machine). Items are data
|
|
3
|
+
// descriptors, the plugin contribution currency. An item carrying `submenu` becomes a
|
|
4
|
+
// trigger row owning a nested machine and list, registered as this machine's child: Zag
|
|
5
|
+
// drives hover intent, arrow-key traversal and the dismiss chain across levels.
|
|
6
|
+
import * as zagMenu from '@zag-js/menu';
|
|
7
|
+
import { msg } from '@luxalgo/vela-ui/i18n';
|
|
8
|
+
import { textOf } from '@luxalgo/vela-ui/i18n';
|
|
9
|
+
import type { Translator } from '@luxalgo/vela-ui/i18n';
|
|
10
|
+
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
11
|
+
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
12
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
13
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
14
|
+
import { addSlot } from '@luxalgo/vela-ui';
|
|
15
|
+
import { t } from '@luxalgo/vela-ui';
|
|
16
|
+
import { announceSurface } from '@luxalgo/vela-ui/surface-events';
|
|
17
|
+
import { holdForExit, type SurfaceExit } from '@luxalgo/vela-ui/surface-exit';
|
|
18
|
+
import { floatingLayerHost } from '@luxalgo/vela-ui/theme';
|
|
19
|
+
import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
|
|
20
|
+
import { splitPlacement, zagPositioning, type PlacementProps } from '@luxalgo/vela-ui/placement';
|
|
21
|
+
|
|
22
|
+
/** One menu entry. `id` is the selection value reported to `onSelect`. */
|
|
23
|
+
export interface MenuItemDescriptor {
|
|
24
|
+
id: string;
|
|
25
|
+
label: string;
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
/** Draw a separator line above this item. */
|
|
28
|
+
separatorBefore?: boolean;
|
|
29
|
+
/** Right-aligned hint (e.g. a shortcut display from KeymapManager). */
|
|
30
|
+
hint?: string;
|
|
31
|
+
/** Selected state (undefined = plain item). A plain checked item renders as a
|
|
32
|
+
* highlighted row (brighter surface + bright ink) — or, in a menu built with
|
|
33
|
+
* `checkmarks`, as a leading ✓ with the row surface left free for hover. */
|
|
34
|
+
checked?: boolean;
|
|
35
|
+
/** Render as a SWITCH row (a right-aligned toggle pill reflecting `checked`)
|
|
36
|
+
* instead of the selected-row highlight, and keep the menu OPEN on selection —
|
|
37
|
+
* the shape for boolean settings living inside a dropdown. */
|
|
38
|
+
toggle?: boolean;
|
|
39
|
+
/** Icon id (see the `vela/ui` icon registry) rendered before the label. A level where
|
|
40
|
+
* any row has one gives its iconless rows an empty slot, so the labels align. */
|
|
41
|
+
icon?: string;
|
|
42
|
+
/** Favorite-star affordance at the row's right edge: `false` renders an outline
|
|
43
|
+
* star revealed on row hover, `true` a filled star that stays visible. Clicking
|
|
44
|
+
* it reports through the menu's `onFavorite` WITHOUT selecting (or closing) the
|
|
45
|
+
* row — undefined rows carry no star at all. */
|
|
46
|
+
favorite?: boolean;
|
|
47
|
+
/** Nested entries — the row becomes a submenu trigger opening its own list to the side.
|
|
48
|
+
* A branch is not selectable itself: `onSelect` only ever reports leaf ids. */
|
|
49
|
+
submenu?: readonly MenuItemDescriptor[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface DropdownMenuControllerOptions extends PlacementProps {
|
|
53
|
+
items: readonly MenuItemDescriptor[];
|
|
54
|
+
onSelect?: (id: string) => void;
|
|
55
|
+
/** @deprecated Use `side` and `align`. */
|
|
56
|
+
placement?: zagMenu.Props['positioning'] extends infer P | undefined ? (P extends { placement?: infer PL } ? PL : never) : never;
|
|
57
|
+
onOpenChange?: (open: boolean) => void;
|
|
58
|
+
/** Pin the floating list to this rect (viewport coords). Used to keep an open
|
|
59
|
+
* menu still when its trigger moves — starring a timeframe adds a chip and
|
|
60
|
+
* would otherwise drag the list with the caret. `null` falls through to the
|
|
61
|
+
* live trigger. */
|
|
62
|
+
getAnchorRect?: () => { x: number; y: number; width: number; height: number } | null;
|
|
63
|
+
/** Share the trigger's DOM id with other machines composed on the same element. */
|
|
64
|
+
triggerId?: string;
|
|
65
|
+
/** Pin the machine's own id instead of taking a fresh one. A parent registers its
|
|
66
|
+
* submenus under this key, so rebuilding a branch replaces its entry rather than
|
|
67
|
+
* piling a new one on top. */
|
|
68
|
+
id?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type DropdownMenuService = zagMenu.Service;
|
|
72
|
+
export type DropdownMenuApi = zagMenu.Api;
|
|
73
|
+
|
|
74
|
+
export interface DropdownMenuController {
|
|
75
|
+
machine: typeof zagMenu.machine;
|
|
76
|
+
props: Partial<zagMenu.Props>;
|
|
77
|
+
connect(service: DropdownMenuService): DropdownMenuApi;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Zag's menu machine with the options mapped onto it, for any view layer. */
|
|
81
|
+
export function dropdownMenuController(opts: DropdownMenuControllerOptions): DropdownMenuController {
|
|
82
|
+
return {
|
|
83
|
+
machine: zagMenu.machine,
|
|
84
|
+
props: {
|
|
85
|
+
id: opts.id ?? nextUid('vela-menu'),
|
|
86
|
+
ids: opts.triggerId ? { trigger: opts.triggerId } : undefined,
|
|
87
|
+
positioning: {
|
|
88
|
+
...zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: 'bottom', align: 'start' }),
|
|
89
|
+
...(opts.getAnchorRect ? { getAnchorRect: () => opts.getAnchorRect!() } : {}),
|
|
90
|
+
},
|
|
91
|
+
onSelect: (d: zagMenu.SelectionDetails) => opts.onSelect?.(d.value),
|
|
92
|
+
onOpenChange: (d: zagMenu.OpenChangeDetails) => opts.onOpenChange?.(d.open),
|
|
93
|
+
} satisfies Partial<zagMenu.Props>,
|
|
94
|
+
connect: (service: DropdownMenuService): DropdownMenuApi => zagMenu.connect(service, normalizeProps),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const dropdownMenuDef = defineComponent({
|
|
99
|
+
name: 'dropdown-menu',
|
|
100
|
+
parts: ['content', 'item', 'icon', 'label', 'hint', 'mark', 'badge', 'star', 'switch', 'arrow', 'separator'],
|
|
101
|
+
settings: { radius: t.radius.md, 'switch-radius': '9px' },
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const injectStyles = defineStyles(
|
|
105
|
+
'vela-ui-menu',
|
|
106
|
+
css`
|
|
107
|
+
@layer vela.ui {
|
|
108
|
+
[data-slot~='dropdown-menu-content'] {
|
|
109
|
+
/* The list is a <ul>: without this, block rows (the separator) paint a ::marker dot. */
|
|
110
|
+
list-style: none;
|
|
111
|
+
margin: 0;
|
|
112
|
+
background: ${t.surface};
|
|
113
|
+
color: ${t.fg};
|
|
114
|
+
border: 1px solid ${t.border.strong};
|
|
115
|
+
border-radius: ${dropdownMenuDef.vars.radius};
|
|
116
|
+
box-shadow: ${t.shadow};
|
|
117
|
+
padding: 6px;
|
|
118
|
+
min-width: 180px;
|
|
119
|
+
/* Never past the room its clipping container leaves on the side it opened on
|
|
120
|
+
(Zag measures it onto the positioner): a menu in a short chart scrolls inside
|
|
121
|
+
itself instead of overflowing the chart and scrolling the page around it. */
|
|
122
|
+
max-height: min(60vh, var(--available-height, 60vh));
|
|
123
|
+
overflow-y: auto;
|
|
124
|
+
font-size: 13px;
|
|
125
|
+
z-index: ${t.z.menu};
|
|
126
|
+
outline: 2px solid transparent;
|
|
127
|
+
|
|
128
|
+
&[data-state='open'] {
|
|
129
|
+
animation: vela-menu-in ${t.dur.fast} ${t.ease};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
@keyframes vela-menu-in {
|
|
133
|
+
from {
|
|
134
|
+
opacity: 0;
|
|
135
|
+
transform: translateY(-3px);
|
|
136
|
+
}
|
|
137
|
+
to {
|
|
138
|
+
opacity: 1;
|
|
139
|
+
transform: translateY(0);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
[data-slot~='dropdown-menu-item'] {
|
|
143
|
+
display: flex;
|
|
144
|
+
align-items: center;
|
|
145
|
+
gap: 10px;
|
|
146
|
+
padding: 7px 10px;
|
|
147
|
+
border-radius: ${t.radius.sm};
|
|
148
|
+
cursor: pointer;
|
|
149
|
+
user-select: none;
|
|
150
|
+
white-space: nowrap;
|
|
151
|
+
|
|
152
|
+
&[data-highlighted] {
|
|
153
|
+
background: ${t.hover};
|
|
154
|
+
}
|
|
155
|
+
&[data-disabled] {
|
|
156
|
+
opacity: 0.4;
|
|
157
|
+
cursor: default;
|
|
158
|
+
}
|
|
159
|
+
& [data-slot~='dropdown-menu-icon'] {
|
|
160
|
+
width: 16px;
|
|
161
|
+
height: 16px;
|
|
162
|
+
font-size: 16px;
|
|
163
|
+
justify-content: center;
|
|
164
|
+
color: ${t.fg.muted};
|
|
165
|
+
}
|
|
166
|
+
& [data-slot~='dropdown-menu-label'] {
|
|
167
|
+
flex: 1 1 auto;
|
|
168
|
+
}
|
|
169
|
+
& [data-slot~='dropdown-menu-hint'] {
|
|
170
|
+
color: ${t.fg.faint};
|
|
171
|
+
font-size: ${t.fontSize.sm};
|
|
172
|
+
}
|
|
173
|
+
/* The active entry: the row surface carries the selection, a stronger wash
|
|
174
|
+
than the hover one plus bright ink. After the hover rule, so a selected row
|
|
175
|
+
stays visibly selected while highlighted. */
|
|
176
|
+
&[data-checked] {
|
|
177
|
+
background: ${t.hover.strong};
|
|
178
|
+
color: ${t.fg.bright};
|
|
179
|
+
}
|
|
180
|
+
/* Checkmark mode (context and action menus): every row reserves the leading
|
|
181
|
+
mark column so labels align; a checked row fills it with a ✓ and brightens
|
|
182
|
+
its ink, leaving the row surface free for the hover wash. */
|
|
183
|
+
& [data-slot~='dropdown-menu-mark'] {
|
|
184
|
+
width: 14px;
|
|
185
|
+
flex: none;
|
|
186
|
+
display: inline-flex;
|
|
187
|
+
align-items: center;
|
|
188
|
+
justify-content: center;
|
|
189
|
+
}
|
|
190
|
+
& [data-slot~='dropdown-menu-mark'] [data-slot~='dropdown-menu-icon'] {
|
|
191
|
+
width: 14px;
|
|
192
|
+
height: 14px;
|
|
193
|
+
font-size: 14px;
|
|
194
|
+
color: ${t.fg.bright};
|
|
195
|
+
}
|
|
196
|
+
&[data-checkmark] {
|
|
197
|
+
color: ${t.fg.bright};
|
|
198
|
+
}
|
|
199
|
+
/* Badge mode: the row icon sits in a rounded tile and the row grows to fit it.
|
|
200
|
+
An icon-less row of the same level keeps an invisible slot, so labels align. */
|
|
201
|
+
& [data-slot~='dropdown-menu-badge'] {
|
|
202
|
+
width: 24px;
|
|
203
|
+
height: 24px;
|
|
204
|
+
padding: 3px;
|
|
205
|
+
flex: none;
|
|
206
|
+
box-sizing: border-box;
|
|
207
|
+
display: inline-flex;
|
|
208
|
+
align-items: center;
|
|
209
|
+
justify-content: center;
|
|
210
|
+
border-radius: ${t.radius.md};
|
|
211
|
+
background: ${t.hover};
|
|
212
|
+
border: 1px solid ${t.border};
|
|
213
|
+
color: ${t.fg.muted};
|
|
214
|
+
transition:
|
|
215
|
+
background ${t.dur.fast} ${t.ease},
|
|
216
|
+
border-color ${t.dur.fast} ${t.ease},
|
|
217
|
+
color ${t.dur.fast} ${t.ease};
|
|
218
|
+
}
|
|
219
|
+
& [data-slot~='dropdown-menu-badge']:empty {
|
|
220
|
+
background: transparent;
|
|
221
|
+
border-color: transparent;
|
|
222
|
+
}
|
|
223
|
+
& [data-slot~='dropdown-menu-badge'] [data-slot~='dropdown-menu-icon'] {
|
|
224
|
+
color: inherit;
|
|
225
|
+
}
|
|
226
|
+
&[data-highlighted] [data-slot~='dropdown-menu-badge']:not(:empty) {
|
|
227
|
+
color: ${t.fg.bright};
|
|
228
|
+
}
|
|
229
|
+
/* The selected tile inverts: a bright fill under the surface-colored glyph.
|
|
230
|
+
After the hover rule, so a selected row keeps its tile while highlighted. */
|
|
231
|
+
&[data-checked] [data-slot~='dropdown-menu-badge']:not(:empty),
|
|
232
|
+
&[data-checkmark] [data-slot~='dropdown-menu-badge']:not(:empty) {
|
|
233
|
+
background: color-mix(in srgb, ${t.fg.bright} 83%, ${t.surface});
|
|
234
|
+
border-color: color-mix(in srgb, ${t.fg.bright} 40%, transparent);
|
|
235
|
+
color: ${t.surface};
|
|
236
|
+
}
|
|
237
|
+
/* The favorite star: reserved space always (it fades in on row hover instead
|
|
238
|
+
of shifting the layout), and a starred row keeps its filled star visible. */
|
|
239
|
+
& [data-slot~='dropdown-menu-star'] {
|
|
240
|
+
order: 98;
|
|
241
|
+
/* Pad the 16px icon slot into a bigger hit target without moving the row's
|
|
242
|
+
layout: the box is the icon plus its padding. */
|
|
243
|
+
width: 26px;
|
|
244
|
+
height: 22px;
|
|
245
|
+
margin-block: -3px;
|
|
246
|
+
margin-inline: 0 -5px;
|
|
247
|
+
padding: 3px 5px;
|
|
248
|
+
border-radius: ${t.radius.sm};
|
|
249
|
+
opacity: 0;
|
|
250
|
+
transition:
|
|
251
|
+
opacity ${t.dur.fast} ease,
|
|
252
|
+
background ${t.dur.fast} ease;
|
|
253
|
+
}
|
|
254
|
+
&[data-highlighted] [data-slot~='dropdown-menu-star'] {
|
|
255
|
+
opacity: 0.55;
|
|
256
|
+
}
|
|
257
|
+
& [data-slot~='dropdown-menu-star']:hover {
|
|
258
|
+
opacity: 1;
|
|
259
|
+
background: ${t.hover.strong};
|
|
260
|
+
}
|
|
261
|
+
& [data-slot~='dropdown-menu-star'][data-fav] {
|
|
262
|
+
opacity: 0.95;
|
|
263
|
+
color: ${t.highlight};
|
|
264
|
+
}
|
|
265
|
+
/* A submenu trigger row: a chevron at the end, and the row stays highlighted
|
|
266
|
+
while its own list is open, so the path you came down remains readable. */
|
|
267
|
+
& [data-slot~='dropdown-menu-arrow'] {
|
|
268
|
+
order: 99;
|
|
269
|
+
margin-inline-start: auto;
|
|
270
|
+
width: 12px;
|
|
271
|
+
flex: none;
|
|
272
|
+
font-size: 11px;
|
|
273
|
+
color: ${t.fg.faint};
|
|
274
|
+
}
|
|
275
|
+
&[data-state='open'] {
|
|
276
|
+
background: ${t.hover};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
[data-slot~='dropdown-menu-content'][data-badges] > [data-slot~='dropdown-menu-item'] {
|
|
280
|
+
padding-block: 5px;
|
|
281
|
+
padding-inline: 6px 10px;
|
|
282
|
+
}
|
|
283
|
+
/* Switch rows (boolean settings in a dropdown): a toggle pill at the row's end,
|
|
284
|
+
the same control language as the settings dialog's toggles. */
|
|
285
|
+
[data-slot~='dropdown-menu-switch'] {
|
|
286
|
+
order: 99;
|
|
287
|
+
margin-inline-start: auto;
|
|
288
|
+
position: relative;
|
|
289
|
+
flex: none;
|
|
290
|
+
width: 34px;
|
|
291
|
+
height: 18px;
|
|
292
|
+
border-radius: ${dropdownMenuDef.vars['switch-radius']};
|
|
293
|
+
background: ${t.surface.overlay};
|
|
294
|
+
border: 1px solid ${t.border.soft};
|
|
295
|
+
transition:
|
|
296
|
+
background ${t.dur.med} ease,
|
|
297
|
+
border-color ${t.dur.med} ease;
|
|
298
|
+
|
|
299
|
+
&::after {
|
|
300
|
+
content: '';
|
|
301
|
+
position: absolute;
|
|
302
|
+
top: 2px;
|
|
303
|
+
inset-inline-start: 2px;
|
|
304
|
+
width: 12px;
|
|
305
|
+
height: 12px;
|
|
306
|
+
border-radius: 50%;
|
|
307
|
+
background: ${t.fg.muted};
|
|
308
|
+
transition:
|
|
309
|
+
transform ${t.dur.med} ease,
|
|
310
|
+
background ${t.dur.med} ease;
|
|
311
|
+
}
|
|
312
|
+
&[data-on] {
|
|
313
|
+
background: ${t.selected.bg};
|
|
314
|
+
border-color: ${t.selected.bg};
|
|
315
|
+
}
|
|
316
|
+
&[data-on]::after {
|
|
317
|
+
transform: translateX(16px);
|
|
318
|
+
background: ${t.selected.fg};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
[data-slot~='dropdown-menu-separator'] {
|
|
322
|
+
height: 1px;
|
|
323
|
+
margin: 4px 6px;
|
|
324
|
+
background: ${t.border};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
`,
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
/** An icon in a menu row, carrying the row icon part. */
|
|
331
|
+
function menuIcon(id: string, doc: Document): HTMLElement {
|
|
332
|
+
const el = iconEl(id, doc);
|
|
333
|
+
el.dataset.slot = dropdownMenuDef.slot('icon');
|
|
334
|
+
return el;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const TEXT = {
|
|
338
|
+
addFavorite: msg('ui.menu.addFavorite', 'Add to favorites'),
|
|
339
|
+
removeFavorite: msg('ui.menu.removeFavorite', 'Remove from favorites'),
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
export type DropdownMenuProps = DropdownMenuOptions;
|
|
343
|
+
|
|
344
|
+
export interface DropdownMenuOptions extends DropdownMenuControllerOptions {
|
|
345
|
+
/** Element that opens the menu on click (gets the machine's trigger props). */
|
|
346
|
+
trigger?: HTMLElement;
|
|
347
|
+
host?: HTMLElement;
|
|
348
|
+
/** Override the root list's min-width (the stylesheet default suits full-word labels;
|
|
349
|
+
* compact lists like the timeframe dropdown pass something snug). Submenus keep the
|
|
350
|
+
* default. */
|
|
351
|
+
minWidth?: string;
|
|
352
|
+
/** Star-toggle reports from items carrying `favorite` (the menu stays open — starring
|
|
353
|
+
* is a side action on a row, never a selection). */
|
|
354
|
+
onFavorite?: (id: string, on: boolean) => void;
|
|
355
|
+
/** Mark checked items with a leading ✓ instead of the selected-row background wash —
|
|
356
|
+
* the shape for pointer-anchored action menus (right-click context menus), where a
|
|
357
|
+
* washed row reads as hover state. A level holding checkable rows reserves the mark
|
|
358
|
+
* column on all its rows so labels align; an all-action level keeps its natural left
|
|
359
|
+
* edge. Submenus inherit the mode. Dropdown menus (a trigger button opening a
|
|
360
|
+
* picker) keep the default wash. */
|
|
361
|
+
checkmarks?: boolean;
|
|
362
|
+
/** Sit each row's icon in a squared badge (a 24px rounded tile with a faint fill and a soft
|
|
363
|
+
* border) instead of a bare glyph: its glyph brightens on a hovered row, and the tile
|
|
364
|
+
* inverts on the selected one. A
|
|
365
|
+
* level where some rows carry an icon reserves the badge column on all its rows so
|
|
366
|
+
* labels align; a level with no icon keeps its natural left edge. With `checkmarks`,
|
|
367
|
+
* the mark column leads and the badge follows. Submenus inherit the mode. */
|
|
368
|
+
iconBadges?: boolean;
|
|
369
|
+
/** The translator of the menu's own words (a star's name). Default: the kit build's, else English. */
|
|
370
|
+
i18n?: Translator | null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Where a menu level opens: the shadcn props, or the deprecated `placement`. */
|
|
374
|
+
type MenuPlacement = PlacementProps & Pick<DropdownMenuControllerOptions, 'placement'>;
|
|
375
|
+
|
|
376
|
+
interface SurfaceOptions {
|
|
377
|
+
host: HTMLElement;
|
|
378
|
+
placement: MenuPlacement;
|
|
379
|
+
onSelect: (id: string) => void;
|
|
380
|
+
onOpenChange?: (open: boolean) => void;
|
|
381
|
+
trigger?: HTMLElement;
|
|
382
|
+
/** A submenu's branch row — announced as its opener (it takes trigger-ITEM props, not `trigger`'s). */
|
|
383
|
+
opener?: HTMLElement;
|
|
384
|
+
triggerId?: string;
|
|
385
|
+
id?: string;
|
|
386
|
+
minWidth?: string;
|
|
387
|
+
onFavorite?: (id: string, on: boolean) => void;
|
|
388
|
+
checkmarks?: boolean;
|
|
389
|
+
iconBadges?: boolean;
|
|
390
|
+
i18n: Translator | null;
|
|
391
|
+
/** A branch's level, opened beside its row. */
|
|
392
|
+
submenu?: boolean;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** How far a row sits inside its list: the list's padding (6px) plus its border (1px). */
|
|
396
|
+
const ROW_INSET_PX = 7;
|
|
397
|
+
/** The space between a submenu and the list it opened from. */
|
|
398
|
+
const SUBMENU_GAP_PX = 4;
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* One menu level: a Zag machine plus the floating list it projects onto. The root menu is a
|
|
402
|
+
* surface and every branch spawns another, so nesting is depth-agnostic. Private — `Menu`
|
|
403
|
+
* is the public face.
|
|
404
|
+
*/
|
|
405
|
+
class Surface {
|
|
406
|
+
readonly list: HTMLElement;
|
|
407
|
+
readonly handle: HandleOf<typeof zagMenu.machine>;
|
|
408
|
+
private readonly positioner: HTMLElement;
|
|
409
|
+
private readonly ctrl: DropdownMenuController;
|
|
410
|
+
private readonly mid: string;
|
|
411
|
+
private readonly doc: Document;
|
|
412
|
+
private readonly host: HTMLElement;
|
|
413
|
+
private readonly onSelect: (id: string) => void;
|
|
414
|
+
private readonly onFavorite?: (id: string, on: boolean) => void;
|
|
415
|
+
private readonly i18n: Translator | null;
|
|
416
|
+
private readonly checkmarks: boolean;
|
|
417
|
+
private readonly iconBadges: boolean;
|
|
418
|
+
private items: readonly MenuItemDescriptor[] = [];
|
|
419
|
+
/** Branch item id → the surface it opens. */
|
|
420
|
+
private readonly subs = new Map<string, Surface>();
|
|
421
|
+
/** Trigger rect captured when this level opened — the list stays put if the
|
|
422
|
+
* trigger then moves (favorite chips shifting the caret). */
|
|
423
|
+
private pinnedAnchor: { x: number; y: number; width: number; height: number } | null = null;
|
|
424
|
+
/** The open state last announced — see {@link announce}. */
|
|
425
|
+
private shown = false;
|
|
426
|
+
/** The list's exit animation after a close; a reopen cancels it. */
|
|
427
|
+
private exit: SurfaceExit | null = null;
|
|
428
|
+
private readonly opener: HTMLElement | null;
|
|
429
|
+
|
|
430
|
+
constructor(doc: Document, opts: SurfaceOptions) {
|
|
431
|
+
this.doc = doc;
|
|
432
|
+
this.host = opts.host;
|
|
433
|
+
this.onSelect = opts.onSelect;
|
|
434
|
+
this.onFavorite = opts.onFavorite;
|
|
435
|
+
this.i18n = opts.i18n;
|
|
436
|
+
this.checkmarks = opts.checkmarks === true;
|
|
437
|
+
this.iconBadges = opts.iconBadges === true;
|
|
438
|
+
|
|
439
|
+
this.positioner = doc.createElement('div');
|
|
440
|
+
this.positioner.className = 'vela-ui-layer';
|
|
441
|
+
this.list = doc.createElement('ul');
|
|
442
|
+
this.list.className = 'vela-menu';
|
|
443
|
+
this.list.dataset.slot = dropdownMenuDef.slot('content');
|
|
444
|
+
if (opts.minWidth) this.list.style.minWidth = opts.minWidth;
|
|
445
|
+
this.positioner.appendChild(this.list);
|
|
446
|
+
this.host.appendChild(this.positioner);
|
|
447
|
+
|
|
448
|
+
const trigger = opts.trigger;
|
|
449
|
+
this.opener = trigger ?? opts.opener ?? null;
|
|
450
|
+
this.ctrl = dropdownMenuController({
|
|
451
|
+
items: [],
|
|
452
|
+
id: opts.id,
|
|
453
|
+
...opts.placement,
|
|
454
|
+
onSelect: (id) => this.onSelect(id),
|
|
455
|
+
getAnchorRect: () => this.pinnedAnchor,
|
|
456
|
+
onOpenChange: (open) => {
|
|
457
|
+
if (open && trigger) {
|
|
458
|
+
const r = trigger.getBoundingClientRect();
|
|
459
|
+
this.pinnedAnchor = { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
460
|
+
} else {
|
|
461
|
+
this.pinnedAnchor = null;
|
|
462
|
+
}
|
|
463
|
+
opts.onOpenChange?.(open);
|
|
464
|
+
},
|
|
465
|
+
triggerId: opts.triggerId,
|
|
466
|
+
});
|
|
467
|
+
if (opts.submenu) {
|
|
468
|
+
// Zag lays a submenu against its row, which sits inside the parent list: push it out
|
|
469
|
+
// past the list's edge (on whichever side it opens) so it never covers the parent,
|
|
470
|
+
// and up by the same inset so its first row lines up with the row it opened from.
|
|
471
|
+
// Zag sets a submenu's gutter itself; an explicit offset outranks it.
|
|
472
|
+
this.ctrl.props.positioning = {
|
|
473
|
+
...this.ctrl.props.positioning,
|
|
474
|
+
offset: { mainAxis: ROW_INSET_PX + SUBMENU_GAP_PX },
|
|
475
|
+
shift: -ROW_INSET_PX,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
this.mid = String(this.ctrl.props.id);
|
|
479
|
+
if (opts.triggerId && trigger) trigger.id = opts.triggerId;
|
|
480
|
+
this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
|
|
481
|
+
const api = this.ctrl.connect(service);
|
|
482
|
+
// Close announces while the list still shows, open once it does.
|
|
483
|
+
const closing = this.shown && !api.open;
|
|
484
|
+
if (closing) this.announce(false);
|
|
485
|
+
if (api.open && this.exit) {
|
|
486
|
+
this.exit.cancel();
|
|
487
|
+
this.exit = null;
|
|
488
|
+
}
|
|
489
|
+
if (trigger) spreadProps(trigger, api.getTriggerProps(), this.mid);
|
|
490
|
+
spreadProps(this.positioner, api.getPositionerProps(), this.mid);
|
|
491
|
+
spreadProps(this.list, api.getContentProps(), this.mid);
|
|
492
|
+
this.project(api);
|
|
493
|
+
if (closing) this.holdExit();
|
|
494
|
+
if (api.open && !this.shown) this.announce(true);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Keep the just-closed list up through its exit animation. `spreadProps` re-applies
|
|
499
|
+
* `hidden` only when the machine's value for it changes, so the list stays shown until
|
|
500
|
+
* the exit ends — or until a reopen, which projects `hidden: false` itself. */
|
|
501
|
+
private holdExit(): void {
|
|
502
|
+
this.list.hidden = false;
|
|
503
|
+
this.exit = holdForExit(this.list, () => {
|
|
504
|
+
this.exit = null;
|
|
505
|
+
if (!this.shown) this.list.hidden = true;
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
get api(): zagMenu.Api {
|
|
510
|
+
return this.ctrl.connect(this.handle.service);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private announce(open: boolean): void {
|
|
514
|
+
this.shown = open;
|
|
515
|
+
announceSurface(this.list, open, 'menu', this.opener);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
setItems(items: readonly MenuItemDescriptor[]): void {
|
|
519
|
+
this.items = items;
|
|
520
|
+
for (const sub of this.subs.values()) sub.destroy();
|
|
521
|
+
this.subs.clear();
|
|
522
|
+
this.build();
|
|
523
|
+
const api = this.api;
|
|
524
|
+
for (const sub of this.subs.values()) {
|
|
525
|
+
api.setChild(sub.handle.service);
|
|
526
|
+
sub.api.setParent(this.handle.service);
|
|
527
|
+
}
|
|
528
|
+
this.handle.flush();
|
|
529
|
+
// Those two are queued sends: a child only learns it IS a submenu on the next
|
|
530
|
+
// microtask, and a branch row's props (hover intent, the trigger-item part) are
|
|
531
|
+
// derived from that. Re-project once the queue has drained.
|
|
532
|
+
if (this.subs.size > 0) queueMicrotask(() => this.handle.flush());
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
destroy(): void {
|
|
536
|
+
for (const sub of this.subs.values()) sub.destroy();
|
|
537
|
+
this.subs.clear();
|
|
538
|
+
this.exit?.finish();
|
|
539
|
+
this.handle.stop();
|
|
540
|
+
// Torn down while open: the stopped machine never projects the close.
|
|
541
|
+
if (this.shown) this.announce(false);
|
|
542
|
+
this.positioner.remove();
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
private byId(id: string): MenuItemDescriptor | undefined {
|
|
546
|
+
return this.items.find((i) => i.id === id);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** Push the machine's item props onto the rendered rows. A branch takes the submenu
|
|
550
|
+
* trigger props, which carry the child's ids and hover handlers; a leaf takes plain ones. */
|
|
551
|
+
private project(api: zagMenu.Api): void {
|
|
552
|
+
for (const li of this.list.children) {
|
|
553
|
+
const id = (li as HTMLElement).dataset.veiId;
|
|
554
|
+
if (!id) continue;
|
|
555
|
+
const sub = this.subs.get(id);
|
|
556
|
+
if (sub) {
|
|
557
|
+
spreadProps(li as HTMLElement, api.getTriggerItemProps(sub.api), this.mid);
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
const item = this.byId(id);
|
|
561
|
+
// Switch rows keep the menu OPEN on selection — flip, see, flip again.
|
|
562
|
+
spreadProps(li as HTMLElement, api.getItemProps({ value: id, disabled: item?.disabled, closeOnSelect: item?.toggle ? false : undefined }), this.mid);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private build(): void {
|
|
567
|
+
const doc = this.doc;
|
|
568
|
+
this.list.replaceChildren();
|
|
569
|
+
// Reserve the leading mark column only when THIS level actually holds checkable
|
|
570
|
+
// rows — an all-action list (e.g. the chart-body context menu) keeps its natural
|
|
571
|
+
// left edge instead of carrying an empty gutter.
|
|
572
|
+
const markable = this.checkmarks
|
|
573
|
+
&& this.items.some((i) => !(i.submenu && i.submenu.length > 0) && !i.toggle && i.checked !== undefined);
|
|
574
|
+
// Same for icons: once a row of this level carries one, the others keep an empty
|
|
575
|
+
// icon slot (an empty badge in badge mode) so every label shares one left edge.
|
|
576
|
+
const iconic = this.items.some((i) => !!i.icon);
|
|
577
|
+
const badged = this.iconBadges && iconic;
|
|
578
|
+
if (badged) this.list.dataset.badges = '1';
|
|
579
|
+
else delete this.list.dataset.badges;
|
|
580
|
+
for (const item of this.items) {
|
|
581
|
+
if (item.separatorBefore) {
|
|
582
|
+
const sep = doc.createElement('li');
|
|
583
|
+
sep.className = 'vela-menu-sep';
|
|
584
|
+
sep.dataset.slot = dropdownMenuDef.slot('separator');
|
|
585
|
+
sep.setAttribute('role', 'separator');
|
|
586
|
+
this.list.appendChild(sep);
|
|
587
|
+
}
|
|
588
|
+
const branch = item.submenu !== undefined && item.submenu.length > 0;
|
|
589
|
+
const li = doc.createElement('li');
|
|
590
|
+
li.className = 'vela-menu-item';
|
|
591
|
+
li.dataset.slot = dropdownMenuDef.slot('item');
|
|
592
|
+
li.dataset.veiId = item.id;
|
|
593
|
+
if (item.toggle) {
|
|
594
|
+
// Switch row: the pill carries the state.
|
|
595
|
+
// (Zag owns the item's ARIA props; the pill below is decorative.)
|
|
596
|
+
li.dataset.toggle = '1';
|
|
597
|
+
}
|
|
598
|
+
if (markable) {
|
|
599
|
+
// Mark mode: a leading ✓ slot on every row of this level (empty when
|
|
600
|
+
// unchecked, so labels share a left edge); the row surface stays free
|
|
601
|
+
// for hover.
|
|
602
|
+
const mark = doc.createElement('span');
|
|
603
|
+
mark.className = 'vela-menu-mark';
|
|
604
|
+
mark.dataset.slot = dropdownMenuDef.slot('mark');
|
|
605
|
+
if (!branch && !item.toggle && item.checked) {
|
|
606
|
+
mark.appendChild(menuIcon('check', doc));
|
|
607
|
+
li.dataset.checkmark = '1';
|
|
608
|
+
}
|
|
609
|
+
li.appendChild(mark);
|
|
610
|
+
} else if (!branch && !item.toggle && item.checked) {
|
|
611
|
+
// Selection reads from the row surface: a stronger background wash
|
|
612
|
+
// marks the active entry (hover stays the lighter wash).
|
|
613
|
+
li.dataset.checked = '1';
|
|
614
|
+
}
|
|
615
|
+
if (badged) {
|
|
616
|
+
const badge = doc.createElement('span');
|
|
617
|
+
badge.className = 'vela-menu-badge';
|
|
618
|
+
badge.dataset.slot = dropdownMenuDef.slot('badge');
|
|
619
|
+
if (item.icon) badge.appendChild(menuIcon(item.icon, doc));
|
|
620
|
+
li.appendChild(badge);
|
|
621
|
+
} else if (item.icon) {
|
|
622
|
+
li.appendChild(menuIcon(item.icon, doc));
|
|
623
|
+
} else if (iconic) {
|
|
624
|
+
li.appendChild(menuIcon('', doc));
|
|
625
|
+
}
|
|
626
|
+
const label = doc.createElement('span');
|
|
627
|
+
label.className = 'vela-menu-label';
|
|
628
|
+
label.dataset.slot = dropdownMenuDef.slot('label');
|
|
629
|
+
label.textContent = item.label;
|
|
630
|
+
li.appendChild(label);
|
|
631
|
+
if (item.hint) {
|
|
632
|
+
const hint = doc.createElement('span');
|
|
633
|
+
hint.className = 'vela-menu-hint';
|
|
634
|
+
hint.dataset.slot = dropdownMenuDef.slot('hint');
|
|
635
|
+
hint.textContent = item.hint;
|
|
636
|
+
li.appendChild(hint);
|
|
637
|
+
}
|
|
638
|
+
if (item.favorite !== undefined && this.onFavorite) {
|
|
639
|
+
const on = item.favorite;
|
|
640
|
+
const star = menuIcon(on ? 'star-filled' : 'star', doc);
|
|
641
|
+
star.classList.add('vela-menu-star');
|
|
642
|
+
addSlot(star, dropdownMenuDef.slot('star'));
|
|
643
|
+
if (on) {
|
|
644
|
+
star.classList.add('vela-fav');
|
|
645
|
+
star.dataset.fav = '';
|
|
646
|
+
}
|
|
647
|
+
star.removeAttribute('aria-hidden'); // iconEl hides decorative icons; this one is a control
|
|
648
|
+
star.setAttribute('role', 'button');
|
|
649
|
+
star.setAttribute('aria-label', textOf(this.i18n, on ? TEXT.removeFavorite : TEXT.addFavorite));
|
|
650
|
+
star.setAttribute('aria-pressed', on ? 'true' : 'false');
|
|
651
|
+
// Swallow the whole pointer sequence: the machine selects rows on click,
|
|
652
|
+
// and a press that starts on the star must never reach the row.
|
|
653
|
+
for (const ev of ['pointerdown', 'pointerup', 'mousedown', 'mouseup'] as const) {
|
|
654
|
+
star.addEventListener(ev, (e) => e.stopPropagation());
|
|
655
|
+
}
|
|
656
|
+
star.addEventListener('click', (e) => {
|
|
657
|
+
e.stopPropagation();
|
|
658
|
+
e.preventDefault();
|
|
659
|
+
this.onFavorite?.(item.id, !on);
|
|
660
|
+
});
|
|
661
|
+
li.appendChild(star);
|
|
662
|
+
}
|
|
663
|
+
if (item.toggle) {
|
|
664
|
+
const sw = doc.createElement('span');
|
|
665
|
+
sw.className = 'vela-menu-switch' + (item.checked ? ' on' : '');
|
|
666
|
+
sw.dataset.slot = dropdownMenuDef.slot('switch');
|
|
667
|
+
if (item.checked) sw.dataset.on = '';
|
|
668
|
+
sw.setAttribute('aria-hidden', 'true');
|
|
669
|
+
li.appendChild(sw);
|
|
670
|
+
}
|
|
671
|
+
if (branch) {
|
|
672
|
+
li.dataset.branch = '1';
|
|
673
|
+
const arrow = menuIcon('chevron-right', doc);
|
|
674
|
+
arrow.classList.add('vela-menu-arrow');
|
|
675
|
+
addSlot(arrow, dropdownMenuDef.slot('arrow'));
|
|
676
|
+
li.appendChild(arrow);
|
|
677
|
+
const sub = new Surface(doc, {
|
|
678
|
+
host: this.host,
|
|
679
|
+
placement: { placement: 'right-start' },
|
|
680
|
+
submenu: true,
|
|
681
|
+
opener: li,
|
|
682
|
+
onSelect: this.onSelect,
|
|
683
|
+
onFavorite: this.onFavorite,
|
|
684
|
+
i18n: this.i18n,
|
|
685
|
+
checkmarks: this.checkmarks,
|
|
686
|
+
iconBadges: this.iconBadges,
|
|
687
|
+
id: `${this.mid}--${item.id}`,
|
|
688
|
+
});
|
|
689
|
+
sub.setItems(item.submenu ?? []);
|
|
690
|
+
this.subs.set(item.id, sub);
|
|
691
|
+
}
|
|
692
|
+
this.list.appendChild(li);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
export class DropdownMenu {
|
|
698
|
+
private readonly root: Surface;
|
|
699
|
+
|
|
700
|
+
constructor(opts: DropdownMenuOptions) {
|
|
701
|
+
const anchor = opts.trigger ?? opts.host ?? document.body;
|
|
702
|
+
const doc = anchor.ownerDocument;
|
|
703
|
+
injectStyles(doc);
|
|
704
|
+
const host = floatingLayerHost(opts.host ?? anchor, doc.body);
|
|
705
|
+
this.root = new Surface(doc, {
|
|
706
|
+
host,
|
|
707
|
+
placement: {
|
|
708
|
+
placement: opts.placement,
|
|
709
|
+
side: opts.side,
|
|
710
|
+
align: opts.align,
|
|
711
|
+
sideOffset: opts.sideOffset,
|
|
712
|
+
alignOffset: opts.alignOffset,
|
|
713
|
+
avoidCollisions: opts.avoidCollisions,
|
|
714
|
+
collisionPadding: opts.collisionPadding,
|
|
715
|
+
collisionBoundary: opts.collisionBoundary,
|
|
716
|
+
},
|
|
717
|
+
onSelect: (id) => opts.onSelect?.(id),
|
|
718
|
+
onOpenChange: opts.onOpenChange,
|
|
719
|
+
trigger: opts.trigger,
|
|
720
|
+
triggerId: opts.triggerId,
|
|
721
|
+
id: opts.id,
|
|
722
|
+
minWidth: opts.minWidth,
|
|
723
|
+
onFavorite: opts.onFavorite,
|
|
724
|
+
i18n: opts.i18n ?? kitTranslator(),
|
|
725
|
+
checkmarks: opts.checkmarks,
|
|
726
|
+
iconBadges: opts.iconBadges,
|
|
727
|
+
});
|
|
728
|
+
this.root.setItems(opts.items);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
get api(): zagMenu.Api {
|
|
732
|
+
return this.root.api;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
open(): void {
|
|
736
|
+
this.api.setOpen(true);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/** Open anchored to a viewport point (context menus). */
|
|
740
|
+
openAt(clientX: number, clientY: number): void {
|
|
741
|
+
const api = this.api;
|
|
742
|
+
api.setOpen(true);
|
|
743
|
+
api.reposition({ getAnchorRect: () => ({ x: clientX, y: clientY, width: 0, height: 0 }) });
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
close(): void {
|
|
747
|
+
this.api.setOpen(false);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/** Swap the item descriptors (e.g. checked states) and re-project. */
|
|
751
|
+
setItems(items: readonly MenuItemDescriptor[]): void {
|
|
752
|
+
this.root.setItems(items);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
destroy(): void {
|
|
756
|
+
this.root.destroy();
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export function createDropdownMenu(props: DropdownMenuProps): DropdownMenu {
|
|
761
|
+
return new DropdownMenu(props);
|
|
762
|
+
}
|