@nanahoshi/mona-ui 0.24.7 → 0.24.9

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.
Files changed (97) hide show
  1. package/fesm2022/nanahoshi-mona-ui-auto-complete.mjs +1 -1
  2. package/fesm2022/nanahoshi-mona-ui-button-group.mjs +37 -9
  3. package/fesm2022/nanahoshi-mona-ui-button.mjs +8 -2
  4. package/fesm2022/nanahoshi-mona-ui-color-gradient.mjs +4 -3
  5. package/fesm2022/nanahoshi-mona-ui-combo-box.mjs +1 -1
  6. package/fesm2022/nanahoshi-mona-ui-contextmenu.mjs +101 -47
  7. package/fesm2022/nanahoshi-mona-ui-dialog.mjs +339 -101
  8. package/fesm2022/nanahoshi-mona-ui-dropdown-button.mjs +119 -69
  9. package/fesm2022/nanahoshi-mona-ui-dropdown-list.mjs +1 -1
  10. package/fesm2022/nanahoshi-mona-ui-editor.mjs +2 -2
  11. package/fesm2022/{nanahoshi-mona-ui-grid-grid-footer-row.component-vXXNWmEN.mjs → nanahoshi-mona-ui-grid-grid-footer-row.component-DhKLnW06.mjs} +2 -2
  12. package/fesm2022/{nanahoshi-mona-ui-grid-grid-list.component-DDgNmgE0.mjs → nanahoshi-mona-ui-grid-grid-list.component-voGbKOiB.mjs} +4 -4
  13. package/fesm2022/{nanahoshi-mona-ui-grid-grid-row.component-Dzf6Vbs_.mjs → nanahoshi-mona-ui-grid-grid-row.component-HQ1Kv6R9.mjs} +2 -3
  14. package/fesm2022/{nanahoshi-mona-ui-grid-grid-virtual-list.component-DAPNqnWV.mjs → nanahoshi-mona-ui-grid-grid-virtual-list.component-PNLJFcuH.mjs} +4 -4
  15. package/fesm2022/{nanahoshi-mona-ui-grid-nanahoshi-mona-ui-grid-B9rmDLq8.mjs → nanahoshi-mona-ui-grid-nanahoshi-mona-ui-grid-BO19AVwC.mjs} +10 -10
  16. package/fesm2022/nanahoshi-mona-ui-grid.mjs +1 -1
  17. package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +44 -3
  18. package/fesm2022/nanahoshi-mona-ui-internal-filter-input.mjs +20 -4
  19. package/fesm2022/nanahoshi-mona-ui-internal-list.mjs +335 -45
  20. package/fesm2022/nanahoshi-mona-ui-internal-tree.mjs +565 -197
  21. package/fesm2022/nanahoshi-mona-ui-internal.mjs +465 -23
  22. package/fesm2022/nanahoshi-mona-ui-list-box.mjs +598 -208
  23. package/fesm2022/nanahoshi-mona-ui-list-view.mjs +193 -71
  24. package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +29 -3
  25. package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +33 -3
  26. package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +33 -3
  27. package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +40 -3
  28. package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +29 -3
  29. package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +33 -3
  30. package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +29 -3
  31. package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +29 -3
  32. package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +33 -3
  33. package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +49 -3
  34. package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +29 -3
  35. package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +29 -3
  36. package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +29 -3
  37. package/fesm2022/nanahoshi-mona-ui-menubar.mjs +127 -133
  38. package/fesm2022/nanahoshi-mona-ui-multi-select.mjs +1 -1
  39. package/fesm2022/nanahoshi-mona-ui-notification.mjs +473 -240
  40. package/fesm2022/nanahoshi-mona-ui-popover.mjs +202 -90
  41. package/fesm2022/nanahoshi-mona-ui-popup-menu.mjs +609 -256
  42. package/fesm2022/nanahoshi-mona-ui-popup.mjs +2 -0
  43. package/fesm2022/nanahoshi-mona-ui-sheet.mjs +45 -10
  44. package/fesm2022/nanahoshi-mona-ui-split-button.mjs +102 -58
  45. package/fesm2022/nanahoshi-mona-ui-tooltip.mjs +286 -443
  46. package/fesm2022/nanahoshi-mona-ui-tree-view.mjs +205 -140
  47. package/fesm2022/nanahoshi-mona-ui-window.mjs +591 -310
  48. package/i18n/catalogs/en-US.messages.json +32 -6
  49. package/locales/ar-sa/ar-sa.messages.json +29 -3
  50. package/locales/de-de/de-de.messages.json +29 -3
  51. package/locales/es-es/es-es.messages.json +29 -3
  52. package/locales/fr-fr/fr-fr.messages.json +29 -3
  53. package/locales/id-id/id-id.messages.json +29 -3
  54. package/locales/it-it/it-it.messages.json +29 -3
  55. package/locales/ja-jp/ja-jp.messages.json +29 -3
  56. package/locales/ko-kr/ko-kr.messages.json +29 -3
  57. package/locales/pt-br/pt-br.messages.json +29 -3
  58. package/locales/ru-ru/ru-ru.messages.json +29 -3
  59. package/locales/tr-tr/tr-tr.messages.json +29 -3
  60. package/locales/zh-cn/zh-cn.messages.json +29 -3
  61. package/locales/zh-tw/zh-tw.messages.json +29 -3
  62. package/package.json +1 -1
  63. package/types/nanahoshi-mona-ui-button-group.d.ts +13 -3
  64. package/types/nanahoshi-mona-ui-button.d.ts +20 -3
  65. package/types/nanahoshi-mona-ui-contextmenu.d.ts +5 -6
  66. package/types/nanahoshi-mona-ui-dialog.d.ts +131 -20
  67. package/types/nanahoshi-mona-ui-dropdown-button.d.ts +75 -56
  68. package/types/nanahoshi-mona-ui-i18n.d.ts +69 -0
  69. package/types/nanahoshi-mona-ui-internal-filter-input.d.ts +14 -2
  70. package/types/nanahoshi-mona-ui-internal-list.d.ts +89 -10
  71. package/types/nanahoshi-mona-ui-internal-tree.d.ts +155 -40
  72. package/types/nanahoshi-mona-ui-internal.d.ts +128 -16
  73. package/types/nanahoshi-mona-ui-list-box.d.ts +148 -47
  74. package/types/nanahoshi-mona-ui-list-view.d.ts +112 -21
  75. package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +26 -0
  76. package/types/nanahoshi-mona-ui-locales-de-de.d.ts +26 -0
  77. package/types/nanahoshi-mona-ui-locales-es-es.d.ts +26 -0
  78. package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +26 -0
  79. package/types/nanahoshi-mona-ui-locales-id-id.d.ts +26 -0
  80. package/types/nanahoshi-mona-ui-locales-it-it.d.ts +26 -0
  81. package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +26 -0
  82. package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +26 -0
  83. package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +26 -0
  84. package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +26 -0
  85. package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +26 -0
  86. package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +26 -0
  87. package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +26 -0
  88. package/types/nanahoshi-mona-ui-menubar.d.ts +65 -62
  89. package/types/nanahoshi-mona-ui-notification.d.ts +76 -16
  90. package/types/nanahoshi-mona-ui-popover.d.ts +44 -17
  91. package/types/nanahoshi-mona-ui-popup-menu.d.ts +110 -32
  92. package/types/nanahoshi-mona-ui-popup.d.ts +2 -0
  93. package/types/nanahoshi-mona-ui-sheet.d.ts +16 -5
  94. package/types/nanahoshi-mona-ui-split-button.d.ts +29 -7
  95. package/types/nanahoshi-mona-ui-tooltip.d.ts +43 -20
  96. package/types/nanahoshi-mona-ui-tree-view.d.ts +84 -40
  97. package/types/nanahoshi-mona-ui-window.d.ts +103 -14
@@ -1,15 +1,14 @@
1
1
  import * as i0 from '@angular/core';
2
- import { InjectionToken, signal, computed, inject, DestroyRef, ElementRef, afterNextRender, Component, contentChild, contentChildren, input, output, afterRenderEffect, linkedSignal, forwardRef, ChangeDetectionStrategy, model, TemplateRef, Directive } from '@angular/core';
2
+ import { InjectionToken, signal, isSignal, inject, DestroyRef, DOCUMENT, ElementRef, computed, afterNextRender, effect, untracked, Component, contentChild, contentChildren, input, output, model, forwardRef, TemplateRef, Directive } from '@angular/core';
3
3
  import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
4
4
  import { PreventableEvent, classInputToClass } from '@nanahoshi/mona-ui/common';
5
- import { themeOverlaySurfaceClasses, createElementControlId, isNavigationKey, isTypeaheadKey, setupTypeahead, toCssValue, rxTimeout } from '@nanahoshi/mona-ui/internal';
5
+ import { createElementControlId, themeOverlaySurfaceClasses, isNavigationKey, isTypeaheadKey, setupTypeahead, rxTimeout, toCssValue } from '@nanahoshi/mona-ui/internal';
6
6
  import { PopupDataInjectionToken, PopupService } from '@nanahoshi/mona-ui/popup';
7
- import { forEach, select, groupBy, selectMany, any } from '@mirei/ts-collections';
8
- import { Subject, take, tap, fromEvent, takeUntil, filter, switchMap } from 'rxjs';
9
- import { v4 } from 'uuid';
7
+ import { forEach, select, selectMany, any } from '@mirei/ts-collections';
8
+ import { Subject, take, fromEvent, tap, takeUntil, filter } from 'rxjs';
10
9
  import { NgTemplateOutlet } from '@angular/common';
11
10
  import { LucideChevronRight, LucideCheck } from '@lucide/angular';
12
- import { injectComponentDirection, MonaI18nService } from '@nanahoshi/mona-ui/i18n';
11
+ import { injectComponentDirection } from '@nanahoshi/mona-ui/i18n';
13
12
  import { twMerge } from 'tailwind-merge';
14
13
  import { cva } from 'class-variance-authority';
15
14
 
@@ -19,6 +18,7 @@ class PopupMenuCloseEvent extends PreventableEvent {
19
18
  }
20
19
  }
21
20
 
21
+ /* eslint-disable @typescript-eslint/naming-convention -- exported token names; renaming them would break consumers. */
22
22
  const PopupMenuToken = new InjectionToken("MONA_POPUP_MENU");
23
23
  const PopupMenuRadioItemToken = new InjectionToken("MONA_POPUP_MENU_RADIO_ITEM");
24
24
  const PopupMenuGroupTemplateToken = new InjectionToken("MONA_POPUP_MENU_GROUP_TEMPLATE");
@@ -91,6 +91,16 @@ const ensurePopupTemplateTypes = (params) => {
91
91
  }
92
92
  });
93
93
  };
94
+ // One id per `MenuItem` object, so a menu rebuilt from the same objects keeps track of its items.
95
+ const menuItemUids = new WeakMap();
96
+ const menuItemUid = (item) => {
97
+ let uid = menuItemUids.get(item);
98
+ if (!uid) {
99
+ uid = createElementControlId();
100
+ menuItemUids.set(item, uid);
101
+ }
102
+ return uid;
103
+ };
94
104
  const preparePopupMenuItems = (items) => {
95
105
  return select(items, item => {
96
106
  const popupItem = {
@@ -98,7 +108,8 @@ const preparePopupMenuItems = (items) => {
98
108
  checked: signal(item.checked ?? false),
99
109
  click$: new Subject(),
100
110
  disabled: item.disabled ?? false,
101
- group: item.group ?? "",
111
+ // An ungrouped item gets a key of its own, so it never joins another item's group.
112
+ group: item.group ?? Symbol(),
102
113
  groupTemplate: null,
103
114
  iconTemplate: null,
104
115
  items: preparePopupMenuItems(item.items ?? []),
@@ -106,7 +117,8 @@ const preparePopupMenuItems = (items) => {
106
117
  separator: item.separator ?? false,
107
118
  shortcutTemplate: null,
108
119
  textTemplate: null,
109
- uid: v4()
120
+ uid: menuItemUid(item),
121
+ value: signal(item.value)
110
122
  };
111
123
  return popupItem;
112
124
  }).toArray();
@@ -116,22 +128,80 @@ const convertToMenuItem = (item) => {
116
128
  checkable: item.checkable,
117
129
  checked: item.checked ? item.checked() : false,
118
130
  disabled: item.disabled,
119
- group: item.group ? String(item.group) : undefined,
131
+ group: typeof item.group === "string" ? item.group || undefined : item.group.description || undefined,
120
132
  items: select(item.items, i => convertToMenuItem(i)).toArray(),
121
133
  label: item.label,
122
- separator: item.separator
134
+ separator: item.separator,
135
+ value: item.value?.()
123
136
  };
124
137
  };
138
+ /**
139
+ * Groups consecutive items that share a group key, keeping the items' order. Items with the same key that aren't
140
+ * next to each other form separate groups.
141
+ */
142
+ const groupPopupMenuItems = (items) => {
143
+ const groups = [];
144
+ for (const item of items) {
145
+ const last = groups.at(-1);
146
+ if (last && last.key === item.group) {
147
+ last.items.push(item);
148
+ }
149
+ else {
150
+ groups.push({ items: [item], key: item.group });
151
+ }
152
+ }
153
+ return groups.map(group => {
154
+ const title = typeof group.key === "string" ? group.key : (group.key.description ?? "");
155
+ return { items: group.items, key: group.key, labelId: title ? createElementControlId() : "", title };
156
+ });
157
+ };
158
+ /**
159
+ * Whether the item's checked state is held by the menu (a checkable `MenuItem` from the `items` input), so the
160
+ * menu toggles it when the item is activated.
161
+ */
162
+ const isMenuOwnedCheckState = (item) => {
163
+ return item.checkable === true && isSignal(item.checked) && "set" in item.checked;
164
+ };
125
165
 
166
+ /**
167
+ * Emitted when a menu item is activated by a click, Enter or Space. Preventing it keeps the menu open and leaves a
168
+ * checkbox or radio item's state unchanged.
169
+ */
126
170
  class PopupMenuItemClickEvent extends PreventableEvent {
171
+ #checked;
127
172
  #item;
128
173
  constructor(item, originalEvent) {
129
174
  super("popupMenuItemClick", originalEvent);
130
175
  this.#item = item;
176
+ if (item.checkable) {
177
+ this.#checked = !(item.checked?.() ?? false);
178
+ }
179
+ else if (item.radio) {
180
+ this.#checked = true;
181
+ }
182
+ }
183
+ /**
184
+ * For a checkbox or radio item, the state the item has once the click is applied (if it isn't prevented).
185
+ * `undefined` for other items.
186
+ */
187
+ get checked() {
188
+ return this.#checked;
131
189
  }
132
190
  get item() {
133
191
  return this.#item;
134
192
  }
193
+ /**
194
+ * The item's label.
195
+ */
196
+ get label() {
197
+ return this.#item.label;
198
+ }
199
+ /**
200
+ * The item's `value`: the `value` input of the item component, or the `value` of a `MenuItem`.
201
+ */
202
+ get value() {
203
+ return this.#item.value?.();
204
+ }
135
205
  }
136
206
 
137
207
  const popupMenuBaseThemeVariants = cva(`h-full w-full overflow-hidden`, {
@@ -178,6 +248,7 @@ const popupMenuItemThemeVariants = cva(`
178
248
  data-[disabled='true']:text-disabled-foreground
179
249
  data-[active='true']:bg-[var(--mona-menu-item-hover-background,var(--color-hover))]
180
250
  data-[active='true']:text-foreground
251
+ data-[active='true']:data-[disabled='true']:text-disabled-foreground
181
252
  `, {
182
253
  variants: {
183
254
  rounded: {
@@ -195,74 +266,53 @@ const popupMenuItemThemeVariants = cva(`
195
266
  });
196
267
  const popupMenuLinkThemeVariants = cva(`flex items-center justify-center`);
197
268
 
269
+ /**
270
+ * How long the pointer can cross other items, while a submenu is open, before that submenu closes. It gives the
271
+ * pointer time to travel diagonally from a parent item to its submenu.
272
+ */
273
+ const SUBMENU_SWITCH_DELAY = 250;
198
274
  class PopupMenuListComponent {
199
275
  #childCloseRequest$;
200
- #childMenuItems;
201
- #close$;
202
- #config;
203
276
  #destroyRef;
204
277
  #direction;
278
+ #document;
205
279
  #host;
206
- #i18n;
280
+ // Disabled items stay in the arrow-key order (they can't be activated), as the APG menu pattern asks.
207
281
  #navigationItems;
282
+ #openSubmenuItem;
208
283
  #parentConfig;
209
284
  #popupService;
285
+ #submenuId;
210
286
  #typeaheadKey$;
211
- #viaKeyboardNavigation;
287
+ #childClose$;
288
+ #closing;
289
+ #hoverTimer;
212
290
  constructor() {
213
291
  this.#childCloseRequest$ = new Subject();
214
- this.#childMenuItems = computed(() => {
215
- const activeMenuItem = this.activeMenuItem();
216
- if (activeMenuItem) {
217
- return groupBy(activeMenuItem.items, item => item.group).toArray();
218
- }
219
- return [];
220
- }, /* @ts-ignore */
221
- ...(ngDevMode ? [{ debugName: "#childMenuItems" }] : /* istanbul ignore next */ []));
222
- this.#close$ = new Subject();
223
- this.#config = computed(() => {
224
- const data = {
225
- childCloseRequest$: this.#childCloseRequest$,
226
- isRoot: false,
227
- items: this.#childMenuItems(),
228
- level: this.#parentConfig.level + 1,
229
- menuId: createElementControlId(this.#parentConfig.level + 1),
230
- menuItemClick$: this.#parentConfig.menuItemClick$,
231
- minWidth: this.#parentConfig.minWidth,
232
- navigate$: this.#parentConfig.navigate$,
233
- parentClose$: this.#close$,
234
- popupGroupTemplate: this.#parentConfig.popupGroupTemplate,
235
- popupIconTemplate: this.#parentConfig.popupIconTemplate,
236
- popupShortcutTemplate: this.#parentConfig.popupShortcutTemplate,
237
- popupTextTemplate: this.#parentConfig.popupTextTemplate,
238
- rounded: this.#parentConfig.rounded,
239
- size: this.#parentConfig.size,
240
- viaKeyboardNavigation: this.#viaKeyboardNavigation(),
241
- width: this.#parentConfig.width
242
- };
243
- return data;
244
- }, /* @ts-ignore */
245
- ...(ngDevMode ? [{ debugName: "#config" }] : /* istanbul ignore next */ []));
246
292
  this.#destroyRef = inject(DestroyRef);
247
293
  this.#direction = injectComponentDirection();
294
+ this.#document = inject(DOCUMENT);
248
295
  this.#host = inject((ElementRef));
249
- this.#i18n = inject(MonaI18nService);
250
- this.isRtl = computed(() => this.#direction() === "rtl", /* @ts-ignore */
251
- ...(ngDevMode ? [{ debugName: "isRtl" }] : /* istanbul ignore next */ []));
296
+ // Disabled items stay in the arrow-key order (they can't be activated), as the APG menu pattern asks.
252
297
  this.#navigationItems = computed(() => {
253
- const items = this.#parentConfig.items;
254
- return selectMany(items, i => i.source)
255
- .where(i => !i.separator && !i.disabled)
298
+ const items = this.#parentConfig.items();
299
+ return selectMany(items, i => i.items)
300
+ .where(i => !i.separator)
256
301
  .toImmutableSet();
257
302
  }, /* @ts-ignore */
258
303
  ...(ngDevMode ? [{ debugName: "#navigationItems" }] : /* istanbul ignore next */ []));
304
+ this.#openSubmenuItem = signal(null, /* @ts-ignore */
305
+ ...(ngDevMode ? [{ debugName: "#openSubmenuItem" }] : /* istanbul ignore next */ []));
259
306
  this.#parentConfig = inject(PopupDataInjectionToken);
260
307
  this.#popupService = inject(PopupService);
308
+ this.#submenuId = signal(null, /* @ts-ignore */
309
+ ...(ngDevMode ? [{ debugName: "#submenuId" }] : /* istanbul ignore next */ []));
261
310
  this.#typeaheadKey$ = new Subject();
262
- this.#viaKeyboardNavigation = signal(false, /* @ts-ignore */
263
- ...(ngDevMode ? [{ debugName: "#viaKeyboardNavigation" }] : /* istanbul ignore next */ []));
311
+ this.#childClose$ = null;
312
+ this.#closing = false;
264
313
  this.activeMenuItem = signal(null, /* @ts-ignore */
265
314
  ...(ngDevMode ? [{ debugName: "activeMenuItem" }] : /* istanbul ignore next */ []));
315
+ this.ariaLabel = this.#parentConfig.ariaLabel || null;
266
316
  this.baseClasses = computed(() => {
267
317
  const rounded = this.#parentConfig.rounded();
268
318
  return popupMenuBaseThemeVariants({ rounded });
@@ -279,8 +329,8 @@ class PopupMenuListComponent {
279
329
  }, /* @ts-ignore */
280
330
  ...(ngDevMode ? [{ debugName: "groupHeaderClasses" }] : /* istanbul ignore next */ []));
281
331
  this.iconAreaVisible = computed(() => {
282
- const items = this.#parentConfig.items;
283
- const hasIconsOrSelectable = selectMany(items, i => i.source).any(i => i.iconTemplate?.() != null || i.checkable === true || i.radio === true);
332
+ const items = this.#parentConfig.items();
333
+ const hasIconsOrSelectable = selectMany(items, i => i.items).any(i => i.iconTemplate?.() != null || i.checkable === true || i.radio === true);
284
334
  return hasIconsOrSelectable || !!this.#parentConfig.popupIconTemplate?.();
285
335
  }, /* @ts-ignore */
286
336
  ...(ngDevMode ? [{ debugName: "iconAreaVisible" }] : /* istanbul ignore next */ []));
@@ -288,6 +338,9 @@ class PopupMenuListComponent {
288
338
  return popupMenuIconContainerThemeVariants();
289
339
  }, /* @ts-ignore */
290
340
  ...(ngDevMode ? [{ debugName: "iconContainerClasses" }] : /* istanbul ignore next */ []));
341
+ this.isRtl = computed(() => this.#direction() === "rtl", /* @ts-ignore */
342
+ ...(ngDevMode ? [{ debugName: "isRtl" }] : /* istanbul ignore next */ []));
343
+ this.labelledBy = this.ariaLabel ? null : this.#parentConfig.labelledBy;
291
344
  this.linkContainerClasses = computed(() => {
292
345
  return popupMenuLinkThemeVariants();
293
346
  }, /* @ts-ignore */
@@ -303,10 +356,9 @@ class PopupMenuListComponent {
303
356
  return twMerge(variantClasses, iconClasses);
304
357
  }, /* @ts-ignore */
305
358
  ...(ngDevMode ? [{ debugName: "menuItemClasses" }] : /* istanbul ignore next */ []));
306
- this.menuItems = computed(() => this.#parentConfig.items, /* @ts-ignore */
359
+ this.menuItems = computed(() => this.#parentConfig.items(), /* @ts-ignore */
307
360
  ...(ngDevMode ? [{ debugName: "menuItems" }] : /* istanbul ignore next */ []));
308
- this.pointerEnter$ = new Subject();
309
- this.pointerLeave$ = new Subject();
361
+ this.openSubmenuItem = this.#openSubmenuItem.asReadonly();
310
362
  this.popupGroupTemplate = computed(() => this.#parentConfig.popupGroupTemplate?.() ?? null, /* @ts-ignore */
311
363
  ...(ngDevMode ? [{ debugName: "popupGroupTemplate" }] : /* istanbul ignore next */ []));
312
364
  this.popupIconTemplate = computed(() => this.#parentConfig.popupIconTemplate?.() ?? null, /* @ts-ignore */
@@ -315,22 +367,87 @@ class PopupMenuListComponent {
315
367
  ...(ngDevMode ? [{ debugName: "popupShortcutTemplate" }] : /* istanbul ignore next */ []));
316
368
  this.popupTextTemplate = computed(() => this.#parentConfig.popupTextTemplate?.(), /* @ts-ignore */
317
369
  ...(ngDevMode ? [{ debugName: "popupTextTemplate" }] : /* istanbul ignore next */ []));
370
+ this.submenuId = this.#submenuId.asReadonly();
318
371
  this.popupRef = null;
319
372
  afterNextRender({
320
- read: () => {
321
- this.#host.nativeElement.firstElementChild?.focus();
322
- if (!this.#parentConfig.isRoot && this.#parentConfig.viaKeyboardNavigation) {
373
+ // Moving focus and the highlight writes to the page, so this is the write phase.
374
+ write: () => {
375
+ const config = this.#parentConfig;
376
+ // A submenu opened by the pointer leaves focus in its parent menu.
377
+ if (config.isRoot || config.viaKeyboardNavigation) {
378
+ this.#menuElement()?.focus();
379
+ }
380
+ if (config.isRoot && config.initialFocus) {
381
+ if (config.initialFocus === "last") {
382
+ this.handleEndKey();
383
+ }
384
+ else {
385
+ this.focusFirstItem();
386
+ }
387
+ }
388
+ else if (!config.isRoot && config.viaKeyboardNavigation) {
323
389
  const item = this.focusFirstItem();
324
390
  const isRtl = this.isRtl();
325
391
  this.notifyNavigation(item, isRtl ? "left" : "right");
326
392
  }
327
393
  }
328
394
  });
395
+ // Items can change while the menu is open: keep the highlight and the open submenu on the same items.
396
+ effect(() => {
397
+ const items = this.#navigationItems();
398
+ untracked(() => {
399
+ const active = this.activeMenuItem();
400
+ if (active && !items.any(i => i === active)) {
401
+ this.activeMenuItem.set(items.firstOrDefault(i => i.uid === active.uid) ?? null);
402
+ }
403
+ const open = this.#openSubmenuItem();
404
+ if (open && !items.any(i => i === open)) {
405
+ const current = items.firstOrDefault(i => i.uid === open.uid);
406
+ if (current && current.items.length > 0) {
407
+ this.#openSubmenuItem.set(current);
408
+ }
409
+ else {
410
+ this.closePopup();
411
+ }
412
+ }
413
+ });
414
+ });
415
+ this.#destroyRef.onDestroy(() => {
416
+ clearTimeout(this.#hoverTimer);
417
+ this.#closing = true;
418
+ this.closePopup();
419
+ });
329
420
  }
330
421
  ngOnInit() {
331
422
  this.setSubscriptions();
332
423
  }
424
+ #menuElement() {
425
+ return this.#host.nativeElement.firstElementChild;
426
+ }
427
+ asItems(items) {
428
+ return items;
429
+ }
430
+ onFocusOut(event) {
431
+ const next = event.relatedTarget;
432
+ // Focus that leaves every menu of the chain closes it. Focus moving between the menus doesn't, and nor does
433
+ // focus going nowhere (the window losing focus).
434
+ if (next instanceof Element && !next.closest("mona-popup-menu-list")) {
435
+ this.#parentConfig.closeMenu({ focusLeftTo: next });
436
+ }
437
+ }
438
+ onHostPointerEnter() {
439
+ this.#parentConfig.submenuPointerEnter?.();
440
+ }
333
441
  onItemClick(event, item) {
442
+ if (item.disabled) {
443
+ return;
444
+ }
445
+ if (item.items.length > 0) {
446
+ // An item with a submenu opens it; only leaf items are activated. A click with no pointer behind it
447
+ // (assistive technology) opens the submenu as the keyboard does.
448
+ this.openSubmenuFromClick(item, event.currentTarget, event.detail === 0);
449
+ return;
450
+ }
334
451
  const popupMenuItemClickEvent = new PopupMenuItemClickEvent(item, event);
335
452
  item.click$?.next(popupMenuItemClickEvent);
336
453
  if (popupMenuItemClickEvent.isDefaultPrevented()) {
@@ -338,39 +455,108 @@ class PopupMenuListComponent {
338
455
  }
339
456
  this.#parentConfig.menuItemClick$.next(popupMenuItemClickEvent);
340
457
  }
458
+ onItemPointerEnter(event, item) {
459
+ clearTimeout(this.#hoverTimer);
460
+ const open = this.#openSubmenuItem();
461
+ if (open === item) {
462
+ this.activeMenuItem.set(item);
463
+ return;
464
+ }
465
+ const target = event.currentTarget;
466
+ const hover = () => {
467
+ this.closePopup();
468
+ if (item.items.length > 0 && !item.disabled) {
469
+ this.activeMenuItem.set(item);
470
+ this.createSubmenu(target, false, item);
471
+ }
472
+ else {
473
+ this.activeMenuItem.set(null);
474
+ }
475
+ };
476
+ if (open) {
477
+ // Wait before switching, so the pointer can cross other items on its way into the open submenu.
478
+ this.#hoverTimer = setTimeout(hover, SUBMENU_SWITCH_DELAY);
479
+ }
480
+ else {
481
+ hover();
482
+ }
483
+ }
341
484
  closePopup() {
342
- if (this.popupRef) {
343
- this.popupRef.close();
344
- this.popupRef = null;
485
+ const ref = this.popupRef;
486
+ this.popupRef = null;
487
+ this.#openSubmenuItem.set(null);
488
+ this.#submenuId.set(null);
489
+ const childClose$ = this.#childClose$;
490
+ this.#childClose$ = null;
491
+ childClose$?.next();
492
+ if (ref) {
493
+ this.reclaimFocus();
494
+ ref.close();
345
495
  }
346
- this.#close$.next();
347
496
  }
348
497
  closeSubmenu() {
349
498
  this.#parentConfig.childCloseRequest$?.next();
350
499
  }
351
- createSubmenu(target, viaKeyboard) {
352
- this.#viaKeyboardNavigation.set(viaKeyboard);
353
- const minWidth = this.#parentConfig.minWidth?.() ?? undefined;
354
- const width = this.#parentConfig.width?.() ?? undefined;
355
- this.popupRef = this.#popupService.create({
500
+ createSubmenu(target, viaKeyboard, item) {
501
+ // One submenu at a time: a submenu that is open (for this item or another) closes first.
502
+ this.closePopup();
503
+ const uid = item.uid;
504
+ const close$ = new Subject();
505
+ const menuId = createElementControlId(this.#parentConfig.level + 1);
506
+ const data = {
507
+ childCloseRequest$: this.#childCloseRequest$,
508
+ closeMenu: this.#parentConfig.closeMenu,
509
+ isRoot: false,
510
+ items: computed(() => groupPopupMenuItems(this.findItem(uid)?.items ?? [])),
511
+ labelledBy: uid,
512
+ level: this.#parentConfig.level + 1,
513
+ menuId,
514
+ menuItemClick$: this.#parentConfig.menuItemClick$,
515
+ minWidth: this.#parentConfig.minWidth,
516
+ navigate$: this.#parentConfig.navigate$,
517
+ parentClose$: close$,
518
+ popupGroupTemplate: this.#parentConfig.popupGroupTemplate,
519
+ popupIconTemplate: this.#parentConfig.popupIconTemplate,
520
+ popupShortcutTemplate: this.#parentConfig.popupShortcutTemplate,
521
+ popupTextTemplate: this.#parentConfig.popupTextTemplate,
522
+ rounded: this.#parentConfig.rounded,
523
+ size: this.#parentConfig.size,
524
+ submenuPointerEnter: () => this.onSubmenuPointerEnter(),
525
+ viaKeyboardNavigation: viaKeyboard,
526
+ width: this.#parentConfig.width
527
+ };
528
+ const ref = this.#popupService.create({
356
529
  anchor: target,
357
530
  anchorConnectionPoint: "topright",
358
531
  closeOnScroll: true,
359
532
  content: PopupMenuListComponent,
360
- data: this.#config(),
361
- minWidth,
533
+ data,
534
+ minWidth: this.#parentConfig.minWidth?.() ?? undefined,
362
535
  popupConnectionPoint: "topleft",
363
536
  restoreFocus: false,
364
- width
537
+ width: this.#parentConfig.width?.() ?? undefined
365
538
  });
366
- return this.popupRef.closed.pipe(take(1), tap(() => {
539
+ this.popupRef = ref;
540
+ this.#childClose$ = close$;
541
+ this.#openSubmenuItem.set(item);
542
+ this.#submenuId.set(menuId);
543
+ ref.closed.pipe(take(1)).subscribe(() => {
544
+ close$.complete();
545
+ // Closed by the popup itself (Escape, scroll); a submenu closed or replaced from here is done already.
546
+ if (this.popupRef !== ref) {
547
+ return;
548
+ }
367
549
  this.popupRef = null;
368
- this.#host.nativeElement.firstElementChild?.focus();
369
- this.#close$.next();
550
+ this.#childClose$ = null;
551
+ this.#openSubmenuItem.set(null);
552
+ this.#submenuId.set(null);
553
+ if (!this.#closing) {
554
+ this.#menuElement()?.focus();
555
+ }
370
556
  if (!viaKeyboard) {
371
557
  this.activeMenuItem.set(null);
372
558
  }
373
- }));
559
+ });
374
560
  }
375
561
  cycleThroughMatchedItems(buffer) {
376
562
  const matchedItems = this.#navigationItems()
@@ -378,7 +564,6 @@ class PopupMenuListComponent {
378
564
  .toArray();
379
565
  const activeMenuItem = this.activeMenuItem();
380
566
  if (matchedItems.length === 0) {
381
- this.activeMenuItem.set(null);
382
567
  return;
383
568
  }
384
569
  if (matchedItems.length === 1) {
@@ -391,6 +576,9 @@ class PopupMenuListComponent {
391
576
  this.activeMenuItem.set(matchedItems[nextIndex]);
392
577
  }
393
578
  }
579
+ findItem(uid) {
580
+ return this.#navigationItems().firstOrDefault(i => i.uid === uid) ?? null;
581
+ }
394
582
  focusFirstItem() {
395
583
  const firstItem = this.#navigationItems().firstOrDefault();
396
584
  if (firstItem) {
@@ -446,10 +634,13 @@ class PopupMenuListComponent {
446
634
  }
447
635
  handleEnterKey() {
448
636
  const activeItem = this.activeMenuItem();
637
+ if (activeItem?.disabled) {
638
+ return;
639
+ }
449
640
  if (activeItem && activeItem.items.length > 0) {
450
641
  const target = this.#host.nativeElement.querySelector('[data-active="true"]');
451
642
  if (target) {
452
- this.createSubmenu(target, true).subscribe();
643
+ this.createSubmenu(target, true, activeItem);
453
644
  }
454
645
  }
455
646
  else if (activeItem) {
@@ -521,14 +712,22 @@ class PopupMenuListComponent {
521
712
  level: this.#parentConfig.level
522
713
  });
523
714
  }
715
+ onSubmenuPointerEnter() {
716
+ // The pointer reached the open submenu: keep it, and its parent item highlighted.
717
+ clearTimeout(this.#hoverTimer);
718
+ const open = this.#openSubmenuItem();
719
+ if (open) {
720
+ this.activeMenuItem.set(open);
721
+ }
722
+ }
524
723
  openSubmenu() {
525
724
  const activeItem = this.activeMenuItem();
526
- if (!activeItem) {
725
+ if (!activeItem || activeItem.disabled) {
527
726
  return { item: null, hasChild: false };
528
727
  }
529
728
  const target = this.#host.nativeElement.querySelector('[data-active="true"]');
530
729
  if (target && activeItem.items.length > 0) {
531
- this.createSubmenu(target, true).subscribe();
730
+ this.createSubmenu(target, true, activeItem);
532
731
  return { item: activeItem, hasChild: true };
533
732
  }
534
733
  if (activeItem && activeItem.items.length > 0) {
@@ -536,11 +735,35 @@ class PopupMenuListComponent {
536
735
  }
537
736
  return { item: null, hasChild: false };
538
737
  }
738
+ openSubmenuFromClick(item, target, viaKeyboard) {
739
+ if (!target || (this.popupRef && this.#openSubmenuItem() === item)) {
740
+ return;
741
+ }
742
+ this.activeMenuItem.set(item);
743
+ this.createSubmenu(target, viaKeyboard, item);
744
+ }
745
+ reclaimFocus() {
746
+ // Focus inside the closing submenu (or deeper) comes back to this menu.
747
+ if (this.#closing) {
748
+ return;
749
+ }
750
+ const active = this.#document.activeElement;
751
+ if (active && !this.#host.nativeElement.contains(active) && active.closest("mona-popup-menu-list")) {
752
+ this.#menuElement()?.focus();
753
+ }
754
+ }
539
755
  setKeyboardEvents() {
540
- const element = this.#host.nativeElement.firstElementChild;
756
+ const element = this.#menuElement();
757
+ if (!element) {
758
+ return;
759
+ }
541
760
  fromEvent(element, "keydown")
542
761
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(e => {
543
- if (isNavigationKey(e.key)) {
762
+ if (e.key === "Tab") {
763
+ // Tab closes every menu and moves on from the trigger, as the APG menu button asks.
764
+ this.#parentConfig.closeMenu({ tab: true });
765
+ }
766
+ else if (isNavigationKey(e.key)) {
544
767
  e.preventDefault();
545
768
  this.#typeaheadKey$.next("");
546
769
  this.handleNavigationKey(e);
@@ -552,25 +775,12 @@ class PopupMenuListComponent {
552
775
  }))
553
776
  .subscribe();
554
777
  }
555
- setPointerSubscriptions() {
556
- this.pointerEnter$
557
- .pipe(takeUntilDestroyed(this.#destroyRef), takeUntil(this.#parentConfig.parentClose$), filter(e => e.item !== this.activeMenuItem()), tap(e => {
558
- this.closePopup();
559
- if (e.item.items.length === 0) {
560
- this.activeMenuItem.set(null);
561
- }
562
- }), filter(e => e.item.items.length > 0), switchMap(e => {
563
- this.activeMenuItem.set(e.item);
564
- return this.createSubmenu(e.event.currentTarget, false);
565
- }))
566
- .subscribe();
567
- this.pointerLeave$
568
- .pipe(takeUntilDestroyed(this.#destroyRef), takeUntil(this.pointerEnter$))
569
- .subscribe(() => this.popupRef?.close());
570
- }
571
778
  setPopupCloseSubscription() {
572
779
  this.#parentConfig.parentClose$
573
- .pipe(takeUntilDestroyed(this.#destroyRef), tap(() => this.closePopup()))
780
+ .pipe(takeUntilDestroyed(this.#destroyRef), tap(() => {
781
+ this.#closing = true;
782
+ this.closePopup();
783
+ }))
574
784
  .subscribe();
575
785
  this.#childCloseRequest$
576
786
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(() => {
@@ -583,7 +793,6 @@ class PopupMenuListComponent {
583
793
  }
584
794
  setSubscriptions() {
585
795
  this.setKeyboardEvents();
586
- this.setPointerSubscriptions();
587
796
  this.setPopupCloseSubscription();
588
797
  this.setTypeaheadSubscription();
589
798
  }
@@ -593,55 +802,41 @@ class PopupMenuListComponent {
593
802
  .subscribe(buffer => this.cycleThroughMatchedItems(buffer));
594
803
  }
595
804
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuListComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
596
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.6", type: PopupMenuListComponent, isStandalone: true, selector: "mona-popup-menu-list", host: { properties: { "class": "baseClasses()" } }, ngImport: i0, template: "<div\n role=\"menu\"\n [attr.tabindex]=\"0\"\n [attr.aria-orientation]=\"'vertical'\"\n [attr.aria-activedescendant]=\"activeMenuItem()?.uid\"\n [attr.id]=\"menuId()\"\n [class]=\"containerClasses()\">\n @for (group of menuItems(); track $index) {\n @if (typeof group.key === \"string\") {\n @let groupTemplate = group.first().groupTemplate?.() ?? popupGroupTemplate() ?? null;\n <div role=\"group\" [attr.aria-labelledby]=\"`${group.key}-${$index}`\" [class]=\"groupHeaderClasses()\">\n <div [id]=\"`${group.key}-${$index}`\" role=\"presentation\">\n @if (groupTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"groupTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.key }\"></ng-container>\n } @else {\n <span>{{ group.key }}</span>\n }\n </div>\n </div>\n }\n @for (item of group.source; track $index) {\n @if (item.separator) {\n <div role=\"separator\" class=\"bg-border my-1 h-px\"></div>\n } @else {\n @let iconTemplate = item.iconTemplate?.() ?? popupIconTemplate() ?? null;\n @let shortcutTemplate = item.shortcutTemplate?.() ?? popupShortcutTemplate() ?? null;\n @let textTemplate = item.textTemplate?.() ?? popupTextTemplate() ?? null;\n <div\n role=\"menuitem\"\n [id]=\"item.uid\"\n (pointerenter)=\"pointerEnter$.next({ event: $event, item: item })\"\n (pointerleave)=\"pointerLeave$.next({ event: $event, item: item })\"\n (click)=\"onItemClick($event, item)\"\n [attr.data-active]=\"item === activeMenuItem()\"\n [attr.aria-disabled]=\"item.disabled\"\n [attr.aria-expanded]=\"item.items.length > 0 && item === activeMenuItem() && !!popupRef\"\n [attr.aria-haspopup]=\"item.items.length > 0 ? 'menu' : null\"\n [attr.aria-label]=\"item.label || null\"\n [attr.data-disabled]=\"item.disabled\"\n [class]=\"menuItemClasses()\">\n @if (iconAreaVisible()) {\n <div [class]=\"iconContainerClasses()\">\n @if (!item.checkable && !item.radio) {\n <ng-container\n [ngTemplateOutlet]=\"iconTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else if (item.checkable) {\n @if (item.checked?.()) {\n <svg lucideCheck [size]=\"14\"></svg>\n }\n } @else if (item.radio) {\n @if (item.selected?.()) {\n <svg\n xmlns=\"http://www.w3.org/2000/svg\"\n width=\"24\"\n height=\"24\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n class=\"lucide lucide-circle size-2 fill-current\">\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\n </svg>\n }\n }\n </div>\n }\n <div class=\"flex-1\">\n @if (textTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"textTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else {\n {{ item.label }}\n }\n </div>\n @if (item.items.length > 0) {\n @if (!item.checkable) {\n <div [class]=\"linkContainerClasses()\">\n <svg lucideChevronRight [size]=\"16\" class=\"rtl:rotate-180\"></svg>\n </div>\n }\n } @else if (shortcutTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"shortcutTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n }\n </div>\n }\n }\n }\n</div>\n", dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: LucideChevronRight, selector: "svg[lucideChevronRight]" }, { kind: "component", type: LucideCheck, selector: "svg[lucideCheck]" }] }); }
805
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.6", type: PopupMenuListComponent, isStandalone: true, selector: "mona-popup-menu-list", host: { listeners: { "focusout": "onFocusOut($event)", "pointerenter": "onHostPointerEnter()" }, properties: { "class": "baseClasses()" } }, ngImport: i0, template: "<div\n role=\"menu\"\n [attr.tabindex]=\"0\"\n [attr.aria-orientation]=\"'vertical'\"\n [attr.aria-activedescendant]=\"activeMenuItem()?.uid\"\n [attr.aria-label]=\"ariaLabel\"\n [attr.aria-labelledby]=\"labelledBy\"\n [attr.id]=\"menuId()\"\n [class]=\"containerClasses()\">\n @for (group of menuItems(); track $index) {\n @if (group.title) {\n @let groupTemplate = group.items[0]?.groupTemplate?.() ?? popupGroupTemplate() ?? null;\n <div role=\"group\" [attr.aria-labelledby]=\"group.labelId\">\n <div [id]=\"group.labelId\" role=\"presentation\" [class]=\"groupHeaderClasses()\">\n @if (groupTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"groupTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.title }\"></ng-container>\n } @else {\n <span>{{ group.title }}</span>\n }\n </div>\n <ng-container\n [ngTemplateOutlet]=\"itemsTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.items }\"></ng-container>\n </div>\n } @else {\n <ng-container\n [ngTemplateOutlet]=\"itemsTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.items }\"></ng-container>\n }\n }\n</div>\n\n<ng-template #itemsTemplate let-items>\n @for (item of asItems(items); track $index) {\n @if (item.separator) {\n <div role=\"separator\" class=\"bg-border my-1 h-px\"></div>\n } @else {\n @let iconTemplate = item.iconTemplate?.() ?? popupIconTemplate() ?? null;\n @let shortcutTemplate = item.shortcutTemplate?.() ?? popupShortcutTemplate() ?? null;\n @let textTemplate = item.textTemplate?.() ?? popupTextTemplate() ?? null;\n <div\n [attr.role]=\"item.checkable ? 'menuitemcheckbox' : item.radio ? 'menuitemradio' : 'menuitem'\"\n [attr.aria-checked]=\"item.checkable ? !!item.checked?.() : item.radio ? !!item.selected?.() : null\"\n [id]=\"item.uid\"\n (pointerenter)=\"onItemPointerEnter($event, item)\"\n (click)=\"onItemClick($event, item)\"\n [attr.data-active]=\"item === activeMenuItem()\"\n [attr.aria-disabled]=\"item.disabled\"\n [attr.aria-expanded]=\"item.items.length > 0 ? item === openSubmenuItem() : null\"\n [attr.aria-controls]=\"item === openSubmenuItem() ? submenuId() : null\"\n [attr.aria-haspopup]=\"item.items.length > 0 ? 'menu' : null\"\n [attr.aria-label]=\"textTemplate ? null : item.label || null\"\n [attr.data-disabled]=\"item.disabled\"\n [class]=\"menuItemClasses()\">\n @if (iconAreaVisible()) {\n <div [class]=\"iconContainerClasses()\">\n @if (!item.checkable && !item.radio) {\n <ng-container\n [ngTemplateOutlet]=\"iconTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else if (item.checkable) {\n @if (item.checked?.()) {\n <svg lucideCheck [size]=\"14\"></svg>\n }\n } @else if (item.radio) {\n @if (item.selected?.()) {\n <svg\n xmlns=\"http://www.w3.org/2000/svg\"\n width=\"24\"\n height=\"24\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n class=\"lucide lucide-circle size-2 fill-current\">\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\n </svg>\n }\n }\n </div>\n }\n <div class=\"flex-1\">\n @if (textTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"textTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else {\n {{ item.label }}\n }\n </div>\n @if (item.items.length > 0) {\n @if (!item.checkable) {\n <div [class]=\"linkContainerClasses()\">\n <svg lucideChevronRight [size]=\"16\" class=\"rtl:rotate-180\"></svg>\n </div>\n }\n } @else if (shortcutTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"shortcutTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n }\n </div>\n }\n }\n</ng-template>\n", dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: LucideChevronRight, selector: "svg[lucideChevronRight]" }, { kind: "component", type: LucideCheck, selector: "svg[lucideCheck]" }] }); }
597
806
  }
598
807
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuListComponent, decorators: [{
599
808
  type: Component,
600
809
  args: [{ selector: "mona-popup-menu-list", imports: [NgTemplateOutlet, LucideChevronRight, LucideCheck], host: {
601
- "[class]": "baseClasses()"
602
- }, template: "<div\n role=\"menu\"\n [attr.tabindex]=\"0\"\n [attr.aria-orientation]=\"'vertical'\"\n [attr.aria-activedescendant]=\"activeMenuItem()?.uid\"\n [attr.id]=\"menuId()\"\n [class]=\"containerClasses()\">\n @for (group of menuItems(); track $index) {\n @if (typeof group.key === \"string\") {\n @let groupTemplate = group.first().groupTemplate?.() ?? popupGroupTemplate() ?? null;\n <div role=\"group\" [attr.aria-labelledby]=\"`${group.key}-${$index}`\" [class]=\"groupHeaderClasses()\">\n <div [id]=\"`${group.key}-${$index}`\" role=\"presentation\">\n @if (groupTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"groupTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.key }\"></ng-container>\n } @else {\n <span>{{ group.key }}</span>\n }\n </div>\n </div>\n }\n @for (item of group.source; track $index) {\n @if (item.separator) {\n <div role=\"separator\" class=\"bg-border my-1 h-px\"></div>\n } @else {\n @let iconTemplate = item.iconTemplate?.() ?? popupIconTemplate() ?? null;\n @let shortcutTemplate = item.shortcutTemplate?.() ?? popupShortcutTemplate() ?? null;\n @let textTemplate = item.textTemplate?.() ?? popupTextTemplate() ?? null;\n <div\n role=\"menuitem\"\n [id]=\"item.uid\"\n (pointerenter)=\"pointerEnter$.next({ event: $event, item: item })\"\n (pointerleave)=\"pointerLeave$.next({ event: $event, item: item })\"\n (click)=\"onItemClick($event, item)\"\n [attr.data-active]=\"item === activeMenuItem()\"\n [attr.aria-disabled]=\"item.disabled\"\n [attr.aria-expanded]=\"item.items.length > 0 && item === activeMenuItem() && !!popupRef\"\n [attr.aria-haspopup]=\"item.items.length > 0 ? 'menu' : null\"\n [attr.aria-label]=\"item.label || null\"\n [attr.data-disabled]=\"item.disabled\"\n [class]=\"menuItemClasses()\">\n @if (iconAreaVisible()) {\n <div [class]=\"iconContainerClasses()\">\n @if (!item.checkable && !item.radio) {\n <ng-container\n [ngTemplateOutlet]=\"iconTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else if (item.checkable) {\n @if (item.checked?.()) {\n <svg lucideCheck [size]=\"14\"></svg>\n }\n } @else if (item.radio) {\n @if (item.selected?.()) {\n <svg\n xmlns=\"http://www.w3.org/2000/svg\"\n width=\"24\"\n height=\"24\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n class=\"lucide lucide-circle size-2 fill-current\">\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\n </svg>\n }\n }\n </div>\n }\n <div class=\"flex-1\">\n @if (textTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"textTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else {\n {{ item.label }}\n }\n </div>\n @if (item.items.length > 0) {\n @if (!item.checkable) {\n <div [class]=\"linkContainerClasses()\">\n <svg lucideChevronRight [size]=\"16\" class=\"rtl:rotate-180\"></svg>\n </div>\n }\n } @else if (shortcutTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"shortcutTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n }\n </div>\n }\n }\n }\n</div>\n" }]
810
+ "[class]": "baseClasses()",
811
+ "(focusout)": "onFocusOut($event)",
812
+ "(pointerenter)": "onHostPointerEnter()"
813
+ }, template: "<div\n role=\"menu\"\n [attr.tabindex]=\"0\"\n [attr.aria-orientation]=\"'vertical'\"\n [attr.aria-activedescendant]=\"activeMenuItem()?.uid\"\n [attr.aria-label]=\"ariaLabel\"\n [attr.aria-labelledby]=\"labelledBy\"\n [attr.id]=\"menuId()\"\n [class]=\"containerClasses()\">\n @for (group of menuItems(); track $index) {\n @if (group.title) {\n @let groupTemplate = group.items[0]?.groupTemplate?.() ?? popupGroupTemplate() ?? null;\n <div role=\"group\" [attr.aria-labelledby]=\"group.labelId\">\n <div [id]=\"group.labelId\" role=\"presentation\" [class]=\"groupHeaderClasses()\">\n @if (groupTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"groupTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.title }\"></ng-container>\n } @else {\n <span>{{ group.title }}</span>\n }\n </div>\n <ng-container\n [ngTemplateOutlet]=\"itemsTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.items }\"></ng-container>\n </div>\n } @else {\n <ng-container\n [ngTemplateOutlet]=\"itemsTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: group.items }\"></ng-container>\n }\n }\n</div>\n\n<ng-template #itemsTemplate let-items>\n @for (item of asItems(items); track $index) {\n @if (item.separator) {\n <div role=\"separator\" class=\"bg-border my-1 h-px\"></div>\n } @else {\n @let iconTemplate = item.iconTemplate?.() ?? popupIconTemplate() ?? null;\n @let shortcutTemplate = item.shortcutTemplate?.() ?? popupShortcutTemplate() ?? null;\n @let textTemplate = item.textTemplate?.() ?? popupTextTemplate() ?? null;\n <div\n [attr.role]=\"item.checkable ? 'menuitemcheckbox' : item.radio ? 'menuitemradio' : 'menuitem'\"\n [attr.aria-checked]=\"item.checkable ? !!item.checked?.() : item.radio ? !!item.selected?.() : null\"\n [id]=\"item.uid\"\n (pointerenter)=\"onItemPointerEnter($event, item)\"\n (click)=\"onItemClick($event, item)\"\n [attr.data-active]=\"item === activeMenuItem()\"\n [attr.aria-disabled]=\"item.disabled\"\n [attr.aria-expanded]=\"item.items.length > 0 ? item === openSubmenuItem() : null\"\n [attr.aria-controls]=\"item === openSubmenuItem() ? submenuId() : null\"\n [attr.aria-haspopup]=\"item.items.length > 0 ? 'menu' : null\"\n [attr.aria-label]=\"textTemplate ? null : item.label || null\"\n [attr.data-disabled]=\"item.disabled\"\n [class]=\"menuItemClasses()\">\n @if (iconAreaVisible()) {\n <div [class]=\"iconContainerClasses()\">\n @if (!item.checkable && !item.radio) {\n <ng-container\n [ngTemplateOutlet]=\"iconTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else if (item.checkable) {\n @if (item.checked?.()) {\n <svg lucideCheck [size]=\"14\"></svg>\n }\n } @else if (item.radio) {\n @if (item.selected?.()) {\n <svg\n xmlns=\"http://www.w3.org/2000/svg\"\n width=\"24\"\n height=\"24\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n stroke-width=\"2\"\n stroke-linecap=\"round\"\n stroke-linejoin=\"round\"\n class=\"lucide lucide-circle size-2 fill-current\">\n <circle cx=\"12\" cy=\"12\" r=\"10\"></circle>\n </svg>\n }\n }\n </div>\n }\n <div class=\"flex-1\">\n @if (textTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"textTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n } @else {\n {{ item.label }}\n }\n </div>\n @if (item.items.length > 0) {\n @if (!item.checkable) {\n <div [class]=\"linkContainerClasses()\">\n <svg lucideChevronRight [size]=\"16\" class=\"rtl:rotate-180\"></svg>\n </div>\n }\n } @else if (shortcutTemplate) {\n <ng-container\n [ngTemplateOutlet]=\"shortcutTemplate\"\n [ngTemplateOutletContext]=\"{ $implicit: item }\"></ng-container>\n }\n </div>\n }\n }\n</ng-template>\n" }]
603
814
  }], ctorParameters: () => [] });
604
815
 
816
+ /** Context menu events that a menu opened for, so menus on ancestor targets leave them alone. */
817
+ const handledContextMenuEvents = new WeakSet();
605
818
  class PopupMenuComponent {
606
- #close$;
607
819
  #destroyRef;
820
+ #document;
608
821
  #menuItemClick$;
609
822
  #navigate$;
610
- #popupMenuListConfig;
611
823
  #popupService;
612
- #triggerSubscription;
613
- #shouldRestoreFocus;
824
+ #destroyed;
825
+ #open;
826
+ #pendingInitialFocus;
614
827
  #suppressNextTrustedContextMenu;
828
+ #triggerSubscription;
615
829
  constructor() {
616
- this.#close$ = new Subject();
617
830
  this.#destroyRef = inject(DestroyRef);
831
+ this.#document = inject(DOCUMENT);
618
832
  this.#menuItemClick$ = new Subject();
619
833
  this.#navigate$ = new Subject();
620
- this.#popupMenuListConfig = computed(() => {
621
- const data = {
622
- isRoot: true,
623
- items: this.menuItems(),
624
- level: 0,
625
- menuId: this.menuId(),
626
- menuItemClick$: this.#menuItemClick$,
627
- minWidth: computed(() => toCssValue(this.minWidth()) ?? null),
628
- navigate$: this.#navigate$,
629
- parentClose$: this.#close$,
630
- popupGroupTemplate: computed(() => this.groupTemplateConfig()?.template ?? null),
631
- popupIconTemplate: computed(() => this.iconTemplateConfig()?.template ?? null),
632
- popupShortcutTemplate: computed(() => this.shortcutTemplateConfig()?.template ?? null),
633
- popupTextTemplate: computed(() => this.textTemplateConfig()?.template ?? null),
634
- rounded: computed(() => this.rounded()),
635
- size: computed(() => this.size()),
636
- width: computed(() => toCssValue(this.width()) ?? null)
637
- };
638
- return data;
639
- }, /* @ts-ignore */
640
- ...(ngDevMode ? [{ debugName: "#popupMenuListConfig" }] : /* istanbul ignore next */ []));
641
834
  this.#popupService = inject(PopupService);
642
- this.#triggerSubscription = null;
643
- this.#shouldRestoreFocus = false;
835
+ this.#destroyed = false;
836
+ this.#open = null;
837
+ this.#pendingInitialFocus = null;
644
838
  this.#suppressNextTrustedContextMenu = false;
839
+ this.#triggerSubscription = null;
645
840
  this.groupTemplateConfig = contentChild(PopupMenuGroupTemplateToken, { ...(ngDevMode ? { debugName: "groupTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
646
841
  this.iconTemplateConfig = contentChild(PopupMenuIconTemplateToken, { ...(ngDevMode ? { debugName: "iconTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
647
842
  this.nestedComponents = contentChildren(PopupMenuToken, { ...(ngDevMode ? { debugName: "nestedComponents" } : /* istanbul ignore next */ {}), descendants: false });
@@ -651,13 +846,12 @@ class PopupMenuComponent {
651
846
  const inputItems = this.items();
652
847
  if (any(inputItems)) {
653
848
  const items = select(inputItems, i => Object.hasOwn(i, "uid") ? i : preparePopupMenuItems([i])[0]);
654
- return groupBy(items, i => i.group).toImmutableSet();
849
+ return groupPopupMenuItems(items);
655
850
  }
656
851
  const items = this.nestedComponents().flatMap(i => i.getPopupMenuItem());
657
- return groupBy(items, i => i.group).toImmutableSet();
852
+ return groupPopupMenuItems(items);
658
853
  }, /* @ts-ignore */
659
854
  ...(ngDevMode ? [{ debugName: "menuItems" }] : /* istanbul ignore next */ []));
660
- this.popupRef = null;
661
855
  /**
662
856
  * @description The anchor element that the context menu will be anchored to.
663
857
  * Only applicable when `precise` is set to `false`.
@@ -670,9 +864,21 @@ class PopupMenuComponent {
670
864
  */
671
865
  this.anchorConnectionPoint = input(/* @ts-ignore */
672
866
  ...(ngDevMode ? [undefined, { debugName: "anchorConnectionPoint" }] : /* istanbul ignore next */ []));
867
+ /**
868
+ * @description The accessible name of the root menu. Takes precedence over `ariaLabelledBy`.
869
+ */
870
+ this.ariaLabel = input("", /* @ts-ignore */
871
+ ...(ngDevMode ? [{ debugName: "ariaLabel" }] : /* istanbul ignore next */ []));
872
+ /**
873
+ * @description The ids of the elements that name the root menu. When neither this nor `ariaLabel` is set, the
874
+ * menu is named by the target element if it has an id.
875
+ */
876
+ this.ariaLabelledBy = input("", /* @ts-ignore */
877
+ ...(ngDevMode ? [{ debugName: "ariaLabelledBy" }] : /* istanbul ignore next */ []));
673
878
  /**
674
879
  * @description The event that is emitted when the popup menu is closed.
675
880
  */
881
+ // eslint-disable-next-line @angular-eslint/no-output-native -- renaming the output would break existing (close) bindings
676
882
  this.close = output();
677
883
  /**
678
884
  * @description The items to render in the popup menu.
@@ -743,44 +949,53 @@ class PopupMenuComponent {
743
949
  */
744
950
  this.trigger = input("click", /* @ts-ignore */
745
951
  ...(ngDevMode ? [{ debugName: "trigger" }] : /* istanbul ignore next */ []));
952
+ this.userClass = input(undefined, { ...(ngDevMode ? { debugName: "userClass" } : /* istanbul ignore next */ {}), alias: "class",
953
+ transform: value => classInputToClass(value) });
746
954
  /**
747
955
  * @description Sets the width of the popup menu.
748
956
  * This will be applied to the root menu and all submenus.
749
957
  */
750
958
  this.width = input(/* @ts-ignore */
751
959
  ...(ngDevMode ? [undefined, { debugName: "width" }] : /* istanbul ignore next */ []));
752
- this.userClass = input(undefined, { ...(ngDevMode ? { debugName: "userClass" } : /* istanbul ignore next */ {}), alias: "class",
753
- transform: value => classInputToClass(value) });
754
- afterNextRender({
755
- read: () => this.setSubscriptions()
960
+ this.setMenuItemClickSubscription();
961
+ this.setNavigationSubscription();
962
+ // Listen on the current target for the current trigger, and move the listener when either changes.
963
+ effect(() => {
964
+ const target = this.#element(this.target());
965
+ const trigger = this.trigger();
966
+ untracked(() => {
967
+ this.#triggerSubscription?.unsubscribe();
968
+ this.#triggerSubscription = target ? this.setTriggerSubscription(target, trigger) : null;
969
+ });
756
970
  });
757
- afterRenderEffect({
758
- read: () => {
759
- rxTimeout(this.#destroyRef, () => {
760
- this.anchor();
761
- this.target();
762
- this.trigger();
763
- if (this.#triggerSubscription) {
764
- this.#triggerSubscription.unsubscribe();
765
- this.#triggerSubscription = null;
766
- }
767
- this.setTriggerSubscription();
768
- });
971
+ this.#destroyRef.onDestroy(() => {
972
+ this.#destroyed = true;
973
+ this.#triggerSubscription?.unsubscribe();
974
+ const open = this.#open;
975
+ if (open) {
976
+ open.restoreFocus = false;
977
+ open.ref?.close();
769
978
  }
770
979
  });
771
980
  }
981
+ /**
982
+ * @description Keeps focus where it is when the open menu closes, instead of returning it to the element that
983
+ * had it before the menu opened. Used when another menu takes over, as in a menubar.
984
+ */
772
985
  clearFocusRestoration() {
773
- this.#shouldRestoreFocus = false;
986
+ if (this.#open) {
987
+ this.#open.restoreFocus = false;
988
+ }
774
989
  }
990
+ /**
991
+ * @description Closes the menu. Focus returns to the element that had it before the menu opened, unless focus
992
+ * has already moved elsewhere.
993
+ */
775
994
  closeMenu() {
776
- if (this.popupRef) {
777
- this.popupRef.close();
778
- this.popupRef = null;
779
- }
995
+ this.#open?.ref?.close();
780
996
  }
781
997
  openMenu(viaContextMenuKey) {
782
- const target = this.target();
783
- const targetElement = target instanceof ElementRef ? target.nativeElement : target;
998
+ const targetElement = this.#element(this.target());
784
999
  if (!targetElement) {
785
1000
  return;
786
1001
  }
@@ -815,38 +1030,87 @@ class PopupMenuComponent {
815
1030
  const event = new MouseEvent(this.trigger(), { bubbles: true, cancelable: true });
816
1031
  targetElement.dispatchEvent(event);
817
1032
  }
818
- openMenuViaKeyboard(viaContextMenuKey = false) {
819
- this.#shouldRestoreFocus = true;
1033
+ /**
1034
+ * @description Opens the menu as the keyboard does, with its first (or last) item highlighted.
1035
+ * @param viaContextMenuKey Whether the menu opens from the ContextMenu key or Shift+F10.
1036
+ * @param focus The item to highlight: "first" (ArrowDown, Enter, Space) or "last" (ArrowUp).
1037
+ */
1038
+ openMenuViaKeyboard(viaContextMenuKey = false, focus = "first") {
1039
+ this.#pendingInitialFocus = focus;
820
1040
  this.openMenu(viaContextMenuKey);
1041
+ this.#pendingInitialFocus = null;
821
1042
  }
1043
+ /**
1044
+ * @deprecated Focus now returns to the element that had it before the menu opened after every close.
1045
+ */
822
1046
  setRestoreFocusForKeyboard() {
823
- this.#shouldRestoreFocus = true;
1047
+ if (this.#open) {
1048
+ this.#open.restoreFocus = true;
1049
+ }
1050
+ }
1051
+ #element(value) {
1052
+ const element = value instanceof ElementRef ? value.nativeElement : value;
1053
+ return element instanceof HTMLElement ? element : null;
1054
+ }
1055
+ closeFromList(state, options) {
1056
+ if (this.#open !== state || !state.ref) {
1057
+ return;
1058
+ }
1059
+ if (options.focusLeftTo) {
1060
+ // Focus moved out of the menus. A move to the trigger is the trigger's own business (it toggles).
1061
+ const target = this.#element(this.target());
1062
+ const anchor = this.#element(this.anchor());
1063
+ if (target?.contains(options.focusLeftTo) || anchor?.contains(options.focusLeftTo)) {
1064
+ return;
1065
+ }
1066
+ state.restoreFocus = false;
1067
+ }
1068
+ if (options.tab) {
1069
+ // Tab leaves the menu as it would leave the trigger: focus the trigger now, before the browser moves
1070
+ // focus on from it.
1071
+ state.restoreFocus = false;
1072
+ state.returnFocusTo?.focus();
1073
+ }
1074
+ state.ref.close();
824
1075
  }
825
- createMenu(event) {
1076
+ createMenu(event, state) {
826
1077
  const anchor = this.getPopupAnchor(event);
827
1078
  const anchorConnectionPoint = this.precise() ? undefined : this.anchorConnectionPoint();
828
- const closeOnScroll = true;
829
- const content = PopupMenuListComponent;
830
- const data = this.#popupMenuListConfig();
831
- const minWidth = toCssValue(this.minWidth());
832
- const offset = this.offset();
833
- const popupConnectionPoint = this.precise() ? undefined : this.popupConnectionPoint();
834
- const width = toCssValue(this.width());
835
- const popupRef = this.#popupService.create({
1079
+ const targetId = this.#element(this.target())?.id ?? "";
1080
+ const data = {
1081
+ ariaLabel: this.ariaLabel(),
1082
+ closeMenu: options => this.closeFromList(state, options),
1083
+ initialFocus: this.#pendingInitialFocus,
1084
+ isRoot: true,
1085
+ items: this.menuItems,
1086
+ labelledBy: this.ariaLabelledBy() || targetId || null,
1087
+ level: 0,
1088
+ menuId: this.menuId(),
1089
+ menuItemClick$: this.#menuItemClick$,
1090
+ minWidth: computed(() => toCssValue(this.minWidth()) ?? null),
1091
+ navigate$: this.#navigate$,
1092
+ parentClose$: state.close$,
1093
+ popupGroupTemplate: computed(() => this.groupTemplateConfig()?.template ?? null),
1094
+ popupIconTemplate: computed(() => this.iconTemplateConfig()?.template ?? null),
1095
+ popupShortcutTemplate: computed(() => this.shortcutTemplateConfig()?.template ?? null),
1096
+ popupTextTemplate: computed(() => this.textTemplateConfig()?.template ?? null),
1097
+ rounded: computed(() => this.rounded()),
1098
+ size: computed(() => this.size()),
1099
+ width: computed(() => toCssValue(this.width()) ?? null)
1100
+ };
1101
+ return this.#popupService.create({
836
1102
  anchor,
837
1103
  anchorConnectionPoint,
838
- closeOnScroll,
839
- content,
1104
+ closeOnScroll: true,
1105
+ content: PopupMenuListComponent,
840
1106
  data,
841
- minWidth,
842
- offset,
1107
+ minWidth: toCssValue(this.minWidth()),
1108
+ offset: this.offset(),
843
1109
  popupClass: this.userClass(),
844
- popupConnectionPoint,
1110
+ popupConnectionPoint: this.precise() ? undefined : this.popupConnectionPoint(),
845
1111
  restoreFocus: false,
846
- width
1112
+ width: toCssValue(this.width())
847
1113
  });
848
- popupRef.closeStart.pipe(take(1)).subscribe(() => this.#close$.next());
849
- return popupRef;
850
1114
  }
851
1115
  getPopupAnchor(event) {
852
1116
  const anchorElement = this.anchor();
@@ -869,6 +1133,69 @@ class PopupMenuComponent {
869
1133
  }
870
1134
  return anchor;
871
1135
  }
1136
+ openFor(event, targetElement) {
1137
+ const opener = this.#document.activeElement;
1138
+ const hasOpener = !!opener && opener !== this.#document.body;
1139
+ // Focus in another menu (a menubar moving to the next menu) leaves with that menu, so it can't come back there.
1140
+ const openerInMenu = hasOpener && !!opener.closest("mona-popup-menu-list");
1141
+ const targetFocusable = targetElement.tabIndex >= 0 || (openerInMenu && targetElement.hasAttribute("tabindex"));
1142
+ // A menu opened from the keyboard goes back to the element the keys came from (a split button's main button).
1143
+ const viaKeyboard = this.#pendingInitialFocus !== null;
1144
+ let returnFocusTo = null;
1145
+ if (hasOpener && !openerInMenu && (viaKeyboard || targetElement.contains(opener))) {
1146
+ returnFocusTo = opener;
1147
+ }
1148
+ else if (targetFocusable) {
1149
+ returnFocusTo = targetElement;
1150
+ }
1151
+ else if (hasOpener && !openerInMenu) {
1152
+ returnFocusTo = opener;
1153
+ }
1154
+ const state = { close$: new Subject(), ref: null, restoreFocus: true, returnFocusTo };
1155
+ const ref = this.createMenu(event, state);
1156
+ state.ref = ref;
1157
+ this.#open = state;
1158
+ this.open.emit();
1159
+ ref.beforeClose.pipe(takeUntil(ref.closed)).subscribe(e => {
1160
+ // A menu closed because its component is destroyed has no one left to tell.
1161
+ if (this.#destroyed) {
1162
+ return;
1163
+ }
1164
+ const popupCloseEvent = e.result;
1165
+ const closeEvent = new PopupMenuCloseEvent(popupCloseEvent);
1166
+ this.close.emit(closeEvent);
1167
+ if (closeEvent.isDefaultPrevented()) {
1168
+ e.preventDefault();
1169
+ }
1170
+ });
1171
+ ref.closeStart.pipe(take(1)).subscribe(() => {
1172
+ state.close$.next();
1173
+ // A menu that is closing is closed for the trigger: the next trigger opens a new one.
1174
+ if (this.#open === state) {
1175
+ this.#open = null;
1176
+ }
1177
+ });
1178
+ ref.closed.pipe(take(1)).subscribe(() => {
1179
+ state.close$.complete();
1180
+ if (this.#open === state) {
1181
+ this.#open = null;
1182
+ }
1183
+ // Return focus unless another menu opened meanwhile, or focus has moved on.
1184
+ if (state.restoreFocus && this.#open === null) {
1185
+ this.restoreFocus(state.returnFocusTo);
1186
+ }
1187
+ });
1188
+ }
1189
+ restoreFocus(element) {
1190
+ if (!element || !element.isConnected) {
1191
+ return;
1192
+ }
1193
+ const active = this.#document.activeElement;
1194
+ if (active && active !== this.#document.body && !active.closest("mona-popup-menu-list")) {
1195
+ return;
1196
+ }
1197
+ element.focus();
1198
+ }
872
1199
  setMenuItemClickSubscription() {
873
1200
  this.#menuItemClick$
874
1201
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(event => {
@@ -876,7 +1203,11 @@ class PopupMenuComponent {
876
1203
  if (event.isDefaultPrevented()) {
877
1204
  return;
878
1205
  }
879
- this.popupRef?.close();
1206
+ const item = event.item;
1207
+ if (isMenuOwnedCheckState(item)) {
1208
+ item.checked.set(event.checked === true);
1209
+ }
1210
+ this.closeMenu();
880
1211
  }))
881
1212
  .subscribe();
882
1213
  }
@@ -885,54 +1216,38 @@ class PopupMenuComponent {
885
1216
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(e => this.navigate.emit(e)))
886
1217
  .subscribe();
887
1218
  }
888
- setSubscriptions() {
889
- this.setMenuItemClickSubscription();
890
- this.setNavigationSubscription();
891
- this.setTriggerSubscription();
892
- }
893
- setTriggerSubscription() {
894
- const element = this.target();
895
- const trigger = this.trigger();
896
- const targetElement = element instanceof ElementRef ? element.nativeElement : element;
897
- this.#triggerSubscription = fromEvent(targetElement, trigger)
1219
+ setTriggerSubscription(targetElement, trigger) {
1220
+ return fromEvent(targetElement, trigger)
898
1221
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(e => e.preventDefault()))
899
1222
  .subscribe(e => {
900
1223
  if (e.isTrusted && this.#suppressNextTrustedContextMenu) {
901
1224
  this.#suppressNextTrustedContextMenu = false;
902
1225
  return;
903
1226
  }
904
- if (this.popupRef) {
905
- this.popupRef.close();
906
- this.popupRef = null;
1227
+ // A context menu on a nested target already opened for this event.
1228
+ if (trigger === "contextmenu" && handledContextMenuEvents.has(e)) {
907
1229
  return;
908
1230
  }
909
- this.popupRef = this.createMenu(e);
910
- this.open.emit();
911
- this.popupRef.beforeClose.pipe(takeUntil(this.popupRef.closed)).subscribe(e => {
912
- const popupCloseEvent = e.result;
913
- const closeEvent = new PopupMenuCloseEvent(popupCloseEvent);
914
- this.close.emit(closeEvent);
915
- if (closeEvent.isDefaultPrevented()) {
916
- e.preventDefault();
1231
+ const open = this.#open;
1232
+ if (open) {
1233
+ if (trigger !== "contextmenu" && !this.precise()) {
1234
+ // The trigger toggles the menu.
1235
+ open.ref?.close();
917
1236
  return;
918
1237
  }
919
- });
920
- this.popupRef.closed.pipe(take(1)).subscribe(() => {
921
- if (this.#shouldRestoreFocus) {
922
- const target = this.target();
923
- const targetElement = target instanceof ElementRef ? target.nativeElement : target;
924
- if (targetElement && "focus" in targetElement) {
925
- targetElement.focus();
926
- }
927
- }
928
- this.popupRef = null;
929
- this.#close$.next();
930
- this.#shouldRestoreFocus = false;
931
- });
1238
+ // A context menu opens again at the new point; focus stays with the new menu.
1239
+ open.restoreFocus = false;
1240
+ open.ref?.close();
1241
+ this.#open = null;
1242
+ }
1243
+ if (trigger === "contextmenu") {
1244
+ handledContextMenuEvents.add(e);
1245
+ }
1246
+ this.openFor(e, targetElement);
932
1247
  });
933
1248
  }
934
1249
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
935
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuComponent, isStandalone: true, selector: "mona-popup-menu", inputs: { anchor: { classPropertyName: "anchor", publicName: "anchor", isSignal: true, isRequired: false, transformFunction: null }, anchorConnectionPoint: { classPropertyName: "anchorConnectionPoint", publicName: "anchorConnectionPoint", isSignal: true, isRequired: false, transformFunction: null }, items: { classPropertyName: "items", publicName: "items", isSignal: true, isRequired: false, transformFunction: null }, menuId: { classPropertyName: "menuId", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, minWidth: { classPropertyName: "minWidth", publicName: "minWidth", isSignal: true, isRequired: false, transformFunction: null }, offset: { classPropertyName: "offset", publicName: "offset", isSignal: true, isRequired: false, transformFunction: null }, popupConnectionPoint: { classPropertyName: "popupConnectionPoint", publicName: "popupConnectionPoint", isSignal: true, isRequired: false, transformFunction: null }, precise: { classPropertyName: "precise", publicName: "precise", isSignal: true, isRequired: false, transformFunction: null }, rounded: { classPropertyName: "rounded", publicName: "rounded", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, target: { classPropertyName: "target", publicName: "target", isSignal: true, isRequired: true, transformFunction: null }, trigger: { classPropertyName: "trigger", publicName: "trigger", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, userClass: { classPropertyName: "userClass", publicName: "class", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { close: "close", menuClick: "menuClick", navigate: "navigate", open: "open" }, queries: [{ propertyName: "groupTemplateConfig", first: true, predicate: PopupMenuGroupTemplateToken, isSignal: true }, { propertyName: "iconTemplateConfig", first: true, predicate: PopupMenuIconTemplateToken, isSignal: true }, { propertyName: "nestedComponents", predicate: PopupMenuToken, isSignal: true }, { propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true }); }
1250
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuComponent, isStandalone: true, selector: "mona-popup-menu", inputs: { anchor: { classPropertyName: "anchor", publicName: "anchor", isSignal: true, isRequired: false, transformFunction: null }, anchorConnectionPoint: { classPropertyName: "anchorConnectionPoint", publicName: "anchorConnectionPoint", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "ariaLabel", isSignal: true, isRequired: false, transformFunction: null }, ariaLabelledBy: { classPropertyName: "ariaLabelledBy", publicName: "ariaLabelledBy", isSignal: true, isRequired: false, transformFunction: null }, items: { classPropertyName: "items", publicName: "items", isSignal: true, isRequired: false, transformFunction: null }, menuId: { classPropertyName: "menuId", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, minWidth: { classPropertyName: "minWidth", publicName: "minWidth", isSignal: true, isRequired: false, transformFunction: null }, offset: { classPropertyName: "offset", publicName: "offset", isSignal: true, isRequired: false, transformFunction: null }, popupConnectionPoint: { classPropertyName: "popupConnectionPoint", publicName: "popupConnectionPoint", isSignal: true, isRequired: false, transformFunction: null }, precise: { classPropertyName: "precise", publicName: "precise", isSignal: true, isRequired: false, transformFunction: null }, rounded: { classPropertyName: "rounded", publicName: "rounded", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, target: { classPropertyName: "target", publicName: "target", isSignal: true, isRequired: true, transformFunction: null }, trigger: { classPropertyName: "trigger", publicName: "trigger", isSignal: true, isRequired: false, transformFunction: null }, userClass: { classPropertyName: "userClass", publicName: "class", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { close: "close", menuClick: "menuClick", navigate: "navigate", open: "open" }, queries: [{ propertyName: "groupTemplateConfig", first: true, predicate: PopupMenuGroupTemplateToken, isSignal: true }, { propertyName: "iconTemplateConfig", first: true, predicate: PopupMenuIconTemplateToken, isSignal: true }, { propertyName: "nestedComponents", predicate: PopupMenuToken, isSignal: true }, { propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true }); }
936
1251
  }
937
1252
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuComponent, decorators: [{
938
1253
  type: Component,
@@ -948,25 +1263,22 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImpor
948
1263
  descendants: false
949
1264
  }, isSignal: true }] }], textTemplateConfig: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PopupMenuTextTemplateToken), { ...{
950
1265
  descendants: false
951
- }, isSignal: true }] }], anchor: [{ type: i0.Input, args: [{ isSignal: true, alias: "anchor", required: false }] }], anchorConnectionPoint: [{ type: i0.Input, args: [{ isSignal: true, alias: "anchorConnectionPoint", required: false }] }], close: [{ type: i0.Output, args: ["close"] }], items: [{ type: i0.Input, args: [{ isSignal: true, alias: "items", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }], menuId: [{ type: i0.Input, args: [{ isSignal: true, alias: "id", required: false }] }], minWidth: [{ type: i0.Input, args: [{ isSignal: true, alias: "minWidth", required: false }] }], navigate: [{ type: i0.Output, args: ["navigate"] }], offset: [{ type: i0.Input, args: [{ isSignal: true, alias: "offset", required: false }] }], open: [{ type: i0.Output, args: ["open"] }], popupConnectionPoint: [{ type: i0.Input, args: [{ isSignal: true, alias: "popupConnectionPoint", required: false }] }], precise: [{ type: i0.Input, args: [{ isSignal: true, alias: "precise", required: false }] }], rounded: [{ type: i0.Input, args: [{ isSignal: true, alias: "rounded", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], target: [{ type: i0.Input, args: [{ isSignal: true, alias: "target", required: true }] }], trigger: [{ type: i0.Input, args: [{ isSignal: true, alias: "trigger", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], userClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "class", required: false }] }] } });
1266
+ }, isSignal: true }] }], anchor: [{ type: i0.Input, args: [{ isSignal: true, alias: "anchor", required: false }] }], anchorConnectionPoint: [{ type: i0.Input, args: [{ isSignal: true, alias: "anchorConnectionPoint", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabel", required: false }] }], ariaLabelledBy: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabelledBy", required: false }] }], close: [{ type: i0.Output, args: ["close"] }], items: [{ type: i0.Input, args: [{ isSignal: true, alias: "items", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }], menuId: [{ type: i0.Input, args: [{ isSignal: true, alias: "id", required: false }] }], minWidth: [{ type: i0.Input, args: [{ isSignal: true, alias: "minWidth", required: false }] }], navigate: [{ type: i0.Output, args: ["navigate"] }], offset: [{ type: i0.Input, args: [{ isSignal: true, alias: "offset", required: false }] }], open: [{ type: i0.Output, args: ["open"] }], popupConnectionPoint: [{ type: i0.Input, args: [{ isSignal: true, alias: "popupConnectionPoint", required: false }] }], precise: [{ type: i0.Input, args: [{ isSignal: true, alias: "precise", required: false }] }], rounded: [{ type: i0.Input, args: [{ isSignal: true, alias: "rounded", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], target: [{ type: i0.Input, args: [{ isSignal: true, alias: "target", required: true }] }], trigger: [{ type: i0.Input, args: [{ isSignal: true, alias: "trigger", required: false }] }], userClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "class", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }] } });
952
1267
 
953
1268
  class PopupMenuCheckboxItemComponent {
954
1269
  constructor() {
955
- this.#checked = linkedSignal(() => this.checked(), /* @ts-ignore */
956
- ...(ngDevMode ? [{ debugName: "#checked" }] : /* istanbul ignore next */ []));
957
1270
  this.#click$ = new Subject();
958
1271
  this.#destroyRef = inject(DestroyRef);
1272
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1273
+ this.#uid = createElementControlId();
959
1274
  this.shortcutTemplateConfig = contentChild(PopupMenuShortcutTemplateToken, { ...(ngDevMode ? { debugName: "shortcutTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
960
1275
  this.textTemplateConfig = contentChild(PopupMenuTextTemplateToken, { ...(ngDevMode ? { debugName: "textTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
961
1276
  /**
962
- * @description Sets the checked state of the menu item.
1277
+ * @description The checked state of the menu item. Activating the item toggles it (unless its `menuClick` is
1278
+ * prevented) and emits `checkedChange`, so it works with or without a two-way binding.
963
1279
  */
964
- this.checked = input(false, /* @ts-ignore */
1280
+ this.checked = model(false, /* @ts-ignore */
965
1281
  ...(ngDevMode ? [{ debugName: "checked" }] : /* istanbul ignore next */ []));
966
- /**
967
- * @description Emits when the checked state of the menu item changes.
968
- */
969
- this.checkedChange = output();
970
1282
  /**
971
1283
  * @description Sets the disabled state of the menu item.
972
1284
  */
@@ -984,15 +1296,21 @@ class PopupMenuCheckboxItemComponent {
984
1296
  this.menuClick = output();
985
1297
  this.origin = PopupMenuOrigin.Popup;
986
1298
  this.type = PopupMenuItemType.CheckboxMenuItem;
1299
+ /**
1300
+ * @description A value that identifies the item. It's the `value` of the item's `menuClick` events.
1301
+ */
1302
+ this.value = input(/* @ts-ignore */
1303
+ ...(ngDevMode ? [undefined, { debugName: "value" }] : /* istanbul ignore next */ []));
987
1304
  }
988
- #checked;
989
1305
  #click$;
990
1306
  #destroyRef;
1307
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1308
+ #uid;
991
1309
  getPopupMenuItem() {
992
1310
  return [
993
1311
  {
994
1312
  checkable: true,
995
- checked: computed(() => this.#checked()),
1313
+ checked: computed(() => this.checked()),
996
1314
  click$: this.#click$,
997
1315
  disabled: this.disabled(),
998
1316
  group: Symbol(),
@@ -1002,7 +1320,8 @@ class PopupMenuCheckboxItemComponent {
1002
1320
  label: this.label(),
1003
1321
  shortcutTemplate: computed(() => this.shortcutTemplateConfig()?.template ?? null),
1004
1322
  textTemplate: computed(() => this.textTemplateConfig()?.template ?? null),
1005
- uid: v4()
1323
+ uid: this.#uid,
1324
+ value: computed(() => this.value())
1006
1325
  }
1007
1326
  ];
1008
1327
  }
@@ -1014,25 +1333,24 @@ class PopupMenuCheckboxItemComponent {
1014
1333
  .pipe(takeUntilDestroyed(this.#destroyRef), tap(e => {
1015
1334
  this.menuClick.emit(e);
1016
1335
  if (!e.isDefaultPrevented()) {
1017
- this.checkedChange.emit(!this.#checked());
1336
+ this.checked.set(e.checked === true);
1018
1337
  }
1019
1338
  }))
1020
1339
  .subscribe();
1021
1340
  }
1022
1341
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuCheckboxItemComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
1023
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuCheckboxItemComponent, isStandalone: true, selector: "mona-popup-menu-checkbox-item", inputs: { checked: { classPropertyName: "checked", publicName: "checked", isSignal: true, isRequired: false, transformFunction: null }, disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { checkedChange: "checkedChange", menuClick: "menuClick" }, providers: [
1342
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuCheckboxItemComponent, isStandalone: true, selector: "mona-popup-menu-checkbox-item", inputs: { checked: { classPropertyName: "checked", publicName: "checked", isSignal: true, isRequired: false, transformFunction: null }, disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { checked: "checkedChange", menuClick: "menuClick" }, providers: [
1024
1343
  {
1025
1344
  provide: PopupMenuToken,
1026
1345
  useExisting: forwardRef(() => PopupMenuCheckboxItemComponent)
1027
1346
  }
1028
- ], queries: [{ propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
1347
+ ], queries: [{ propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true }); }
1029
1348
  }
1030
1349
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuCheckboxItemComponent, decorators: [{
1031
1350
  type: Component,
1032
1351
  args: [{
1033
1352
  selector: "mona-popup-menu-checkbox-item",
1034
1353
  template: "",
1035
- changeDetection: ChangeDetectionStrategy.OnPush,
1036
1354
  providers: [
1037
1355
  {
1038
1356
  provide: PopupMenuToken,
@@ -1044,12 +1362,14 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImpor
1044
1362
  descendants: false
1045
1363
  }, isSignal: true }] }], textTemplateConfig: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PopupMenuTextTemplateToken), { ...{
1046
1364
  descendants: false
1047
- }, isSignal: true }] }], checked: [{ type: i0.Input, args: [{ isSignal: true, alias: "checked", required: false }] }], checkedChange: [{ type: i0.Output, args: ["checkedChange"] }], disabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "disabled", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }] } });
1365
+ }, isSignal: true }] }], checked: [{ type: i0.Input, args: [{ isSignal: true, alias: "checked", required: false }] }, { type: i0.Output, args: ["checkedChange"] }], disabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "disabled", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }], value: [{ type: i0.Input, args: [{ isSignal: true, alias: "value", required: false }] }] } });
1048
1366
 
1049
1367
  class PopupMenuItemComponent {
1050
1368
  constructor() {
1051
1369
  this.#click$ = new Subject();
1052
1370
  this.#destroyRef = inject(DestroyRef);
1371
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1372
+ this.#uid = createElementControlId();
1053
1373
  this.iconTemplateConfig = contentChild(PopupMenuIconTemplateToken, { ...(ngDevMode ? { debugName: "iconTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
1054
1374
  this.items = contentChildren(PopupMenuToken, { ...(ngDevMode ? { debugName: "items" } : /* istanbul ignore next */ {}), descendants: false });
1055
1375
  this.shortcutTemplateConfig = contentChild(PopupMenuShortcutTemplateToken, { ...(ngDevMode ? { debugName: "shortcutTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
@@ -1071,9 +1391,17 @@ class PopupMenuItemComponent {
1071
1391
  this.menuClick = output();
1072
1392
  this.origin = PopupMenuOrigin.Popup;
1073
1393
  this.type = PopupMenuItemType.MenuItem;
1394
+ /**
1395
+ * @description A value that identifies the item. It's the `value` of the item's `menuClick` events, so
1396
+ * handlers don't depend on the label's text.
1397
+ */
1398
+ this.value = input(/* @ts-ignore */
1399
+ ...(ngDevMode ? [undefined, { debugName: "value" }] : /* istanbul ignore next */ []));
1074
1400
  }
1075
1401
  #click$;
1076
1402
  #destroyRef;
1403
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1404
+ #uid;
1077
1405
  getPopupMenuItem() {
1078
1406
  return [
1079
1407
  {
@@ -1086,7 +1414,8 @@ class PopupMenuItemComponent {
1086
1414
  label: this.label(),
1087
1415
  shortcutTemplate: computed(() => this.shortcutTemplateConfig()?.template ?? null),
1088
1416
  textTemplate: computed(() => this.textTemplateConfig()?.template ?? null),
1089
- uid: v4()
1417
+ uid: this.#uid,
1418
+ value: computed(() => this.value())
1090
1419
  }
1091
1420
  ];
1092
1421
  }
@@ -1099,7 +1428,7 @@ class PopupMenuItemComponent {
1099
1428
  .subscribe();
1100
1429
  }
1101
1430
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuItemComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
1102
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuItemComponent, isStandalone: true, selector: "mona-popup-menu-item", inputs: { disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { menuClick: "menuClick" }, providers: [
1431
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.0.6", type: PopupMenuItemComponent, isStandalone: true, selector: "mona-popup-menu-item", inputs: { disabled: { classPropertyName: "disabled", publicName: "disabled", isSignal: true, isRequired: false, transformFunction: null }, label: { classPropertyName: "label", publicName: "label", isSignal: true, isRequired: false, transformFunction: null }, value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { menuClick: "menuClick" }, providers: [
1103
1432
  {
1104
1433
  provide: PopupMenuToken,
1105
1434
  useExisting: forwardRef(() => PopupMenuItemComponent)
@@ -1124,11 +1453,13 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImpor
1124
1453
  descendants: false
1125
1454
  }, isSignal: true }] }], textTemplateConfig: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PopupMenuTextTemplateToken), { ...{
1126
1455
  descendants: false
1127
- }, isSignal: true }] }], disabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "disabled", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }] } });
1456
+ }, isSignal: true }] }], disabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "disabled", required: false }] }], label: [{ type: i0.Input, args: [{ isSignal: true, alias: "label", required: false }] }], menuClick: [{ type: i0.Output, args: ["menuClick"] }], value: [{ type: i0.Input, args: [{ isSignal: true, alias: "value", required: false }] }] } });
1128
1457
 
1129
1458
  class PopupMenuGroupComponent {
1130
1459
  constructor() {
1131
- this.#groupSymbol = Symbol(v4());
1460
+ // One key per group: consecutive items with it form the group, and its description is the title.
1461
+ this.#groupKey = computed(() => Symbol(this.title() ?? ""), /* @ts-ignore */
1462
+ ...(ngDevMode ? [{ debugName: "#groupKey" }] : /* istanbul ignore next */ []));
1132
1463
  this.groupTemplateConfig = contentChild(PopupMenuGroupTemplateToken, { ...(ngDevMode ? { debugName: "groupTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
1133
1464
  this.items = contentChildren(PopupMenuToken, { ...(ngDevMode ? { debugName: "items" } : /* istanbul ignore next */ {}), descendants: false });
1134
1465
  this.origin = PopupMenuOrigin.Popup;
@@ -1139,11 +1470,13 @@ class PopupMenuGroupComponent {
1139
1470
  ...(ngDevMode ? [undefined, { debugName: "title" }] : /* istanbul ignore next */ []));
1140
1471
  this.type = PopupMenuItemType.MenuGroup;
1141
1472
  }
1142
- #groupSymbol;
1473
+ // One key per group: consecutive items with it form the group, and its description is the title.
1474
+ #groupKey;
1143
1475
  getPopupMenuItem() {
1476
+ const group = this.#groupKey();
1144
1477
  const menuItems = this.items().map(i => i.getPopupMenuItem().map(item => ({
1145
1478
  ...item,
1146
- group: this.title() || this.#groupSymbol,
1479
+ group,
1147
1480
  groupTemplate: computed(() => this.groupTemplateConfig()?.template ?? null)
1148
1481
  })));
1149
1482
  return selectMany(menuItems, i => i).toArray();
@@ -1176,6 +1509,11 @@ class PopupMenuRadioGroupComponent {
1176
1509
  constructor() {
1177
1510
  this.#click$ = new Subject();
1178
1511
  this.#destroyRef = inject(DestroyRef);
1512
+ // One key per group: consecutive items with it form the group, and its description is the title.
1513
+ this.#groupKey = computed(() => Symbol(this.title()), /* @ts-ignore */
1514
+ ...(ngDevMode ? [{ debugName: "#groupKey" }] : /* istanbul ignore next */ []));
1515
+ // The radio items' own click subjects, so their `menuClick` runs before the group selects.
1516
+ this.#itemClicks = new WeakMap();
1179
1517
  this.groupTemplateConfig = contentChild(PopupMenuGroupTemplateToken, { ...(ngDevMode ? { debugName: "groupTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
1180
1518
  this.items = contentChildren(PopupMenuRadioItemToken, { ...(ngDevMode ? { debugName: "items" } : /* istanbul ignore next */ {}), descendants: false });
1181
1519
  this.origin = PopupMenuOrigin.Popup;
@@ -1193,15 +1531,26 @@ class PopupMenuRadioGroupComponent {
1193
1531
  }
1194
1532
  #click$;
1195
1533
  #destroyRef;
1534
+ // One key per group: consecutive items with it form the group, and its description is the title.
1535
+ #groupKey;
1536
+ // The radio items' own click subjects, so their `menuClick` runs before the group selects.
1537
+ #itemClicks;
1196
1538
  getPopupMenuItem() {
1197
- const menuItems = this.items().map(i => i.getPopupMenuItem().map(item => ({
1198
- ...item,
1199
- click$: this.#click$,
1200
- group: this.title() || Symbol(),
1201
- groupTemplate: computed(() => this.groupTemplateConfig()?.template ?? null),
1202
- radio: true,
1203
- selected: computed(() => item.value?.() === this.value())
1204
- })));
1539
+ const group = this.#groupKey();
1540
+ const menuItems = this.items().map(i => i.getPopupMenuItem().map(item => {
1541
+ const groupItem = {
1542
+ ...item,
1543
+ click$: this.#click$,
1544
+ group,
1545
+ groupTemplate: computed(() => this.groupTemplateConfig()?.template ?? null),
1546
+ radio: true,
1547
+ selected: computed(() => item.value?.() === this.value())
1548
+ };
1549
+ if (item.click$) {
1550
+ this.#itemClicks.set(groupItem, item.click$);
1551
+ }
1552
+ return groupItem;
1553
+ }));
1205
1554
  return selectMany(menuItems, i => i).toArray();
1206
1555
  }
1207
1556
  ngOnInit() {
@@ -1210,11 +1559,12 @@ class PopupMenuRadioGroupComponent {
1210
1559
  setSubscription() {
1211
1560
  this.#click$
1212
1561
  .pipe(takeUntilDestroyed(this.#destroyRef), filter(e => e.item.radio == true), tap(event => {
1562
+ this.#itemClicks.get(event.item)?.next(event);
1213
1563
  if (event.isDefaultPrevented()) {
1214
1564
  return;
1215
1565
  }
1216
1566
  const value = event.item.value?.();
1217
- if (value != null) {
1567
+ if (typeof value === "string") {
1218
1568
  this.value.set(value);
1219
1569
  }
1220
1570
  }))
@@ -1226,14 +1576,13 @@ class PopupMenuRadioGroupComponent {
1226
1576
  provide: PopupMenuToken,
1227
1577
  useExisting: forwardRef(() => PopupMenuRadioGroupComponent)
1228
1578
  }
1229
- ], queries: [{ propertyName: "groupTemplateConfig", first: true, predicate: PopupMenuGroupTemplateToken, isSignal: true }, { propertyName: "items", predicate: PopupMenuRadioItemToken, isSignal: true }], ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
1579
+ ], queries: [{ propertyName: "groupTemplateConfig", first: true, predicate: PopupMenuGroupTemplateToken, isSignal: true }, { propertyName: "items", predicate: PopupMenuRadioItemToken, isSignal: true }], ngImport: i0, template: "", isInline: true }); }
1230
1580
  }
1231
1581
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuRadioGroupComponent, decorators: [{
1232
1582
  type: Component,
1233
1583
  args: [{
1234
1584
  selector: "mona-popup-menu-radio-group",
1235
1585
  template: "",
1236
- changeDetection: ChangeDetectionStrategy.OnPush,
1237
1586
  providers: [
1238
1587
  {
1239
1588
  provide: PopupMenuToken,
@@ -1249,6 +1598,8 @@ class PopupMenuRadioItemComponent {
1249
1598
  constructor() {
1250
1599
  this.#click$ = new Subject();
1251
1600
  this.#destroyRef = inject(DestroyRef);
1601
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1602
+ this.#uid = createElementControlId();
1252
1603
  this.shortcutTemplateConfig = contentChild(PopupMenuShortcutTemplateToken, { ...(ngDevMode ? { debugName: "shortcutTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
1253
1604
  this.textTemplateConfig = contentChild(PopupMenuTextTemplateToken, { ...(ngDevMode ? { debugName: "textTemplateConfig" } : /* istanbul ignore next */ {}), descendants: false });
1254
1605
  /**
@@ -1263,7 +1614,8 @@ class PopupMenuRadioItemComponent {
1263
1614
  ...(ngDevMode ? [{ debugName: "label" }] : /* istanbul ignore next */ []));
1264
1615
  /**
1265
1616
  * @description The click event of the menu item.
1266
- * Emits when the menu item is selected by keyboard or mouse.
1617
+ * Emits when the menu item is selected by keyboard or mouse, before the radio group selects it. Preventing it
1618
+ * keeps the group's value and the menu open.
1267
1619
  */
1268
1620
  this.menuClick = output();
1269
1621
  this.origin = PopupMenuOrigin.Popup;
@@ -1276,9 +1628,12 @@ class PopupMenuRadioItemComponent {
1276
1628
  }
1277
1629
  #click$;
1278
1630
  #destroyRef;
1631
+ // One id per item for its lifetime, so the menu keeps track of it when the items are rebuilt.
1632
+ #uid;
1279
1633
  getPopupMenuItem() {
1280
1634
  return [
1281
1635
  {
1636
+ click$: this.#click$,
1282
1637
  disabled: this.disabled(),
1283
1638
  group: Symbol(),
1284
1639
  groupTemplate: null,
@@ -1288,7 +1643,7 @@ class PopupMenuRadioItemComponent {
1288
1643
  radio: true,
1289
1644
  shortcutTemplate: computed(() => this.shortcutTemplateConfig()?.template ?? null),
1290
1645
  textTemplate: computed(() => this.textTemplateConfig()?.template ?? null),
1291
- uid: v4(),
1646
+ uid: this.#uid,
1292
1647
  value: computed(() => this.value())
1293
1648
  }
1294
1649
  ];
@@ -1311,14 +1666,13 @@ class PopupMenuRadioItemComponent {
1311
1666
  provide: PopupMenuRadioItemToken,
1312
1667
  useExisting: forwardRef(() => PopupMenuRadioItemComponent)
1313
1668
  }
1314
- ], queries: [{ propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
1669
+ ], queries: [{ propertyName: "shortcutTemplateConfig", first: true, predicate: PopupMenuShortcutTemplateToken, isSignal: true }, { propertyName: "textTemplateConfig", first: true, predicate: PopupMenuTextTemplateToken, isSignal: true }], ngImport: i0, template: "", isInline: true }); }
1315
1670
  }
1316
1671
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuRadioItemComponent, decorators: [{
1317
1672
  type: Component,
1318
1673
  args: [{
1319
1674
  selector: "mona-popup-menu-radio-item",
1320
1675
  template: "",
1321
- changeDetection: ChangeDetectionStrategy.OnPush,
1322
1676
  providers: [
1323
1677
  {
1324
1678
  provide: PopupMenuToken,
@@ -1363,14 +1717,13 @@ class PopupMenuSeparatorComponent {
1363
1717
  provide: PopupMenuToken,
1364
1718
  useExisting: forwardRef(() => PopupMenuSeparatorComponent)
1365
1719
  }
1366
- ], ngImport: i0, template: "", isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
1720
+ ], ngImport: i0, template: "", isInline: true }); }
1367
1721
  }
1368
1722
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: PopupMenuSeparatorComponent, decorators: [{
1369
1723
  type: Component,
1370
1724
  args: [{
1371
1725
  selector: "mona-popup-menu-separator",
1372
1726
  template: "",
1373
- changeDetection: ChangeDetectionStrategy.OnPush,
1374
1727
  providers: [
1375
1728
  {
1376
1729
  provide: PopupMenuToken,