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.
- package/LICENSE +21 -0
- package/README.md +153 -2
- package/cli/ochreui.mjs +601 -0
- package/mcp/core.mjs +126 -0
- package/mcp/docs.json +906 -0
- package/mcp/server.mjs +23 -0
- package/package.json +393 -3
- package/registry/agent-team.ts +350 -0
- package/registry/avatar.ts +300 -0
- package/registry/badge.ts +207 -0
- package/registry/breadcrumb.ts +435 -0
- package/registry/button.ts +238 -0
- package/registry/card.ts +221 -0
- package/registry/change-summary.ts +200 -0
- package/registry/checkbox.ts +204 -0
- package/registry/code-block.ts +332 -0
- package/registry/color-picker.ts +720 -0
- package/registry/comment-thread.ts +503 -0
- package/registry/context-menu.ts +281 -0
- package/registry/cover-flow.ts +623 -0
- package/registry/date-picker.ts +799 -0
- package/registry/decision-inbox.ts +340 -0
- package/registry/dialog.ts +577 -0
- package/registry/dropdown-menu.ts +864 -0
- package/registry/email-draft.ts +916 -0
- package/registry/empty-calendar.ts +199 -0
- package/registry/empty-state.ts +164 -0
- package/registry/enrichment-orbit.ts +164 -0
- package/registry/filter-menu.ts +541 -0
- package/registry/form-field.ts +308 -0
- package/registry/frost-cloud.ts +381 -0
- package/registry/ghosted-chat.ts +210 -0
- package/registry/glow-cloud.ts +402 -0
- package/registry/grid-background.ts +164 -0
- package/registry/hover-card.ts +401 -0
- package/registry/icon-button.ts +133 -0
- package/registry/index.json +2124 -0
- package/registry/integration-beam.ts +588 -0
- package/registry/integration-grid.ts +209 -0
- package/registry/kbd.ts +176 -0
- package/registry/layer-stack.ts +351 -0
- package/registry/lead-sourcing.ts +285 -0
- package/registry/logo-cluster.ts +239 -0
- package/registry/logo-marquee.ts +505 -0
- package/registry/message-actions.ts +833 -0
- package/registry/model-orbit.ts +583 -0
- package/registry/model-wheel.ts +372 -0
- package/registry/notification-stack.ts +301 -0
- package/registry/number-input.ts +552 -0
- package/registry/number-ticker.ts +261 -0
- package/registry/ochre.css +917 -0
- package/registry/otp-input.ts +704 -0
- package/registry/overlay.ts +312 -0
- package/registry/pagination.ts +362 -0
- package/registry/popover.ts +296 -0
- package/registry/profile-menu.ts +332 -0
- package/registry/progress-tracker.ts +291 -0
- package/registry/rating.ts +461 -0
- package/registry/reaction-bar.ts +322 -0
- package/registry/region-map.ts +537 -0
- package/registry/resizable.ts +489 -0
- package/registry/scroll-area.ts +271 -0
- package/registry/segmented-control.ts +433 -0
- package/registry/select.ts +769 -0
- package/registry/separator.ts +97 -0
- package/registry/shimmer-loader.ts +211 -0
- package/registry/side-folder.ts +152 -0
- package/registry/skeleton.ts +136 -0
- package/registry/skill/INSTRUCTIONS.md +11 -0
- package/registry/skill/SKILL.md +65 -0
- package/registry/skill/checklist.md +45 -0
- package/registry/skill/references/agent-team.md +105 -0
- package/registry/skill/references/avatar.md +88 -0
- package/registry/skill/references/badge.md +92 -0
- package/registry/skill/references/breadcrumb.md +99 -0
- package/registry/skill/references/button.md +82 -0
- package/registry/skill/references/card.md +111 -0
- package/registry/skill/references/catalog.md +114 -0
- package/registry/skill/references/change-summary.md +103 -0
- package/registry/skill/references/checkbox.md +105 -0
- package/registry/skill/references/code-block.md +109 -0
- package/registry/skill/references/color-picker.md +138 -0
- package/registry/skill/references/comment-thread.md +164 -0
- package/registry/skill/references/context-menu.md +205 -0
- package/registry/skill/references/cover-flow.md +128 -0
- package/registry/skill/references/date-picker.md +154 -0
- package/registry/skill/references/decision-inbox.md +126 -0
- package/registry/skill/references/dialog.md +155 -0
- package/registry/skill/references/dropdown-menu.md +144 -0
- package/registry/skill/references/email-draft.md +182 -0
- package/registry/skill/references/empty-calendar.md +74 -0
- package/registry/skill/references/empty-state.md +118 -0
- package/registry/skill/references/enrichment-orbit.md +71 -0
- package/registry/skill/references/filter-menu.md +143 -0
- package/registry/skill/references/form-field.md +118 -0
- package/registry/skill/references/frost-cloud.md +80 -0
- package/registry/skill/references/ghosted-chat.md +75 -0
- package/registry/skill/references/glow-cloud.md +84 -0
- package/registry/skill/references/grid-background.md +76 -0
- package/registry/skill/references/hover-card.md +125 -0
- package/registry/skill/references/icon-button.md +99 -0
- package/registry/skill/references/integration-beam.md +107 -0
- package/registry/skill/references/integration-grid.md +100 -0
- package/registry/skill/references/kbd.md +65 -0
- package/registry/skill/references/layer-stack.md +115 -0
- package/registry/skill/references/lead-sourcing.md +92 -0
- package/registry/skill/references/logo-cluster.md +92 -0
- package/registry/skill/references/logo-marquee.md +123 -0
- package/registry/skill/references/message-actions.md +146 -0
- package/registry/skill/references/model-orbit.md +106 -0
- package/registry/skill/references/model-wheel.md +84 -0
- package/registry/skill/references/notification-stack.md +138 -0
- package/registry/skill/references/number-input.md +117 -0
- package/registry/skill/references/number-ticker.md +73 -0
- package/registry/skill/references/otp-input.md +142 -0
- package/registry/skill/references/pagination.md +112 -0
- package/registry/skill/references/popover.md +143 -0
- package/registry/skill/references/profile-menu.md +123 -0
- package/registry/skill/references/progress-tracker.md +122 -0
- package/registry/skill/references/rating.md +111 -0
- package/registry/skill/references/reaction-bar.md +128 -0
- package/registry/skill/references/region-map.md +109 -0
- package/registry/skill/references/resizable.md +127 -0
- package/registry/skill/references/scroll-area.md +113 -0
- package/registry/skill/references/segmented-control.md +96 -0
- package/registry/skill/references/select.md +122 -0
- package/registry/skill/references/separator.md +69 -0
- package/registry/skill/references/shimmer-loader.md +83 -0
- package/registry/skill/references/side-folder.md +75 -0
- package/registry/skill/references/skeleton.md +95 -0
- package/registry/skill/references/smart-recommendation.md +132 -0
- package/registry/skill/references/spinner.md +84 -0
- package/registry/skill/references/switch.md +79 -0
- package/registry/skill/references/tab-overload.md +70 -0
- package/registry/skill/references/tag-input.md +126 -0
- package/registry/skill/references/team-invite.md +94 -0
- package/registry/skill/references/testimonial-wall.md +110 -0
- package/registry/skill/references/text-area.md +115 -0
- package/registry/skill/references/text-field.md +126 -0
- package/registry/skill/references/text-reveal.md +71 -0
- package/registry/skill/references/thinking-loader.md +87 -0
- package/registry/skill/references/time-picker.md +122 -0
- package/registry/skill/references/tips-panel.md +115 -0
- package/registry/skill/references/toggle-group.md +98 -0
- package/registry/skill/references/tool-call-card.md +151 -0
- package/registry/skill/references/tooltip.md +94 -0
- package/registry/skill/references/tree-view.md +172 -0
- package/registry/skill/theming.md +57 -0
- package/registry/smart-recommendation.ts +415 -0
- package/registry/spinner.ts +67 -0
- package/registry/switch.ts +146 -0
- package/registry/tab-overload.ts +211 -0
- package/registry/tag-input.ts +620 -0
- package/registry/team-invite.ts +248 -0
- package/registry/testimonial-wall.ts +521 -0
- package/registry/text-area.ts +432 -0
- package/registry/text-field.ts +415 -0
- package/registry/text-reveal.ts +175 -0
- package/registry/theme.ts +68 -0
- package/registry/thinking-loader.ts +302 -0
- package/registry/time-picker.ts +828 -0
- package/registry/tips-panel.ts +312 -0
- package/registry/toggle-group.ts +357 -0
- package/registry/tool-call-card.ts +578 -0
- package/registry/tooltip.ts +486 -0
- package/registry/tree-view.ts +454 -0
- package/styles/base.css +500 -0
- package/styles/tokens.css +415 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Text Area, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (the --ui-* tokens and shared keyframes), which `npx ochreui init` adds once.
|
|
5
|
+
*
|
|
6
|
+
* <ui-text-area label="Notes" hint="…" counter expandable><textarea uiTextarea [formField]="form.notes"></textarea></ui-text-area>
|
|
7
|
+
* Your own <textarea> goes inside, so any Angular forms API works on it. It grows between minRows and maxRows.
|
|
8
|
+
*
|
|
9
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
10
|
+
* --ui-text-field-edge resting edge (default: the hairline), shared with Text Field
|
|
11
|
+
* --ui-text-field-edge-hover edge on hover
|
|
12
|
+
* --ui-text-field-edge-focus edge while focused (default: the input border, 3:1 or more)
|
|
13
|
+
* --ui-text-area-radius corners (default 8px; Classic 10px)
|
|
14
|
+
*/
|
|
15
|
+
import {
|
|
16
|
+
Component,
|
|
17
|
+
DOCUMENT,
|
|
18
|
+
DestroyRef,
|
|
19
|
+
Directive,
|
|
20
|
+
ElementRef,
|
|
21
|
+
LOCALE_ID,
|
|
22
|
+
afterNextRender,
|
|
23
|
+
afterRenderEffect,
|
|
24
|
+
booleanAttribute,
|
|
25
|
+
computed,
|
|
26
|
+
contentChild,
|
|
27
|
+
effect,
|
|
28
|
+
inject,
|
|
29
|
+
input,
|
|
30
|
+
isDevMode,
|
|
31
|
+
model,
|
|
32
|
+
numberAttribute,
|
|
33
|
+
output,
|
|
34
|
+
signal,
|
|
35
|
+
untracked,
|
|
36
|
+
viewChild,
|
|
37
|
+
} from '@angular/core';
|
|
38
|
+
|
|
39
|
+
export interface TextAreaLabels {
|
|
40
|
+
optional: string;
|
|
41
|
+
moreRoom: string;
|
|
42
|
+
lessRoom: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const LABELS: TextAreaLabels = { optional: 'Optional', moreRoom: 'More room', lessRoom: 'Less room' };
|
|
46
|
+
|
|
47
|
+
let nextId = 0;
|
|
48
|
+
|
|
49
|
+
/** Whether `now` is `was` with text added in one place (typing or a paste), nothing taken out. */
|
|
50
|
+
function inserted(was: string, now: string) {
|
|
51
|
+
if (now.length < was.length) return false;
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (i < was.length && was[i] === now[i]) i++;
|
|
54
|
+
return now.endsWith(was.slice(i));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Put it on the `<textarea>` inside a `<ui-text-area>`. It takes the field's label, description, error state and height. */
|
|
58
|
+
@Directive({
|
|
59
|
+
selector: 'textarea[uiTextarea]',
|
|
60
|
+
host: {
|
|
61
|
+
class: 'ui-textarea',
|
|
62
|
+
dir: 'auto',
|
|
63
|
+
style: 'outline: 0',
|
|
64
|
+
'[id]': 'id()',
|
|
65
|
+
'[attr.rows]': 'field.minRows()',
|
|
66
|
+
'[attr.aria-describedby]': 'describedBy()',
|
|
67
|
+
'[attr.aria-invalid]': 'field.invalid() || null',
|
|
68
|
+
'[attr.aria-keyshortcuts]': "field.submitShortcut() ? 'Meta+Enter Control+Enter' : null",
|
|
69
|
+
'(input)': 'read(); field.typed()',
|
|
70
|
+
'(keydown)': 'field.key($event)',
|
|
71
|
+
'(document:reset)': 'reset($event)',
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
export class UiTextarea {
|
|
75
|
+
readonly id = input(`ui-text-area-${nextId++}`);
|
|
76
|
+
/** Your own descriptions, kept alongside the field's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
|
|
77
|
+
readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
|
|
78
|
+
|
|
79
|
+
readonly el = inject<ElementRef<HTMLTextAreaElement>>(ElementRef).nativeElement;
|
|
80
|
+
protected readonly field = inject(UiTextArea);
|
|
81
|
+
private readonly destroy = inject(DestroyRef);
|
|
82
|
+
readonly value = signal('');
|
|
83
|
+
readonly disabled = signal(false);
|
|
84
|
+
readonly readOnly = signal(false);
|
|
85
|
+
readonly maxLength = signal(-1);
|
|
86
|
+
|
|
87
|
+
protected readonly describedBy = computed(
|
|
88
|
+
() => [this.ariaDescribedby(), ...this.field.describers()].filter(Boolean).join(' ') || null,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
constructor() {
|
|
92
|
+
const el = this.el;
|
|
93
|
+
const native = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value');
|
|
94
|
+
if (native?.set && native.get) {
|
|
95
|
+
Object.defineProperty(el, 'value', {
|
|
96
|
+
configurable: true,
|
|
97
|
+
get: () => native.get!.call(el),
|
|
98
|
+
set: (v: string) => {
|
|
99
|
+
native.set!.call(el, v);
|
|
100
|
+
queueMicrotask(() => this.read());
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
afterNextRender(() => {
|
|
105
|
+
this.read();
|
|
106
|
+
const view = el.ownerDocument.defaultView;
|
|
107
|
+
if (!view?.MutationObserver) return;
|
|
108
|
+
const watch = new view.MutationObserver(() => this.read());
|
|
109
|
+
watch.observe(el, { attributes: true, attributeFilter: ['disabled', 'readonly', 'maxlength'] });
|
|
110
|
+
this.destroy.onDestroy(() => watch.disconnect());
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
read() {
|
|
115
|
+
const el = this.el;
|
|
116
|
+
this.value.set(el.value);
|
|
117
|
+
this.disabled.set(el.disabled);
|
|
118
|
+
this.readOnly.set(el.readOnly);
|
|
119
|
+
this.maxLength.set(el.maxLength);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected reset(e: Event) {
|
|
123
|
+
if (e.target !== this.el.form) return;
|
|
124
|
+
setTimeout(() => {
|
|
125
|
+
this.read();
|
|
126
|
+
this.field.fit();
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@Component({
|
|
132
|
+
selector: 'ui-text-area',
|
|
133
|
+
host: {
|
|
134
|
+
'[attr.data-invalid]': 'invalid() || null',
|
|
135
|
+
'[attr.data-readonly]': 'control()?.readOnly() || null',
|
|
136
|
+
'[attr.data-disabled]': 'control()?.disabled() || null',
|
|
137
|
+
'[attr.data-expandable]': 'expandable() || null',
|
|
138
|
+
},
|
|
139
|
+
template: `
|
|
140
|
+
@if (label()) {
|
|
141
|
+
<div class="top">
|
|
142
|
+
<label [for]="control()?.id()">{{ label() }}@if (optional()) {<span class="ui-sr"> ({{ t().optional }})</span>}</label>
|
|
143
|
+
@if (optional()) {<span class="opt" aria-hidden="true">{{ t().optional }}</span>}
|
|
144
|
+
</div>
|
|
145
|
+
}
|
|
146
|
+
<div class="box" [attr.data-nudge]="nudge() || null" (animationend)="$event.target === $event.currentTarget && nudge.set(false)" (animationcancel)="$event.target === $event.currentTarget && nudge.set(false)">
|
|
147
|
+
<ng-content select="textarea" />
|
|
148
|
+
@if (expandable()) {
|
|
149
|
+
<button type="button" class="ui-btn-icon corner" [attr.aria-label]="expanded() ? t().lessRoom : t().moreRoom" [attr.aria-controls]="control()?.id()" [disabled]="control()?.disabled()" (click)="toggle()">
|
|
150
|
+
@if (expanded()) {
|
|
151
|
+
<svg class="ui-icon" [class.ui-enter]="swapped" viewBox="0 0 16 16" aria-hidden="true"><path d="M13.5 2.5l-4 4M9.5 3v3.5H13M2.5 13.5l4-4M3 9.5h3.5V13" /></svg>
|
|
152
|
+
} @else {
|
|
153
|
+
<svg class="ui-icon" [class.ui-enter]="swapped" viewBox="0 0 16 16" aria-hidden="true"><path d="M9.5 6.5l4-4M10 2.5h3.5V6M6.5 9.5l-4 4M2.5 10v3.5H6" /></svg>
|
|
154
|
+
}
|
|
155
|
+
</button>
|
|
156
|
+
}
|
|
157
|
+
</div>
|
|
158
|
+
<div class="foot" [class.ui-sr]="!message() && !counter()">
|
|
159
|
+
<p #msg [id]="messageId" dir="auto" aria-live="polite">{{ message() }}</p>
|
|
160
|
+
@if (counter() && limit() >= 0) {<span class="count" [id]="countId" [attr.data-state]="countState()">{{ num.format(length()) }}/{{ num.format(limit()) }}</span>}
|
|
161
|
+
</div>
|
|
162
|
+
`,
|
|
163
|
+
styles: `
|
|
164
|
+
:host {
|
|
165
|
+
--_t: var(--ui-look-tray, 0);
|
|
166
|
+
--_e: var(--ui-text-field-edge, var(--ui-hairline));
|
|
167
|
+
--_w: calc(0.5px + 0.5px * var(--_t));
|
|
168
|
+
--_fill: var(--ui-bg-surface);
|
|
169
|
+
--_py: calc(var(--ui-space-1-5) + 1px * var(--_t));
|
|
170
|
+
--_px: calc(var(--ui-space-2-5) - 1px + 2px * var(--_t));
|
|
171
|
+
--_lh: calc(var(--ui-leading-base) + 1px * var(--_t));
|
|
172
|
+
--_b: calc(var(--ui-space-6) + 2px);
|
|
173
|
+
display: flex;
|
|
174
|
+
flex-direction: column;
|
|
175
|
+
gap: calc(var(--ui-space-1-5) + 1px * var(--_t));
|
|
176
|
+
min-inline-size: 0;
|
|
177
|
+
color: var(--ui-text);
|
|
178
|
+
font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
|
|
179
|
+
}
|
|
180
|
+
:host([data-expandable]) .box { padding-inline-end: calc(var(--_b) + var(--ui-space-2) + 1px - var(--_px)); }
|
|
181
|
+
.top { display: flex; align-items: baseline; gap: var(--ui-space-1-5); padding-inline: calc(2px * var(--_t)); }
|
|
182
|
+
label { font-weight: var(--ui-weight-medium); }
|
|
183
|
+
.opt, .foot { color: var(--ui-text-muted); font-size: calc(var(--ui-text-sm) + 0.5px * var(--_t)); line-height: calc(var(--ui-leading-xs) + 1px * var(--_t)); }
|
|
184
|
+
.box {
|
|
185
|
+
position: relative;
|
|
186
|
+
display: flex;
|
|
187
|
+
border: 1px solid transparent;
|
|
188
|
+
border-radius: var(--ui-text-area-radius, calc(var(--ui-radius-md) + 2px * var(--_t)));
|
|
189
|
+
background: var(--_fill);
|
|
190
|
+
box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
191
|
+
font-size: calc(var(--ui-text-base) + 0.5px * var(--_t));
|
|
192
|
+
transition: var(--ui-duration-2) var(--ui-ease-out);
|
|
193
|
+
transition-property: box-shadow, background;
|
|
194
|
+
}
|
|
195
|
+
@media (hover: hover) { .box:hover { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
|
|
196
|
+
.box:focus-within { --_w: 1px; --_e: var(--ui-text-field-edge-focus, var(--ui-border-input)); }
|
|
197
|
+
:host([data-invalid]) .box {
|
|
198
|
+
--_w: 1px;
|
|
199
|
+
--_e: color-mix(in srgb, var(--ui-danger) 55%, transparent);
|
|
200
|
+
--_fill: color-mix(in srgb, var(--ui-danger-soft) calc(50% * var(--_t)), var(--ui-bg-surface));
|
|
201
|
+
}
|
|
202
|
+
:host([data-invalid]) .box:focus-within { --_e: var(--ui-danger); }
|
|
203
|
+
:host([data-readonly]) .box { --_fill: var(--ui-bg-subtle); }
|
|
204
|
+
:host([data-disabled]) { opacity: var(--ui-disabled-opacity); }
|
|
205
|
+
:host([data-disabled]) .box { cursor: not-allowed; --_e: var(--ui-hairline); }
|
|
206
|
+
:host([data-disabled]) .corner { opacity: 1; }
|
|
207
|
+
.box[data-nudge] { animation: ui-nudge calc(320ms * var(--ui-motion-scale)) var(--ui-ease-out); }
|
|
208
|
+
.corner { --_s: var(--_b); position: absolute; inset-block-start: calc(var(--ui-space-1) - 1px); inset-inline-end: calc(var(--ui-space-1) - 1px); }
|
|
209
|
+
.foot { display: flex; gap: var(--ui-space-3); padding-inline: calc(2px * var(--_t)); }
|
|
210
|
+
p { flex: 1; margin: 0; }
|
|
211
|
+
:host([data-invalid]) p { color: var(--ui-danger); }
|
|
212
|
+
.count { margin-inline-start: auto; font-variant-numeric: tabular-nums; }
|
|
213
|
+
.count[data-state='full'] { color: var(--ui-text); }
|
|
214
|
+
.count[data-state='over'] { color: var(--ui-danger); }
|
|
215
|
+
@media (pointer: coarse) {
|
|
216
|
+
:host { --_b: var(--ui-space-8); --_lh: calc(var(--ui-text-field-touch) * 1.5); }
|
|
217
|
+
.box { font-size: var(--ui-text-field-touch); }
|
|
218
|
+
}
|
|
219
|
+
@media (prefers-reduced-motion: reduce) {
|
|
220
|
+
.box[data-nudge] { animation: none; }
|
|
221
|
+
}
|
|
222
|
+
@media (forced-colors: active) {
|
|
223
|
+
.box { border-color: CanvasText; }
|
|
224
|
+
.box:focus-within { outline: 2px solid Highlight; }
|
|
225
|
+
:host([data-invalid]) .box { outline: 1px solid CanvasText; }
|
|
226
|
+
}
|
|
227
|
+
`,
|
|
228
|
+
})
|
|
229
|
+
export class UiTextArea {
|
|
230
|
+
/** Shown above the field. Clicking it focuses the text. */
|
|
231
|
+
readonly label = input<string>();
|
|
232
|
+
/** A quiet line under the field. The error takes its place while there is one. */
|
|
233
|
+
readonly hint = input<string>();
|
|
234
|
+
/** Marks the field invalid and shows this message where the hint was. */
|
|
235
|
+
readonly error = input<string | null | undefined>();
|
|
236
|
+
/** Adds "Optional" after the label. */
|
|
237
|
+
readonly optional = input(false, { transform: booleanAttribute });
|
|
238
|
+
/** Shows "120/2,000" under the field. Needs `maxlength` on the textarea. */
|
|
239
|
+
readonly counter = input(false, { transform: booleanAttribute });
|
|
240
|
+
/** Height while empty, in lines. */
|
|
241
|
+
readonly minRows = input(3, { transform: (v: unknown) => numberAttribute(v, 3) });
|
|
242
|
+
/** The field grows with the text up to this many lines, then scrolls. */
|
|
243
|
+
readonly maxRows = input(10, { transform: (v: unknown) => numberAttribute(v, 10) });
|
|
244
|
+
/** A corner button that gives the field more room, and takes it back. */
|
|
245
|
+
readonly expandable = input(false, { transform: booleanAttribute });
|
|
246
|
+
/** Lines while expanded. */
|
|
247
|
+
readonly expandedRows = input(16, { transform: (v: unknown) => numberAttribute(v, 16) });
|
|
248
|
+
/** Whether the field has more room. Bind `[(expanded)]` to control it. */
|
|
249
|
+
readonly expanded = model(false);
|
|
250
|
+
/** ⌘+Enter or Ctrl+Enter emits `submitted`. Enter alone still starts a new line. */
|
|
251
|
+
readonly submitShortcut = input(false, { transform: booleanAttribute });
|
|
252
|
+
/** Words for the tag and the corner button, to translate them. */
|
|
253
|
+
readonly labels = input<Partial<TextAreaLabels>>({});
|
|
254
|
+
/** The value, when ⌘+Enter or Ctrl+Enter is pressed with `submitShortcut`. */
|
|
255
|
+
readonly submitted = output<string>();
|
|
256
|
+
|
|
257
|
+
protected readonly control = contentChild(UiTextarea);
|
|
258
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
259
|
+
protected readonly num = new Intl.NumberFormat(inject(LOCALE_ID));
|
|
260
|
+
protected readonly messageId = `ui-text-area-msg-${nextId++}`;
|
|
261
|
+
protected readonly countId = `${this.messageId}-count`;
|
|
262
|
+
|
|
263
|
+
readonly invalid = computed(() => !!this.error());
|
|
264
|
+
protected readonly message = computed(() => this.error() || this.hint() || '');
|
|
265
|
+
protected readonly limit = computed(() => this.control()?.maxLength() ?? -1);
|
|
266
|
+
protected readonly length = computed(() => this.control()?.value().length ?? 0);
|
|
267
|
+
protected readonly countState = computed(() => {
|
|
268
|
+
const [n, max] = [this.length(), this.limit()];
|
|
269
|
+
return n > max ? 'over' : n === max ? 'full' : null;
|
|
270
|
+
});
|
|
271
|
+
readonly describers = computed(() => [
|
|
272
|
+
this.message() ? this.messageId : '',
|
|
273
|
+
this.counter() && this.limit() >= 0 ? this.countId : '',
|
|
274
|
+
]);
|
|
275
|
+
|
|
276
|
+
protected readonly nudge = signal(false);
|
|
277
|
+
protected swapped = false;
|
|
278
|
+
private readonly msg = viewChild<ElementRef<HTMLElement>>('msg');
|
|
279
|
+
private readonly doc = inject(DOCUMENT);
|
|
280
|
+
private readonly destroyRef = inject(DestroyRef);
|
|
281
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
282
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
283
|
+
private fitted = '';
|
|
284
|
+
private fittedText = '';
|
|
285
|
+
private metrics?: { line: number; pad: number };
|
|
286
|
+
|
|
287
|
+
constructor() {
|
|
288
|
+
let was: boolean | undefined;
|
|
289
|
+
effect(() => {
|
|
290
|
+
const now = this.invalid();
|
|
291
|
+
if (now && was === false && this.motion()) this.nudge.set(true);
|
|
292
|
+
was = now;
|
|
293
|
+
});
|
|
294
|
+
let shown: string | undefined;
|
|
295
|
+
afterRenderEffect(() => {
|
|
296
|
+
const text = this.message();
|
|
297
|
+
const p = this.msg()?.nativeElement;
|
|
298
|
+
if (shown !== undefined && text !== shown && p?.animate && this.motion()) {
|
|
299
|
+
p.animate([{ opacity: 0, filter: 'blur(2px)', transform: 'translateY(-2px)' }, {}], { duration: 260 * this.motion(), easing: 'ease-out' });
|
|
300
|
+
}
|
|
301
|
+
shown = text;
|
|
302
|
+
});
|
|
303
|
+
let open: boolean | undefined;
|
|
304
|
+
afterRenderEffect(() => {
|
|
305
|
+
this.control()?.value();
|
|
306
|
+
this.minRows();
|
|
307
|
+
this.maxRows();
|
|
308
|
+
this.expandedRows();
|
|
309
|
+
const now = this.expanded();
|
|
310
|
+
const toggled = open !== undefined && now !== open;
|
|
311
|
+
open = now;
|
|
312
|
+
untracked(() => {
|
|
313
|
+
if (toggled || this.fitted !== this.state()) this.fit(toggled);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
afterNextRender(() => {
|
|
317
|
+
const c = this.control();
|
|
318
|
+
if (isDevMode() && c && !this.label() && !c.el.ariaLabel && !c.el.hasAttribute('aria-labelledby')) {
|
|
319
|
+
console.warn('ui-text-area: give it a label, or an aria-label on the textarea.', c.el);
|
|
320
|
+
}
|
|
321
|
+
const view = this.doc.defaultView;
|
|
322
|
+
if (!c || !view?.ResizeObserver) return;
|
|
323
|
+
let width = 0;
|
|
324
|
+
let frame = 0;
|
|
325
|
+
const refit = () => {
|
|
326
|
+
this.metrics = undefined;
|
|
327
|
+
this.fit();
|
|
328
|
+
};
|
|
329
|
+
const watch = new view.ResizeObserver(([entry]) => {
|
|
330
|
+
if (entry.contentRect.width === width) return;
|
|
331
|
+
width = entry.contentRect.width;
|
|
332
|
+
view.cancelAnimationFrame(frame);
|
|
333
|
+
frame = view.requestAnimationFrame(refit);
|
|
334
|
+
});
|
|
335
|
+
watch.observe(c.el);
|
|
336
|
+
this.doc.fonts?.ready.then(refit);
|
|
337
|
+
this.destroyRef.onDestroy(() => {
|
|
338
|
+
watch.disconnect();
|
|
339
|
+
view.cancelAnimationFrame(frame);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
this.destroyRef.onDestroy(() => clearTimeout(this.timer));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Sets the height for the current text: grows between minRows and maxRows, or holds expandedRows while expanded. */
|
|
346
|
+
fit(animate = false) {
|
|
347
|
+
const el = this.control()?.el;
|
|
348
|
+
const m = this.measure();
|
|
349
|
+
if (!el || !m) return;
|
|
350
|
+
const [from, top] = [el.offsetHeight, el.scrollTop];
|
|
351
|
+
el.removeAttribute('data-grow');
|
|
352
|
+
el.style.overflowY = 'hidden';
|
|
353
|
+
el.style.height = '0';
|
|
354
|
+
const content = el.scrollHeight;
|
|
355
|
+
const to = this.height(content, m);
|
|
356
|
+
if (animate && from && from !== to && this.motion()) {
|
|
357
|
+
el.style.height = `${from}px`;
|
|
358
|
+
void el.offsetHeight;
|
|
359
|
+
el.setAttribute('data-grow', '');
|
|
360
|
+
clearTimeout(this.timer);
|
|
361
|
+
this.timer = setTimeout(() => el.removeAttribute('data-grow'), 400 * this.motion());
|
|
362
|
+
}
|
|
363
|
+
el.style.height = `${to}px`;
|
|
364
|
+
el.style.overflowY = content > to + 1 ? 'auto' : 'hidden';
|
|
365
|
+
el.scrollTop = top;
|
|
366
|
+
this.done();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** An insertion can only add lines, so it grows straight to the text; anything else measures from scratch. */
|
|
370
|
+
typed() {
|
|
371
|
+
const el = this.control()?.el;
|
|
372
|
+
const m = this.metrics;
|
|
373
|
+
const text = this.control()?.value() ?? '';
|
|
374
|
+
if (!el || !m || !inserted(this.fittedText, text) || this.fitted !== this.state(this.fittedText)) return this.fit();
|
|
375
|
+
const content = el.scrollHeight;
|
|
376
|
+
if (content > el.clientHeight) {
|
|
377
|
+
const to = this.height(content, m);
|
|
378
|
+
el.removeAttribute('data-grow');
|
|
379
|
+
el.style.height = `${to}px`;
|
|
380
|
+
el.style.overflowY = content > to + 1 ? 'auto' : 'hidden';
|
|
381
|
+
}
|
|
382
|
+
this.done();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private measure() {
|
|
386
|
+
const el = this.control()?.el;
|
|
387
|
+
const view = this.doc.defaultView;
|
|
388
|
+
if (!this.metrics && el && view) {
|
|
389
|
+
const cs = view.getComputedStyle(el);
|
|
390
|
+
const line = parseFloat(cs.lineHeight) || parseFloat(cs.fontSize) * 1.2;
|
|
391
|
+
if (line) this.metrics = { line, pad: parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom) };
|
|
392
|
+
}
|
|
393
|
+
return this.metrics;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private height(content: number, { line, pad }: { line: number; pad: number }) {
|
|
397
|
+
const rows = (n: number) => Math.max(1, n) * line + pad;
|
|
398
|
+
const min = rows(this.minRows());
|
|
399
|
+
const compact = Math.min(Math.max(content, min), Math.max(rows(this.maxRows()), min));
|
|
400
|
+
return this.expanded() && this.expandable() ? Math.max(rows(this.expandedRows()), compact) : compact;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private done() {
|
|
404
|
+
this.fittedText = this.control()?.value() ?? '';
|
|
405
|
+
this.fitted = this.state();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private state(text = this.control()?.value()) {
|
|
409
|
+
return [text, this.expanded(), this.minRows(), this.maxRows(), this.expandedRows()].join('|');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
key(event: Event) {
|
|
413
|
+
const e = event as KeyboardEvent;
|
|
414
|
+
const el = this.control()?.el;
|
|
415
|
+
if (!el || el.readOnly || !this.submitShortcut() || e.key !== 'Enter' || !(e.metaKey || e.ctrlKey) || e.altKey || e.repeat || e.isComposing) return;
|
|
416
|
+
e.preventDefault();
|
|
417
|
+
this.submitted.emit(el.value);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
protected toggle() {
|
|
421
|
+
this.swapped = true;
|
|
422
|
+
this.expanded.update((v) => !v);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** 0 under reduced motion, otherwise the app's motion scale (1 by default). */
|
|
426
|
+
private motion() {
|
|
427
|
+
const view = this.doc.defaultView;
|
|
428
|
+
if (!view?.matchMedia || view.matchMedia('(prefers-reduced-motion: reduce)').matches) return 0;
|
|
429
|
+
const scale = parseFloat(view.getComputedStyle(this.host).getPropertyValue('--ui-motion-scale'));
|
|
430
|
+
return Number.isNaN(scale) ? 1 : scale;
|
|
431
|
+
}
|
|
432
|
+
}
|