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,620 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Tag Input, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (tokens and shared keyframes), added once by `npx ochreui init`.
5
+ *
6
+ * <ui-tag-input label="Invite people" avatar><input uiTagInput [formField]="form.emails" splitOnSpace [validate]="isEmail" /></ui-tag-input>
7
+ * Your own <input> goes inside. Enter, comma, semicolon or Tab (Space with `splitOnSpace`) turns the typed text
8
+ * into a chip; a pasted list adds every piece. Works with signal forms, reactive forms, ngModel or [(value)].
9
+ *
10
+ * Public styling hooks (set them on the element or any ancestor):
11
+ * --ui-text-field-edge resting edge (default: the hairline), shared with Text Field
12
+ * --ui-text-field-edge-hover edge on hover
13
+ * --ui-text-field-edge-focus edge while focused (default: the input border)
14
+ * --ui-text-field-radius corners (default 7px; Classic 10px)
15
+ * --ui-tag-input-max-height height after which the chips scroll (default: about six rows)
16
+ */
17
+ import {
18
+ Component,
19
+ DOCUMENT,
20
+ DestroyRef,
21
+ Directive,
22
+ ElementRef,
23
+ Injector,
24
+ LOCALE_ID,
25
+ afterNextRender,
26
+ afterRenderEffect,
27
+ booleanAttribute,
28
+ computed,
29
+ contentChild,
30
+ forwardRef,
31
+ inject,
32
+ input,
33
+ isDevMode,
34
+ linkedSignal,
35
+ model,
36
+ output,
37
+ signal,
38
+ untracked,
39
+ viewChild,
40
+ } from '@angular/core';
41
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
42
+
43
+ export interface TagInputLabels {
44
+ optional: string;
45
+ /** Placeholder while there are no chips and the input has none of its own. */
46
+ add: string;
47
+ /** Placeholder once there is a chip. */
48
+ addMore: string;
49
+ /** Name of each ×. `{tag}` is replaced. */
50
+ remove: string;
51
+ added: string;
52
+ /** After a paste. `{n}` is replaced. */
53
+ addedMany: string;
54
+ removed: string;
55
+ duplicate: string;
56
+ /** After a first Backspace on an empty field. */
57
+ armed: string;
58
+ /** When the cap stops an addition. `{n}` is replaced. */
59
+ limit: string;
60
+ }
61
+
62
+ const LABELS: TagInputLabels = {
63
+ optional: 'Optional',
64
+ add: 'Add…',
65
+ addMore: 'Add more…',
66
+ remove: 'Remove {tag}',
67
+ added: '{tag} added',
68
+ addedMany: '{n} added',
69
+ removed: '{tag} removed',
70
+ duplicate: '{tag} is already in the list',
71
+ armed: '{tag} selected. Press Backspace again to remove it.',
72
+ limit: 'You can add up to {n}.',
73
+ };
74
+
75
+ let nextId = 0;
76
+
77
+ const fill = (text: string, tag: string | number) => text.replace(/\{(tag|n)\}/g, String(tag));
78
+ const bound = (v: unknown) => (v == null || v === '' ? undefined : Number(v));
79
+ const trimTag = (tag: string) => tag.trim();
80
+
81
+ /** Put it on the `<input>` inside a `<ui-tag-input>`. It owns the list of chips; the text in the input is a draft. */
82
+ @Directive({
83
+ selector: 'input[uiTagInput]',
84
+ providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiTag), multi: true }],
85
+ host: {
86
+ class: 'ui-input',
87
+ dir: 'auto',
88
+ autocomplete: 'off',
89
+ spellcheck: 'false',
90
+ style: 'outline: 0; flex: 1 1 calc(var(--ui-space-10) * 2); box-sizing: border-box; min-inline-size: calc(var(--ui-space-10) * 2); block-size: var(--_row); padding-inline: var(--ui-space-1-5)',
91
+ '[id]': 'id()',
92
+ '[attr.name]': 'null',
93
+ '[attr.maxlength]': 'charLimit() ?? null',
94
+ '[attr.placeholder]': 'shownPlaceholder()',
95
+ '[attr.aria-describedby]': 'describedBy()',
96
+ '[attr.aria-invalid]': 'field.invalid() || null',
97
+ '(input)': 'typed($event)',
98
+ '(keydown)': 'key($event)',
99
+ '(paste)': 'paste($event)',
100
+ '(document:reset)': 'formReset($event)',
101
+ },
102
+ })
103
+ export class UiTag implements ControlValueAccessor {
104
+ /** The chips. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
105
+ readonly value = model<string[]>([]);
106
+ /** The chips shown, kept in step with `value` and with what a form writes, without echoing it back. */
107
+ readonly list = linkedSignal(() => this.value());
108
+ /** Checks a value before it becomes a chip. Return `true`, or the message to show. */
109
+ readonly validate = input<(tag: string) => true | string>();
110
+ /** Cleans a value before the check and the duplicate test, e.g. lower-casing an email. Default: trim. */
111
+ readonly normalize = input<(tag: string) => string>(trimTag);
112
+ /** Space also adds a chip. Right for emails and single words, wrong for labels with spaces. */
113
+ readonly splitOnSpace = input(false, { transform: booleanAttribute });
114
+ /** "Design" and "design" are two chips. By default they count as the same one. */
115
+ readonly caseSensitive = input(false, { transform: booleanAttribute });
116
+ /** At most this many chips; shows "3/10" under the field. Signal forms set it from `maxLength()` in the schema. */
117
+ readonly maxLength = input<number | undefined, unknown>(undefined, { transform: bound });
118
+ /** A character limit for what is typed, written in lower case: `maxlength="40"`. */
119
+ readonly charLimit = input<number | undefined, unknown>(undefined, { alias: 'maxlength', transform: bound });
120
+ /** Posts the chips with a native form, one hidden input each. Signal forms set it from the field. */
121
+ readonly name = input<string>();
122
+ /** Shown while there are no chips. Default "Add…". */
123
+ readonly placeholder = input<string>();
124
+ readonly id = input(`ui-tag-input-${nextId++}`);
125
+ /** Your own descriptions, kept alongside the field's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
126
+ readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
127
+ /** The values just added (one, or several from a paste). */
128
+ readonly added = output<string[]>();
129
+ /** The value just removed. */
130
+ readonly removed = output<string>();
131
+
132
+ readonly el = inject<ElementRef<HTMLInputElement>>(ElementRef).nativeElement;
133
+ protected readonly field = inject(UiTagInput);
134
+ private readonly injector = inject(Injector);
135
+ private readonly destroy = inject(DestroyRef);
136
+ readonly disabled = signal(false);
137
+ readonly readOnly = signal(false);
138
+ readonly armed = signal(false);
139
+ readonly flash = signal<{ tag: string; n: number } | null>(null);
140
+ readonly note = signal('');
141
+ private readonly draft = signal('');
142
+ private readonly rejected = signal('');
143
+ private readonly over = signal(false);
144
+ private flashes = 0;
145
+ private initial: string[] = [];
146
+ private onChange: (v: string[]) => void = () => {};
147
+ private onTouched: () => void = () => {};
148
+
149
+ readonly full = computed(() => this.maxLength() !== undefined && this.list().length >= this.maxLength()!);
150
+ readonly issue = computed(
151
+ () => this.rejected() || (this.over() && this.full() && this.draft().trim() ? fill(this.field.l().limit, this.maxLength()!) : ''),
152
+ );
153
+ readonly locked = computed(() => this.disabled() || this.readOnly());
154
+ protected readonly shownPlaceholder = computed(() =>
155
+ this.full() ? '' : this.list().length ? this.field.l().addMore : (this.placeholder() ?? this.field.l().add),
156
+ );
157
+ protected readonly describedBy = computed(
158
+ () => [this.ariaDescribedby(), ...this.field.describers()].filter(Boolean).join(' ') || null,
159
+ );
160
+
161
+ constructor() {
162
+ afterNextRender(() => {
163
+ const el = this.el;
164
+ this.initial = this.list();
165
+ const read = () => {
166
+ this.disabled.set(el.disabled);
167
+ this.readOnly.set(el.readOnly);
168
+ };
169
+ read();
170
+ const view = el.ownerDocument.defaultView;
171
+ if (!view?.MutationObserver) return;
172
+ const watch = new view.MutationObserver(read);
173
+ watch.observe(el, { attributes: true, attributeFilter: ['disabled', 'readonly'] });
174
+ this.destroy.onDestroy(() => watch.disconnect());
175
+ });
176
+ }
177
+
178
+ private same(a: string, b: string) {
179
+ return this.caseSensitive() ? a === b : a.toLocaleLowerCase() === b.toLocaleLowerCase();
180
+ }
181
+
182
+ private write(text: string) {
183
+ if (this.el.value !== text) this.el.value = text;
184
+ this.draft.set(text);
185
+ }
186
+
187
+ private set(list: string[]) {
188
+ this.list.set(list);
189
+ this.value.set(list);
190
+ this.onChange(list);
191
+ }
192
+
193
+ private say(text: string) {
194
+ this.note.set(this.note() === text ? text + ' ' : text);
195
+ }
196
+
197
+ /** Turns text into chips. `final` adds the last piece too; otherwise it stays as the draft. */
198
+ commit(text = this.el.value, final = true) {
199
+ const parts = text.split(this.splitOnSpace() ? /[,;\n\r\s]+/ : /[,;\n\r]+/);
200
+ const tail = final ? '' : parts.pop()!.trimStart();
201
+ const list = [...this.list()];
202
+ const added: string[] = [];
203
+ let left: string[] = [];
204
+ let message = '';
205
+ let over = false;
206
+ let dup: string | undefined;
207
+ for (let i = 0; i < parts.length; i++) {
208
+ const tag = this.normalize()(parts[i]);
209
+ if (!tag) continue;
210
+ const known = list.find((t) => this.same(t, tag));
211
+ if (known !== undefined) {
212
+ dup = known;
213
+ continue;
214
+ }
215
+ const ok = list.length >= (this.maxLength() ?? Infinity) ? null : (this.validate()?.(tag) ?? true);
216
+ if (ok !== true) {
217
+ [over, message, left] = [ok === null, ok ?? '', parts.slice(i)];
218
+ break;
219
+ }
220
+ list.push(tag);
221
+ added.push(tag);
222
+ }
223
+ this.write([...left.map((s) => s.trim()).filter(Boolean), ...(tail ? [tail] : [])].join(this.splitOnSpace() ? ' ' : ', '));
224
+ this.rejected.set(message);
225
+ this.over.set(over);
226
+ if (dup !== undefined) this.blink(dup);
227
+ if (!added.length) return;
228
+ this.set(list);
229
+ this.say(added.length === 1 ? fill(this.field.l().added, added[0]) : fill(this.field.l().addedMany, added.length));
230
+ this.added.emit(added);
231
+ afterNextRender(() => this.el.scrollIntoView?.({ block: 'nearest', inline: 'nearest' }), { injector: this.injector });
232
+ }
233
+
234
+ private blink(tag: string) {
235
+ this.flash.set({ tag, n: this.flashes++ });
236
+ this.say(fill(this.field.l().duplicate, tag));
237
+ }
238
+
239
+ /** Removes the chip at `index`. With `keyboard`, focus moves to the next chip (or the input) first. */
240
+ removeAt(index: number, keyboard = false) {
241
+ const list = this.list();
242
+ const tag = list[index];
243
+ if (tag === undefined || this.locked()) return;
244
+ const next = keyboard && index + 1 < list.length ? this.field.chip(index + 1) : this.el;
245
+ next?.focus();
246
+ this.armed.set(false);
247
+ this.set(list.filter((_, i) => i !== index));
248
+ this.say(fill(this.field.l().removed, tag));
249
+ this.removed.emit(tag);
250
+ }
251
+
252
+ protected typed(event: Event) {
253
+ if ((event as InputEvent).isComposing) return;
254
+ this.armed.set(false);
255
+ this.rejected.set('');
256
+ this.over.set(false);
257
+ const text = this.el.value;
258
+ if ((this.splitOnSpace() ? /[,;\n\r\s]/ : /[,;\n\r]/).test(text)) this.commit(text, false);
259
+ else this.draft.set(text);
260
+ }
261
+
262
+ protected paste(event: ClipboardEvent) {
263
+ const text = event.clipboardData?.getData('text') ?? '';
264
+ if (this.locked() || !(this.splitOnSpace() ? /[,;\n\r\s]/ : /[,;\n\r]/).test(text.trim())) return;
265
+ event.preventDefault();
266
+ const el = this.el;
267
+ this.commit(el.value.slice(0, el.selectionStart ?? 0) + text + el.value.slice(el.selectionEnd ?? el.value.length));
268
+ }
269
+
270
+ protected key(e: KeyboardEvent) {
271
+ if (e.isComposing || this.locked()) return;
272
+ const text = this.el.value;
273
+ const armed = this.armed();
274
+ if (e.key !== 'Backspace') this.armed.set(false);
275
+ const back = this.field.rtl() ? 'ArrowRight' : 'ArrowLeft';
276
+ switch (e.key) {
277
+ case 'Enter':
278
+ if (!text) return;
279
+ e.preventDefault();
280
+ return this.commit();
281
+ case 'Tab':
282
+ if (e.shiftKey || !text.trim()) return;
283
+ this.commit();
284
+ if (this.el.value) e.preventDefault();
285
+ return;
286
+ case 'Backspace': {
287
+ const n = this.list().length;
288
+ if (text || !n) return;
289
+ e.preventDefault();
290
+ if (armed) return this.removeAt(n - 1);
291
+ this.armed.set(true);
292
+ return this.say(fill(this.field.l().armed, this.list()[n - 1]));
293
+ }
294
+ case 'Escape':
295
+ if (!text && !armed && !this.issue()) return;
296
+ e.preventDefault();
297
+ e.stopPropagation();
298
+ this.write('');
299
+ this.rejected.set('');
300
+ this.over.set(false);
301
+ return;
302
+ case back:
303
+ if (this.el.selectionStart || this.el.selectionEnd || !this.list().length) return;
304
+ e.preventDefault();
305
+ if (text.trim()) this.commit();
306
+ this.field.chip(this.list().length - 1)?.focus();
307
+ }
308
+ }
309
+
310
+ /** Keys on a chip's ×, which is the chip's focus stop. */
311
+ chipKey(e: KeyboardEvent, index: number) {
312
+ const n = this.list().length;
313
+ const [back, fwd] = this.field.rtl() ? ['ArrowRight', 'ArrowLeft'] : ['ArrowLeft', 'ArrowRight'];
314
+ const go = (target?: HTMLElement | null) => {
315
+ e.preventDefault();
316
+ target?.focus();
317
+ };
318
+ switch (e.key) {
319
+ case back:
320
+ return go(this.field.chip(Math.max(0, index - 1)));
321
+ case fwd:
322
+ return go(index + 1 < n ? this.field.chip(index + 1) : this.el);
323
+ case 'Home':
324
+ return go(this.field.chip(0));
325
+ case 'End':
326
+ case 'Escape':
327
+ return go(this.el);
328
+ case 'Backspace':
329
+ case 'Delete':
330
+ e.preventDefault();
331
+ return this.removeAt(index, true);
332
+ default:
333
+ if (e.key.length === 1 && e.key !== ' ' && !e.ctrlKey && !e.metaKey && !e.altKey) this.el.focus();
334
+ }
335
+ }
336
+
337
+ /** Focus left the whole field: add what was typed, and mark it touched. */
338
+ leave() {
339
+ this.armed.set(false);
340
+ if (this.el.value.trim() && !this.locked()) this.commit();
341
+ this.onTouched();
342
+ }
343
+
344
+ /** A native form reset puts back the chips it started with. */
345
+ protected formReset(e: Event) {
346
+ if (!(e.target as Element | null)?.contains?.(this.el)) return;
347
+ setTimeout(() => {
348
+ if (e.defaultPrevented) return;
349
+ this.write('');
350
+ this.rejected.set('');
351
+ this.over.set(false);
352
+ this.armed.set(false);
353
+ if (this.list().join('\n') !== this.initial.join('\n')) this.set(this.initial);
354
+ });
355
+ }
356
+
357
+ writeValue(v: unknown) {
358
+ const list: string[] = [];
359
+ for (const tag of Array.isArray(v) ? v.map(String) : []) if (!list.some((t) => this.same(t, tag))) list.push(tag);
360
+ this.list.set(list);
361
+ this.rejected.set('');
362
+ this.over.set(false);
363
+ this.armed.set(false);
364
+ }
365
+
366
+ registerOnChange(fn: (v: string[]) => void) {
367
+ this.onChange = fn;
368
+ }
369
+
370
+ registerOnTouched(fn: () => void) {
371
+ this.onTouched = fn;
372
+ }
373
+
374
+ setDisabledState(disabled: boolean) {
375
+ this.el.disabled = disabled;
376
+ this.disabled.set(disabled);
377
+ }
378
+ }
379
+
380
+ @Component({
381
+ selector: 'ui-tag-input',
382
+ host: {
383
+ '[attr.data-invalid]': 'invalid() || null',
384
+ '[attr.data-readonly]': 'c()?.readOnly() || null',
385
+ '[attr.data-disabled]': 'c()?.disabled() || null',
386
+ '[style.opacity]': "c()?.disabled() ? 'var(--ui-disabled-opacity)' : null",
387
+ '(focusout)': 'leave($event)',
388
+ },
389
+ template: `
390
+ @if (label()) {
391
+ <div style="display: flex; align-items: baseline; gap: var(--ui-space-1-5); padding-inline: calc(2px * var(--_t))">
392
+ <label [for]="c()?.id()" style="font-weight: var(--ui-weight-medium)">{{ label() }}@if (optional()) {<span class="ui-sr"> ({{ l().optional }})</span>}</label>
393
+ @if (optional()) {<span aria-hidden="true" [style]="SMALL">{{ l().optional }}</span>}
394
+ </div>
395
+ }
396
+ <div #box class="box" [style]="BOX" (pointerdown)="focusInput($event)">
397
+ @for (tag of tags(); track tag; let i = $index, last = $last) {
398
+ <span class="chip" [title]="tag" [style]="CHIP" [style.padding-inline]="pad()" [attr.data-armed]="(last && c()?.armed()) || null">
399
+ @if (avatar()) {<span aria-hidden="true" [style]="AV">{{ initial(tag) }}</span>}
400
+ <span dir="auto" style="overflow: hidden; text-overflow: ellipsis">{{ tag }}</span>
401
+ @if (editable()) {
402
+ <button type="button" class="ui-btn-icon x" tabindex="-1" [attr.aria-label]="fmt(l().remove, tag)" (click)="c()?.removeAt(i, $event.detail === 0)" (keydown)="c()?.chipKey($event, i)">
403
+ <svg class="ui-icon" viewBox="0 0 12 12" aria-hidden="true" style="width: var(--ui-space-2-5); height: var(--ui-space-2-5)"><path d="M3 3l6 6M9 3l-6 6" /></svg>
404
+ </button>
405
+ }
406
+ </span>
407
+ }
408
+ <ng-content select="input" />
409
+ </div>
410
+ <div [style]="SMALL + '; display: flex; gap: var(--ui-space-3); padding-inline: calc(2px * var(--_t))'" [class.ui-sr]="!message() && cap() === undefined">
411
+ <p #msg [id]="messageId" dir="auto" aria-live="polite" style="flex: 1; margin: 0" [style.color]="invalid() ? 'var(--ui-danger)' : null">{{ message() }}</p>
412
+ @if (cap() !== undefined) {<span [style.color]="c()?.full() ? 'var(--ui-text)' : null" style="margin-inline-start: auto; font-variant-numeric: tabular-nums">{{ num.format(tags().length) }}/{{ num.format(cap()!) }}</span>}
413
+ </div>
414
+ <span class="ui-sr" role="status" [id]="liveId">{{ c()?.note() }}</span>
415
+ @if (c()?.name(); as n) {
416
+ @for (tag of tags(); track $index) {<input type="hidden" [name]="n" [value]="tag" />}
417
+ }
418
+ `,
419
+ styles: `
420
+ :host {
421
+ --_t: var(--ui-look-tray, 0);
422
+ --_h: calc(var(--ui-control-h-md) + var(--ui-space-1) * var(--_t));
423
+ --_row: var(--ui-space-6);
424
+ --_e: var(--ui-text-field-edge, var(--ui-hairline));
425
+ --_w: calc(0.5px + 0.5px * var(--_t));
426
+ --_cw: calc(0.5px + 0.5px * var(--_t));
427
+ --_d: calc(140ms * var(--ui-motion-scale));
428
+ --_well: light-dark(var(--ui-bg-surface), color-mix(in srgb, var(--ui-text) calc(3.5% * (1 - var(--_t))), var(--ui-bg-surface)));
429
+ --_fill: var(--_well);
430
+ --_chip: light-dark(var(--ui-bg-subtle), color-mix(in srgb, var(--ui-bg-muted) calc(100% * (1 - var(--_t))), var(--ui-bg-subtle)));
431
+ --ui-text-placeholder: light-dark(color-mix(in srgb, var(--ui-text-muted) 68%, var(--_fill)), color-mix(in srgb, var(--ui-text-muted) 57%, var(--_fill)));
432
+ display: flex;
433
+ flex-direction: column;
434
+ gap: calc(var(--ui-space-1-5) + 1px * var(--_t));
435
+ min-inline-size: 0;
436
+ color: var(--ui-text);
437
+ font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
438
+ }
439
+ .box {
440
+ background: var(--_fill);
441
+ box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
442
+ font-size: calc(var(--ui-text-base) + 0.5px * var(--_t));
443
+ cursor: text;
444
+ transition: var(--_d) var(--ui-ease-out);
445
+ transition-property: box-shadow, background;
446
+ }
447
+ @media (hover: hover) { .box:hover { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
448
+ .box:focus-within { --_w: 1px; --_e: var(--ui-text-field-edge-focus, var(--ui-border-input)); }
449
+ :host([data-invalid]) .box {
450
+ --_w: 1px;
451
+ --_e: color-mix(in srgb, var(--ui-danger) 42%, transparent);
452
+ --_fill: color-mix(in srgb, var(--ui-danger-soft) calc(50% * var(--_t)), var(--_well));
453
+ }
454
+ :host([data-readonly]) .box { --_fill: var(--ui-bg-subtle); }
455
+ :host([data-disabled]) .box { cursor: not-allowed; --_e: var(--ui-hairline); }
456
+ .chip {
457
+ background: var(--_chip);
458
+ box-shadow: inset 0 0 0 var(--_cw) var(--_cr, light-dark(var(--ui-border), color-mix(in srgb, var(--ui-border-strong) calc(100% * (1 - var(--_t))), var(--ui-border))));
459
+ font-size: calc(var(--ui-text-code) + 0.5px * var(--_t));
460
+ transition: var(--_d) var(--ui-ease-out);
461
+ transition-property: background-color, box-shadow;
462
+ }
463
+ .chip:has(.x:focus-visible) { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
464
+ .chip[data-armed] { --_cw: 1px; --_cr: color-mix(in srgb, var(--ui-text) 8%, var(--ui-border)); background: light-dark(color-mix(in srgb, var(--ui-text) 6%, var(--_chip)), color-mix(in srgb, var(--ui-text) 9%, var(--_chip))); }
465
+ .x { --_s: calc(var(--ui-space-5) - var(--ui-space-0-5)); color: color-mix(in srgb, var(--ui-text-muted) 74%, var(--_chip)); position: relative; border-radius: var(--ui-space-1); outline: 0 !important; transition-duration: var(--_d); }
466
+ @media (hover: hover) { .chip .x:hover { background: light-dark(color-mix(in srgb, var(--ui-text) 12%, transparent), color-mix(in srgb, var(--ui-text) 15%, transparent)); } }
467
+ .x::before { content: ''; position: absolute; inset: calc(var(--ui-space-1) * -1 - 1px); }
468
+ @media (pointer: coarse) {
469
+ :host { --_h: calc(var(--ui-space-8) + var(--ui-space-1-5) + var(--ui-space-1) * var(--_t)); --_row: calc(var(--ui-space-6) + var(--ui-space-1-5)); }
470
+ .box { font-size: var(--ui-text-field-touch); }
471
+ .chip { font-size: var(--ui-text-md); }
472
+ .x { --_s: var(--ui-space-6); }
473
+ }
474
+ @media (forced-colors: active) {
475
+ .box { border: 1px solid CanvasText; }
476
+ .box:has(input:focus) { outline: 2px solid Highlight; }
477
+ :host([data-invalid]) .box { outline: 1px solid CanvasText; }
478
+ .chip { outline: 1px solid transparent; outline-offset: -1px; }
479
+ .chip[data-armed] { outline: 2px solid Highlight; }
480
+ }
481
+ `,
482
+ })
483
+ export class UiTagInput {
484
+ /** Shown above the field. Clicking it focuses the input. */
485
+ readonly label = input<string>();
486
+ /** A quiet line under the field. A message takes its place while there is one. */
487
+ readonly hint = input<string>();
488
+ /** Marks the field invalid and shows this message where the hint was. */
489
+ readonly error = input<string | null | undefined>();
490
+ /** Adds "Optional" after the label. */
491
+ readonly optional = input(false, { transform: booleanAttribute });
492
+ /** A small circle with the first letter on every chip, for people. */
493
+ readonly avatar = input(false, { transform: booleanAttribute });
494
+ /** Words for the tag, the buttons and the announcements, to translate them. */
495
+ readonly labels = input<Partial<TagInputLabels>>({});
496
+
497
+ protected readonly c = contentChild(UiTag);
498
+ readonly l = computed(() => ({ ...LABELS, ...this.labels() }));
499
+ protected readonly num = new Intl.NumberFormat(inject(LOCALE_ID));
500
+ protected readonly messageId = `ui-tag-input-msg-${nextId++}`;
501
+ protected readonly liveId = `${this.messageId}-live`;
502
+
503
+ protected readonly tags = computed(() => this.c()?.list() ?? []);
504
+ protected readonly cap = computed(() => this.c()?.maxLength());
505
+ readonly invalid = computed(() => !!this.error() || !!this.c()?.issue());
506
+ protected readonly message = computed(() => this.error() || this.c()?.issue() || this.hint() || '');
507
+ protected readonly editable = computed(() => !!this.c() && !this.c()!.locked());
508
+ readonly describers = computed(() => [this.message() ? this.messageId : '', this.liveId]);
509
+ protected readonly pad = computed(() => {
510
+ const [tight, roomy] = ['calc(var(--ui-space-1) - 1px)', 'var(--ui-space-2)'];
511
+ return `${this.avatar() ? tight : roomy} ${this.editable() ? tight : roomy}`;
512
+ });
513
+ protected readonly BOX =
514
+ 'display: flex; flex-wrap: wrap; align-items: center; gap: var(--ui-space-1); box-sizing: border-box; min-block-size: var(--_h); max-block-size: var(--ui-tag-input-max-height, calc(var(--ui-space-6) * (6.5 + 0.5 * var(--_t)))); overflow-y: auto; scrollbar-width: thin; padding: calc(var(--ui-space-1) - 1px + 2px * var(--_t)) calc(var(--ui-space-1) + 2px * var(--_t)); border-radius: var(--ui-text-field-radius, calc(var(--ui-radius-sm) + 1px + 3px * var(--_t)))';
515
+ protected readonly CHIP =
516
+ 'display: inline-flex; align-items: center; gap: calc(var(--ui-space-1) + 1px); box-sizing: border-box; max-inline-size: 100%; block-size: var(--_row); border-radius: calc(var(--ui-radius-sm) - 1px + 2px * var(--_t)); white-space: nowrap; cursor: default';
517
+ protected readonly SMALL =
518
+ 'color: color-mix(in srgb, var(--ui-text-muted) 90%, var(--_fill)); font-size: calc(var(--ui-text-sm) + 0.5px * var(--_t)); line-height: calc(var(--ui-leading-xs) + 1px * var(--_t))';
519
+ protected readonly AV =
520
+ 'display: grid; place-items: center; flex: none; inline-size: calc(var(--ui-space-5) - var(--ui-space-0-5)); block-size: calc(var(--ui-space-5) - var(--ui-space-0-5)); border-radius: 50%; background: color-mix(in srgb, var(--ui-text) 8%, transparent); color: color-mix(in srgb, var(--ui-text) 25%, var(--ui-text-muted)); font-size: calc(var(--ui-text-xs) - 1px); font-weight: var(--ui-weight-medium); line-height: 1';
521
+
522
+ private readonly box = viewChild.required<ElementRef<HTMLElement>>('box');
523
+ private readonly msg = viewChild<ElementRef<HTMLElement>>('msg');
524
+ private readonly doc = inject(DOCUMENT);
525
+ private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
526
+
527
+ constructor() {
528
+ let shown: string | undefined;
529
+ afterRenderEffect(() => {
530
+ const text = this.message();
531
+ const p = this.msg()?.nativeElement;
532
+ const motion = this.motion();
533
+ if (shown !== undefined && text !== shown && p?.animate && motion) {
534
+ p.animate([{ opacity: 0, filter: 'blur(2px)', transform: 'translateY(-3px)' }, {}], { duration: 260 * motion, easing: this.sharp() });
535
+ }
536
+ shown = text;
537
+ });
538
+ let before: Set<string> | undefined;
539
+ afterRenderEffect(() => {
540
+ const tags = this.tags();
541
+ const motion = this.motion();
542
+ const chips = this.box().nativeElement.querySelectorAll<HTMLElement>('.chip');
543
+ tags.forEach((tag, i) => {
544
+ if (before && !before.has(tag) && motion && typeof chips[i]?.animate === 'function') this.pop(chips[i], motion);
545
+ });
546
+ before = new Set(tags);
547
+ });
548
+ let flashing: Animation[] = [];
549
+ afterRenderEffect(() => {
550
+ const f = this.c()?.flash();
551
+ const chip = f && this.box().nativeElement.querySelectorAll<HTMLElement>('.chip')[untracked(this.tags).indexOf(f.tag)];
552
+ const motion = untracked(() => this.motion());
553
+ if (!chip?.animate || !motion) return;
554
+ flashing.forEach((a) => a.cancel());
555
+ const tint = {
556
+ backgroundColor: 'light-dark(color-mix(in srgb, var(--ui-focus-ring) 10%, var(--_chip)), color-mix(in srgb, var(--ui-focus-ring) 18%, var(--_chip)))',
557
+ boxShadow: 'inset 0 0 0 1px color-mix(in srgb, var(--ui-focus-ring) 15%, var(--ui-border))',
558
+ easing: 'ease-out',
559
+ };
560
+ flashing = [
561
+ chip.animate([{ easing: this.sharp() }, { ...tint, offset: 0.19 }, { ...tint, offset: 0.64 }, {}], 700 * motion),
562
+ this.pop(chip, motion, 700 * motion),
563
+ ];
564
+ });
565
+ afterNextRender(() => {
566
+ const c = this.c();
567
+ if (isDevMode() && c && !this.label() && !c.el.ariaLabel && !c.el.hasAttribute('aria-labelledby')) {
568
+ console.warn('ui-tag-input: give it a label, or an aria-label on the input.', c.el);
569
+ }
570
+ });
571
+ }
572
+
573
+ /** The × of the chip at `index`, which is where keyboard focus goes for that chip. */
574
+ chip(index: number) {
575
+ return this.box().nativeElement.querySelectorAll<HTMLElement>('.x')[index] ?? null;
576
+ }
577
+
578
+ rtl() {
579
+ return this.doc.defaultView?.getComputedStyle(this.host).direction === 'rtl';
580
+ }
581
+
582
+ protected fmt(text: string, tag: string) {
583
+ return fill(text, tag);
584
+ }
585
+
586
+ protected initial(tag: string) {
587
+ return [...tag.trim()][0]?.toLocaleUpperCase() ?? '';
588
+ }
589
+
590
+ protected focusInput(event: PointerEvent) {
591
+ const el = this.c()?.el;
592
+ const target = event.target as Element;
593
+ if (!el || target === el || target.closest('button, a, input')) return;
594
+ event.preventDefault();
595
+ const chip = target.closest('.chip');
596
+ const x = chip?.querySelector<HTMLElement>('.x');
597
+ (x ?? el).focus();
598
+ }
599
+
600
+ protected leave(event: FocusEvent) {
601
+ if (!this.host.contains(event.relatedTarget as Node | null)) this.c()?.leave();
602
+ }
603
+
604
+ private pop(chip: HTMLElement, motion: number, delay = 0) {
605
+ return chip.animate([{ opacity: 0, filter: 'blur(2px)', transform: 'scale(0.85)' }, {}], { duration: 220 * motion, delay, easing: this.sharp() });
606
+ }
607
+
608
+ /** The sharp ease-out shared by the chip pop, the duplicate flash and the message swap. */
609
+ private sharp() {
610
+ return this.doc.defaultView?.getComputedStyle(this.host).getPropertyValue('--ui-ease-out').trim() || 'ease-out';
611
+ }
612
+
613
+ /** 0 under reduced motion, otherwise the app's motion scale (1 by default). */
614
+ private motion() {
615
+ const view = this.doc.defaultView;
616
+ if (!view?.matchMedia || view.matchMedia('(prefers-reduced-motion: reduce)').matches) return 0;
617
+ const scale = parseFloat(view.getComputedStyle(this.host).getPropertyValue('--ui-motion-scale'));
618
+ return Number.isNaN(scale) ? 1 : scale;
619
+ }
620
+ }