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,577 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Dialog, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (`npx ochreui init`), button.ts (`npx ochreui add button`) and overlay.ts (`npx ochreui add overlay`).
5
+ *
6
+ * <button uiButton variant="secondary" [uiDialogTrigger]="edit">Edit</button>
7
+ * <ui-dialog #edit [(open)]="editing" heading="Edit report" description="…">
8
+ * your content
9
+ * <div uiDialogFooter><button uiButton variant="secondary" uiDialogClose>Cancel</button> …</div>
10
+ * </ui-dialog>
11
+ * <ui-alert-dialog heading="Delete it?" confirmLabel="Delete" danger [action]="remove" />
12
+ *
13
+ * A native <dialog> with a fixed header and footer around scrolling content. It traps focus, locks page scroll,
14
+ * closes on Escape, the dim or the ×, and returns focus. Under about 520px it becomes a bottom sheet.
15
+ *
16
+ * Public styling hooks (set them on the element or any ancestor):
17
+ * --ui-dialog-scrim the dim (default: 40% of the theme scrim)
18
+ * --ui-dialog-radius card corners (default 14px; Classic 20px with a 16px inner panel)
19
+ * --ui-dialog-shadow card ring and drop shadow
20
+ */
21
+ import {
22
+ Component,
23
+ DOCUMENT,
24
+ DestroyRef,
25
+ Directive,
26
+ ElementRef,
27
+ afterRenderEffect,
28
+ booleanAttribute,
29
+ computed,
30
+ inject,
31
+ input,
32
+ isDevMode,
33
+ model,
34
+ output,
35
+ signal,
36
+ untracked,
37
+ viewChild,
38
+ } from '@angular/core';
39
+ import { ButtonSize, UiButton } from './button';
40
+ import { OverlayLayer, TABBABLE, overlayStack } from './overlay';
41
+
42
+ /** Page scroll lock, counted per document so nested dialogs (and copies of this file) share it. */
43
+ const LOCK = Symbol.for('ochre.scroll-lock');
44
+ function lockScroll(doc: Document): () => void {
45
+ const root = doc.documentElement;
46
+ const state = ((doc as unknown as Record<symbol, { n: number; overflow: string; gutter: string }>)[LOCK] ??= { n: 0, overflow: '', gutter: '' });
47
+ if (!state.n++) {
48
+ state.overflow = root.style.overflow;
49
+ state.gutter = root.style.scrollbarGutter;
50
+ const bar = (doc.defaultView?.innerWidth ?? 0) - root.clientWidth;
51
+ root.style.overflow = 'hidden';
52
+ if (bar > 0) root.style.scrollbarGutter = 'stable';
53
+ }
54
+ let done = false;
55
+ return () => {
56
+ if (done) return;
57
+ done = true;
58
+ if (--state.n) return;
59
+ root.style.overflow = state.overflow;
60
+ root.style.scrollbarGutter = state.gutter;
61
+ };
62
+ }
63
+
64
+ const tabbables = (root: Element | undefined) =>
65
+ root
66
+ ? [...root.querySelectorAll<HTMLElement>(TABBABLE)].filter(
67
+ (n) => n.tabIndex >= 0 && !n.matches(':disabled, [inert] *') && n.checkVisibility?.() !== false,
68
+ )
69
+ : [];
70
+
71
+ export type DialogSize = 'sm' | 'md' | 'lg';
72
+ /** Why it closed: Escape, a press on the dim, the ×, a `uiDialogClose` or your code, a form with `method="dialog"`, or the alert's buttons. */
73
+ export type DialogCloseReason = 'escape' | 'backdrop' | 'close-button' | 'action' | 'cancel' | 'confirm';
74
+
75
+ export interface DialogLabels {
76
+ /** The ×'s accessible name. */
77
+ close: string;
78
+ /** Announced while `busy`. */
79
+ busy: string;
80
+ }
81
+ const LABELS: DialogLabels = { close: 'Close', busy: 'Working…' };
82
+
83
+ const space = (n: string) => `var(--ui-space-${n})`;
84
+ const WIDTHS: Record<DialogSize, string> = { sm: `calc(${space('10')} * 10)`, md: `calc(${space('10')} * 12)`, lg: `calc(${space('10')} * 18)` };
85
+ /** Static layout, kept inline so the stylesheet stays small. */
86
+ const LAYER =
87
+ '--_t: var(--ui-look-tray, 0); inset: 0; z-index: var(--ui-z-modal); box-sizing: border-box; width: auto; height: auto; ' +
88
+ 'max-width: none; max-height: none; margin: 0; padding: 0; border: 0; overflow: hidden; overscroll-behavior: contain; ' +
89
+ 'container-type: inline-size; background: var(--ui-dialog-scrim, color-mix(in srgb, var(--ui-bg-scrim) 40%, transparent)); ' +
90
+ 'color: var(--ui-text); font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans); text-align: start';
91
+ const PANEL =
92
+ 'display: flex; flex-direction: column; min-height: 0; border-radius: calc(var(--_dlg-r) - var(--_dlg-p)); background: var(--ui-bg-overlay); ' +
93
+ 'box-shadow: 0 0 0 calc(1px * var(--_t)) var(--ui-border), ' +
94
+ '0 calc(1px * var(--_t)) calc(2px * var(--_t)) color-mix(in srgb, var(--ui-gray-1000) calc(6% * var(--_t)), transparent)';
95
+ const TEXT = 'display: grid; flex: 1; gap: var(--ui-space-1); min-width: 0; overflow-wrap: anywhere';
96
+ const TITLE = 'margin: 0; font: var(--ui-weight-medium) calc(var(--ui-text-md) + 1px) / var(--ui-leading-md) var(--ui-font-sans); letter-spacing: calc(-0.005em - 0.005em * var(--_t))';
97
+ const DESC = 'margin: 0; color: var(--ui-text-muted); font-size: calc(var(--ui-text-base) + 0.5px * var(--_t))';
98
+ const ICON = 'width: calc(var(--ui-icon-md) - 1px + 1px * var(--_t)); height: calc(var(--ui-icon-md) - 1px + 1px * var(--_t)); stroke-width: 1.8';
99
+
100
+ let nextId = 0;
101
+
102
+ @Component({
103
+ selector: 'ui-dialog',
104
+ exportAs: 'uiDialog',
105
+ host: { '[attr.id]': 'null', '[attr.role]': 'null', '[attr.title]': 'null' },
106
+ template: `
107
+ <dialog #dlg class="layer" style="${LAYER}" [style.position]="contained() ? 'absolute' : 'fixed'" [id]="id()"
108
+ [attr.role]="role() === 'alertdialog' ? role() : null"
109
+ [attr.aria-labelledby]="id() + '-title'" [attr.aria-describedby]="described() ? id() + '-desc' : null"
110
+ [attr.aria-modal]="contained() || null" [attr.aria-busy]="busy() || null"
111
+ (cancel)="onCancel($event)" (close)="onNativeClose()" (pointerdown)="press($event)"
112
+ (mousedown)="hold($event)" (click)="onClick($event)" (keydown)="onKey($event)">
113
+ <div class="frame">
114
+ <div #card class="card" tabindex="-1" [style.--_dlg-w]="w()">
115
+ <div style="${PANEL}">
116
+ <div class="head" [class.plain]="!closeButton()" [style.box-shadow]="top() ? '0 1px var(--ui-hairline)' : null">
117
+ <div style="${TEXT}">
118
+ <h2 style="${TITLE}" [id]="id() + '-title'" dir="auto">{{ heading() }}<ng-content select="[uiDialogHeading]" /></h2>
119
+ <p #desc class="desc" style="${DESC}" [id]="id() + '-desc'" dir="auto">{{ description() }}<ng-content select="[uiDialogDescription]" /></p>
120
+ </div>
121
+ @if (closeButton()) {
122
+ <button type="button" class="x ui-btn-icon" [attr.aria-label]="t().close" [attr.aria-disabled]="busy() || null" (click)="request('close-button')">
123
+ <svg class="ui-icon" style="${ICON}" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M6.5 6.5l11 11M17.5 6.5l-11 11" /></svg>
124
+ </button>
125
+ }
126
+ </div>
127
+ <div #body class="body" (scroll)="edges()"><ng-content /></div>
128
+ </div>
129
+ <div #foot class="foot" [style.box-shadow]="bottom() ? '0 -1px color-mix(in srgb, var(--ui-hairline) calc(100% - 100% * var(--_t)), transparent)' : null">
130
+ <ng-content select="[uiDialogFooter]" />
131
+ </div>
132
+ </div>
133
+ </div>
134
+ <span class="ui-sr" aria-live="polite">{{ busy() ? t().busy : '' }}</span>
135
+ </dialog>
136
+ `,
137
+ styles: `
138
+ :host { display: contents; }
139
+ .layer { animation: ui-fade-in calc(180ms * var(--ui-motion-scale)) var(--ui-ease-out); }
140
+ .layer::backdrop { background: none; }
141
+ .layer[data-leaving] { animation: ui-fade-in var(--ui-duration-3) var(--ui-ease-in) reverse forwards; }
142
+ .frame { display: flex; align-items: center; justify-content: center; box-sizing: border-box; height: 100%; padding: var(--ui-space-6) var(--ui-space-4); }
143
+ .card {
144
+ --_dlg-p: calc(var(--ui-space-1) * var(--_t));
145
+ --_dlg-r: var(--ui-dialog-radius, calc(var(--ui-radius-lg) + 2px + 6px * var(--_t)));
146
+ display: flex;
147
+ flex-direction: column;
148
+ box-sizing: border-box;
149
+ width: var(--_dlg-w);
150
+ max-width: 100%;
151
+ max-height: 100%;
152
+ padding: var(--_dlg-p);
153
+ border-radius: var(--_dlg-r);
154
+ background: color-mix(in srgb, var(--ui-text) calc(5% * var(--_t)), var(--ui-bg-overlay));
155
+ box-shadow: var(--ui-dialog-shadow, var(--ui-shadow-lg));
156
+ outline: 0;
157
+ animation: in calc((240ms + 20ms * var(--_t)) * var(--ui-motion-scale)) var(--ui-ease-out);
158
+ }
159
+ .card[data-pulse] { animation: pulse calc(240ms * var(--ui-motion-scale)) var(--ui-ease-out); }
160
+ [data-leaving] .card { animation: out var(--ui-duration-2) var(--ui-ease-in) forwards; }
161
+ .head {
162
+ position: relative;
163
+ display: flex;
164
+ align-items: flex-start;
165
+ gap: var(--ui-space-3);
166
+ padding: calc(var(--ui-space-4) + 2px * var(--_t)) 0 0;
167
+ padding-inline: var(--ui-space-5) calc(var(--ui-space-3) + 1px * var(--_t));
168
+ }
169
+ .plain { padding: var(--ui-space-5) var(--ui-space-5) 0; }
170
+ .desc:empty { display: none; }
171
+ .x { --_s: calc(var(--ui-space-6) + 4px + 4px * var(--_t)); margin-top: calc(-3px - 2px * var(--_t)); border-radius: calc(var(--ui-radius-sm) + 4px * var(--_t)); }
172
+ .x:focus-visible { outline-offset: var(--ui-focus-offset); }
173
+ .body { min-height: 0; padding: var(--ui-space-3) var(--ui-space-5) var(--ui-space-5); overflow-y: auto; overscroll-behavior: contain; }
174
+ .body:empty { padding-top: 0; }
175
+ .foot {
176
+ --_dlg-f: calc(var(--ui-space-4) - var(--ui-space-3) * var(--_t));
177
+ position: relative;
178
+ display: flex;
179
+ flex-wrap: wrap;
180
+ justify-content: flex-end;
181
+ gap: var(--ui-space-2);
182
+ padding: calc(var(--ui-space-2) * var(--_t)) var(--_dlg-f) var(--_dlg-f);
183
+ }
184
+ .foot:empty { display: none; }
185
+ .foot ::ng-deep > [uiDialogFooter]:not(button) { display: contents; }
186
+ @keyframes in { from { opacity: 0; transform: translateY(var(--ui-rise-distance)) scale(0.97); } }
187
+ @keyframes out { to { opacity: 0; transform: scale(0.98); } }
188
+ @keyframes pulse { 40% { transform: scale(1.015); } }
189
+ @keyframes up { from { transform: translateY(100%); } }
190
+ @container (max-width: 552px) {
191
+ .frame { align-items: flex-end; padding: var(--ui-space-6) 0 0; }
192
+ .card { width: 100%; border-end-start-radius: 0; border-end-end-radius: 0; animation: up var(--ui-duration-4) var(--ui-ease-out); }
193
+ [data-leaving] .card { animation: up var(--ui-duration-3) var(--ui-ease-in) reverse forwards; }
194
+ .foot { padding-bottom: max(var(--_dlg-f), env(safe-area-inset-bottom)); }
195
+ }
196
+ @media (pointer: coarse) {
197
+ .x { --_s: var(--ui-space-8); }
198
+ }
199
+ @media (forced-colors: active) {
200
+ .card { outline: 2px solid CanvasText; }
201
+ }
202
+ `,
203
+ })
204
+ export class UiDialog {
205
+ /** Shows it. Bind `[(open)]`; it turns false by itself on Escape, a press on the dim, the × or `uiDialogClose`. */
206
+ readonly open = model(false);
207
+ /** The title, and the dialog's accessible name. For rich content, project an element with `uiDialogHeading`. */
208
+ readonly heading = input<string>();
209
+ /** A quiet line under the title, read as the description. Or project an element with `uiDialogDescription`. */
210
+ readonly description = input<string>();
211
+ /** 400, 480 or 720px wide, never wider than the window. */
212
+ readonly size = input<DialogSize>('md');
213
+ /** Any width instead of a size: a number of px or a CSS length. */
214
+ readonly width = input<number | string>();
215
+ /** A press on the dim closes it. When off, the card pulses instead. */
216
+ readonly closeOnBackdrop = input(true, { transform: booleanAttribute });
217
+ /** The × in the corner. */
218
+ readonly closeButton = input(true, { transform: booleanAttribute });
219
+ /** Stays inside its nearest positioned parent instead of covering the window. The page around stays usable. */
220
+ readonly contained = input(false, { transform: booleanAttribute });
221
+ /** Gets focus on open. Default: `[autofocus]`, else the first control in the content, then in the footer, else the card. */
222
+ readonly initialFocus = input<HTMLElement | ElementRef<HTMLElement> | null>();
223
+ /** While true, Escape, the dim, the × and `uiDialogClose` do nothing, and screen readers hear `labels.busy`. */
224
+ readonly busy = input(false, { transform: booleanAttribute });
225
+ /** `alertdialog` for a confirmation; `ui-alert-dialog` sets it. */
226
+ readonly role = input<'dialog' | 'alertdialog'>('dialog');
227
+ readonly id = input(`ui-dialog-${nextId++}`);
228
+ readonly labels = input<Partial<DialogLabels>>({});
229
+ /** After it has closed and focus has gone back. Move focus yourself here if the trigger is gone. */
230
+ readonly closed = output<DialogCloseReason>();
231
+
232
+ protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
233
+ protected readonly w = computed(() => {
234
+ const w = this.width();
235
+ return typeof w === 'number' ? `${w}px` : w || WIDTHS[this.size()] || WIDTHS.md;
236
+ });
237
+ protected readonly top = signal(false);
238
+ protected readonly bottom = signal(false);
239
+ protected readonly described = signal(false);
240
+ protected down = false;
241
+
242
+ private readonly dlg = viewChild.required<ElementRef<HTMLDialogElement>>('dlg');
243
+ private readonly card = viewChild.required<ElementRef<HTMLElement>>('card');
244
+ private readonly body = viewChild.required<ElementRef<HTMLElement>>('body');
245
+ private readonly foot = viewChild.required<ElementRef<HTMLElement>>('foot');
246
+ private readonly desc = viewChild.required<ElementRef<HTMLElement>>('desc');
247
+ private readonly doc = inject(DOCUMENT);
248
+ private phase: 'closed' | 'open' | 'leaving' = 'closed';
249
+ private reason: DialogCloseReason = 'action';
250
+ private pending: HTMLElement | null = null;
251
+ private back: Element | null = null;
252
+ private unlock?: () => void;
253
+ private resize?: ResizeObserver;
254
+ private first = true;
255
+ private warned = false;
256
+ private readonly layer: OverlayLayer = {
257
+ el: null as unknown as HTMLElement,
258
+ trigger: () => this.back,
259
+ dismiss: (reason) => reason === 'escape' && this.request('escape'),
260
+ };
261
+
262
+ constructor() {
263
+ afterRenderEffect(() => {
264
+ const on = this.open();
265
+ untracked(() => (on ? this.enter() : this.exit()));
266
+ });
267
+ inject(DestroyRef).onDestroy(() => {
268
+ overlayStack(this.doc).remove(this.layer);
269
+ this.unlock?.();
270
+ this.resize?.disconnect();
271
+ });
272
+ }
273
+
274
+ /** Opens it. Focus comes back to `returnFocusTo` (default: what had focus) when it closes. */
275
+ show(returnFocusTo?: HTMLElement) {
276
+ this.pending = returnFocusTo ?? null;
277
+ this.open.set(true);
278
+ }
279
+
280
+ /** Closes it, with the exit motion, and gives focus back. */
281
+ close(reason: DialogCloseReason = 'action') {
282
+ if (!this.open()) return;
283
+ this.reason = reason;
284
+ this.open.set(false);
285
+ }
286
+
287
+ /** A close asked for by the person (Escape, the dim, the ×, `uiDialogClose`): ignored while busy. */
288
+ request(reason: DialogCloseReason) {
289
+ if (!this.busy()) this.close(reason);
290
+ }
291
+
292
+ protected backdrop(event: Event) {
293
+ const dlg = this.dlg().nativeElement;
294
+ return event.target === dlg || event.target === dlg.firstElementChild;
295
+ }
296
+
297
+ protected press(event: PointerEvent) {
298
+ this.down = this.backdrop(event);
299
+ }
300
+
301
+ /** A press on the dim keeps focus where it is; inside the card it focuses as usual. */
302
+ protected hold(event: MouseEvent) {
303
+ if (this.backdrop(event)) event.preventDefault();
304
+ }
305
+
306
+ protected onClick(event: MouseEvent) {
307
+ if (!this.backdrop(event) || !this.down) return;
308
+ this.down = false;
309
+ if (this.closeOnBackdrop() && !this.busy()) return this.close('backdrop');
310
+ const card = this.card().nativeElement;
311
+ card.removeAttribute('data-pulse');
312
+ void card.offsetWidth;
313
+ card.setAttribute('data-pulse', '');
314
+ }
315
+
316
+ protected onCancel(event: Event) {
317
+ event.preventDefault();
318
+ this.request('escape');
319
+ }
320
+
321
+ protected onNativeClose() {
322
+ if (this.phase !== 'open') return;
323
+ this.phase = 'leaving';
324
+ this.reason = 'action';
325
+ this.open.set(false);
326
+ this.finish();
327
+ }
328
+
329
+ protected onKey(event: KeyboardEvent) {
330
+ if (!this.contained() || event.defaultPrevented) return;
331
+ if (event.key === 'Escape' && !event.isComposing) {
332
+ event.preventDefault();
333
+ this.request('escape');
334
+ } else if (event.key === 'Tab') {
335
+ const all = tabbables(this.card().nativeElement);
336
+ const edge = event.shiftKey ? all[0] : all.at(-1);
337
+ const active = this.doc.activeElement;
338
+ if (!all.length || active === edge || active === this.card().nativeElement) {
339
+ event.preventDefault();
340
+ (event.shiftKey ? all.at(-1) : all[0])?.focus();
341
+ }
342
+ }
343
+ }
344
+
345
+ protected edges() {
346
+ const b = this.body().nativeElement;
347
+ this.top.set(b.scrollTop > 0);
348
+ this.bottom.set(b.scrollTop + b.clientHeight < b.scrollHeight - 1);
349
+ }
350
+
351
+ private enter() {
352
+ const dlg = this.dlg().nativeElement;
353
+ const first = this.first;
354
+ this.first = false;
355
+ if (this.phase === 'open') return;
356
+ if (this.phase === 'leaving') {
357
+ dlg.removeAttribute('data-leaving');
358
+ this.phase = 'open';
359
+ if (!this.contained()) overlayStack(this.doc).add(Object.assign(this.layer, { el: dlg }), this.back);
360
+ return;
361
+ }
362
+ this.back = this.pending ?? this.doc.activeElement;
363
+ this.pending = null;
364
+ this.phase = 'open';
365
+ const modal = !this.contained();
366
+ if (modal && dlg.showModal) dlg.showModal();
367
+ else dlg.setAttribute('open', '');
368
+ if (modal) {
369
+ this.unlock = lockScroll(this.doc);
370
+ overlayStack(this.doc).add(Object.assign(this.layer, { el: dlg }), this.back);
371
+ }
372
+ const desc = this.desc().nativeElement;
373
+ this.described.set(!!desc.textContent?.trim());
374
+ const view = this.doc.defaultView;
375
+ if (view?.ResizeObserver) {
376
+ this.resize = new view.ResizeObserver(() => this.edges());
377
+ const body = this.body().nativeElement;
378
+ for (const node of [body, ...body.children]) this.resize.observe(node);
379
+ }
380
+ this.edges();
381
+ if (modal || !first) this.target().focus({ preventScroll: true });
382
+ if (isDevMode() && !this.warned) {
383
+ this.warned = true;
384
+ if (!this.doc.getElementById(`${this.id()}-title`)?.textContent?.trim()) {
385
+ console.warn('ui-dialog: give it a heading, or project one with uiDialogHeading.', dlg);
386
+ }
387
+ if (!modal && dlg.offsetParent === this.doc.body) {
388
+ console.warn('ui-dialog: a contained dialog needs a positioned parent (position: relative).', dlg);
389
+ }
390
+ }
391
+ }
392
+
393
+ private exit() {
394
+ if (this.phase !== 'open') return;
395
+ const dlg = this.dlg().nativeElement;
396
+ const card = this.card().nativeElement;
397
+ this.phase = 'leaving';
398
+ overlayStack(this.doc).remove(this.layer);
399
+ dlg.setAttribute('data-leaving', '');
400
+ const motion = (dlg.getAnimations?.({ subtree: true }) ?? []).filter((a) => {
401
+ const target = (a.effect as KeyframeEffect | null)?.target;
402
+ return target === dlg || target === card;
403
+ });
404
+ if (!motion.length) return this.finish();
405
+ const timer = setTimeout(() => this.finish(), 600);
406
+ Promise.all(motion.map((a) => a.finished)).then(
407
+ () => (clearTimeout(timer), this.finish()),
408
+ () => undefined,
409
+ );
410
+ }
411
+
412
+ private finish() {
413
+ if (this.phase !== 'leaving') return;
414
+ const dlg = this.dlg().nativeElement;
415
+ const active = this.doc.activeElement;
416
+ const restore = !active || active === this.doc.body || dlg.contains(active);
417
+ this.phase = 'closed';
418
+ dlg.removeAttribute('data-leaving');
419
+ this.card().nativeElement.removeAttribute('data-pulse');
420
+ if (dlg.close) dlg.close();
421
+ else dlg.removeAttribute('open');
422
+ this.unlock?.();
423
+ this.unlock = undefined;
424
+ this.resize?.disconnect();
425
+ const back = this.back;
426
+ this.back = null;
427
+ if (restore && back instanceof HTMLElement && back.isConnected && back !== this.doc.body) {
428
+ const stack = overlayStack(this.doc);
429
+ stack.returning = back;
430
+ back.focus({ preventScroll: true });
431
+ stack.returning = null;
432
+ }
433
+ this.closed.emit(this.reason);
434
+ this.reason = 'action';
435
+ }
436
+
437
+ private target(): HTMLElement {
438
+ const chosen = this.initialFocus();
439
+ const el = chosen instanceof ElementRef ? chosen.nativeElement : chosen;
440
+ const card = this.card().nativeElement;
441
+ return (
442
+ el ??
443
+ card.querySelector<HTMLElement>('[autofocus]') ??
444
+ tabbables(this.body().nativeElement)[0] ??
445
+ tabbables(this.foot().nativeElement)[0] ??
446
+ card
447
+ );
448
+ }
449
+ }
450
+
451
+ /** Put it on the button that opens a dialog: `[uiDialogTrigger]="edit"`. It opens it, sets the ARIA, and gets focus back on close. */
452
+ @Directive({
453
+ selector: '[uiDialogTrigger]',
454
+ host: {
455
+ 'aria-haspopup': 'dialog',
456
+ '[attr.aria-expanded]': 'uiDialogTrigger().open()',
457
+ '[attr.aria-controls]': 'uiDialogTrigger().open() ? uiDialogTrigger().id() : null',
458
+ '(click)': 'uiDialogTrigger().show(el)',
459
+ },
460
+ })
461
+ export class UiDialogTrigger {
462
+ readonly uiDialogTrigger = input.required<UiDialog | UiAlertDialog>();
463
+ protected readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
464
+ }
465
+
466
+ /** Closes the dialog it is in, unless it is busy: `<button uiButton variant="secondary" uiDialogClose>Cancel</button>`. */
467
+ @Directive({ selector: '[uiDialogClose]', host: { '(click)': 'dialog?.request("action")' } })
468
+ export class UiDialogClose {
469
+ protected readonly dialog = inject(UiDialog, { optional: true });
470
+
471
+ constructor() {
472
+ if (isDevMode() && !this.dialog) console.warn('uiDialogClose: put it inside a ui-dialog.');
473
+ }
474
+ }
475
+
476
+ /**
477
+ * A confirmation before something that can't be undone. No ×, no closing on the dim, focus starts on the safe button.
478
+ * If `action` returns a promise, confirm stays busy until it settles, then closes (or stays open and emits `failed`).
479
+ */
480
+ @Component({
481
+ selector: 'ui-alert-dialog',
482
+ exportAs: 'uiAlertDialog',
483
+ imports: [UiDialog, UiButton],
484
+ host: { '[attr.id]': 'null', '[attr.title]': 'null' },
485
+ template: `
486
+ <ui-dialog [id]="id()" [role]="'alertdialog'" size="sm" [heading]="heading()" [description]="description()"
487
+ [closeButton]="false" [closeOnBackdrop]="false" [contained]="contained()" [busy]="busy()" [labels]="labels()"
488
+ [initialFocus]="cancelButton()" [(open)]="open" (closed)="closed.emit($event)">
489
+ <ng-content />
490
+ <div uiDialogFooter>
491
+ <button #safe type="button" uiButton variant="secondary" [size]="buttons()" [attr.aria-disabled]="busy() || null" (click)="cancel()">{{ cancelLabel() }}</button>
492
+ <button type="button" uiButton [variant]="danger() ? 'destructive' : 'primary'" [size]="buttons()" [loading]="busy()" (click)="confirm()">{{ confirmLabel() }}</button>
493
+ </div>
494
+ </ui-dialog>
495
+ `,
496
+ styles: `
497
+ :host { display: contents; }
498
+ [aria-disabled='true']:not([aria-busy]) { opacity: var(--ui-disabled-opacity); cursor: default; }
499
+ @container (max-width: 552px) {
500
+ button { flex: 1; }
501
+ }
502
+ `,
503
+ })
504
+ export class UiAlertDialog {
505
+ /** Shows it. Bind `[(open)]`, or open it with `[uiDialogTrigger]`. */
506
+ readonly open = model(false);
507
+ readonly heading = input<string>();
508
+ readonly description = input<string>();
509
+ readonly cancelLabel = input('Cancel');
510
+ readonly confirmLabel = input('Confirm');
511
+ /** The confirm button turns into the destructive button. */
512
+ readonly danger = input(false, { transform: booleanAttribute });
513
+ /** Called on confirm. Return a promise to keep it open, busy, until it settles. */
514
+ readonly action = input<() => unknown>();
515
+ readonly contained = input(false, { transform: booleanAttribute });
516
+ readonly id = input(`ui-alert-dialog-${nextId++}`);
517
+ readonly labels = input<Partial<DialogLabels>>({});
518
+ /** Confirm was pressed (before a returned promise settles). */
519
+ readonly confirmed = output<void>();
520
+ /** The promise returned by `action` rejected, or `action` threw. It stays open. */
521
+ readonly failed = output<unknown>();
522
+ /** After it has closed and focus has gone back. */
523
+ readonly closed = output<DialogCloseReason>();
524
+
525
+ protected readonly busy = signal(false);
526
+ protected readonly buttons = signal<ButtonSize>('md');
527
+ protected readonly cancelButton = viewChild('safe', { read: ElementRef });
528
+ private readonly dialog = viewChild.required(UiDialog);
529
+ private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
530
+
531
+ constructor() {
532
+ afterRenderEffect(() => {
533
+ if (!this.open()) return;
534
+ const view = this.el.ownerDocument.defaultView;
535
+ const tray = view?.getComputedStyle(this.el).getPropertyValue('--ui-look-tray').trim() === '1';
536
+ untracked(() => this.buttons.set(tray ? 'sm' : 'md'));
537
+ });
538
+ }
539
+
540
+ show(returnFocusTo?: HTMLElement) {
541
+ this.dialog().show(returnFocusTo);
542
+ }
543
+
544
+ close(reason: DialogCloseReason = 'action') {
545
+ this.dialog().close(reason);
546
+ }
547
+
548
+ protected cancel() {
549
+ if (!this.busy()) this.dialog().close('cancel');
550
+ }
551
+
552
+ protected confirm() {
553
+ if (this.busy()) return;
554
+ let result: unknown;
555
+ try {
556
+ result = this.action()?.();
557
+ } catch (error) {
558
+ this.failed.emit(error);
559
+ return;
560
+ }
561
+ this.confirmed.emit();
562
+ if (!(result instanceof Promise || typeof (result as PromiseLike<unknown> | null)?.then === 'function')) {
563
+ return this.dialog().close('confirm');
564
+ }
565
+ this.busy.set(true);
566
+ Promise.resolve(result as PromiseLike<unknown>).then(
567
+ () => {
568
+ this.busy.set(false);
569
+ this.dialog().close('confirm');
570
+ },
571
+ (error) => {
572
+ this.busy.set(false);
573
+ this.failed.emit(error);
574
+ },
575
+ );
576
+ }
577
+ }