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,720 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Color Picker, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (`npx ochreui init`) and overlay.ts (`npx ochreui add overlay`).
|
|
5
|
+
*
|
|
6
|
+
* <button uiColorPicker label="Project color" [presets]="presets" align="end" [formField]="form.color"></button>
|
|
7
|
+
* <ui-color-picker-panel alpha aria-label="Highlight color" [(value)]="highlight" />
|
|
8
|
+
* A swatch button that opens a card with saturation and brightness, hue, optional opacity, a hex field and presets.
|
|
9
|
+
* The value is a lowercase hex string (signal forms, reactive forms, ngModel or [(value)]); the panel is the card inline.
|
|
10
|
+
*
|
|
11
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
12
|
+
* --ui-color-picker-width card width (default 256px; Classic 264px)
|
|
13
|
+
* --ui-color-picker-area-height saturation and brightness area (default 148px; Classic 152px; touch 168px)
|
|
14
|
+
* --ui-color-picker-swatch preset size (default 20px; Classic 22px; touch 28px)
|
|
15
|
+
* --ui-color-picker-thumb area thumb (default 12px; touch 16px)
|
|
16
|
+
*/
|
|
17
|
+
import {
|
|
18
|
+
Component,
|
|
19
|
+
DOCUMENT,
|
|
20
|
+
DestroyRef,
|
|
21
|
+
Directive,
|
|
22
|
+
ElementRef,
|
|
23
|
+
PLATFORM_ID,
|
|
24
|
+
ViewContainerRef,
|
|
25
|
+
afterNextRender,
|
|
26
|
+
afterRenderEffect,
|
|
27
|
+
booleanAttribute,
|
|
28
|
+
computed,
|
|
29
|
+
effect,
|
|
30
|
+
forwardRef,
|
|
31
|
+
inject,
|
|
32
|
+
input,
|
|
33
|
+
inputBinding,
|
|
34
|
+
isDevMode,
|
|
35
|
+
linkedSignal,
|
|
36
|
+
model,
|
|
37
|
+
output,
|
|
38
|
+
outputBinding,
|
|
39
|
+
signal,
|
|
40
|
+
untracked,
|
|
41
|
+
type ComponentRef,
|
|
42
|
+
} from '@angular/core';
|
|
43
|
+
import { isPlatformBrowser } from '@angular/common';
|
|
44
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
45
|
+
import { OverlayDismiss, OverlayRef } from './overlay';
|
|
46
|
+
|
|
47
|
+
export type ColorPreset = string | { value: string; label: string };
|
|
48
|
+
export type ColorPickerSize = 'sm' | 'md';
|
|
49
|
+
export type ColorPickerSide = 'bottom' | 'top';
|
|
50
|
+
export type ColorPickerAlign = 'start' | 'end';
|
|
51
|
+
|
|
52
|
+
/** Hue 0 to 360; saturation, brightness and alpha 0 to 100. */
|
|
53
|
+
export interface ColorHsva {
|
|
54
|
+
h: number;
|
|
55
|
+
s: number;
|
|
56
|
+
v: number;
|
|
57
|
+
a: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ColorPickerLabels {
|
|
61
|
+
area: string;
|
|
62
|
+
/** `{s}` and `{v}` are replaced by the percentages. */
|
|
63
|
+
areaValue: string;
|
|
64
|
+
hue: string;
|
|
65
|
+
/** `{h}` is the angle, `{name}` one of `hueNames`. */
|
|
66
|
+
hueValue: string;
|
|
67
|
+
/** Red, orange, yellow, green, cyan, blue, purple, pink, red: below 15°, 45°, 70°, 165°, 195°, 255°, 290°, 335°, then up to 360°. */
|
|
68
|
+
hueNames: readonly string[];
|
|
69
|
+
opacity: string;
|
|
70
|
+
/** `{a}` is the percentage. */
|
|
71
|
+
opacityValue: string;
|
|
72
|
+
hex: string;
|
|
73
|
+
presets: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const LABELS: ColorPickerLabels = {
|
|
77
|
+
area: 'Saturation and brightness',
|
|
78
|
+
areaValue: 'Saturation {s}%, brightness {v}%',
|
|
79
|
+
hue: 'Hue',
|
|
80
|
+
hueValue: '{h}°, {name}',
|
|
81
|
+
hueNames: ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'pink', 'red'],
|
|
82
|
+
opacity: 'Opacity',
|
|
83
|
+
opacityValue: '{a}%',
|
|
84
|
+
hex: 'Hex color',
|
|
85
|
+
presets: 'Presets',
|
|
86
|
+
};
|
|
87
|
+
const HUE_EDGES = [15, 45, 70, 165, 195, 255, 290, 335];
|
|
88
|
+
const DEFAULT = '#2f7bf6';
|
|
89
|
+
|
|
90
|
+
type Rgba = [number, number, number, number];
|
|
91
|
+
|
|
92
|
+
const clamp = (n: number, max = 100) => Math.min(max, Math.max(0, n));
|
|
93
|
+
const fill = (text: string, vars: Record<string, string | number>) => text.replace(/\{(\w+)\}/g, (all, k) => String(vars[k] ?? all));
|
|
94
|
+
|
|
95
|
+
/** Reads `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`, with commas or spaces. Alpha is 0 to 100. */
|
|
96
|
+
function parse(text: string): Rgba | null {
|
|
97
|
+
const s = text.trim().toLowerCase();
|
|
98
|
+
const hex = /^#?([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/.exec(s)?.[1];
|
|
99
|
+
if (hex) {
|
|
100
|
+
const full = hex.length < 5 ? [...hex].map((c) => c + c).join('') : hex;
|
|
101
|
+
const at = (i: number) => parseInt(full.slice(i, i + 2), 16);
|
|
102
|
+
return [at(0), at(2), at(4), full.length > 6 ? at(6) / 2.55 : 100];
|
|
103
|
+
}
|
|
104
|
+
const m = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)\s*(?:[,/]\s*([\d.]+)(%?)\s*)?\)$/.exec(s);
|
|
105
|
+
if (!m) return null;
|
|
106
|
+
const a = m[4] === undefined ? 100 : m[5] ? +m[4] : +m[4] * 100;
|
|
107
|
+
return [clamp(+m[1], 255), clamp(+m[2], 255), clamp(+m[3], 255), clamp(a)];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function rgbOf({ h, s, v }: ColorHsva): [number, number, number] {
|
|
111
|
+
const at = (n: number) => {
|
|
112
|
+
const k = (n + h / 60) % 6;
|
|
113
|
+
return Math.round(2.55 * v * (1 - (s / 100) * Math.max(0, Math.min(k, 4 - k, 1))));
|
|
114
|
+
};
|
|
115
|
+
return [at(5), at(3), at(1)];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function hexOf([r, g, b, a]: Rgba, alpha: boolean) {
|
|
119
|
+
const two = (n: number) => Math.round(n).toString(16).padStart(2, '0');
|
|
120
|
+
const al = Math.round(a * 2.55);
|
|
121
|
+
return `#${two(r)}${two(g)}${two(b)}${alpha && al < 255 ? two(al) : ''}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const toHex = (c: ColorHsva, alpha: boolean) => hexOf([...rgbOf(c), c.a], alpha);
|
|
125
|
+
const cssOf = (c: ColorHsva, alpha: boolean) => `rgb(${rgbOf(c).join(' ')} / ${alpha ? c.a : 100}%)`;
|
|
126
|
+
|
|
127
|
+
/** The color as HSV. A grey keeps the hue it had, and black keeps its saturation too. */
|
|
128
|
+
function hsvaOf([r, g, b, a]: Rgba, alpha: boolean, prev?: ColorHsva): ColorHsva {
|
|
129
|
+
const max = Math.max(r, g, b);
|
|
130
|
+
const d = max - Math.min(r, g, b);
|
|
131
|
+
const h = !d ? 0 : max === r ? ((g - b) / d + 6) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
|
132
|
+
const next = { h: h * 60, s: max ? (d / max) * 100 : 0, v: max / 2.55, a: alpha ? a : 100 };
|
|
133
|
+
if (prev && (!next.s || !next.v)) next.h = prev.h;
|
|
134
|
+
if (prev && !next.v) next.s = prev.s;
|
|
135
|
+
return next;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function read(text: string, alpha: boolean, prev?: ColorHsva): ColorHsva {
|
|
139
|
+
if (prev && toHex(prev, alpha) === text) return prev;
|
|
140
|
+
const c = parse(text);
|
|
141
|
+
return c ? hsvaOf(c, alpha, prev) : (prev ?? hsvaOf(parse(DEFAULT)!, alpha));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The card body, shared by the picker's floating card and the panel. */
|
|
145
|
+
@Component({
|
|
146
|
+
selector: 'ui-color-card',
|
|
147
|
+
host: {
|
|
148
|
+
'[style.--_hue]': 'hsva().h',
|
|
149
|
+
'[style.--_c]': 'solid()',
|
|
150
|
+
'[style.--_ca]': 'css()',
|
|
151
|
+
},
|
|
152
|
+
template: `
|
|
153
|
+
<div class="area" role="slider" tabindex="0" dir="ltr" aria-valuemin="0" aria-valuemax="100" [attr.aria-label]="t().area"
|
|
154
|
+
[attr.aria-valuenow]="round(hsva().s)" [attr.aria-valuetext]="areaText()"
|
|
155
|
+
style="block-size: var(--_ah); border-radius: calc(var(--ui-radius-sm) + 4px * var(--_t)); background: linear-gradient(transparent, black), linear-gradient(90deg, white, hsl(var(--_hue) 100% 50%)); cursor: crosshair"
|
|
156
|
+
(pointerdown)="down($event, 0)" (pointermove)="move($event, 0)" (lostpointercapture)="drag = -1" (keydown)="key($event, 0)">
|
|
157
|
+
<span class="thumb" [style.left.%]="hsva().s" [style.top.%]="100 - hsva().v" style="inline-size: var(--_at)"></span>
|
|
158
|
+
</div>
|
|
159
|
+
<div class="track" role="slider" tabindex="0" dir="ltr" aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="360"
|
|
160
|
+
[attr.aria-label]="t().hue" [attr.aria-valuenow]="round(hsva().h)" [attr.aria-valuetext]="hueText()" [style.--_f]="hsva().h / 360"
|
|
161
|
+
style="background: linear-gradient(90deg in hsl longer hue, red, red)"
|
|
162
|
+
(pointerdown)="down($event, 1)" (pointermove)="move($event, 1)" (lostpointercapture)="drag = -1" (keydown)="key($event, 1)">
|
|
163
|
+
<span class="thumb" style="background: hsl(var(--_hue) 100% 50%)"></span>
|
|
164
|
+
</div>
|
|
165
|
+
@if (alpha()) {
|
|
166
|
+
<div style="display: flex; align-items: center; gap: var(--ui-space-2)">
|
|
167
|
+
<div class="track" role="slider" tabindex="0" dir="ltr" aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="100"
|
|
168
|
+
[attr.aria-label]="t().opacity" [attr.aria-valuenow]="round(hsva().a)" [attr.aria-valuetext]="alphaText()" [style.--_f]="hsva().a / 100"
|
|
169
|
+
style="background: linear-gradient(90deg, transparent, var(--_c)), var(--_chk)"
|
|
170
|
+
(pointerdown)="down($event, 2)" (pointermove)="move($event, 2)" (lostpointercapture)="drag = -1" (keydown)="key($event, 2)">
|
|
171
|
+
<span class="thumb" style="background: linear-gradient(var(--_ca), var(--_ca)), var(--ui-bg-knob)"></span>
|
|
172
|
+
</div>
|
|
173
|
+
<span aria-hidden="true" style="min-inline-size: 4ch; color: var(--ui-text-muted); text-align: end">{{ round(hsva().a) }}%</span>
|
|
174
|
+
</div>
|
|
175
|
+
}
|
|
176
|
+
<div style="display: flex; gap: var(--ui-space-2)">
|
|
177
|
+
<span aria-hidden="true" style="flex: none; inline-size: var(--_h); block-size: var(--_h); border-radius: var(--_r); background: linear-gradient(var(--_ca), var(--_ca)), var(--_chk); box-shadow: var(--_in); forced-color-adjust: none"></span>
|
|
178
|
+
<label class="field" [attr.data-invalid]="invalid() || null"
|
|
179
|
+
style="flex: 1; display: flex; align-items: center; gap: var(--ui-space-0-5); box-sizing: border-box; block-size: var(--_h); min-inline-size: 0; padding-inline: calc(7px + 2px * var(--_t)); border: 1px solid transparent; border-radius: var(--_r); cursor: text">
|
|
180
|
+
<span aria-hidden="true" style="color: var(--ui-text-muted)">#</span>
|
|
181
|
+
<input class="ui-input" style="outline: 0" spellcheck="false" autocomplete="off" autocapitalize="off" [attr.aria-label]="t().hex"
|
|
182
|
+
[attr.aria-invalid]="invalid() || null" [value]="text()" (input)="typed($event)" (keydown.enter)="enter($event)" (blur)="draft.set(null); invalid.set(false)" />
|
|
183
|
+
</label>
|
|
184
|
+
</div>
|
|
185
|
+
@if (items().length) {
|
|
186
|
+
<div role="radiogroup" [attr.aria-label]="t().presets" style="display: flex; flex-wrap: wrap; gap: var(--_pg); padding: var(--ui-space-0-5)" (keydown)="presetKey($event)">
|
|
187
|
+
@for (p of items(); track $index) {
|
|
188
|
+
<button type="button" role="radio" [attr.aria-checked]="$index === checked()" [attr.aria-label]="p.label" [title]="p.label"
|
|
189
|
+
[tabIndex]="$index === stop() ? 0 : -1" [style.--_c]="p.hex" (click)="choose($index)"></button>
|
|
190
|
+
}
|
|
191
|
+
</div>
|
|
192
|
+
}
|
|
193
|
+
`,
|
|
194
|
+
styles: `
|
|
195
|
+
:host {
|
|
196
|
+
--_t: var(--ui-look-tray, 0);
|
|
197
|
+
--_w: calc(0.5px + 0.5px * var(--_t));
|
|
198
|
+
--_in: inset 0 0 0 var(--_w) var(--ui-hairline);
|
|
199
|
+
--_h: calc(var(--ui-control-h-sm) + 4px * var(--_t));
|
|
200
|
+
--_r: calc(var(--ui-radius-sm) + 3px * var(--_t));
|
|
201
|
+
--_ah: var(--ui-color-picker-area-height, calc(148px + 4px * var(--_t)));
|
|
202
|
+
--_at: var(--ui-color-picker-thumb, 12px);
|
|
203
|
+
--_th: calc(10px + 2px * var(--_t));
|
|
204
|
+
--_k: calc(14px + 2px * var(--_t));
|
|
205
|
+
--_p: var(--ui-color-picker-swatch, calc(20px + 2px * var(--_t)));
|
|
206
|
+
--_pg: var(--ui-space-2);
|
|
207
|
+
--_chk: repeating-conic-gradient(var(--ui-bg-muted) 0 25%, var(--ui-bg-surface) 0 50%) 0 0 / 8px 8px;
|
|
208
|
+
display: grid;
|
|
209
|
+
grid-template-columns: minmax(0, 1fr);
|
|
210
|
+
gap: calc(var(--ui-space-3) + 2px * var(--_t));
|
|
211
|
+
padding: calc(11px + 2px * var(--_t));
|
|
212
|
+
color: var(--ui-text);
|
|
213
|
+
font: var(--ui-weight-medium) var(--ui-text-sm) / 20px var(--ui-font-mono);
|
|
214
|
+
}
|
|
215
|
+
[role='slider'] { position: relative; box-shadow: var(--_in); touch-action: none; forced-color-adjust: none; outline-offset: var(--ui-focus-offset); }
|
|
216
|
+
.track { flex: 1; block-size: var(--_th); border-radius: var(--ui-radius-full); cursor: pointer; }
|
|
217
|
+
.track::before { content: ''; position: absolute; inset: calc(var(--_th) / 2 - 10px) 0; }
|
|
218
|
+
.thumb {
|
|
219
|
+
position: absolute;
|
|
220
|
+
aspect-ratio: 1;
|
|
221
|
+
translate: -50% -50%;
|
|
222
|
+
border-radius: 50%;
|
|
223
|
+
background: var(--_c);
|
|
224
|
+
box-shadow: 0 0 0 2px var(--ui-bg-knob), 0 0 0 2.5px color-mix(in srgb, var(--ui-gray-1000) 18%, transparent), 0 1px 4px color-mix(in srgb, var(--ui-gray-1000) 24%, transparent);
|
|
225
|
+
pointer-events: none;
|
|
226
|
+
}
|
|
227
|
+
.track .thumb { top: 50%; left: calc(5px + (100% - 10px) * var(--_f)); inline-size: var(--_k); }
|
|
228
|
+
[tabindex]:focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); }
|
|
229
|
+
.field {
|
|
230
|
+
--_e: var(--ui-text-field-edge, var(--ui-hairline));
|
|
231
|
+
box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
232
|
+
transition: box-shadow var(--ui-duration-2) var(--ui-ease-out);
|
|
233
|
+
}
|
|
234
|
+
@media (hover: hover) { .field:hover { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
|
|
235
|
+
.field:focus-within { --_w: 1px; --_e: var(--ui-text-field-edge-focus, var(--ui-border-input)); box-shadow: 0 0 0 var(--_w) var(--_e); }
|
|
236
|
+
.field[data-invalid] { --_e: color-mix(in srgb, var(--ui-danger) calc(60% + 10% * var(--_t)), transparent); }
|
|
237
|
+
button {
|
|
238
|
+
inline-size: var(--_p);
|
|
239
|
+
aspect-ratio: 1;
|
|
240
|
+
padding: 0;
|
|
241
|
+
border: 0;
|
|
242
|
+
border-radius: 50%;
|
|
243
|
+
background: var(--_c);
|
|
244
|
+
box-shadow: var(--_in), 0 0 0 2px transparent, 0 0 0 3.5px transparent;
|
|
245
|
+
cursor: pointer;
|
|
246
|
+
-webkit-tap-highlight-color: transparent;
|
|
247
|
+
forced-color-adjust: none;
|
|
248
|
+
transition: var(--ui-duration-2) var(--ui-ease-out);
|
|
249
|
+
transition-property: scale, box-shadow;
|
|
250
|
+
}
|
|
251
|
+
[aria-checked='true'] { box-shadow: var(--_in), 0 0 0 2px var(--_bg), 0 0 0 3.5px var(--_c); }
|
|
252
|
+
@media (hover: hover) {
|
|
253
|
+
button:hover { scale: calc(1 + 0.1 * var(--ui-motion-scale)); }
|
|
254
|
+
}
|
|
255
|
+
@media (pointer: coarse) {
|
|
256
|
+
:host {
|
|
257
|
+
--_ah: var(--ui-color-picker-area-height, calc(168px + 8px * var(--_t)));
|
|
258
|
+
--_at: var(--ui-color-picker-thumb, 16px);
|
|
259
|
+
--_th: 14px;
|
|
260
|
+
--_k: 20px;
|
|
261
|
+
--_p: var(--ui-color-picker-swatch, 28px);
|
|
262
|
+
--_pg: var(--ui-space-2-5);
|
|
263
|
+
gap: 14px;
|
|
264
|
+
}
|
|
265
|
+
input { font-size: var(--ui-text-field-touch); }
|
|
266
|
+
}
|
|
267
|
+
@media (forced-colors: active) {
|
|
268
|
+
[aria-checked='true']:not(:focus-visible) { outline: 2px solid CanvasText; outline-offset: 2px; }
|
|
269
|
+
}
|
|
270
|
+
`,
|
|
271
|
+
})
|
|
272
|
+
class UiColorCard {
|
|
273
|
+
readonly hsva = input.required<ColorHsva>();
|
|
274
|
+
readonly alpha = input(false);
|
|
275
|
+
readonly presets = input<readonly ColorPreset[]>([]);
|
|
276
|
+
readonly labels = input<Partial<ColorPickerLabels>>({});
|
|
277
|
+
readonly pick = output<ColorHsva>();
|
|
278
|
+
|
|
279
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
280
|
+
protected readonly round = Math.round;
|
|
281
|
+
protected readonly solid = computed(() => cssOf(this.hsva(), false));
|
|
282
|
+
protected readonly css = computed(() => cssOf(this.hsva(), this.alpha()));
|
|
283
|
+
private readonly hex = computed(() => toHex(this.hsva(), this.alpha()));
|
|
284
|
+
protected readonly draft = signal<string | null>(null);
|
|
285
|
+
protected readonly invalid = signal(false);
|
|
286
|
+
protected readonly text = computed(() => this.draft() ?? this.hex().slice(1, 7).toUpperCase());
|
|
287
|
+
protected readonly items = computed(() =>
|
|
288
|
+
this.presets().flatMap((p) => {
|
|
289
|
+
const { value, label } = typeof p === 'string' ? { value: p, label: '' } : p;
|
|
290
|
+
const c = parse(value);
|
|
291
|
+
if (!c) return [];
|
|
292
|
+
const hex = hexOf(c, this.alpha());
|
|
293
|
+
return [{ hex, label: label || hex.toUpperCase() }];
|
|
294
|
+
}),
|
|
295
|
+
);
|
|
296
|
+
protected readonly checked = computed(() => this.items().findIndex((p) => p.hex === this.hex()));
|
|
297
|
+
protected readonly stop = computed(() => Math.max(0, this.checked()));
|
|
298
|
+
protected readonly areaText = computed(() => fill(this.t().areaValue, { s: Math.round(this.hsva().s), v: Math.round(this.hsva().v) }));
|
|
299
|
+
protected readonly hueText = computed(() => {
|
|
300
|
+
const h = Math.round(this.hsva().h);
|
|
301
|
+
const i = HUE_EDGES.findIndex((edge) => h < edge);
|
|
302
|
+
return fill(this.t().hueValue, { h, name: this.t().hueNames[i < 0 ? HUE_EDGES.length : i] ?? '' });
|
|
303
|
+
});
|
|
304
|
+
protected readonly alphaText = computed(() => fill(this.t().opacityValue, { a: Math.round(this.hsva().a) }));
|
|
305
|
+
protected drag = -1;
|
|
306
|
+
|
|
307
|
+
private apply(text: string) {
|
|
308
|
+
const c = parse(text);
|
|
309
|
+
if (c) this.pick.emit(hsvaOf(c, this.alpha(), this.hsva()));
|
|
310
|
+
return !!c;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** 0 is the area, 1 the hue, 2 the opacity. The thumb follows the pointer, even outside the card. */
|
|
314
|
+
protected down(e: PointerEvent, kind: number) {
|
|
315
|
+
if (e.button) return;
|
|
316
|
+
const el = e.currentTarget as HTMLElement;
|
|
317
|
+
e.preventDefault();
|
|
318
|
+
el.setPointerCapture?.(e.pointerId);
|
|
319
|
+
el.focus({ preventScroll: true, focusVisible: false } as FocusOptions);
|
|
320
|
+
this.drag = kind;
|
|
321
|
+
this.move(e, kind);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
protected move(e: PointerEvent, kind: number) {
|
|
325
|
+
if (this.drag !== kind) return;
|
|
326
|
+
const r = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
|
327
|
+
const c = this.hsva();
|
|
328
|
+
if (!kind) {
|
|
329
|
+
this.pick.emit({ ...c, s: clamp(((e.clientX - r.left) / r.width) * 100), v: clamp(100 - ((e.clientY - r.top) / r.height) * 100) });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const f = clamp((e.clientX - r.left - 5) / (r.width - 10), 1);
|
|
333
|
+
this.pick.emit(kind === 1 ? { ...c, h: f * 360 } : { ...c, a: f * 100 });
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
protected key(e: KeyboardEvent, kind: number) {
|
|
337
|
+
const c = this.hsva();
|
|
338
|
+
const n = e.shiftKey ? 10 : 1;
|
|
339
|
+
const d: Record<string, [number, number]> = {
|
|
340
|
+
ArrowLeft: [-n, 0],
|
|
341
|
+
ArrowRight: [n, 0],
|
|
342
|
+
ArrowUp: [0, n],
|
|
343
|
+
ArrowDown: [0, -n],
|
|
344
|
+
PageUp: [0, 10],
|
|
345
|
+
PageDown: [0, -10],
|
|
346
|
+
};
|
|
347
|
+
const step = d[e.key];
|
|
348
|
+
let next: ColorHsva;
|
|
349
|
+
if (!kind) {
|
|
350
|
+
const s = e.key === 'Home' ? 0 : e.key === 'End' ? 100 : step ? Math.round(c.s) + step[0] : NaN;
|
|
351
|
+
if (Number.isNaN(s)) return;
|
|
352
|
+
next = { ...c, s: clamp(s), v: clamp(Math.round(c.v) + (step?.[1] ?? 0)) };
|
|
353
|
+
} else {
|
|
354
|
+
const [k, max] = kind === 1 ? (['h', 360] as const) : (['a', 100] as const);
|
|
355
|
+
const v = e.key === 'Home' ? 0 : e.key === 'End' ? max : step ? Math.round(c[k]) + (step[0] || step[1]) : NaN;
|
|
356
|
+
if (Number.isNaN(v)) return;
|
|
357
|
+
next = { ...c, [k]: clamp(v, max) };
|
|
358
|
+
}
|
|
359
|
+
e.preventDefault();
|
|
360
|
+
this.pick.emit(next);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Six or eight digits and rgb() apply as you type; three digits wait for Enter. */
|
|
364
|
+
protected typed(e: Event) {
|
|
365
|
+
if ((e as InputEvent).isComposing) return;
|
|
366
|
+
const text = (e.target as HTMLInputElement).value;
|
|
367
|
+
this.draft.set(text);
|
|
368
|
+
this.invalid.set(false);
|
|
369
|
+
if (/^#?([\da-f]{6}|[\da-f]{8})$|^rgba?\(/i.test(text.trim())) this.apply(text);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
protected enter(e: Event) {
|
|
373
|
+
if ((e as KeyboardEvent).isComposing) return;
|
|
374
|
+
if (this.apply((e.target as HTMLInputElement).value)) this.draft.set(null);
|
|
375
|
+
else this.invalid.set(true);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
protected choose(i: number) {
|
|
379
|
+
const p = this.items()[i];
|
|
380
|
+
if (p) this.apply(p.hex);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
protected presetKey(e: KeyboardEvent) {
|
|
384
|
+
const group = e.currentTarget as HTMLElement;
|
|
385
|
+
const total = this.items().length;
|
|
386
|
+
const back = group.ownerDocument.defaultView?.getComputedStyle(group).direction === 'rtl' ? -1 : 1;
|
|
387
|
+
const moves: Record<string, number> = { ArrowRight: back, ArrowDown: 1, ArrowLeft: -back, ArrowUp: -1 };
|
|
388
|
+
const i = e.key === 'Home' ? 0 : e.key === 'End' ? total - 1 : e.key in moves ? (this.stop() + moves[e.key] + total) % total : -1;
|
|
389
|
+
if (i < 0) return;
|
|
390
|
+
e.preventDefault();
|
|
391
|
+
this.choose(i);
|
|
392
|
+
(group.children[i] as HTMLElement | undefined)?.focus();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Shared by the picker and the panel: the value, its HSV state and the form wiring. */
|
|
397
|
+
@Directive()
|
|
398
|
+
export abstract class UiColorControl implements ControlValueAccessor {
|
|
399
|
+
/** Lowercase `#rrggbb`, or `#rrggbbaa` with `alpha` below 100%. Also reads `#rgb`, uppercase and `rgb()`. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
|
|
400
|
+
readonly value = model(DEFAULT);
|
|
401
|
+
/** Adds the opacity slider and its percentage. Without it, opacity in an incoming value is dropped. */
|
|
402
|
+
readonly alpha = input(false, { transform: booleanAttribute });
|
|
403
|
+
/** Round swatches under the sliders. A label is read out and shown as the tooltip; a plain string reads as its hex. */
|
|
404
|
+
readonly presets = input<readonly ColorPreset[]>([]);
|
|
405
|
+
/** Words read out by screen readers, to translate them. */
|
|
406
|
+
readonly labels = input<Partial<ColorPickerLabels>>({});
|
|
407
|
+
readonly disabled = input(false, { transform: booleanAttribute });
|
|
408
|
+
|
|
409
|
+
protected readonly formDisabled = signal(false);
|
|
410
|
+
readonly isDisabled = computed(() => this.disabled() || this.formDisabled());
|
|
411
|
+
private readonly current = linkedSignal(() => this.value());
|
|
412
|
+
protected readonly hsva = linkedSignal<{ text: string; alpha: boolean }, ColorHsva>({
|
|
413
|
+
source: () => ({ text: this.current(), alpha: this.alpha() }),
|
|
414
|
+
computation: ({ text, alpha }, prev) => read(text, alpha, prev?.value),
|
|
415
|
+
});
|
|
416
|
+
/** The color now, normalized: lowercase `#rrggbb` or `#rrggbbaa`. */
|
|
417
|
+
readonly hex = computed(() => toHex(this.hsva(), this.alpha()));
|
|
418
|
+
protected onChange: (v: string) => void = () => {};
|
|
419
|
+
protected onTouched: () => void = () => {};
|
|
420
|
+
|
|
421
|
+
protected pick(next: ColorHsva) {
|
|
422
|
+
this.hsva.set(next);
|
|
423
|
+
const hex = toHex(next, this.alpha());
|
|
424
|
+
if (hex === this.current()) return;
|
|
425
|
+
this.current.set(hex);
|
|
426
|
+
this.value.set(hex);
|
|
427
|
+
this.onChange(hex);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
writeValue(v: unknown) {
|
|
431
|
+
this.current.set(typeof v === 'string' ? v : DEFAULT);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
registerOnChange(fn: (v: string) => void) {
|
|
435
|
+
this.onChange = fn;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
registerOnTouched(fn: () => void) {
|
|
439
|
+
this.onTouched = fn;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
setDisabledState(disabled: boolean) {
|
|
443
|
+
this.formDisabled.set(disabled);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** The floating card: the top-layer host of the picker's card body. */
|
|
448
|
+
@Component({
|
|
449
|
+
selector: 'ui-color-picker-card',
|
|
450
|
+
imports: [UiColorCard],
|
|
451
|
+
host: {
|
|
452
|
+
popover: 'manual',
|
|
453
|
+
tabindex: '-1',
|
|
454
|
+
role: 'dialog',
|
|
455
|
+
'[id]': 'cardId()',
|
|
456
|
+
'[attr.aria-label]': 'label()',
|
|
457
|
+
},
|
|
458
|
+
template: `<ui-color-card [hsva]="hsva()" [alpha]="alpha()" [presets]="presets()" [labels]="labels()" (pick)="pick.emit($event)" />`,
|
|
459
|
+
styles: `
|
|
460
|
+
:host {
|
|
461
|
+
--_t: var(--ui-look-tray, 0);
|
|
462
|
+
--_bg: var(--ui-bg-overlay);
|
|
463
|
+
--_y: 0;
|
|
464
|
+
position: fixed;
|
|
465
|
+
inset: auto;
|
|
466
|
+
z-index: var(--ui-z-popover);
|
|
467
|
+
box-sizing: border-box;
|
|
468
|
+
inline-size: var(--ui-color-picker-width, calc(256px + 8px * var(--_t)));
|
|
469
|
+
max-inline-size: calc(100vw - 16px);
|
|
470
|
+
max-block-size: calc(100dvh - 16px);
|
|
471
|
+
margin: 0;
|
|
472
|
+
padding: 0;
|
|
473
|
+
overflow: auto;
|
|
474
|
+
overscroll-behavior: contain;
|
|
475
|
+
border: 1px solid transparent;
|
|
476
|
+
border-radius: calc(var(--ui-radius-lg) + 4px * var(--_t));
|
|
477
|
+
outline: none;
|
|
478
|
+
background: var(--_bg);
|
|
479
|
+
box-shadow: var(--ui-shadow-md);
|
|
480
|
+
color: var(--ui-text);
|
|
481
|
+
transition: var(--ui-duration-3) var(--ui-ease-out);
|
|
482
|
+
transition-property: opacity, scale, translate;
|
|
483
|
+
}
|
|
484
|
+
:host(:not([data-open])) { display: none !important; }
|
|
485
|
+
:host([data-side='top']) { --_y: 4px; }
|
|
486
|
+
:host([data-side='bottom']) { --_y: -4px; }
|
|
487
|
+
:host([data-enter]) { opacity: 0; scale: 0.96; translate: 0 var(--_y); transition: none; }
|
|
488
|
+
`,
|
|
489
|
+
})
|
|
490
|
+
class UiColorPickerCard {
|
|
491
|
+
readonly hsva = input.required<ColorHsva>();
|
|
492
|
+
readonly alpha = input(false);
|
|
493
|
+
readonly presets = input<readonly ColorPreset[]>([]);
|
|
494
|
+
readonly labels = input<Partial<ColorPickerLabels>>({});
|
|
495
|
+
readonly label = input('');
|
|
496
|
+
readonly cardId = input('');
|
|
497
|
+
readonly pick = output<ColorHsva>();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
let nextId = 0;
|
|
501
|
+
|
|
502
|
+
/** Put it on your own `<button>`: it draws the swatch and the hex, and opens the card next to it. */
|
|
503
|
+
@Component({
|
|
504
|
+
selector: 'button[uiColorPicker]',
|
|
505
|
+
exportAs: 'uiColorPicker',
|
|
506
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiColorPicker), multi: true }],
|
|
507
|
+
host: {
|
|
508
|
+
type: 'button',
|
|
509
|
+
'aria-haspopup': 'dialog',
|
|
510
|
+
'[attr.aria-expanded]': 'open()',
|
|
511
|
+
'[attr.aria-controls]': 'open() ? cardId : null',
|
|
512
|
+
'[attr.aria-label]': 'ariaLabel() ?? (showValue() ? null : label() + ": " + face())',
|
|
513
|
+
'[attr.name]': 'null',
|
|
514
|
+
'[attr.data-size]': 'size()',
|
|
515
|
+
'[attr.data-bare]': '!showValue() || null',
|
|
516
|
+
'[style.--_c]': 'swatch()',
|
|
517
|
+
'[disabled]': 'isDisabled()',
|
|
518
|
+
'(click)': 'toggle()',
|
|
519
|
+
'(keydown.arrowdown)': 'arrow($event)',
|
|
520
|
+
'(blur)': 'open() || onTouched()',
|
|
521
|
+
'(document:reset)': 'reset($event)',
|
|
522
|
+
},
|
|
523
|
+
template: `<span class="sw" aria-hidden="true"></span>@if (showValue()) {<span class="hex"><span class="ui-sr">{{ label() }}: </span>{{ face() }}</span>}@if (name()) {<input type="hidden" [name]="name()" [value]="hex()" />}`,
|
|
524
|
+
styles: `
|
|
525
|
+
:host {
|
|
526
|
+
--_t: var(--ui-look-tray, 0);
|
|
527
|
+
--_e: var(--ui-text-field-edge, var(--ui-hairline));
|
|
528
|
+
--_w: calc(0.5px + 0.5px * var(--_t));
|
|
529
|
+
--_h: calc(var(--ui-control-h-sm) + 4px * var(--_t));
|
|
530
|
+
--_s: calc(16px + 2px * var(--_t));
|
|
531
|
+
display: inline-flex;
|
|
532
|
+
flex-shrink: 0;
|
|
533
|
+
align-items: center;
|
|
534
|
+
gap: var(--ui-space-1-5);
|
|
535
|
+
box-sizing: border-box;
|
|
536
|
+
block-size: var(--_h);
|
|
537
|
+
margin: 0;
|
|
538
|
+
padding-block: 0;
|
|
539
|
+
padding-inline: calc(5px + 2px * var(--_t)) calc(9px + 2px * var(--_t));
|
|
540
|
+
border: 1px solid transparent;
|
|
541
|
+
border-radius: calc(var(--ui-radius-sm) + 3px * var(--_t));
|
|
542
|
+
background: var(--ui-bg-overlay);
|
|
543
|
+
box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
544
|
+
color: var(--ui-text);
|
|
545
|
+
font: var(--ui-weight-medium) var(--ui-text-sm) / 20px var(--ui-font-mono);
|
|
546
|
+
white-space: nowrap;
|
|
547
|
+
cursor: pointer;
|
|
548
|
+
-webkit-tap-highlight-color: transparent;
|
|
549
|
+
transition: box-shadow var(--ui-duration-2) var(--ui-ease-out);
|
|
550
|
+
}
|
|
551
|
+
:host([aria-expanded='true']) { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); }
|
|
552
|
+
@media (hover: hover) { :host(:hover) { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
|
|
553
|
+
:host(:focus-visible) { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
|
|
554
|
+
:host(:disabled) { --_e: var(--ui-hairline); opacity: var(--ui-disabled-opacity); cursor: not-allowed; }
|
|
555
|
+
:host([data-size='sm']) { --_h: calc(var(--ui-control-h-sm) - 4px + 4px * var(--_t)); --_s: calc(14px + 2px * var(--_t)); padding-inline: calc(4px + 2px * var(--_t)) calc(7px + 2px * var(--_t)); }
|
|
556
|
+
:host([data-bare]) { justify-content: center; inline-size: var(--_h); padding: 0; }
|
|
557
|
+
.sw { flex: none; inline-size: var(--_s); block-size: var(--_s); border-radius: calc(4px + 2px * var(--_t)); background: var(--_c); box-shadow: inset 0 0 0 var(--_w) var(--ui-hairline); forced-color-adjust: none; }
|
|
558
|
+
`,
|
|
559
|
+
})
|
|
560
|
+
export class UiColorPicker extends UiColorControl {
|
|
561
|
+
/** Shows the card. Bind `[(open)]`: it turns false on the button, a press outside, Escape or Tab out. True from the start opens it without moving focus. */
|
|
562
|
+
readonly open = model(false);
|
|
563
|
+
/** Names the button ("Project color: #2F7BF6") and the card. */
|
|
564
|
+
readonly label = input('Color');
|
|
565
|
+
/** The hex next to the swatch. Off gives a square, swatch-only button. */
|
|
566
|
+
readonly showValue = input(true, { transform: booleanAttribute });
|
|
567
|
+
/** 28 or 24px tall; Classic 32 or 28px. Taller on touch screens. */
|
|
568
|
+
readonly size = input<ColorPickerSize>('md');
|
|
569
|
+
/** Preferred side. It flips when the other side has more room. */
|
|
570
|
+
readonly side = input<ColorPickerSide>('bottom');
|
|
571
|
+
/** Which edges line up. Start is the left edge, or the right one in right-to-left pages. Switches when it would stick out. */
|
|
572
|
+
readonly align = input<ColorPickerAlign>('start');
|
|
573
|
+
/** Submits the hex with a native form, through a hidden input. */
|
|
574
|
+
readonly name = input<string>();
|
|
575
|
+
readonly ariaLabel = input<string>(undefined, { alias: 'aria-label' });
|
|
576
|
+
|
|
577
|
+
protected readonly cardId = `ui-color-picker-${nextId++}-card`;
|
|
578
|
+
protected readonly face = computed(() => this.hex().toUpperCase());
|
|
579
|
+
protected readonly swatch = computed(() => cssOf(this.hsva(), this.alpha()));
|
|
580
|
+
|
|
581
|
+
private readonly el = inject<ElementRef<HTMLButtonElement>>(ElementRef).nativeElement;
|
|
582
|
+
private readonly vcr = inject(ViewContainerRef);
|
|
583
|
+
private readonly doc = inject(DOCUMENT);
|
|
584
|
+
private readonly browser = isPlatformBrowser(inject(PLATFORM_ID));
|
|
585
|
+
private card?: ComponentRef<UiColorPickerCard>;
|
|
586
|
+
private overlay?: OverlayRef;
|
|
587
|
+
private reason?: OverlayDismiss;
|
|
588
|
+
private shown = false;
|
|
589
|
+
private first = true;
|
|
590
|
+
private initial = '';
|
|
591
|
+
|
|
592
|
+
constructor() {
|
|
593
|
+
super();
|
|
594
|
+
afterRenderEffect(() => {
|
|
595
|
+
const on = this.open() && !this.isDisabled();
|
|
596
|
+
untracked(() => this.sync(on));
|
|
597
|
+
});
|
|
598
|
+
afterRenderEffect(() => {
|
|
599
|
+
this.side();
|
|
600
|
+
this.align();
|
|
601
|
+
untracked(() => this.overlay?.place());
|
|
602
|
+
});
|
|
603
|
+
effect(() => {
|
|
604
|
+
if (this.isDisabled() && untracked(this.open)) this.open.set(false);
|
|
605
|
+
});
|
|
606
|
+
afterNextRender(() => {
|
|
607
|
+
this.initial = this.hex();
|
|
608
|
+
if (isDevMode() && !this.label() && !this.ariaLabel() && !this.el.hasAttribute('aria-labelledby')) {
|
|
609
|
+
console.warn('button[uiColorPicker]: give it a label or an aria-label.', this.el);
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
inject(DestroyRef).onDestroy(() => this.overlay?.hide(false));
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
toggle() {
|
|
616
|
+
if (this.isDisabled()) return;
|
|
617
|
+
if (this.open()) this.dismiss('trigger');
|
|
618
|
+
else this.open.set(true);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
protected arrow(e: Event) {
|
|
622
|
+
if (this.open() || this.isDisabled()) return;
|
|
623
|
+
e.preventDefault();
|
|
624
|
+
this.open.set(true);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** A native form reset puts back the first color. */
|
|
628
|
+
protected reset(e: Event) {
|
|
629
|
+
if (e.target === this.el.form) setTimeout(() => this.pick(read(this.initial, this.alpha(), this.hsva())));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
private dismiss(reason: OverlayDismiss) {
|
|
633
|
+
if (!this.open()) return;
|
|
634
|
+
this.reason = reason;
|
|
635
|
+
this.open.set(false);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
private sync(on: boolean) {
|
|
639
|
+
const focus = !this.first;
|
|
640
|
+
this.first = false;
|
|
641
|
+
if (!this.browser) return;
|
|
642
|
+
if (!on) {
|
|
643
|
+
if (!this.shown) return;
|
|
644
|
+
const reason = this.reason;
|
|
645
|
+
this.reason = undefined;
|
|
646
|
+
this.shown = false;
|
|
647
|
+
this.overlay?.hide(reason !== 'outside' && reason !== 'focus-out');
|
|
648
|
+
this.onTouched();
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (!this.card) {
|
|
652
|
+
this.card = this.vcr.createComponent(UiColorPickerCard, {
|
|
653
|
+
bindings: [
|
|
654
|
+
inputBinding('hsva', this.hsva),
|
|
655
|
+
inputBinding('alpha', this.alpha),
|
|
656
|
+
inputBinding('presets', this.presets),
|
|
657
|
+
inputBinding('labels', this.labels),
|
|
658
|
+
inputBinding('label', this.label),
|
|
659
|
+
inputBinding('cardId', () => this.cardId),
|
|
660
|
+
outputBinding<ColorHsva>('pick', (c) => this.pick(c)),
|
|
661
|
+
],
|
|
662
|
+
});
|
|
663
|
+
this.overlay = new OverlayRef(this.card.location.nativeElement, this.doc, {
|
|
664
|
+
anchor: () => this.el,
|
|
665
|
+
trigger: () => this.el,
|
|
666
|
+
side: () => this.side(),
|
|
667
|
+
align: () => this.align(),
|
|
668
|
+
offset: () => 6,
|
|
669
|
+
dismiss: (reason) => this.dismiss(reason),
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
this.card.changeDetectorRef.detectChanges();
|
|
673
|
+
this.shown = true;
|
|
674
|
+
this.overlay!.show(focus);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** The same card without the button, placed right in a form or a settings page. Name it with `aria-label`. */
|
|
679
|
+
@Component({
|
|
680
|
+
selector: 'ui-color-picker-panel',
|
|
681
|
+
imports: [UiColorCard],
|
|
682
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiColorPickerPanel), multi: true }],
|
|
683
|
+
host: {
|
|
684
|
+
role: 'group',
|
|
685
|
+
'[attr.aria-disabled]': 'isDisabled() || null',
|
|
686
|
+
'[inert]': 'isDisabled()',
|
|
687
|
+
'(focusout)': 'left($event)',
|
|
688
|
+
},
|
|
689
|
+
template: `<ui-color-card [hsva]="hsva()" [alpha]="alpha()" [presets]="presets()" [labels]="labels()" (pick)="pick($event)" />`,
|
|
690
|
+
styles: `
|
|
691
|
+
:host {
|
|
692
|
+
--_t: var(--ui-look-tray, 0);
|
|
693
|
+
--_bg: var(--ui-bg-surface);
|
|
694
|
+
display: block;
|
|
695
|
+
box-sizing: border-box;
|
|
696
|
+
inline-size: min(var(--ui-color-picker-width, calc(256px + 8px * var(--_t))), 100%);
|
|
697
|
+
border: 1px solid transparent;
|
|
698
|
+
border-radius: calc(var(--ui-radius-lg) + 4px * var(--_t));
|
|
699
|
+
background: var(--_bg);
|
|
700
|
+
box-shadow: var(--ui-shadow-sm);
|
|
701
|
+
}
|
|
702
|
+
:host([aria-disabled]) { opacity: var(--ui-disabled-opacity); }
|
|
703
|
+
`,
|
|
704
|
+
})
|
|
705
|
+
export class UiColorPickerPanel extends UiColorControl {
|
|
706
|
+
private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
707
|
+
|
|
708
|
+
constructor() {
|
|
709
|
+
super();
|
|
710
|
+
afterNextRender(() => {
|
|
711
|
+
if (isDevMode() && !this.el.hasAttribute('aria-label') && !this.el.hasAttribute('aria-labelledby')) {
|
|
712
|
+
console.warn('ui-color-picker-panel: give it an aria-label or an aria-labelledby.', this.el);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
protected left(e: FocusEvent) {
|
|
718
|
+
if (!this.el.contains(e.relatedTarget as Node | null)) this.onTouched();
|
|
719
|
+
}
|
|
720
|
+
}
|