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,461 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Rating, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (`npx ochreui init`).
|
|
5
|
+
*
|
|
6
|
+
* <ui-rating aria-label="Rate this deploy" [formField]="form.stars" />
|
|
7
|
+
* <ui-rating readonly [value]="4.3" size="sm" />
|
|
8
|
+
* <ui-rating-thumbs aria-label="Rate this answer" [(value)]="vote" />
|
|
9
|
+
* Stars preview on hover, a press sets the value and a second press clears it; the row is one tab stop.
|
|
10
|
+
* Both work with signal forms, reactive forms, ngModel or [(value)]. Read-only stars show halves.
|
|
11
|
+
*
|
|
12
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
13
|
+
* --ui-rating-fill filled star (default: amber)
|
|
14
|
+
* --ui-rating-empty empty star (default: a quiet tint of the text color)
|
|
15
|
+
*/
|
|
16
|
+
import {
|
|
17
|
+
Component,
|
|
18
|
+
Directive,
|
|
19
|
+
ElementRef,
|
|
20
|
+
afterNextRender,
|
|
21
|
+
booleanAttribute,
|
|
22
|
+
computed,
|
|
23
|
+
forwardRef,
|
|
24
|
+
inject,
|
|
25
|
+
input,
|
|
26
|
+
isDevMode,
|
|
27
|
+
linkedSignal,
|
|
28
|
+
model,
|
|
29
|
+
output,
|
|
30
|
+
signal,
|
|
31
|
+
type WritableSignal,
|
|
32
|
+
} from '@angular/core';
|
|
33
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
34
|
+
import type { FormValueControl } from '@angular/forms/signals';
|
|
35
|
+
|
|
36
|
+
export type RatingSize = 'sm' | 'md' | 'lg';
|
|
37
|
+
export type ThumbsValue = 'up' | 'down' | null;
|
|
38
|
+
|
|
39
|
+
export interface RatingLabels {
|
|
40
|
+
/** The group's name when you give it none. */
|
|
41
|
+
group: string;
|
|
42
|
+
/** Read-only stars with no value. */
|
|
43
|
+
none: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface RatingThumbsLabels {
|
|
47
|
+
up: string;
|
|
48
|
+
down: string;
|
|
49
|
+
group: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const LABELS: RatingLabels = { group: 'Rating', none: 'No rating' };
|
|
53
|
+
const THUMBS: RatingThumbsLabels = { up: 'Good response', down: 'Bad response', group: 'Rate this response' };
|
|
54
|
+
const STAR =
|
|
55
|
+
'M10.6 3.77Q12 1.12 13.4 3.77L14.95 6.72Q15.61 7.96 16.99 8.2L20.27 8.76Q23.22 9.28 21.13 11.43L18.81 13.81Q17.84 14.82 18.03 16.2L18.51 19.5Q18.94 22.47 16.24 21.14L13.26 19.68Q12 19.06 10.74 19.68L7.76 21.14Q5.06 22.47 5.49 19.5L5.97 16.2Q6.16 14.82 5.19 13.81L2.87 11.43Q0.78 9.28 3.73 8.76L7.01 8.2Q8.39 7.96 9.05 6.72Z';
|
|
56
|
+
const THUMB = 'M7 10.5V20.5M7 10.5L10.6 3.6A2.1 2.1 0 0 1 14.6 4.9L13.9 9H19A2 2 0 0 1 21 11.3L19.7 18.8A2 2 0 0 1 17.7 20.5H4.5A1.5 1.5 0 0 1 3 19V12A1.5 1.5 0 0 1 4.5 10.5H7';
|
|
57
|
+
|
|
58
|
+
const bound = (v: unknown) => (v == null || v === '' ? undefined : Number(v));
|
|
59
|
+
const rate = (v: unknown) => (v == null || v === '' || Number.isNaN(Number(v)) ? null : Number(v));
|
|
60
|
+
|
|
61
|
+
/** 0 under reduced motion, otherwise the app's motion scale (1 by default). */
|
|
62
|
+
function motion(host: HTMLElement) {
|
|
63
|
+
const view = host.ownerDocument.defaultView;
|
|
64
|
+
if (!view?.matchMedia || view.matchMedia('(prefers-reduced-motion: reduce)').matches) return 0;
|
|
65
|
+
const scale = parseFloat(view.getComputedStyle(host).getPropertyValue('--ui-motion-scale'));
|
|
66
|
+
return Number.isNaN(scale) ? 1 : scale;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const rtl = (host: HTMLElement) => host.ownerDocument.defaultView?.getComputedStyle(host).direction === 'rtl';
|
|
70
|
+
|
|
71
|
+
/** Shared by both: the value through forms, the roving tab stop and the dev warning. */
|
|
72
|
+
@Directive()
|
|
73
|
+
abstract class Group<T> implements ControlValueAccessor {
|
|
74
|
+
abstract readonly value: { (): T; set(v: T): void };
|
|
75
|
+
/** The value now. Form writes land here without emitting `valueChange`. */
|
|
76
|
+
abstract readonly state: WritableSignal<T>;
|
|
77
|
+
readonly disabled = input(false, { transform: booleanAttribute });
|
|
78
|
+
readonly readonly = input(false, { transform: booleanAttribute });
|
|
79
|
+
readonly required = input(false, { transform: booleanAttribute });
|
|
80
|
+
readonly invalid = input(false, { transform: booleanAttribute });
|
|
81
|
+
/** Pressing the chosen one again, or Delete, clears it. */
|
|
82
|
+
readonly allowClear = input(true, { transform: booleanAttribute });
|
|
83
|
+
/** Default: `md`, or `sm` for read-only stars. */
|
|
84
|
+
readonly size = input<RatingSize>();
|
|
85
|
+
/** Submits the value with a native form through a hidden input, empty when nothing is chosen. */
|
|
86
|
+
readonly name = input('');
|
|
87
|
+
readonly ariaLabel = input<string>(undefined, { alias: 'aria-label' });
|
|
88
|
+
/** Emitted when focus leaves the group. */
|
|
89
|
+
readonly touch = output<void>();
|
|
90
|
+
|
|
91
|
+
protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
92
|
+
private readonly formDisabled = signal(false);
|
|
93
|
+
protected readonly off = computed(() => this.disabled() || this.formDisabled());
|
|
94
|
+
protected readonly focused = signal<unknown>(null);
|
|
95
|
+
private onChange: (v: T) => void = () => {};
|
|
96
|
+
private onTouched: () => void = () => {};
|
|
97
|
+
private initial?: T;
|
|
98
|
+
|
|
99
|
+
protected abstract readonly tag: string;
|
|
100
|
+
|
|
101
|
+
constructor() {
|
|
102
|
+
// defineProperty, not assignment: the server-side DOM makes focus read-only.
|
|
103
|
+
Object.defineProperty(this.host, 'focus', { value: (options?: FocusOptions) => this.focus(options), configurable: true, writable: true });
|
|
104
|
+
afterNextRender(() => {
|
|
105
|
+
const el = this.host;
|
|
106
|
+
this.initial = this.state();
|
|
107
|
+
if (isDevMode() && !this.readonly() && !this.ariaLabel() && !el.hasAttribute('aria-labelledby')) {
|
|
108
|
+
console.warn(`${this.tag}: give it an aria-label or aria-labelledby that says what is being rated.`, el);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Focuses the chosen item, or the first. */
|
|
114
|
+
focus(options?: FocusOptions) {
|
|
115
|
+
this.host.querySelector<HTMLElement>('button[tabindex="0"]')?.focus(options);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
protected commit(v: T) {
|
|
119
|
+
this.state.set(v);
|
|
120
|
+
this.value.set(v);
|
|
121
|
+
this.onChange(v);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** With a name, a native form reset puts back the value it started with. */
|
|
125
|
+
protected formReset(e: Event) {
|
|
126
|
+
if (!this.name() || e.target !== this.host.closest('form')) return;
|
|
127
|
+
setTimeout(() => {
|
|
128
|
+
const v = this.initial as T;
|
|
129
|
+
if (!e.defaultPrevented && this.state() !== v) this.commit(v);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
protected left(event: FocusEvent) {
|
|
134
|
+
if (this.host.contains(event.relatedTarget as Node | null)) return;
|
|
135
|
+
this.focused.set(null);
|
|
136
|
+
this.onTouched();
|
|
137
|
+
this.touch.emit();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
protected item(i: number) {
|
|
141
|
+
return this.host.querySelectorAll<HTMLElement>('button')[i];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
abstract writeValue(v: unknown): void;
|
|
145
|
+
|
|
146
|
+
registerOnChange(fn: (v: T) => void) {
|
|
147
|
+
this.onChange = fn;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
registerOnTouched(fn: () => void) {
|
|
151
|
+
this.onTouched = fn;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
setDisabledState(disabled: boolean) {
|
|
155
|
+
this.formDisabled.set(disabled);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Component({
|
|
160
|
+
selector: 'ui-rating',
|
|
161
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiRating), multi: true }],
|
|
162
|
+
host: {
|
|
163
|
+
'[attr.role]': "readonly() ? 'img' : 'radiogroup'",
|
|
164
|
+
'[attr.aria-label]': 'groupLabel()',
|
|
165
|
+
'[attr.aria-required]': '(!readonly() && required()) || null',
|
|
166
|
+
'[attr.aria-invalid]': '(!readonly() && invalid()) || null',
|
|
167
|
+
'[attr.aria-disabled]': '(!readonly() && off()) || null',
|
|
168
|
+
'[attr.data-size]': "size() ?? (readonly() ? 'sm' : 'md')",
|
|
169
|
+
'[attr.data-readonly]': 'readonly() || null',
|
|
170
|
+
'[attr.data-disabled]': 'off() || null',
|
|
171
|
+
'(keydown)': 'key($event)',
|
|
172
|
+
'(pointerleave)': 'hover.set(null)',
|
|
173
|
+
'(focusout)': 'left($event)',
|
|
174
|
+
'(document:reset)': 'formReset($event)',
|
|
175
|
+
},
|
|
176
|
+
template: `
|
|
177
|
+
@for (i of stars(); track i) {
|
|
178
|
+
@if (readonly()) {
|
|
179
|
+
<span class="s" [style.--_f]="fill(i)"><span class="g" aria-hidden="true"><svg class="e" viewBox="0 0 24 24" focusable="false"><path [attr.d]="star" /></svg><svg class="f" viewBox="0 0 24 24" focusable="false"><path [attr.d]="star" /></svg></span></span>
|
|
180
|
+
} @else {
|
|
181
|
+
<button type="button" role="radio" class="s" [attr.aria-checked]="current() === i" [attr.aria-label]="label(i)"
|
|
182
|
+
[tabindex]="i === stop() ? 0 : -1" [disabled]="off()" [style.--_f]="fill(i)"
|
|
183
|
+
(pointerenter)="enter($event, i)" (focus)="focused.set(i)" (click)="press(i)"><span class="g" aria-hidden="true"><svg class="e" viewBox="0 0 24 24" focusable="false"><path [attr.d]="star" /></svg><svg class="f" viewBox="0 0 24 24" focusable="false"><path [attr.d]="star" /></svg></span></button>
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
@if (name() && !readonly()) {<input type="hidden" [name]="name()" [value]="current() ?? ''" />}
|
|
187
|
+
`,
|
|
188
|
+
styles: `
|
|
189
|
+
:host {
|
|
190
|
+
--_t: var(--ui-look-tray, 0);
|
|
191
|
+
--_g: calc(var(--ui-icon-md) + 2px + 2px * var(--_t));
|
|
192
|
+
--_b: max(var(--_g) + 6px, var(--ui-space-6));
|
|
193
|
+
--_gap: var(--ui-space-0-5);
|
|
194
|
+
--_e: var(--ui-rating-empty, color-mix(in srgb, var(--ui-text) 15%, transparent));
|
|
195
|
+
display: inline-flex;
|
|
196
|
+
flex-wrap: nowrap;
|
|
197
|
+
gap: var(--_gap);
|
|
198
|
+
vertical-align: middle;
|
|
199
|
+
}
|
|
200
|
+
:host([data-size='sm']) { --_g: calc(var(--ui-icon-sm) + 2px * var(--_t)); }
|
|
201
|
+
:host([data-size='lg']) { --_g: calc(var(--ui-icon-xl) + 2px * var(--_t)); }
|
|
202
|
+
:host([data-readonly]) { --_gap: 1px; --_b: var(--_g); }
|
|
203
|
+
:host([data-disabled]) { opacity: var(--ui-disabled-opacity); }
|
|
204
|
+
.s {
|
|
205
|
+
all: unset;
|
|
206
|
+
flex: none;
|
|
207
|
+
display: grid;
|
|
208
|
+
place-items: center;
|
|
209
|
+
inline-size: var(--_b);
|
|
210
|
+
block-size: var(--_b);
|
|
211
|
+
border-radius: calc(var(--ui-radius-sm) + 2px * var(--_t));
|
|
212
|
+
cursor: pointer;
|
|
213
|
+
-webkit-tap-highlight-color: transparent;
|
|
214
|
+
}
|
|
215
|
+
span.s { cursor: default; }
|
|
216
|
+
.s:disabled { cursor: not-allowed; }
|
|
217
|
+
.s:focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
|
|
218
|
+
.g { display: grid; inline-size: var(--_g); block-size: var(--_g); transition: transform var(--ui-duration-2) var(--ui-ease-out); }
|
|
219
|
+
svg { display: block; grid-area: 1 / 1; inline-size: 100%; block-size: 100%; }
|
|
220
|
+
.e { fill: var(--_e); clip-path: inset(0 0 0 calc(var(--_f) * 100%)); transition: fill var(--ui-duration-2) var(--ui-ease-out); }
|
|
221
|
+
.f { fill: var(--ui-rating-fill, var(--ui-hue-amber)); clip-path: inset(0 calc(100% - var(--_f) * 100%) 0 0); }
|
|
222
|
+
:host(:dir(rtl)) .e { clip-path: inset(0 calc(var(--_f) * 100%) 0 0); }
|
|
223
|
+
:host(:dir(rtl)) .f { clip-path: inset(0 0 0 calc(100% - var(--_f) * 100%)); }
|
|
224
|
+
@media (hover: hover) {
|
|
225
|
+
.s:not(:disabled):hover .e { fill: color-mix(in srgb, var(--ui-text) 8%, var(--_e)); }
|
|
226
|
+
}
|
|
227
|
+
@media (hover: hover) and (prefers-reduced-motion: no-preference) {
|
|
228
|
+
.s:not(:disabled):hover .g { transform: scale(1.12); }
|
|
229
|
+
}
|
|
230
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
231
|
+
.s:not(:disabled):active .g { transform: scale(0.9); }
|
|
232
|
+
}
|
|
233
|
+
@container style(--ui-motion-scale: 0) {
|
|
234
|
+
.g { transform: none !important; }
|
|
235
|
+
}
|
|
236
|
+
@media (pointer: coarse) {
|
|
237
|
+
:host(:not([data-readonly])) { --_gap: 0px; --_b: max(var(--_g) + 6px, var(--ui-space-8)); }
|
|
238
|
+
}
|
|
239
|
+
@media (prefers-contrast: more) {
|
|
240
|
+
:host { --_e: var(--ui-rating-empty, var(--ui-border-input)); }
|
|
241
|
+
}
|
|
242
|
+
@media (forced-colors: active) {
|
|
243
|
+
.e { fill: none; stroke: CanvasText; stroke-width: 1.5px; }
|
|
244
|
+
.f { fill: Highlight; }
|
|
245
|
+
:host([data-disabled]) .e { stroke: GrayText; }
|
|
246
|
+
:host([data-disabled]) .f { fill: GrayText; }
|
|
247
|
+
}
|
|
248
|
+
`,
|
|
249
|
+
})
|
|
250
|
+
export class UiRating extends Group<number | null> implements FormValueControl<number | null> {
|
|
251
|
+
/** 1 to `max`, or `null` for none. Read-only, it may be a fraction, drawn to the nearest half. */
|
|
252
|
+
readonly value = model<number | null>(null);
|
|
253
|
+
readonly state = linkedSignal(() => this.value());
|
|
254
|
+
/** How many stars, 1 to 50, on one line. With signal forms, set it with the schema's `max()`. */
|
|
255
|
+
readonly max = input<number | undefined, unknown>(5, { transform: bound });
|
|
256
|
+
/** How each star, and the read-only value, is read out. Default: "3 out of 5". */
|
|
257
|
+
readonly valueLabel = input<(value: number, max: number) => string>((v, max) => `${v} out of ${max}`);
|
|
258
|
+
/** Words to translate: `group` ("Rating") and `none` ("No rating"). */
|
|
259
|
+
readonly labels = input<Partial<RatingLabels>>({});
|
|
260
|
+
|
|
261
|
+
protected readonly tag = 'ui-rating';
|
|
262
|
+
protected readonly star = STAR;
|
|
263
|
+
protected readonly hover = signal<number | null>(null);
|
|
264
|
+
protected readonly count = computed(() => Math.min(50, Math.max(1, Math.round(this.max() ?? 5) || 5)));
|
|
265
|
+
protected readonly stars = computed(() => Array.from({ length: this.count() }, (_, i) => i + 1));
|
|
266
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
267
|
+
/** The whole-star value shown by the buttons. */
|
|
268
|
+
readonly current = computed(() => {
|
|
269
|
+
const v = this.state();
|
|
270
|
+
return v === null || !(v > 0) ? null : Math.min(this.count(), Math.max(1, Math.round(v)));
|
|
271
|
+
});
|
|
272
|
+
protected readonly stop = computed(() => (this.focused() as number | null) ?? this.current() ?? 1);
|
|
273
|
+
protected readonly groupLabel = computed(() => {
|
|
274
|
+
if (!this.readonly()) return this.ariaLabel() ?? this.t().group;
|
|
275
|
+
const v = this.state();
|
|
276
|
+
const text = v === null ? this.t().none : this.valueLabel()(Math.round(Math.min(Math.max(v, 0), this.count()) * 10) / 10, this.count());
|
|
277
|
+
return [this.ariaLabel(), text].filter(Boolean).join(', ');
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
protected label(i: number) {
|
|
281
|
+
return this.valueLabel()(i, this.count());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
protected fill(i: number) {
|
|
285
|
+
if (this.readonly()) {
|
|
286
|
+
const v = Math.round(Math.min(Math.max(this.state() ?? 0, 0), this.count()) * 2) / 2;
|
|
287
|
+
return Math.min(1, Math.max(0, v - i + 1));
|
|
288
|
+
}
|
|
289
|
+
return i <= (this.hover() ?? this.current() ?? 0) ? 1 : 0;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
protected enter(event: PointerEvent, i: number) {
|
|
293
|
+
if (event.pointerType === 'mouse' && !this.off()) this.hover.set(i);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
protected press(i: number) {
|
|
297
|
+
if (this.off() || this.readonly()) return;
|
|
298
|
+
if (this.current() !== i) return this.set(i);
|
|
299
|
+
if (!this.allowClear()) return;
|
|
300
|
+
this.hover.set(null);
|
|
301
|
+
this.commit(null);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private set(v: number) {
|
|
305
|
+
this.commit(v);
|
|
306
|
+
const k = motion(this.host);
|
|
307
|
+
if (!k) return;
|
|
308
|
+
this.host.querySelectorAll<HTMLElement>('.g').forEach((g, i) => {
|
|
309
|
+
if (i < v) {
|
|
310
|
+
g.animate?.([{ scale: 0.6 }, { scale: 1.22, offset: 0.5 }, { scale: 1 }], {
|
|
311
|
+
duration: 420 * k,
|
|
312
|
+
delay: i * 40 * k,
|
|
313
|
+
easing: 'ease-out',
|
|
314
|
+
fill: 'backwards',
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
protected key(e: KeyboardEvent) {
|
|
321
|
+
if (this.readonly() || this.off()) return;
|
|
322
|
+
this.hover.set(null);
|
|
323
|
+
const cur = this.current();
|
|
324
|
+
const dir = rtl(this.host) ? -1 : 1;
|
|
325
|
+
const by = (d: number) => (cur === null ? 1 : Math.min(this.count(), Math.max(1, cur + d)));
|
|
326
|
+
const next: Record<string, () => number | null> = {
|
|
327
|
+
ArrowRight: () => by(dir),
|
|
328
|
+
ArrowLeft: () => by(-dir),
|
|
329
|
+
ArrowUp: () => by(1),
|
|
330
|
+
ArrowDown: () => by(-1),
|
|
331
|
+
Home: () => 1,
|
|
332
|
+
End: () => this.count(),
|
|
333
|
+
};
|
|
334
|
+
if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
335
|
+
if (!this.allowClear() || cur === null) return;
|
|
336
|
+
e.preventDefault();
|
|
337
|
+
return this.commit(null);
|
|
338
|
+
}
|
|
339
|
+
const go = next[e.key];
|
|
340
|
+
if (!go) return;
|
|
341
|
+
e.preventDefault();
|
|
342
|
+
const v = go()!;
|
|
343
|
+
if (v !== cur) this.set(v);
|
|
344
|
+
this.item(v - 1)?.focus();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
writeValue(v: unknown) {
|
|
348
|
+
this.state.set(rate(v));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
@Component({
|
|
353
|
+
selector: 'ui-rating-thumbs',
|
|
354
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiRatingThumbs), multi: true }],
|
|
355
|
+
host: {
|
|
356
|
+
role: 'radiogroup',
|
|
357
|
+
'[attr.aria-label]': 'ariaLabel() ?? t().group',
|
|
358
|
+
'[attr.aria-required]': 'required() || null',
|
|
359
|
+
'[attr.aria-invalid]': 'invalid() || null',
|
|
360
|
+
'[attr.aria-disabled]': 'off() || null',
|
|
361
|
+
'[attr.aria-readonly]': 'readonly() || null',
|
|
362
|
+
'[attr.data-size]': "size() ?? 'md'",
|
|
363
|
+
'[attr.data-readonly]': 'readonly() || null',
|
|
364
|
+
'(keydown)': 'key($event)',
|
|
365
|
+
'(focusout)': 'left($event)',
|
|
366
|
+
'(document:reset)': 'formReset($event)',
|
|
367
|
+
},
|
|
368
|
+
template: `
|
|
369
|
+
@for (k of kinds; track k) {
|
|
370
|
+
<button type="button" role="radio" class="ui-btn-icon b" [attr.aria-checked]="state() === k" [attr.aria-label]="t()[k]"
|
|
371
|
+
[tabindex]="k === stop() ? 0 : -1" [disabled]="off()" (focus)="focused.set(k)" (click)="press(k)">
|
|
372
|
+
<span class="g" aria-hidden="true">
|
|
373
|
+
<svg class="ui-icon" viewBox="0 0 24 24" focusable="false" [attr.data-down]="k === 'down' || null"><path [attr.d]="thumb" /></svg>
|
|
374
|
+
</span>
|
|
375
|
+
</button>
|
|
376
|
+
}
|
|
377
|
+
@if (name()) {<input type="hidden" [name]="name()" [value]="state() ?? ''" />}
|
|
378
|
+
`,
|
|
379
|
+
styles: `
|
|
380
|
+
:host {
|
|
381
|
+
--_t: var(--ui-look-tray, 0);
|
|
382
|
+
--_bs: calc(var(--ui-space-6) + 4px);
|
|
383
|
+
--_i: calc(var(--ui-icon-md) + 2px * var(--_t));
|
|
384
|
+
display: inline-flex;
|
|
385
|
+
gap: 1px;
|
|
386
|
+
vertical-align: middle;
|
|
387
|
+
}
|
|
388
|
+
:host([data-size='sm']) { --_bs: var(--ui-space-6); --_i: calc(var(--ui-icon-sm) + 2px * var(--_t)); }
|
|
389
|
+
:host([data-size='lg']) { --_bs: var(--ui-space-8); --_i: calc(var(--ui-icon-md) + 2px + 2px * var(--_t)); }
|
|
390
|
+
.b { --_s: calc(var(--_bs) + 4px * var(--_t)); border-radius: calc(var(--ui-radius-sm) + 3px * var(--_t)); }
|
|
391
|
+
.b:focus-visible { outline-offset: var(--ui-focus-offset); }
|
|
392
|
+
.b[aria-checked='true'] { background: color-mix(in srgb, var(--ui-text) 10%, transparent); color: var(--ui-text); }
|
|
393
|
+
@media (hover: hover) { .b[aria-checked='true']:hover { background: color-mix(in srgb, var(--ui-text) 10%, transparent); color: var(--ui-text); } }
|
|
394
|
+
.b[aria-checked='true'] path { fill: currentColor; }
|
|
395
|
+
.b:disabled { cursor: not-allowed; }
|
|
396
|
+
:host([data-readonly]) .b { cursor: default; }
|
|
397
|
+
@media (hover: hover) { :host([data-readonly]) .b:not([aria-checked='true']):hover { background: none; color: var(--ui-icon); } }
|
|
398
|
+
.g { display: grid; }
|
|
399
|
+
svg { inline-size: var(--_i); block-size: var(--_i); }
|
|
400
|
+
path { transition: fill var(--ui-duration-2) var(--ui-ease-out); }
|
|
401
|
+
[data-down] { rotate: 180deg; }
|
|
402
|
+
@media (pointer: coarse) {
|
|
403
|
+
:host { gap: 0; }
|
|
404
|
+
.b { --_s: max(var(--_bs) + 4px * var(--_t), var(--ui-space-8)); }
|
|
405
|
+
}
|
|
406
|
+
@media (forced-colors: active) {
|
|
407
|
+
.b[aria-checked='true'] { color: Highlight; }
|
|
408
|
+
}
|
|
409
|
+
`,
|
|
410
|
+
})
|
|
411
|
+
export class UiRatingThumbs extends Group<ThumbsValue> implements FormValueControl<ThumbsValue> {
|
|
412
|
+
/** `'up'`, `'down'`, or `null` for no vote. */
|
|
413
|
+
readonly value = model<ThumbsValue>(null);
|
|
414
|
+
readonly state = linkedSignal(() => this.value());
|
|
415
|
+
/** The buttons' names and the group's default name, to translate them. */
|
|
416
|
+
readonly labels = input<Partial<RatingThumbsLabels>>({});
|
|
417
|
+
|
|
418
|
+
protected readonly tag = 'ui-rating-thumbs';
|
|
419
|
+
protected readonly thumb = THUMB;
|
|
420
|
+
protected readonly kinds = ['up', 'down'] as const;
|
|
421
|
+
protected readonly t = computed(() => ({ ...THUMBS, ...this.labels() }));
|
|
422
|
+
protected readonly stop = computed(() => (this.focused() as ThumbsValue) ?? this.state() ?? 'up');
|
|
423
|
+
|
|
424
|
+
protected press(k: 'up' | 'down') {
|
|
425
|
+
if (this.off() || this.readonly()) return;
|
|
426
|
+
if (this.state() !== k) return this.set(k);
|
|
427
|
+
if (this.allowClear()) this.commit(null);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private set(k: 'up' | 'down') {
|
|
431
|
+
this.commit(k);
|
|
432
|
+
const m = motion(this.host);
|
|
433
|
+
if (!m) return;
|
|
434
|
+
const tilt = rtl(this.host) ? 12 : -12;
|
|
435
|
+
this.host.querySelectorAll<HTMLElement>('.g')[k === 'up' ? 0 : 1]?.animate?.(
|
|
436
|
+
[{ scale: 0.8 }, { scale: 1.15, rotate: `${tilt}deg`, offset: 0.45 }, { scale: 1, rotate: '0deg' }],
|
|
437
|
+
{ duration: 460 * m, easing: 'ease-out' },
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
protected key(e: KeyboardEvent) {
|
|
442
|
+
if (this.off()) return;
|
|
443
|
+
const at = (this.focused() as ThumbsValue) ?? this.state() ?? 'up';
|
|
444
|
+
let next: 'up' | 'down';
|
|
445
|
+
if (/^Arrow(Left|Right|Up|Down)$/.test(e.key)) next = at === 'up' ? 'down' : 'up';
|
|
446
|
+
else if (e.key === 'Home') next = 'up';
|
|
447
|
+
else if (e.key === 'End') next = 'down';
|
|
448
|
+
else if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
449
|
+
if (this.readonly() || !this.allowClear() || this.state() === null) return;
|
|
450
|
+
e.preventDefault();
|
|
451
|
+
return this.commit(null);
|
|
452
|
+
} else return;
|
|
453
|
+
e.preventDefault();
|
|
454
|
+
if (!this.readonly() && next !== this.state()) this.set(next);
|
|
455
|
+
this.item(next === 'up' ? 0 : 1)?.focus();
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
writeValue(v: unknown) {
|
|
459
|
+
this.state.set(v === 'up' || v === 'down' ? v : null);
|
|
460
|
+
}
|
|
461
|
+
}
|