ochreui 0.0.0-stage → 0.0.2

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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -2
  3. package/cli/ochreui.mjs +601 -0
  4. package/mcp/core.mjs +126 -0
  5. package/mcp/docs.json +906 -0
  6. package/mcp/server.mjs +23 -0
  7. package/package.json +393 -3
  8. package/registry/agent-team.ts +350 -0
  9. package/registry/avatar.ts +300 -0
  10. package/registry/badge.ts +207 -0
  11. package/registry/breadcrumb.ts +435 -0
  12. package/registry/button.ts +238 -0
  13. package/registry/card.ts +221 -0
  14. package/registry/change-summary.ts +200 -0
  15. package/registry/checkbox.ts +204 -0
  16. package/registry/code-block.ts +332 -0
  17. package/registry/color-picker.ts +720 -0
  18. package/registry/comment-thread.ts +503 -0
  19. package/registry/context-menu.ts +281 -0
  20. package/registry/cover-flow.ts +623 -0
  21. package/registry/date-picker.ts +799 -0
  22. package/registry/decision-inbox.ts +340 -0
  23. package/registry/dialog.ts +577 -0
  24. package/registry/dropdown-menu.ts +864 -0
  25. package/registry/email-draft.ts +916 -0
  26. package/registry/empty-calendar.ts +199 -0
  27. package/registry/empty-state.ts +164 -0
  28. package/registry/enrichment-orbit.ts +164 -0
  29. package/registry/filter-menu.ts +541 -0
  30. package/registry/form-field.ts +308 -0
  31. package/registry/frost-cloud.ts +381 -0
  32. package/registry/ghosted-chat.ts +210 -0
  33. package/registry/glow-cloud.ts +402 -0
  34. package/registry/grid-background.ts +164 -0
  35. package/registry/hover-card.ts +401 -0
  36. package/registry/icon-button.ts +133 -0
  37. package/registry/index.json +2124 -0
  38. package/registry/integration-beam.ts +588 -0
  39. package/registry/integration-grid.ts +209 -0
  40. package/registry/kbd.ts +176 -0
  41. package/registry/layer-stack.ts +351 -0
  42. package/registry/lead-sourcing.ts +285 -0
  43. package/registry/logo-cluster.ts +239 -0
  44. package/registry/logo-marquee.ts +505 -0
  45. package/registry/message-actions.ts +833 -0
  46. package/registry/model-orbit.ts +583 -0
  47. package/registry/model-wheel.ts +372 -0
  48. package/registry/notification-stack.ts +301 -0
  49. package/registry/number-input.ts +552 -0
  50. package/registry/number-ticker.ts +261 -0
  51. package/registry/ochre.css +917 -0
  52. package/registry/otp-input.ts +704 -0
  53. package/registry/overlay.ts +312 -0
  54. package/registry/pagination.ts +362 -0
  55. package/registry/popover.ts +296 -0
  56. package/registry/profile-menu.ts +332 -0
  57. package/registry/progress-tracker.ts +291 -0
  58. package/registry/rating.ts +461 -0
  59. package/registry/reaction-bar.ts +322 -0
  60. package/registry/region-map.ts +537 -0
  61. package/registry/resizable.ts +489 -0
  62. package/registry/scroll-area.ts +271 -0
  63. package/registry/segmented-control.ts +433 -0
  64. package/registry/select.ts +769 -0
  65. package/registry/separator.ts +97 -0
  66. package/registry/shimmer-loader.ts +211 -0
  67. package/registry/side-folder.ts +152 -0
  68. package/registry/skeleton.ts +136 -0
  69. package/registry/skill/INSTRUCTIONS.md +11 -0
  70. package/registry/skill/SKILL.md +65 -0
  71. package/registry/skill/checklist.md +45 -0
  72. package/registry/skill/references/agent-team.md +105 -0
  73. package/registry/skill/references/avatar.md +88 -0
  74. package/registry/skill/references/badge.md +92 -0
  75. package/registry/skill/references/breadcrumb.md +99 -0
  76. package/registry/skill/references/button.md +82 -0
  77. package/registry/skill/references/card.md +111 -0
  78. package/registry/skill/references/catalog.md +114 -0
  79. package/registry/skill/references/change-summary.md +103 -0
  80. package/registry/skill/references/checkbox.md +105 -0
  81. package/registry/skill/references/code-block.md +109 -0
  82. package/registry/skill/references/color-picker.md +138 -0
  83. package/registry/skill/references/comment-thread.md +164 -0
  84. package/registry/skill/references/context-menu.md +205 -0
  85. package/registry/skill/references/cover-flow.md +128 -0
  86. package/registry/skill/references/date-picker.md +154 -0
  87. package/registry/skill/references/decision-inbox.md +126 -0
  88. package/registry/skill/references/dialog.md +155 -0
  89. package/registry/skill/references/dropdown-menu.md +144 -0
  90. package/registry/skill/references/email-draft.md +182 -0
  91. package/registry/skill/references/empty-calendar.md +74 -0
  92. package/registry/skill/references/empty-state.md +118 -0
  93. package/registry/skill/references/enrichment-orbit.md +71 -0
  94. package/registry/skill/references/filter-menu.md +143 -0
  95. package/registry/skill/references/form-field.md +118 -0
  96. package/registry/skill/references/frost-cloud.md +80 -0
  97. package/registry/skill/references/ghosted-chat.md +75 -0
  98. package/registry/skill/references/glow-cloud.md +84 -0
  99. package/registry/skill/references/grid-background.md +76 -0
  100. package/registry/skill/references/hover-card.md +125 -0
  101. package/registry/skill/references/icon-button.md +99 -0
  102. package/registry/skill/references/integration-beam.md +107 -0
  103. package/registry/skill/references/integration-grid.md +100 -0
  104. package/registry/skill/references/kbd.md +65 -0
  105. package/registry/skill/references/layer-stack.md +115 -0
  106. package/registry/skill/references/lead-sourcing.md +92 -0
  107. package/registry/skill/references/logo-cluster.md +92 -0
  108. package/registry/skill/references/logo-marquee.md +123 -0
  109. package/registry/skill/references/message-actions.md +146 -0
  110. package/registry/skill/references/model-orbit.md +106 -0
  111. package/registry/skill/references/model-wheel.md +84 -0
  112. package/registry/skill/references/notification-stack.md +138 -0
  113. package/registry/skill/references/number-input.md +117 -0
  114. package/registry/skill/references/number-ticker.md +73 -0
  115. package/registry/skill/references/otp-input.md +142 -0
  116. package/registry/skill/references/pagination.md +112 -0
  117. package/registry/skill/references/popover.md +143 -0
  118. package/registry/skill/references/profile-menu.md +123 -0
  119. package/registry/skill/references/progress-tracker.md +122 -0
  120. package/registry/skill/references/rating.md +111 -0
  121. package/registry/skill/references/reaction-bar.md +128 -0
  122. package/registry/skill/references/region-map.md +109 -0
  123. package/registry/skill/references/resizable.md +127 -0
  124. package/registry/skill/references/scroll-area.md +113 -0
  125. package/registry/skill/references/segmented-control.md +96 -0
  126. package/registry/skill/references/select.md +122 -0
  127. package/registry/skill/references/separator.md +69 -0
  128. package/registry/skill/references/shimmer-loader.md +83 -0
  129. package/registry/skill/references/side-folder.md +75 -0
  130. package/registry/skill/references/skeleton.md +95 -0
  131. package/registry/skill/references/smart-recommendation.md +132 -0
  132. package/registry/skill/references/spinner.md +84 -0
  133. package/registry/skill/references/switch.md +79 -0
  134. package/registry/skill/references/tab-overload.md +70 -0
  135. package/registry/skill/references/tag-input.md +126 -0
  136. package/registry/skill/references/team-invite.md +94 -0
  137. package/registry/skill/references/testimonial-wall.md +110 -0
  138. package/registry/skill/references/text-area.md +115 -0
  139. package/registry/skill/references/text-field.md +126 -0
  140. package/registry/skill/references/text-reveal.md +71 -0
  141. package/registry/skill/references/thinking-loader.md +87 -0
  142. package/registry/skill/references/time-picker.md +122 -0
  143. package/registry/skill/references/tips-panel.md +115 -0
  144. package/registry/skill/references/toggle-group.md +98 -0
  145. package/registry/skill/references/tool-call-card.md +151 -0
  146. package/registry/skill/references/tooltip.md +94 -0
  147. package/registry/skill/references/tree-view.md +172 -0
  148. package/registry/skill/theming.md +57 -0
  149. package/registry/smart-recommendation.ts +415 -0
  150. package/registry/spinner.ts +67 -0
  151. package/registry/switch.ts +146 -0
  152. package/registry/tab-overload.ts +211 -0
  153. package/registry/tag-input.ts +620 -0
  154. package/registry/team-invite.ts +248 -0
  155. package/registry/testimonial-wall.ts +521 -0
  156. package/registry/text-area.ts +432 -0
  157. package/registry/text-field.ts +415 -0
  158. package/registry/text-reveal.ts +175 -0
  159. package/registry/theme.ts +68 -0
  160. package/registry/thinking-loader.ts +302 -0
  161. package/registry/time-picker.ts +828 -0
  162. package/registry/tips-panel.ts +312 -0
  163. package/registry/toggle-group.ts +357 -0
  164. package/registry/tool-call-card.ts +578 -0
  165. package/registry/tooltip.ts +486 -0
  166. package/registry/tree-view.ts +454 -0
  167. package/styles/base.css +500 -0
  168. package/styles/tokens.css +415 -0
@@ -0,0 +1,864 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Dropdown Menu, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (`npx ochreui init`) and overlay.ts (`npx ochreui add overlay`).
5
+ *
6
+ * <button uiButton [uiMenuTrigger]="actions">Actions</button>
7
+ * <ui-menu #actions align="end">
8
+ * <button uiMenuItem shortcut="⌘D" keys="Meta+D" (selected)="duplicate()"><svg uiMenuIcon …/>Duplicate</button>
9
+ * <button uiMenuItem [uiSubmenu]="folders">Move to</button>
10
+ * <ui-menu-separator />
11
+ * <button uiMenuItem danger (selected)="remove()">Delete</button>
12
+ * </ui-menu>
13
+ * A desktop-style menu whose rows are your own buttons and links, with checkbox, radio and submenu rows.
14
+ * While open, the panel carries --ui-menu-trigger-w, its trigger's width.
15
+ *
16
+ * Public styling hooks (set them on the element or any ancestor):
17
+ * --ui-menu-bg panel fill (default: the overlay surface, a step lighter in dark mode)
18
+ * --ui-menu-shadow ring and drop shadow
19
+ * --ui-menu-radius panel corners (default 12px; Classic 14px); rows follow it
20
+ * --ui-menu-pad space around the rows (default 4px; Classic 5px)
21
+ * --ui-menu-min-width narrowest the panel gets (default 168px; Classic 180px)
22
+ * --ui-menu-max-width widest the panel gets (default 320px)
23
+ */
24
+ import {
25
+ Component,
26
+ DOCUMENT,
27
+ DestroyRef,
28
+ Directive,
29
+ ElementRef,
30
+ InjectionToken,
31
+ PLATFORM_ID,
32
+ afterRenderEffect,
33
+ booleanAttribute,
34
+ computed,
35
+ contentChildren,
36
+ effect,
37
+ forwardRef,
38
+ inject,
39
+ input,
40
+ isDevMode,
41
+ model,
42
+ numberAttribute,
43
+ output,
44
+ signal,
45
+ untracked,
46
+ viewChild,
47
+ } from '@angular/core';
48
+ import { isPlatformBrowser } from '@angular/common';
49
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
50
+ import { OverlayRef, placeOverlay } from './overlay';
51
+
52
+ export type MenuSide = 'bottom' | 'top';
53
+ export type MenuAlign = 'start' | 'end';
54
+ /** Why a menu closed: a row was picked, Escape (or ← in a submenu), a press outside, focus left, Tab, or its trigger. */
55
+ export type MenuCloseReason = 'select' | 'escape' | 'outside' | 'focus-out' | 'tab' | 'trigger';
56
+ /** Where focus goes on open: the checked row or the first, the last row, the menu itself, or nowhere. */
57
+ export type MenuFocus = 'first' | 'last' | 'menu' | 'none';
58
+
59
+ export interface MenuLabels {
60
+ /** Shown when the menu has no rows. */
61
+ empty: string;
62
+ }
63
+
64
+ const LABELS: MenuLabels = { empty: 'Nothing here yet' };
65
+ const TYPEAHEAD_MS = 700;
66
+
67
+ let nextId = 0;
68
+
69
+ /** What a menu rendered by another directive talks to, such as `[uiContextMenu]` (context-menu.ts). */
70
+ export interface MenuHost {
71
+ /** The menu's name when it has no title or aria-label. */
72
+ label(): string;
73
+ register(menu: UiMenu): () => void;
74
+ closed(menu: UiMenu): void;
75
+ }
76
+
77
+ export const MENU_HOST = new InjectionToken<MenuHost>('ui-menu-host');
78
+
79
+ /** A set of radio rows with one value: `<ui-menu-group [(value)]="sort" label="Sort by">`. Also takes forms. */
80
+ @Component({
81
+ selector: 'ui-menu-group',
82
+ providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiMenuGroup), multi: true }],
83
+ host: { role: 'group', class: 'ui-menu-group', '[attr.aria-label]': 'label() || null' },
84
+ template: `@if (label()) {<span class="ui-menu-label" aria-hidden="true">{{ label() }}</span>}<ng-content />`,
85
+ })
86
+ export class UiMenuGroup<T = unknown> implements ControlValueAccessor {
87
+ /** The picked radio row's value. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
88
+ readonly value = model<T>(undefined as T);
89
+ /** A small heading above the rows, and the group's accessible name. */
90
+ readonly label = input<string>();
91
+ readonly disabled = input(false, { transform: booleanAttribute });
92
+ private readonly formOff = signal(false);
93
+ readonly off = computed(() => this.disabled() || this.formOff());
94
+ private onChange: (v: unknown) => void = () => {};
95
+ private onTouched: () => void = () => {};
96
+
97
+ pick(v: unknown) {
98
+ this.value.set(v as T);
99
+ this.onChange(v);
100
+ this.onTouched();
101
+ }
102
+
103
+ writeValue(v: unknown) {
104
+ this.value.set(v as T);
105
+ }
106
+ registerOnChange(fn: (v: unknown) => void) {
107
+ this.onChange = fn;
108
+ }
109
+ registerOnTouched(fn: () => void) {
110
+ this.onTouched = fn;
111
+ }
112
+ setDisabledState(off: boolean) {
113
+ this.formOff.set(off);
114
+ }
115
+ }
116
+
117
+ /** Marks the 16px icon at the start of a row: `<svg uiMenuIcon>`. Hidden from screen readers. */
118
+ @Directive({ selector: '[uiMenuIcon]', host: { class: 'ui-menu-icon', 'aria-hidden': 'true', focusable: 'false' } })
119
+ export class UiMenuIcon {}
120
+
121
+ /** A hairline between groups of rows. */
122
+ @Component({ selector: 'ui-menu-separator', host: { role: 'separator', class: 'ui-menu-sep' }, template: '' })
123
+ export class UiMenuSeparator {}
124
+
125
+ /** A small grey heading between rows. It is visual only: name a set of rows with `<ui-menu-group label>`. */
126
+ @Component({ selector: 'ui-menu-label', host: { class: 'ui-menu-label', 'aria-hidden': 'true' }, template: '<ng-content />' })
127
+ export class UiMenuLabel {}
128
+
129
+ const ROW = `
130
+ <ng-content select="[uiMenuIcon]" />
131
+ <span class="ui-menu-text">
132
+ <span class="ui-menu-name"><ng-content /></span>
133
+ @if (description()) {<span class="ui-menu-desc" aria-hidden="true" [id]="descId">{{ description() }}</span>}
134
+ </span>
135
+ @if (shortcut()) {<kbd class="ui-menu-kbd" aria-hidden="true" dir="ltr">{{ shortcut() }}</kbd>}
136
+ @if (submenu()) {
137
+ <svg class="ui-menu-mark ui-menu-chevron" viewBox="0 0 14 14" aria-hidden="true"><path d="M5.5 3.5 9 7l-3.5 3.5" /></svg>
138
+ } @else if (on() !== null) {
139
+ <svg class="ui-menu-mark" viewBox="0 0 14 14" aria-hidden="true">@if (on()) {<path class="ui-draw" pathLength="1" d="M3 7.4 5.7 10 11 4" />}</svg>
140
+ }
141
+ `;
142
+
143
+ /** What every row shares. Use UiMenuItem, UiMenuItemCheckbox or UiMenuItemRadio. */
144
+ @Directive({
145
+ host: {
146
+ class: 'ui-menu-item',
147
+ tabindex: '-1',
148
+ '[attr.type]': 'button ? "button" : null',
149
+ '[attr.aria-disabled]': 'off() || null',
150
+ '[attr.aria-checked]': 'on()',
151
+ '[attr.aria-keyshortcuts]': 'keys() || null',
152
+ '[attr.aria-describedby]': 'description() ? descId : null',
153
+ '[attr.aria-haspopup]': 'submenu() ? "menu" : null',
154
+ '[attr.aria-expanded]': 'submenu() ? expanded() : null',
155
+ '[attr.aria-controls]': 'expanded() ? submenu()!.listId() : null',
156
+ '[attr.data-danger]': 'danger() || null',
157
+ '[attr.data-active]': 'menu.active() === this || expanded() || null',
158
+ '[attr.data-quiet]': 'quiet() || null',
159
+ '(click)': 'clicked($event)',
160
+ '(pointermove)': 'moved($event)',
161
+ '(pointerenter)': 'measure()',
162
+ '(focus)': 'focused()',
163
+ },
164
+ })
165
+ export abstract class MenuItem {
166
+ /** Faint, skipped by the arrow keys and typeahead, and it does nothing when clicked. */
167
+ readonly disabled = input(false, { transform: booleanAttribute });
168
+ /** Red label and icon, for actions that destroy something. */
169
+ readonly danger = input(false, { transform: booleanAttribute });
170
+ /** The menu stays open after this row is picked. */
171
+ readonly keepOpen = input(false, { transform: booleanAttribute });
172
+ /** Shown at the end of the row, such as "⌘D". Display only: wire the keys yourself. */
173
+ readonly shortcut = input<string>();
174
+ /** The same keys for screen readers, as `aria-keyshortcuts`, such as "Meta+D". */
175
+ readonly keys = input<string>();
176
+ /** What typeahead matches. Default: the row's text. */
177
+ readonly textValue = input<string>();
178
+ /** A quiet second line under the label, read as the row's description. */
179
+ readonly description = input<string>();
180
+ /** Opens this menu beside the row on hover, →, Enter or Space. */
181
+ readonly submenu = input<UiMenu | undefined>(undefined, { alias: 'uiSubmenu' });
182
+ /** After the row is clicked or picked with Enter or Space (not for submenu rows). */
183
+ readonly selected = output<void>();
184
+
185
+ readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
186
+ readonly menu = inject<UiMenu>(forwardRef(() => UiMenu));
187
+ protected readonly group = inject(UiMenuGroup, { optional: true });
188
+ protected readonly button = this.el.localName === 'button';
189
+ protected readonly formOff = signal(false);
190
+ /** Just unticked: the label is back at rest until the pointer moves or focus comes back. */
191
+ protected readonly quiet = signal(false);
192
+ readonly descId = `ui-menu-desc-${nextId++}`;
193
+ /** `true` or `false` on checkbox and radio rows, `null` on plain ones. */
194
+ readonly on = computed<boolean | null>(() => this.state());
195
+ readonly off = computed(() => this.disabled() || this.formOff() || !!this.group?.off());
196
+ readonly expanded = computed(() => {
197
+ const sub = this.submenu();
198
+ return !!sub && sub.open() && sub.owner() === this;
199
+ });
200
+
201
+ protected state(): boolean | null {
202
+ return null;
203
+ }
204
+
205
+ protected toggle() {}
206
+
207
+ /** The row's own text, without its description or shortcut. */
208
+ label() {
209
+ return this.el.querySelector('.ui-menu-name')?.textContent?.trim() ?? '';
210
+ }
211
+
212
+ skipped() {
213
+ return this.off() || (this.el as HTMLButtonElement).disabled === true;
214
+ }
215
+
216
+ protected clicked(e: MouseEvent) {
217
+ if (this.skipped()) {
218
+ e.preventDefault();
219
+ return;
220
+ }
221
+ const sub = this.submenu();
222
+ if (sub) {
223
+ e.preventDefault();
224
+ sub.openFrom(this, e.detail === 0 ? 'first' : 'none');
225
+ return;
226
+ }
227
+ this.toggle();
228
+ this.quiet.set(this.on() === false);
229
+ this.selected.emit();
230
+ if (!this.keepOpen()) this.menu.closeAll('select');
231
+ }
232
+
233
+ protected moved(e: PointerEvent) {
234
+ this.quiet.set(false);
235
+ if (this.menu.fade()) return;
236
+ this.menu.hover(this, e);
237
+ }
238
+
239
+ protected focused() {
240
+ this.quiet.set(false);
241
+ this.menu.active.set(this);
242
+ }
243
+
244
+ protected measure() {
245
+ const name = this.el.querySelector<HTMLElement>('.ui-menu-name');
246
+ if (!name) return;
247
+ if (name.scrollWidth > name.clientWidth) name.title = name.textContent?.trim() ?? '';
248
+ else name.removeAttribute('title');
249
+ }
250
+ }
251
+
252
+ /** An action row: `<button uiMenuItem>` or `<a uiMenuItem routerLink>`. */
253
+ @Component({
254
+ selector: 'button[uiMenuItem], a[uiMenuItem]',
255
+ providers: [{ provide: MenuItem, useExisting: forwardRef(() => UiMenuItem) }],
256
+ host: { role: 'menuitem' },
257
+ template: ROW,
258
+ })
259
+ export class UiMenuItem extends MenuItem {}
260
+
261
+ /** An on/off row with a check at the end: `<button uiMenuItemCheckbox [(checked)]="on" keepOpen>`. Also takes forms. */
262
+ @Component({
263
+ selector: 'button[uiMenuItemCheckbox]',
264
+ providers: [
265
+ { provide: MenuItem, useExisting: forwardRef(() => UiMenuItemCheckbox) },
266
+ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiMenuItemCheckbox), multi: true },
267
+ ],
268
+ host: { role: 'menuitemcheckbox' },
269
+ template: ROW,
270
+ })
271
+ export class UiMenuItemCheckbox extends MenuItem implements ControlValueAccessor {
272
+ /** Bind `[(checked)]`, `[formField]`, `formControlName` or `ngModel`. */
273
+ readonly checked = model(false);
274
+ private onChange: (v: boolean) => void = () => {};
275
+ private onTouched: () => void = () => {};
276
+
277
+ protected override state() {
278
+ return this.checked();
279
+ }
280
+
281
+ protected override toggle() {
282
+ const v = !this.checked();
283
+ this.checked.set(v);
284
+ this.onChange(v);
285
+ this.onTouched();
286
+ }
287
+
288
+ writeValue(v: unknown) {
289
+ this.checked.set(!!v);
290
+ }
291
+ registerOnChange(fn: (v: boolean) => void) {
292
+ this.onChange = fn;
293
+ }
294
+ registerOnTouched(fn: () => void) {
295
+ this.onTouched = fn;
296
+ }
297
+ setDisabledState(off: boolean) {
298
+ this.formOff.set(off);
299
+ }
300
+ }
301
+
302
+ /** One choice of a `<ui-menu-group>`: `<button uiMenuItemRadio [value]="'name'">Name</button>`. */
303
+ @Component({
304
+ selector: 'button[uiMenuItemRadio]',
305
+ providers: [{ provide: MenuItem, useExisting: forwardRef(() => UiMenuItemRadio) }],
306
+ host: { role: 'menuitemradio' },
307
+ template: ROW,
308
+ })
309
+ export class UiMenuItemRadio extends MenuItem {
310
+ readonly value = input.required<unknown>();
311
+
312
+ protected override state() {
313
+ return !!this.group && Object.is(this.group.value(), this.value());
314
+ }
315
+
316
+ protected override toggle() {
317
+ this.group?.pick(this.value());
318
+ }
319
+ }
320
+
321
+ /** The panel. Write it right after its trigger; it is promoted to the top layer while open. */
322
+ @Component({
323
+ selector: 'ui-menu',
324
+ exportAs: 'uiMenu',
325
+ host: {
326
+ popover: 'manual',
327
+ '[id]': 'id()',
328
+ '[attr.data-sub]': 'owner() ? "" : null',
329
+ '[attr.data-fade]': 'fade() ? "" : null',
330
+ '[style.--_w]': 'size()',
331
+ '(pointerenter)': 'owner()?.menu?.hold()',
332
+ '(contextmenu)': 'host && $event.preventDefault()',
333
+ },
334
+ template: `
335
+ @if (title()) {<span class="ui-menu-label" aria-hidden="true" [id]="id() + '-title'">{{ title() }}</span>}
336
+ <ng-content select="[uiMenuHeader]" />
337
+ <div #list class="list" role="menu" tabindex="-1" aria-orientation="vertical" [id]="listId()"
338
+ [attr.aria-labelledby]="labelledBy()" [attr.aria-label]="name()"
339
+ (keydown)="key($event)" (pointerleave)="leave()" (focus)="active.set(null)">
340
+ <span #hl class="ui-menu-hl" aria-hidden="true" [attr.data-danger]="active()?.danger() || null"></span>
341
+ <ng-content />
342
+ @if (!items().length) {<span class="empty" role="menuitem" aria-disabled="true">{{ l().empty }}</span>}
343
+ </div>
344
+ `,
345
+ styles: `
346
+ :host {
347
+ --_t: var(--ui-look-tray, 0);
348
+ --_p: var(--ui-menu-pad, calc(var(--ui-space-1) + 1px * var(--_t)));
349
+ --_d: var(--ui-space-1);
350
+ --_y: 0;
351
+ position: fixed;
352
+ inset: auto;
353
+ z-index: var(--ui-z-popover);
354
+ box-sizing: border-box;
355
+ flex-direction: column;
356
+ inline-size: var(--_w, max-content);
357
+ min-inline-size: min(var(--ui-menu-min-width, calc(168px + 12px * var(--_t))), calc(100vw - 16px));
358
+ max-inline-size: min(var(--ui-menu-max-width, 320px), calc(100vw - 16px));
359
+ max-block-size: calc(100dvh - 16px);
360
+ margin: 0;
361
+ padding: var(--_p);
362
+ overflow: hidden;
363
+ border: 0;
364
+ outline: 1px solid transparent;
365
+ border-radius: var(--ui-menu-radius, calc(var(--ui-radius-lg) + 2px * var(--_t)));
366
+ background: var(--ui-menu-bg, var(--ui-bg-overlay));
367
+ box-shadow: var(--ui-menu-shadow,
368
+ 0 0 0 calc(0.5px + 0.5px * var(--_t)) color-mix(in srgb, var(--ui-text) calc(15% - 6% * var(--_t)), transparent),
369
+ 0 calc(12px + 6px * var(--_t)) calc(32px + 8px * var(--_t)) calc(-8px - 4px * var(--_t)) color-mix(in srgb, var(--ui-bg-scrim) 45%, transparent),
370
+ 0 4px 12px -4px color-mix(in srgb, var(--ui-bg-scrim) calc(20% * var(--_t)), transparent));
371
+ color: var(--ui-text);
372
+ font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
373
+ text-align: start;
374
+ transition: calc(220ms * var(--ui-motion-scale)) var(--ui-ease-out);
375
+ transition-property: opacity, scale, translate;
376
+ }
377
+ :host([data-open]) { display: flex; }
378
+ :host(:not([data-open])) { display: none !important; }
379
+ :host([data-side='top']) { --_y: var(--_d); }
380
+ :host([data-side='bottom']) { --_y: calc(var(--_d) * -1); }
381
+ :host([data-sub]) { --_y: 0px; margin-block-start: calc(var(--_p) * -1); transition-duration: calc(160ms * var(--ui-motion-scale)); }
382
+ :host([data-enter]) { opacity: 0; scale: 0.96; translate: 0 var(--_y); transition: none; }
383
+ :host([data-sub][data-enter]) { scale: 0.98; }
384
+ :host([data-sub]:not([data-side='left'], [data-side='right'])) { margin-inline-start: var(--ui-space-2); }
385
+ .list {
386
+ position: relative;
387
+ display: grid;
388
+ gap: 1px;
389
+ min-block-size: 0;
390
+ overflow-y: auto;
391
+ overscroll-behavior: contain;
392
+ scrollbar-width: thin;
393
+ outline: none;
394
+ }
395
+ .empty { padding: var(--ui-space-1-5) var(--ui-space-2-5); color: var(--ui-text-muted); }
396
+ :host([data-fade]) ::ng-deep .ui-menu-item { transition-property: color, background-color; }
397
+ :host([data-fade]) ::ng-deep .ui-menu-item[data-active] { background: rgb(from var(--ui-text) r g b / calc(0.036 + 0.011 * var(--_t) + (0.024 - 0.011 * var(--_t)) * r / 255)); }
398
+ @media (hover: hover) { :host([data-fade]) ::ng-deep .ui-menu-item:hover:not(:disabled, [aria-disabled='true']) { background: rgb(from var(--ui-text) r g b / calc(0.036 + 0.011 * var(--_t) + (0.024 - 0.011 * var(--_t)) * r / 255)); } }
399
+ :host([data-fade]) ::ng-deep .ui-menu-item[data-danger][data-active] { background: var(--ui-danger-soft); }
400
+ @media (hover: hover) {
401
+ :host([data-fade]) ::ng-deep .ui-menu-item[data-danger]:hover:not(:disabled, [aria-disabled='true']) { background: var(--ui-danger-soft); }
402
+ :host([data-fade]) ::ng-deep .ui-menu-item:hover:not([data-danger], :disabled, [aria-disabled='true']) { color: var(--ui-text); }
403
+ }
404
+ @media (forced-colors: active) {
405
+ :host([data-fade]) ::ng-deep .ui-menu-item[data-active] { forced-color-adjust: none; background: Highlight; }
406
+ }
407
+ `,
408
+ })
409
+ export class UiMenu {
410
+ /** Shows it. Bind `[(open)]`, or let the trigger open and close it. */
411
+ readonly open = model(false);
412
+ /** Preferred side. It flips when the other side has more room. */
413
+ readonly side = input<MenuSide>('bottom');
414
+ /** Which edges of menu and trigger line up: start is the left edge, or the right one in right-to-left pages. */
415
+ readonly align = input<MenuAlign>('start');
416
+ /** Gap to the trigger, in px. */
417
+ readonly offset = input(6, { transform: numberAttribute });
418
+ /** A small grey heading above the rows, and the menu's accessible name. */
419
+ readonly title = input<string>();
420
+ /** Panel width: px or any CSS length. Default: fits the rows, 168 to 320px. */
421
+ readonly width = input<number | string>();
422
+ /** Names the menu when there is no title. Default: the trigger's name. */
423
+ readonly ariaLabel = input<string>(undefined, { alias: 'aria-label' });
424
+ /** Every visible and spoken string. Pass only the ones you change. */
425
+ readonly labels = input<Partial<MenuLabels>>({});
426
+ /** Each row fades its own wash in and out, instead of one highlight gliding from row to row. Hover leaves focus where it is; no typeahead. */
427
+ readonly fade = input(false, { transform: booleanAttribute });
428
+ readonly id = input(`ui-menu-${nextId++}`);
429
+ /** Why it closed by itself, just before `open` turns false. */
430
+ readonly closed = output<MenuCloseReason>();
431
+
432
+ readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
433
+ readonly listId = computed(() => `${this.id()}-list`);
434
+ /** The highlighted row. */
435
+ readonly active = signal<MenuItem | null>(null);
436
+ /** The row that opened it, while it is open as a submenu. */
437
+ readonly owner = signal<MenuItem | null>(null);
438
+ /** Its open submenu. */
439
+ readonly child = signal<UiMenu | null>(null);
440
+ readonly trigger = signal<HTMLElement | null>(null);
441
+
442
+ protected readonly l = computed(() => ({ ...LABELS, ...this.labels() }));
443
+ private readonly all = contentChildren(MenuItem, { descendants: true });
444
+ readonly items = computed(() => this.all().filter((i) => i.menu === this));
445
+ private readonly list = viewChild.required<ElementRef<HTMLElement>>('list');
446
+ private readonly hl = viewChild.required<ElementRef<HTMLElement>>('hl');
447
+ private readonly ownerName = signal('');
448
+ protected readonly size = computed(() => {
449
+ const w = this.width();
450
+ return typeof w === 'number' ? `${w}px` : w || null;
451
+ });
452
+ protected readonly labelledBy = computed(() =>
453
+ this.title() ? `${this.id()}-title` : this.ariaLabel() || this.owner() ? null : (this.trigger()?.id ?? null),
454
+ );
455
+ protected readonly name = computed(() =>
456
+ this.title() ? null : this.ariaLabel() || (this.owner() ? this.ownerName() : (this.host?.label() ?? null)),
457
+ );
458
+
459
+ protected readonly host = inject(MENU_HOST, { optional: true });
460
+
461
+ private readonly doc = inject(DOCUMENT);
462
+ /** The point it opened at, for a context menu. */
463
+ private at: { x: number; y: number } | null = null;
464
+ private live = false;
465
+ private readonly overlay = isPlatformBrowser(inject(PLATFORM_ID))
466
+ ? new OverlayRef(this.el, this.doc, {
467
+ anchor: () => this.owner()?.el ?? this.trigger() ?? (this.at ? null : this.el.parentElement),
468
+ trigger: () => this.owner()?.el ?? this.trigger(),
469
+ side: () => (this.owner() ? (this.below() ? 'bottom' : this.rtl() ? 'left' : 'right') : this.side()),
470
+ align: () => (this.owner() ? 'start' : this.align()),
471
+ offset: () => (this.owner() ? this.gap() : this.offset()),
472
+ dismiss: (reason) => this.close(reason),
473
+ })
474
+ : null;
475
+ private how: MenuFocus = 'menu';
476
+ private by: 'key' | 'pointer' = 'key';
477
+ private hovered: MenuItem | null = null;
478
+ private last?: { x: number; y: number };
479
+ private timer?: ReturnType<typeof setTimeout>;
480
+ private typed = '';
481
+ private typedAt = 0;
482
+ private lit = false;
483
+ private first = true;
484
+ private warned = false;
485
+
486
+ constructor() {
487
+ afterRenderEffect(() => {
488
+ const on = this.open();
489
+ untracked(() => this.sync(on));
490
+ });
491
+ afterRenderEffect(() => {
492
+ const item = this.active();
493
+ untracked(() => this.glide(item));
494
+ });
495
+ afterRenderEffect(() => {
496
+ this.side();
497
+ this.align();
498
+ this.offset();
499
+ this.size();
500
+ untracked(() => this.overlay?.place());
501
+ });
502
+ const leave = this.host?.register(this);
503
+ inject(DestroyRef).onDestroy(() => {
504
+ leave?.();
505
+ clearTimeout(this.timer);
506
+ this.overlay?.hide(false);
507
+ });
508
+ }
509
+
510
+ /** Opens it with its corner at a point of the viewport, flipping to stay 8px inside. Used by `[uiContextMenu]`. */
511
+ openAt(x: number, y: number, how: MenuFocus = 'menu') {
512
+ this.at = { x, y };
513
+ this.how = how;
514
+ this.first = false;
515
+ this.open.set(true);
516
+ this.sync(true);
517
+ }
518
+
519
+ /** Opens it and moves focus: `first` (the checked row, else the first), `last`, `menu` or `none`. */
520
+ openWith(how: MenuFocus = 'first') {
521
+ this.how = how;
522
+ if (this.open()) return this.focusStart(how);
523
+ this.first = false;
524
+ this.open.set(true);
525
+ this.sync(true);
526
+ }
527
+
528
+ toggle(how: MenuFocus = 'first') {
529
+ if (this.open()) this.close('trigger');
530
+ else this.openWith(how);
531
+ }
532
+
533
+ /** Closes it and its submenus. Focus goes back to the trigger if it was inside, unless the reason is a press or focus elsewhere. */
534
+ close(reason: MenuCloseReason = 'trigger', restore = reason !== 'outside' && reason !== 'focus-out') {
535
+ if (!this.open()) return;
536
+ this.shut(restore, reason);
537
+ this.closed.emit(reason);
538
+ this.open.set(false);
539
+ if (!this.owner()) this.host?.closed(this);
540
+ }
541
+
542
+ /** Closes the whole tree, from the top menu down. */
543
+ closeAll(reason: MenuCloseReason) {
544
+ let top: UiMenu = this;
545
+ for (let o = top.owner(); o; o = top.owner()) top = o.menu;
546
+ top.close(reason);
547
+ }
548
+
549
+ /** Called by a row with `[uiSubmenu]`. */
550
+ openFrom(item: MenuItem, how: MenuFocus) {
551
+ const parent = item.menu;
552
+ clearTimeout(parent.timer);
553
+ if (this.open() && this.owner() === item) {
554
+ if (how !== 'none') this.focusStart(how);
555
+ return;
556
+ }
557
+ this.close('trigger', false);
558
+ parent.child()?.close('trigger', false);
559
+ this.how = how;
560
+ this.owner.set(item);
561
+ this.ownerName.set(item.label());
562
+ parent.child.set(this);
563
+ this.first = false;
564
+ this.open.set(true);
565
+ this.sync(true);
566
+ }
567
+
568
+ /** Called by `uiMenuTrigger`. */
569
+ registerTrigger(el: HTMLElement | null) {
570
+ this.trigger.set(el);
571
+ }
572
+
573
+ /** Places it again, after a change of size it cannot see. */
574
+ reposition() {
575
+ this.overlay?.place();
576
+ }
577
+
578
+ /** A row under the pointer: highlight it, and open or close submenus with a short delay. */
579
+ hover(item: MenuItem, e: PointerEvent) {
580
+ if (e.pointerType === 'touch') return;
581
+ const aiming = this.aim(e);
582
+ this.owner()?.menu.hold();
583
+ const child = this.child();
584
+ const changed = item !== this.hovered;
585
+ this.hovered = item;
586
+ if (child && child.owner() !== item) {
587
+ if (changed || aiming) this.later(() => this.settle(), aiming ? 300 : 250);
588
+ return;
589
+ }
590
+ if (!changed && (this.active() === item || item.skipped())) return;
591
+ this.later();
592
+ if (item.skipped()) return this.leave();
593
+ this.pick(item, 'pointer');
594
+ const sub = item.submenu();
595
+ if (sub && !child) this.later(() => sub.openFrom(item, 'none'), 100);
596
+ }
597
+
598
+ /** The pointer is over this menu's open submenu: keep it. */
599
+ hold() {
600
+ this.later();
601
+ this.hovered = this.child()?.owner() ?? null;
602
+ }
603
+
604
+ protected leave() {
605
+ if (this.child() || this.by !== 'pointer') return;
606
+ this.later();
607
+ this.hovered = null;
608
+ if (!this.active()) return;
609
+ this.active.set(null);
610
+ this.glide(null);
611
+ this.list().nativeElement.focus({ preventScroll: true });
612
+ }
613
+
614
+ protected key(e: KeyboardEvent) {
615
+ if (e.isComposing || e.ctrlKey || e.metaKey || e.altKey) return;
616
+ const items = this.items().filter((i) => !i.skipped());
617
+ const cur = this.active();
618
+ const n = items.length;
619
+ const i = cur ? items.indexOf(cur) : -1;
620
+ const typing = this.typed !== '' && Date.now() - this.typedAt < TYPEAHEAD_MS;
621
+ const [fwd, back] = this.rtl() ? ['ArrowLeft', 'ArrowRight'] : ['ArrowRight', 'ArrowLeft'];
622
+ let to: MenuItem | undefined;
623
+ if (e.key.length === 1 && (e.key !== ' ' || typing)) {
624
+ e.preventDefault();
625
+ if (!this.fade()) to = this.find(items, i, e.key);
626
+ } else if (e.key === 'Tab') {
627
+ this.closeAll('tab');
628
+ return;
629
+ } else if (e.key === 'F10' ? e.shiftKey : e.key === 'ContextMenu') to = items[0];
630
+ else if (e.key === 'ArrowDown') to = items[(i + 1) % n];
631
+ else if (e.key === 'ArrowUp') to = items[i < 0 ? n - 1 : (i - 1 + n) % n];
632
+ else if (e.key === 'Home' || e.key === 'PageUp') to = items[0];
633
+ else if (e.key === 'End' || e.key === 'PageDown') to = items[n - 1];
634
+ else if (e.key === fwd && cur?.submenu() && !cur.skipped()) {
635
+ e.preventDefault();
636
+ cur.submenu()!.openFrom(cur, 'first');
637
+ return;
638
+ } else if (e.key === back && this.owner()) {
639
+ e.preventDefault();
640
+ this.close('escape');
641
+ return;
642
+ } else if (e.key === 'Enter' || e.key === ' ') {
643
+ e.preventDefault();
644
+ cur?.el.click();
645
+ return;
646
+ } else return;
647
+ e.preventDefault();
648
+ if (to) this.pick(to, 'key');
649
+ }
650
+
651
+ private find(items: MenuItem[], i: number, key: string) {
652
+ const now = Date.now();
653
+ this.typed = (now - this.typedAt > TYPEAHEAD_MS ? '' : this.typed) + key.toLowerCase();
654
+ this.typedAt = now;
655
+ const same = [...this.typed].every((c) => c === this.typed[0]);
656
+ const q = same ? this.typed[0] : this.typed;
657
+ const from = same ? i + 1 : Math.max(i, 0);
658
+ for (let k = 0; k < items.length; k++) {
659
+ const item = items[(from + k) % items.length];
660
+ if ((item.textValue() ?? item.label()).trim().toLowerCase().startsWith(q)) return item;
661
+ }
662
+ return undefined;
663
+ }
664
+
665
+ private pick(item: MenuItem, by: 'key' | 'pointer') {
666
+ this.by = by;
667
+ this.active.set(item);
668
+ item.el.focus({ preventScroll: true, focusVisible: by === 'key' } as FocusOptions);
669
+ if (by === 'key') item.el.scrollIntoView?.({ block: 'nearest' });
670
+ }
671
+
672
+ private focusStart(how: MenuFocus) {
673
+ const items = this.items().filter((i) => !i.skipped());
674
+ const to = how === 'first' ? (items.find((i) => i.on()) ?? items[0]) : how === 'last' ? items.at(-1) : undefined;
675
+ if (to) this.pick(to, 'key');
676
+ else if (how !== 'none') {
677
+ this.by = 'pointer';
678
+ this.active.set(null);
679
+ this.list().nativeElement.focus({ preventScroll: true });
680
+ }
681
+ }
682
+
683
+ private sync(on: boolean) {
684
+ const focus = !this.first;
685
+ this.first = false;
686
+ if (!this.overlay || on === this.live) return;
687
+ if (!on) return this.shut(true);
688
+ this.live = true;
689
+ if (this.at && !this.owner()) this.pin(this.at);
690
+ this.overlay.show(false);
691
+ for (const a of this.owner()?.menu.el.getAnimations?.() ?? []) a.finished.then(() => this.overlay?.place(), () => {});
692
+ const anchor = this.owner()?.el ?? this.trigger();
693
+ if (anchor) this.el.style.setProperty('--ui-menu-trigger-w', `${anchor.offsetWidth}px`);
694
+ if (focus) this.focusStart(this.how);
695
+ this.how = 'menu';
696
+ this.warn();
697
+ }
698
+
699
+ /** Shows it hidden behind the enter state and places its corner at the point, so the motion grows from there. */
700
+ private pin(at: { x: number; y: number }) {
701
+ const { el } = this;
702
+ const root = this.doc.documentElement;
703
+ el.setAttribute('data-open', '');
704
+ el.setAttribute('data-enter', '');
705
+ if (el.showPopover && !el.matches(':popover-open')) el.showPopover();
706
+ const spot = placeOverlay(
707
+ { top: at.y, bottom: at.y, left: at.x, right: at.x, width: 0, height: 0 },
708
+ { width: el.offsetWidth, height: el.offsetHeight - el.clientHeight + el.scrollHeight },
709
+ { width: root.clientWidth, height: root.clientHeight },
710
+ 'bottom',
711
+ 'start',
712
+ 0,
713
+ this.rtl(),
714
+ );
715
+ Object.assign(el.style, { left: `${spot.x}px`, top: `${spot.y}px`, maxHeight: `${spot.maxHeight}px`, transformOrigin: spot.origin });
716
+ el.dataset['side'] = spot.side;
717
+ el.dataset['align'] = spot.align;
718
+ }
719
+
720
+ private shut(restore: boolean, reason: MenuCloseReason = 'trigger') {
721
+ this.live = false;
722
+ this.later();
723
+ this.hovered = null;
724
+ this.typed = '';
725
+ const child = this.child();
726
+ if (child) {
727
+ const deep = restore && child.holds(this.doc.activeElement);
728
+ child.close(reason, false);
729
+ if (deep) this.list().nativeElement.focus({ preventScroll: true });
730
+ }
731
+ this.overlay?.hide(restore);
732
+ this.active.set(null);
733
+ const parent = this.owner()?.menu;
734
+ if (parent?.child() === this) parent.child.set(null);
735
+ }
736
+
737
+ /** Whether `node` is inside this menu or one of its open submenus. */
738
+ holds(node: Node | null): boolean {
739
+ return !!node && (this.el.contains(node) || !!this.child()?.holds(node));
740
+ }
741
+
742
+ private settle() {
743
+ this.child()?.close('focus-out');
744
+ const item = this.hovered;
745
+ if (!item || item.skipped()) return;
746
+ this.pick(item, 'pointer');
747
+ const sub = item.submenu();
748
+ if (sub) this.later(() => sub.openFrom(item, 'none'), 100);
749
+ }
750
+
751
+ private later(fn?: () => void, ms = 0) {
752
+ clearTimeout(this.timer);
753
+ this.timer = fn ? setTimeout(fn, ms) : undefined;
754
+ }
755
+
756
+ /** Whether the pointer is heading for the open submenu: inside the triangle from its last spot to the submenu's near edge. */
757
+ private aim(e: PointerEvent) {
758
+ const a = this.last;
759
+ const p = { x: e.clientX, y: e.clientY };
760
+ this.last = p;
761
+ const r = this.child()?.el.getBoundingClientRect();
762
+ if (!a || !r) return false;
763
+ const across = a.x < r.left || a.x > r.right;
764
+ const x = a.x < r.left ? r.left : r.right;
765
+ const y = a.y < r.top ? r.top : r.bottom;
766
+ const [b, c] = across ? [{ x, y: r.top }, { x, y: r.bottom }] : [{ x: r.left, y }, { x: r.right, y }];
767
+ const side = (u: typeof p, v: typeof p) => Math.sign((v.x - u.x) * (p.y - u.y) - (v.y - u.y) * (p.x - u.x));
768
+ const s = [side(a, b), side(b, c), side(c, a)];
769
+ return !(s.includes(1) && s.includes(-1));
770
+ }
771
+
772
+ private glide(item: MenuItem | null) {
773
+ const hl = this.hl().nativeElement;
774
+ if (!item || !this.open() || this.fade()) {
775
+ hl.style.opacity = '0';
776
+ this.lit = false;
777
+ return;
778
+ }
779
+ if (!this.lit) hl.style.transition = 'none';
780
+ hl.style.translate = `0 ${item.el.offsetTop}px`;
781
+ hl.style.blockSize = `${item.el.offsetHeight}px`;
782
+ hl.style.opacity = '1';
783
+ if (!this.lit) {
784
+ void hl.offsetWidth;
785
+ hl.style.transition = '';
786
+ }
787
+ this.lit = true;
788
+ }
789
+
790
+ private rtl() {
791
+ const at = this.owner()?.el ?? this.el;
792
+ return at.closest('[dir]')?.getAttribute('dir') === 'rtl' || this.doc.defaultView?.getComputedStyle(at).direction === 'rtl';
793
+ }
794
+
795
+ /** A submenu's distance from its row: beside it, or below it when neither side has room, moved up only as far as it must to fit. */
796
+ private gap() {
797
+ const owner = this.owner();
798
+ if (!owner) return this.offset();
799
+ const r = owner.el.getBoundingClientRect();
800
+ if (!this.below()) return this.beside();
801
+ const h = this.el.offsetHeight - this.el.clientHeight + this.el.scrollHeight;
802
+ return Math.max(8 - r.bottom, Math.min(this.beside(), this.doc.documentElement.clientHeight - 8 - r.bottom - h));
803
+ }
804
+
805
+ private beside() {
806
+ const owner = this.owner()!;
807
+ const p = owner.menu.el.getBoundingClientRect();
808
+ const r = owner.el.getBoundingClientRect();
809
+ return Math.max(p.right - r.right, r.left - p.left) + 4;
810
+ }
811
+
812
+ /** Whether neither side of its row has room for the submenu, as on a phone. */
813
+ private below() {
814
+ const owner = this.owner();
815
+ if (!owner) return false;
816
+ const r = owner.el.getBoundingClientRect();
817
+ const room = this.el.offsetWidth + this.beside() + 8;
818
+ return r.left < room && this.doc.documentElement.clientWidth - r.right < room;
819
+ }
820
+
821
+ private warn() {
822
+ if (!isDevMode() || this.warned || this.owner()) return;
823
+ const t = this.trigger();
824
+ const named = this.title() || this.ariaLabel() || t?.textContent?.trim() || t?.getAttribute('aria-label') || t?.hasAttribute('aria-labelledby');
825
+ if (!named) console.warn(`ui-menu: give it a title or an aria-label${this.host ? '' : ', or give its trigger a name'}.`, this.el);
826
+ for (const item of this.items()) {
827
+ if (!item.label() && !item.el.hasAttribute('aria-label')) console.warn('ui-menu: a row with only an icon needs an aria-label.', item.el);
828
+ }
829
+ this.warned = true;
830
+ }
831
+ }
832
+
833
+ /** Put it on the button that opens the menu: `[uiMenuTrigger]="menu"`. Click, Enter, Space, ↓ and ↑ open it. */
834
+ @Directive({
835
+ selector: '[uiMenuTrigger]',
836
+ exportAs: 'uiMenuTrigger',
837
+ host: {
838
+ 'aria-haspopup': 'menu',
839
+ '[id]': 'id()',
840
+ '[attr.aria-expanded]': 'uiMenuTrigger().open()',
841
+ '[attr.aria-controls]': 'uiMenuTrigger().open() ? uiMenuTrigger().listId() : null',
842
+ '(click)': 'uiMenuTrigger().toggle($event.detail === 0 ? "first" : "menu")',
843
+ '(keydown)': 'key($event)',
844
+ },
845
+ })
846
+ export class UiMenuTrigger {
847
+ readonly uiMenuTrigger = input.required<UiMenu>();
848
+ readonly id = input(`ui-menu-trigger-${nextId++}`);
849
+ private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
850
+
851
+ constructor() {
852
+ effect((onCleanup) => {
853
+ const menu = this.uiMenuTrigger();
854
+ menu.registerTrigger(this.el);
855
+ onCleanup(() => menu.registerTrigger(null));
856
+ });
857
+ }
858
+
859
+ protected key(e: KeyboardEvent) {
860
+ if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') return;
861
+ e.preventDefault();
862
+ this.uiMenuTrigger().openWith(e.key === 'ArrowUp' ? 'last' : 'first');
863
+ }
864
+ }