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,552 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Number Input, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (`npx ochreui init`).
|
|
5
|
+
*
|
|
6
|
+
* <ui-number-input label="Replicas" unit="pods"><input uiNumberInput [formField]="form.replicas" min="1" max="20" /></ui-number-input>
|
|
7
|
+
* Your own <input> goes inside. It shows "32,000" but the form gets 32000; − and + step it and repeat while held.
|
|
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-field-radius corners (default 7px; 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
|
+
forwardRef,
|
|
29
|
+
inject,
|
|
30
|
+
input,
|
|
31
|
+
isDevMode,
|
|
32
|
+
linkedSignal,
|
|
33
|
+
model,
|
|
34
|
+
signal,
|
|
35
|
+
untracked,
|
|
36
|
+
viewChild,
|
|
37
|
+
} from '@angular/core';
|
|
38
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
39
|
+
|
|
40
|
+
export type NumberInputSize = 'sm' | 'md' | 'lg';
|
|
41
|
+
|
|
42
|
+
export interface NumberInputLabels {
|
|
43
|
+
optional: string;
|
|
44
|
+
decrease: string;
|
|
45
|
+
increase: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const LABELS: NumberInputLabels = { optional: 'Optional', decrease: 'Decrease', increase: 'Increase' };
|
|
49
|
+
|
|
50
|
+
let nextId = 0;
|
|
51
|
+
|
|
52
|
+
const bound = (v: unknown) => (v == null || v === '' ? undefined : Number(v));
|
|
53
|
+
const ascii = (s: string) => s.replace(/[\uff01-\uff5e]/g, (c) => String.fromCharCode(c.charCodeAt(0) - 0xfee0));
|
|
54
|
+
const decimals = (n: number) => {
|
|
55
|
+
const [, frac = ''] = String(n).split('.');
|
|
56
|
+
return frac.length;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** Put it on the `<input>` inside a `<ui-number-input>`. It parses, formats, clamps and steps the number. */
|
|
60
|
+
@Directive({
|
|
61
|
+
selector: 'input[uiNumberInput]',
|
|
62
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiNumber), multi: true }],
|
|
63
|
+
host: {
|
|
64
|
+
class: 'ui-input',
|
|
65
|
+
type: 'text',
|
|
66
|
+
role: 'spinbutton',
|
|
67
|
+
dir: 'ltr',
|
|
68
|
+
autocomplete: 'off',
|
|
69
|
+
autocorrect: 'off',
|
|
70
|
+
spellcheck: 'false',
|
|
71
|
+
style: 'flex: 0 1 auto; min-inline-size: 1ch; field-sizing: content; outline: 0; font-variant-numeric: tabular-nums',
|
|
72
|
+
'[id]': 'id()',
|
|
73
|
+
'[size]': 'size()',
|
|
74
|
+
'[attr.name]': 'null',
|
|
75
|
+
'[attr.inputmode]': 'inputMode()',
|
|
76
|
+
'[attr.aria-valuenow]': 'current()',
|
|
77
|
+
'[attr.aria-valuemin]': 'min() ?? null',
|
|
78
|
+
'[attr.aria-valuemax]': 'max() ?? null',
|
|
79
|
+
'[attr.aria-valuetext]': 'valueText()',
|
|
80
|
+
'[attr.aria-describedby]': 'describedBy()',
|
|
81
|
+
'[attr.aria-invalid]': 'field.invalid() || null',
|
|
82
|
+
'(input)': 'typed($event)',
|
|
83
|
+
'(compositionend)': 'typed()',
|
|
84
|
+
'(blur)': 'commit(); touched()',
|
|
85
|
+
'(keydown)': 'key($event)',
|
|
86
|
+
'(document:reset)': 'reset($event)',
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
export class UiNumber implements ControlValueAccessor {
|
|
90
|
+
/** The number; `null` when empty. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
|
|
91
|
+
readonly value = model<number | null>(null);
|
|
92
|
+
/** What the field holds now. Form writes land here without emitting `valueChange`. */
|
|
93
|
+
readonly current = linkedSignal(() => this.value());
|
|
94
|
+
readonly min = input<number | undefined, unknown>(undefined, { transform: bound });
|
|
95
|
+
readonly max = input<number | undefined, unknown>(undefined, { transform: bound });
|
|
96
|
+
/** What − / + and the arrow keys add or remove. Steps land on multiples of it, counted from `min`. */
|
|
97
|
+
readonly step = input(1, { transform: (v: unknown) => bound(v) || 1 });
|
|
98
|
+
/** What Page Up / Page Down add or remove, landing on the step grid. Default: ten steps. */
|
|
99
|
+
readonly largeStep = input<number | undefined, unknown>(undefined, { transform: bound });
|
|
100
|
+
/** Display and typing locale. Default: the app's LOCALE_ID. */
|
|
101
|
+
readonly locale = input<string>();
|
|
102
|
+
/** How the value is shown, e.g. `{ minimumFractionDigits: 2, maximumFractionDigits: 2 }` or `{ style: 'percent' }`. */
|
|
103
|
+
readonly format = input<Intl.NumberFormatOptions>();
|
|
104
|
+
/** Submits the plain number (4000, not "4,000") with the form, through a hidden input. */
|
|
105
|
+
readonly name = input<string>();
|
|
106
|
+
readonly id = input(`ui-number-input-${nextId++}`);
|
|
107
|
+
/** Your own descriptions, kept alongside the field's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
|
|
108
|
+
readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
|
|
109
|
+
|
|
110
|
+
readonly el = inject<ElementRef<HTMLInputElement>>(ElementRef).nativeElement;
|
|
111
|
+
protected readonly field = inject(UiNumberInput);
|
|
112
|
+
private readonly appLocale = inject(LOCALE_ID);
|
|
113
|
+
private readonly destroy = inject(DestroyRef);
|
|
114
|
+
readonly disabled = signal(false);
|
|
115
|
+
readonly readOnly = signal(false);
|
|
116
|
+
private readonly text = signal('');
|
|
117
|
+
private committed: number | null = null;
|
|
118
|
+
private roll?: Animation;
|
|
119
|
+
private onChange: (v: number | null) => void = () => {};
|
|
120
|
+
private onTouched: () => void = () => {};
|
|
121
|
+
|
|
122
|
+
readonly fmt = computed(
|
|
123
|
+
() =>
|
|
124
|
+
new Intl.NumberFormat(
|
|
125
|
+
this.locale() ?? this.appLocale,
|
|
126
|
+
this.format() ?? { maximumFractionDigits: Math.max(decimals(this.step()), 3) },
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
private readonly marks = computed(() => {
|
|
130
|
+
const parts = this.fmt().formatToParts(-12345.6);
|
|
131
|
+
const of = (type: string) => parts.find((p) => p.type === type)?.value ?? '';
|
|
132
|
+
return { group: of('group'), decimal: of('decimal') || '.' };
|
|
133
|
+
});
|
|
134
|
+
private readonly percent = computed(() => this.fmt().resolvedOptions().style === 'percent');
|
|
135
|
+
protected readonly size = computed(() => Math.max(1, this.text().length));
|
|
136
|
+
protected readonly inputMode = computed(() => {
|
|
137
|
+
const min = this.min();
|
|
138
|
+
if (min === undefined || min < 0) return 'text';
|
|
139
|
+
return decimals(this.step()) || this.format()?.maximumFractionDigits || this.percent() ? 'decimal' : 'numeric';
|
|
140
|
+
});
|
|
141
|
+
protected readonly valueText = computed(() => {
|
|
142
|
+
const v = this.current();
|
|
143
|
+
return v === null ? null : [this.field.prefix(), this.display(v), this.field.unit()].filter(Boolean).join(' ');
|
|
144
|
+
});
|
|
145
|
+
readonly atMin = computed(() => this.current() !== null && this.min() !== undefined && this.current()! <= this.min()!);
|
|
146
|
+
readonly atMax = computed(() => this.current() !== null && this.max() !== undefined && this.current()! >= this.max()!);
|
|
147
|
+
protected readonly describedBy = computed(
|
|
148
|
+
() => [this.ariaDescribedby(), ...this.field.describers()].filter(Boolean).join(' ') || null,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
constructor() {
|
|
152
|
+
effect(() => {
|
|
153
|
+
const v = this.current();
|
|
154
|
+
this.fmt();
|
|
155
|
+
untracked(() => this.show(v));
|
|
156
|
+
});
|
|
157
|
+
afterNextRender(() => {
|
|
158
|
+
const el = this.el;
|
|
159
|
+
const read = () => {
|
|
160
|
+
this.disabled.set(el.disabled);
|
|
161
|
+
this.readOnly.set(el.readOnly);
|
|
162
|
+
};
|
|
163
|
+
read();
|
|
164
|
+
const view = el.ownerDocument.defaultView;
|
|
165
|
+
if (!view?.MutationObserver) return;
|
|
166
|
+
const watch = new view.MutationObserver(read);
|
|
167
|
+
watch.observe(el, { attributes: true, attributeFilter: ['disabled', 'readonly'] });
|
|
168
|
+
this.destroy.onDestroy(() => watch.disconnect());
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
display(v: number | null) {
|
|
173
|
+
return v === null ? '' : this.fmt().format(v);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Reads text as a number in the field's locale: group marks and spaces are ignored, `null` when empty. */
|
|
177
|
+
parse(text: string): number | null {
|
|
178
|
+
const { group, decimal } = this.marks();
|
|
179
|
+
let s = ascii(text).replace(/[\s ]/g, '');
|
|
180
|
+
if (group) s = s.split(group).join('');
|
|
181
|
+
s = s.split(decimal).join('.').replace(/[−‒–]/g, '-').replace(/[^0-9.-]/g, '');
|
|
182
|
+
if (!s || s === '-' || s === '.') return null;
|
|
183
|
+
const n = parseFloat(s.replace(/(?!^)-/g, ''));
|
|
184
|
+
if (Number.isNaN(n)) return null;
|
|
185
|
+
return this.percent() ? n / 100 : n;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private clamp(v: number) {
|
|
189
|
+
const [min, max] = [this.min(), this.max()];
|
|
190
|
+
return Math.min(max ?? Infinity, Math.max(min ?? -Infinity, v));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private round(v: number) {
|
|
194
|
+
const digits = this.fmt().resolvedOptions().maximumFractionDigits ?? 3;
|
|
195
|
+
return +v.toFixed(Math.min(20, digits + (this.percent() ? 2 : 0)));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private show(v: number | null) {
|
|
199
|
+
this.committed = v;
|
|
200
|
+
const text = this.display(v);
|
|
201
|
+
if (this.el.value !== text) this.el.value = text;
|
|
202
|
+
this.text.set(text);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private set(v: number | null) {
|
|
206
|
+
if (v === this.current()) return this.show(v);
|
|
207
|
+
this.current.set(v);
|
|
208
|
+
this.value.set(v);
|
|
209
|
+
this.onChange(v);
|
|
210
|
+
this.show(v);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
protected typed(e?: Event) {
|
|
214
|
+
if ((e as InputEvent | undefined)?.isComposing) return;
|
|
215
|
+
const el = this.el;
|
|
216
|
+
const { group, decimal } = this.marks();
|
|
217
|
+
const allowed = new Set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '−', ' ', group, decimal, this.percent() ? '%' : '']);
|
|
218
|
+
const clean = [...ascii(el.value)].filter((c) => allowed.has(c) || /\s/.test(c)).join('');
|
|
219
|
+
if (clean !== el.value) {
|
|
220
|
+
const at = (el.selectionStart ?? clean.length) - (el.value.length - clean.length);
|
|
221
|
+
el.value = clean;
|
|
222
|
+
el.setSelectionRange(Math.max(0, at), Math.max(0, at));
|
|
223
|
+
}
|
|
224
|
+
this.text.set(clean);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Parses, clamps and rounds what was typed, then shows it formatted. */
|
|
228
|
+
commit() {
|
|
229
|
+
const raw = this.parse(this.el.value);
|
|
230
|
+
const v = raw === null ? null : this.round(this.clamp(raw));
|
|
231
|
+
if (v !== this.current() || this.el.value !== this.display(v)) {
|
|
232
|
+
this.set(v);
|
|
233
|
+
this.el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
protected touched() {
|
|
238
|
+
this.onTouched();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Moves one step (or `largeStep` with `large`) up or down. Returns false at a limit. */
|
|
242
|
+
stepBy(dir: 1 | -1, large = false): boolean {
|
|
243
|
+
if (this.disabled() || this.readOnly()) return false;
|
|
244
|
+
this.commit();
|
|
245
|
+
const v = this.current();
|
|
246
|
+
const [min, max] = [this.min(), this.max()];
|
|
247
|
+
const size = large ? (this.largeStep() ?? this.step() * 10) : this.step();
|
|
248
|
+
const grid = min ?? 0;
|
|
249
|
+
let next: number;
|
|
250
|
+
if (v === null) next = this.clamp(min ?? 0);
|
|
251
|
+
else {
|
|
252
|
+
const step = this.step();
|
|
253
|
+
const k = (v - grid) / step;
|
|
254
|
+
next = large
|
|
255
|
+
? grid + Math.round((v + dir * size - grid) / step) * step
|
|
256
|
+
: grid + (dir > 0 ? Math.floor(k + 1e-9) + 1 : Math.ceil(k - 1e-9) - 1) * step;
|
|
257
|
+
next = +next.toFixed(Math.max(decimals(size), decimals(grid), decimals(this.step())));
|
|
258
|
+
next = this.clamp(next);
|
|
259
|
+
}
|
|
260
|
+
if (next === v || (dir > 0 && max !== undefined && v !== null && v >= max) || (dir < 0 && min !== undefined && v !== null && v <= min)) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
this.set(next);
|
|
264
|
+
this.el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
265
|
+
this.animate(dir);
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private animate(dir: number) {
|
|
270
|
+
const motion = this.field.motion();
|
|
271
|
+
if (!motion || !this.el.animate) return;
|
|
272
|
+
this.roll?.cancel();
|
|
273
|
+
this.roll = this.el.animate(
|
|
274
|
+
[{ transform: `translateY(${dir * 5}px)`, opacity: 0.35, filter: 'blur(1.5px)' }, {}],
|
|
275
|
+
{ duration: 220 * motion, easing: 'ease-out' },
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
protected key(e: KeyboardEvent) {
|
|
280
|
+
if (e.isComposing) return;
|
|
281
|
+
const [min, max] = [this.min(), this.max()];
|
|
282
|
+
const go = (fn: () => unknown) => {
|
|
283
|
+
e.preventDefault();
|
|
284
|
+
fn();
|
|
285
|
+
};
|
|
286
|
+
switch (e.key) {
|
|
287
|
+
case 'ArrowUp':
|
|
288
|
+
return go(() => this.stepBy(1));
|
|
289
|
+
case 'ArrowDown':
|
|
290
|
+
return go(() => this.stepBy(-1));
|
|
291
|
+
case 'PageUp':
|
|
292
|
+
return go(() => this.stepBy(1, true));
|
|
293
|
+
case 'PageDown':
|
|
294
|
+
return go(() => this.stepBy(-1, true));
|
|
295
|
+
case 'Home':
|
|
296
|
+
if (min !== undefined && !this.readOnly()) go(() => this.set(min));
|
|
297
|
+
return;
|
|
298
|
+
case 'End':
|
|
299
|
+
if (max !== undefined && !this.readOnly()) go(() => this.set(max));
|
|
300
|
+
return;
|
|
301
|
+
case 'Enter':
|
|
302
|
+
return this.commit();
|
|
303
|
+
case 'Escape':
|
|
304
|
+
if (this.el.value === this.display(this.committed)) return;
|
|
305
|
+
e.preventDefault();
|
|
306
|
+
e.stopPropagation();
|
|
307
|
+
return this.show(this.committed);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** A native form reset puts back the input's value attribute; reading that beats a pending redraw of the old number. */
|
|
312
|
+
protected reset(e: Event) {
|
|
313
|
+
if (e.target === this.el.form) setTimeout(() => this.set(this.parse(this.el.defaultValue)));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
writeValue(v: unknown) {
|
|
317
|
+
const n = v === null || v === undefined || v === '' ? null : Number(v);
|
|
318
|
+
this.current.set(Number.isNaN(n) ? null : n);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
registerOnChange(fn: (v: number | null) => void) {
|
|
322
|
+
this.onChange = fn;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
registerOnTouched(fn: () => void) {
|
|
326
|
+
this.onTouched = fn;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
setDisabledState(disabled: boolean) {
|
|
330
|
+
this.el.disabled = disabled;
|
|
331
|
+
this.disabled.set(disabled);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Marks a 16px icon placed before the value: `<svg uiNumberInputPrefix>`. */
|
|
336
|
+
@Directive({ selector: '[uiNumberInputPrefix]', host: { class: 'ui-tf-affix', 'aria-hidden': 'true' } })
|
|
337
|
+
export class UiNumberInputPrefix {}
|
|
338
|
+
|
|
339
|
+
const space = (n: string) => `var(--ui-space-${n})`;
|
|
340
|
+
const SIZES: Record<NumberInputSize, Record<string, string>> = {
|
|
341
|
+
sm: { '--_h0': 'var(--ui-control-h-sm)', '--_ht': `calc(${space('8')} + ${space('1')})` },
|
|
342
|
+
md: { '--_h0': 'var(--ui-control-h-md)', '--_ht': space('10') },
|
|
343
|
+
lg: { '--_h0': `calc(var(--ui-control-h-md) + ${space('1')})`, '--_ht': `calc(${space('10')} + ${space('1')})` },
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
@Component({
|
|
347
|
+
selector: 'ui-number-input',
|
|
348
|
+
host: {
|
|
349
|
+
'[attr.data-size]': 'size()',
|
|
350
|
+
'[attr.data-invalid]': 'invalid() || null',
|
|
351
|
+
'[attr.data-readonly]': 'control()?.readOnly() || null',
|
|
352
|
+
'[attr.data-disabled]': 'control()?.disabled() || null',
|
|
353
|
+
'[style]': 'sizes[size()]',
|
|
354
|
+
},
|
|
355
|
+
template: `
|
|
356
|
+
@if (label()) {
|
|
357
|
+
<div class="top">
|
|
358
|
+
<label [for]="control()?.id()">{{ label() }}@if (optional()) {<span class="ui-sr"> ({{ t().optional }})</span>}</label>
|
|
359
|
+
@if (optional()) {<span class="opt" aria-hidden="true">{{ t().optional }}</span>}
|
|
360
|
+
</div>
|
|
361
|
+
}
|
|
362
|
+
<div class="box" [attr.data-nudge]="nudge() || null" (pointerdown)="focusInput($event)" (animationend)="$event.target === $event.currentTarget && nudge.set(false)" (animationcancel)="$event.target === $event.currentTarget && nudge.set(false)">
|
|
363
|
+
@if (prefix()) {<span class="aff pre" aria-hidden="true">{{ prefix() }}</span>}
|
|
364
|
+
<ng-content select="[uiNumberInputPrefix]" />
|
|
365
|
+
<ng-content select="input" />
|
|
366
|
+
@if (unit()) {<span class="aff" aria-hidden="true" style="margin-inline-start: var(--ui-space-1-5)">{{ unit() }}</span>}
|
|
367
|
+
<span class="steps" style="--ui-icon: color-mix(in srgb, var(--ui-text-muted) 75%, var(--ui-bg-surface))">
|
|
368
|
+
@for (b of buttons; track b.dir) {
|
|
369
|
+
<button type="button" class="ui-btn-icon" tabindex="-1" [attr.aria-label]="t()[b.label]" [attr.aria-controls]="control()?.id()"
|
|
370
|
+
[attr.aria-disabled]="(b.dir < 0 ? control()?.atMin() : control()?.atMax()) || null" [disabled]="!editable()"
|
|
371
|
+
(pointerdown)="hold($event, b.dir)" (pointerup)="release()" (pointerleave)="release()" (pointercancel)="release()" (click)="tap($event, b.dir)">
|
|
372
|
+
<svg class="ui-icon" viewBox="0 0 16 16" aria-hidden="true" style="stroke-width: calc(var(--ui-icon-stroke) * 0.8)"><path [attr.d]="b.dir < 0 ? 'M2 8h12' : 'M2 8h12M8 2v12'" /></svg>
|
|
373
|
+
</button>
|
|
374
|
+
}
|
|
375
|
+
</span>
|
|
376
|
+
</div>
|
|
377
|
+
<div class="foot" [class.ui-sr]="!message()">
|
|
378
|
+
<p #msg [id]="messageId" dir="auto" aria-live="polite">{{ message() }}</p>
|
|
379
|
+
</div>
|
|
380
|
+
@if (control()?.name(); as name) {<input type="hidden" [name]="name" [value]="control()?.current() ?? ''" />}
|
|
381
|
+
`,
|
|
382
|
+
styles: `
|
|
383
|
+
:host {
|
|
384
|
+
--_t: var(--ui-look-tray, 0);
|
|
385
|
+
--_h: calc(var(--_h0) + var(--ui-space-1) * var(--_t));
|
|
386
|
+
--_e: var(--ui-text-field-edge, var(--ui-hairline));
|
|
387
|
+
--_w: calc(0.5px + 0.5px * var(--_t));
|
|
388
|
+
--_fill: var(--ui-bg-surface);
|
|
389
|
+
display: flex;
|
|
390
|
+
flex-direction: column;
|
|
391
|
+
gap: calc(var(--ui-space-1-5) + 1px * var(--_t));
|
|
392
|
+
min-inline-size: 0;
|
|
393
|
+
color: var(--ui-text);
|
|
394
|
+
font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
|
|
395
|
+
}
|
|
396
|
+
.top { display: flex; align-items: baseline; gap: var(--ui-space-1-5); padding-inline: calc(2px * var(--_t)); }
|
|
397
|
+
label { font-weight: var(--ui-weight-medium); }
|
|
398
|
+
.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)); }
|
|
399
|
+
.box {
|
|
400
|
+
display: flex;
|
|
401
|
+
align-items: center;
|
|
402
|
+
gap: var(--ui-space-1);
|
|
403
|
+
box-sizing: border-box;
|
|
404
|
+
block-size: var(--_h);
|
|
405
|
+
padding-inline: calc(var(--ui-space-2-5) + 2px * var(--_t) - 1px) calc(var(--ui-space-1) + 1px * var(--_t) - 1px);
|
|
406
|
+
border: 1px solid transparent;
|
|
407
|
+
border-radius: var(--ui-text-field-radius, calc(var(--ui-radius-sm) + 1px + 3px * var(--_t)));
|
|
408
|
+
background: var(--_fill);
|
|
409
|
+
box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
410
|
+
font-size: calc(var(--ui-text-base) + 0.5px * var(--_t));
|
|
411
|
+
cursor: text;
|
|
412
|
+
transition: var(--ui-duration-2) var(--ui-ease-out);
|
|
413
|
+
transition-property: box-shadow, background;
|
|
414
|
+
}
|
|
415
|
+
@media (hover: hover) { .box:hover { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
|
|
416
|
+
.box:has(input:focus) { --_w: 1px; --_e: var(--ui-text-field-edge-focus, var(--ui-border-input)); }
|
|
417
|
+
:host([data-invalid]) .box {
|
|
418
|
+
--_w: 1px;
|
|
419
|
+
--_e: color-mix(in srgb, var(--ui-danger) 55%, transparent);
|
|
420
|
+
--_fill: color-mix(in srgb, var(--ui-danger-soft) calc(50% * var(--_t)), var(--ui-bg-surface));
|
|
421
|
+
}
|
|
422
|
+
:host([data-invalid]) .box:has(input:focus) { --_e: var(--ui-danger); }
|
|
423
|
+
:host([data-readonly]) .box { --_fill: var(--ui-bg-subtle); }
|
|
424
|
+
:host([data-disabled]) { opacity: var(--ui-disabled-opacity); }
|
|
425
|
+
:host([data-disabled]) .box { cursor: not-allowed; --_e: var(--ui-hairline); }
|
|
426
|
+
:host([data-disabled]) .steps button { opacity: 1; }
|
|
427
|
+
.box[data-nudge] { animation: ui-nudge calc(320ms * var(--ui-motion-scale)) var(--ui-ease-out); }
|
|
428
|
+
.aff { color: var(--ui-text-muted); white-space: nowrap; }
|
|
429
|
+
.pre { margin-inline-end: calc(var(--ui-space-0-5) * -1); }
|
|
430
|
+
.steps { display: flex; gap: var(--ui-space-0-5); margin-inline-start: auto; }
|
|
431
|
+
.steps [aria-disabled='true'] { opacity: var(--ui-disabled-opacity); cursor: default; background: none; color: var(--ui-icon); }
|
|
432
|
+
.foot { padding-inline: calc(2px * var(--_t)); }
|
|
433
|
+
p { margin: 0; }
|
|
434
|
+
:host([data-invalid]) p { color: var(--ui-danger); }
|
|
435
|
+
@media (pointer: coarse) {
|
|
436
|
+
:host { --_h: calc(var(--_ht) + var(--ui-space-1) * var(--_t)); }
|
|
437
|
+
.box { font-size: var(--ui-text-field-touch); }
|
|
438
|
+
.steps button { touch-action: manipulation; user-select: none; }
|
|
439
|
+
}
|
|
440
|
+
@media (prefers-reduced-motion: reduce) {
|
|
441
|
+
.box[data-nudge] { animation: none; }
|
|
442
|
+
}
|
|
443
|
+
@media (forced-colors: active) {
|
|
444
|
+
.box { border-color: CanvasText; }
|
|
445
|
+
.box:has(input:focus) { outline: 2px solid Highlight; }
|
|
446
|
+
:host([data-invalid]) .box { outline: 1px solid CanvasText; }
|
|
447
|
+
.steps [aria-disabled='true'] { color: GrayText; }
|
|
448
|
+
}
|
|
449
|
+
`,
|
|
450
|
+
})
|
|
451
|
+
export class UiNumberInput {
|
|
452
|
+
/** Shown above the input. Clicking it focuses the input. */
|
|
453
|
+
readonly label = input<string>();
|
|
454
|
+
/** A quiet line under the input. The error takes its place while there is one. */
|
|
455
|
+
readonly hint = input<string>();
|
|
456
|
+
/** Marks the field invalid and shows this message where the hint was. */
|
|
457
|
+
readonly error = input<string | null | undefined>();
|
|
458
|
+
/** Quiet text before the value, such as "$". For an icon, use `uiNumberInputPrefix`. */
|
|
459
|
+
readonly prefix = input<string>();
|
|
460
|
+
/** Quiet text right after the value, such as "tokens" or "ms". Screen readers hear it with the value. */
|
|
461
|
+
readonly unit = input<string>();
|
|
462
|
+
/** 28, 32 or 36px tall; Classic 32, 36 or 40px. Taller, with 16px text, on touch screens. */
|
|
463
|
+
readonly size = input<NumberInputSize>('md');
|
|
464
|
+
/** Adds "Optional" after the label. */
|
|
465
|
+
readonly optional = input(false, { transform: booleanAttribute });
|
|
466
|
+
/** Words for the tag and the buttons, to translate them. */
|
|
467
|
+
readonly labels = input<Partial<NumberInputLabels>>({});
|
|
468
|
+
|
|
469
|
+
protected readonly control = contentChild(UiNumber);
|
|
470
|
+
protected readonly sizes = SIZES;
|
|
471
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
472
|
+
protected readonly buttons = [
|
|
473
|
+
{ dir: -1 as const, label: 'decrease' as const },
|
|
474
|
+
{ dir: 1 as const, label: 'increase' as const },
|
|
475
|
+
];
|
|
476
|
+
protected readonly messageId = `ui-number-input-msg-${nextId++}`;
|
|
477
|
+
|
|
478
|
+
readonly invalid = computed(() => !!this.error());
|
|
479
|
+
protected readonly message = computed(() => this.error() || this.hint() || '');
|
|
480
|
+
protected readonly editable = computed(() => !this.control()?.readOnly() && !this.control()?.disabled());
|
|
481
|
+
readonly describers = computed(() => [this.message() ? this.messageId : '']);
|
|
482
|
+
|
|
483
|
+
protected readonly nudge = signal(false);
|
|
484
|
+
private readonly msg = viewChild<ElementRef<HTMLElement>>('msg');
|
|
485
|
+
private readonly doc = inject(DOCUMENT);
|
|
486
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
487
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
488
|
+
|
|
489
|
+
constructor() {
|
|
490
|
+
let was: boolean | undefined;
|
|
491
|
+
effect(() => {
|
|
492
|
+
const now = this.invalid();
|
|
493
|
+
if (now && was === false && this.motion()) this.nudge.set(true);
|
|
494
|
+
was = now;
|
|
495
|
+
});
|
|
496
|
+
let shown: string | undefined;
|
|
497
|
+
afterRenderEffect(() => {
|
|
498
|
+
const text = this.message();
|
|
499
|
+
const p = this.msg()?.nativeElement;
|
|
500
|
+
const motion = this.motion();
|
|
501
|
+
if (shown !== undefined && text !== shown && p?.animate && motion) {
|
|
502
|
+
p.animate([{ opacity: 0, filter: 'blur(2px)', transform: 'translateY(-2px)' }, {}], { duration: 260 * motion, easing: 'ease-out' });
|
|
503
|
+
}
|
|
504
|
+
shown = text;
|
|
505
|
+
});
|
|
506
|
+
afterNextRender(() => {
|
|
507
|
+
const c = this.control();
|
|
508
|
+
if (isDevMode() && c && !this.label() && !c.el.ariaLabel && !c.el.hasAttribute('aria-labelledby')) {
|
|
509
|
+
console.warn('ui-number-input: give it a label, or an aria-label on the input.', c.el);
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
protected focusInput(event: PointerEvent) {
|
|
516
|
+
const el = this.control()?.el;
|
|
517
|
+
if (!el || event.target === el || (event.target as Element).closest('button, a, input, select, textarea')) return;
|
|
518
|
+
event.preventDefault();
|
|
519
|
+
el.focus();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** Steps once on press, then repeats faster and faster while the button is held. */
|
|
523
|
+
protected hold(event: PointerEvent, dir: 1 | -1) {
|
|
524
|
+
if (event.button !== 0) return;
|
|
525
|
+
event.preventDefault();
|
|
526
|
+
this.release();
|
|
527
|
+
const c = this.control();
|
|
528
|
+
if (!c?.stepBy(dir)) return;
|
|
529
|
+
let n = 0;
|
|
530
|
+
const again = () => {
|
|
531
|
+
if (!c.stepBy(dir)) return;
|
|
532
|
+
this.timer = setTimeout(again, Math.max(40, 120 * 0.85 ** n++));
|
|
533
|
+
};
|
|
534
|
+
this.timer = setTimeout(again, 400);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
protected release() {
|
|
538
|
+
clearTimeout(this.timer);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
protected tap(event: MouseEvent, dir: 1 | -1) {
|
|
542
|
+
if (event.detail === 0) this.control()?.stepBy(dir);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** 0 under reduced motion, otherwise the app's motion scale (1 by default). */
|
|
546
|
+
motion() {
|
|
547
|
+
const view = this.doc.defaultView;
|
|
548
|
+
if (!view?.matchMedia || view.matchMedia('(prefers-reduced-motion: reduce)').matches) return 0;
|
|
549
|
+
const scale = parseFloat(view.getComputedStyle(this.host).getPropertyValue('--ui-motion-scale'));
|
|
550
|
+
return Number.isNaN(scale) ? 1 : scale;
|
|
551
|
+
}
|
|
552
|
+
}
|