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,769 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Select, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (`npx ochreui init`) and overlay.ts (`npx ochreui add overlay`).
|
|
5
|
+
*
|
|
6
|
+
* <ui-select label="Region"><button uiSelect [formField]="form.region"></button>
|
|
7
|
+
* @for (r of regions; track r.id) {<ui-option [value]="r.id" [description]="r.note">{{ r.name }}</ui-option>}
|
|
8
|
+
* </ui-select>
|
|
9
|
+
* Your own <button> is the field and owns the value, so signal forms, reactive forms, ngModel and [(value)] work.
|
|
10
|
+
* The list opens in the top layer, with type-ahead, icons and a quieter second line.
|
|
11
|
+
*
|
|
12
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
13
|
+
* --ui-text-field-edge resting edge (default: the hairline), shared with Text Field
|
|
14
|
+
* --ui-text-field-edge-hover edge on hover
|
|
15
|
+
* --ui-text-field-edge-focus edge while the list is open (default: the input border, 3:1 or more)
|
|
16
|
+
* --ui-text-field-radius corners (default 7px; Classic 10px)
|
|
17
|
+
* --ui-select-panel-max-height tallest the list gets before it scrolls (default 288px)
|
|
18
|
+
*/
|
|
19
|
+
import {
|
|
20
|
+
Component,
|
|
21
|
+
DOCUMENT,
|
|
22
|
+
DestroyRef,
|
|
23
|
+
Directive,
|
|
24
|
+
ElementRef,
|
|
25
|
+
PLATFORM_ID,
|
|
26
|
+
Signal,
|
|
27
|
+
afterEveryRender,
|
|
28
|
+
afterNextRender,
|
|
29
|
+
afterRenderEffect,
|
|
30
|
+
booleanAttribute,
|
|
31
|
+
computed,
|
|
32
|
+
contentChild,
|
|
33
|
+
contentChildren,
|
|
34
|
+
effect,
|
|
35
|
+
forwardRef,
|
|
36
|
+
inject,
|
|
37
|
+
input,
|
|
38
|
+
isDevMode,
|
|
39
|
+
model,
|
|
40
|
+
signal,
|
|
41
|
+
untracked,
|
|
42
|
+
viewChild,
|
|
43
|
+
} from '@angular/core';
|
|
44
|
+
import { isPlatformBrowser } from '@angular/common';
|
|
45
|
+
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
46
|
+
import { OverlayOptions, OverlayRef, placeOverlay } from './overlay';
|
|
47
|
+
|
|
48
|
+
export type SelectSize = 'sm' | 'md' | 'lg';
|
|
49
|
+
|
|
50
|
+
export interface SelectLabels {
|
|
51
|
+
optional: string;
|
|
52
|
+
placeholder: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const LABELS: SelectLabels = { optional: 'Optional', placeholder: 'Choose…' };
|
|
56
|
+
const OFFSET = 4;
|
|
57
|
+
const MIN_ROOM = 160;
|
|
58
|
+
const TYPEAHEAD_MS = 650;
|
|
59
|
+
|
|
60
|
+
let nextId = 0;
|
|
61
|
+
|
|
62
|
+
/** Marks the 16px icon of an option: `<svg uiOptionIcon>`. The chosen option's icon is repeated in the field. */
|
|
63
|
+
@Directive({
|
|
64
|
+
selector: '[uiOptionIcon]',
|
|
65
|
+
host: {
|
|
66
|
+
class: 'ui-icon',
|
|
67
|
+
'aria-hidden': 'true',
|
|
68
|
+
style: 'inline-size: var(--ui-icon-md); block-size: var(--ui-icon-md); color: var(--_ic)',
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
export class UiOptionIcon {
|
|
72
|
+
readonly el = inject<ElementRef<Element>>(ElementRef).nativeElement;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One choice in a `<ui-select>`. Its text is the label; `description` adds a quieter second line. */
|
|
76
|
+
@Component({
|
|
77
|
+
selector: 'ui-option',
|
|
78
|
+
host: {
|
|
79
|
+
role: 'option',
|
|
80
|
+
'[id]': 'id',
|
|
81
|
+
'[attr.aria-selected]': 'selected()',
|
|
82
|
+
'[attr.aria-disabled]': 'disabled() || null',
|
|
83
|
+
'[attr.aria-labelledby]': 'id + "-l"',
|
|
84
|
+
'[attr.aria-describedby]': 'description() ? id + "-d" : null',
|
|
85
|
+
'[attr.data-active]': 'active() || null',
|
|
86
|
+
'(pointermove)': 'field.hover(this, $event)',
|
|
87
|
+
'(click)': 'field.pick(this)',
|
|
88
|
+
},
|
|
89
|
+
template: `
|
|
90
|
+
<ng-content select="[uiOptionIcon]" />
|
|
91
|
+
<span class="text">
|
|
92
|
+
<span #label class="label" [id]="id + '-l'"><ng-content /></span>
|
|
93
|
+
@if (description()) {<span class="desc" [id]="id + '-d'">{{ description() }}</span>}
|
|
94
|
+
</span>
|
|
95
|
+
<span class="check" aria-hidden="true">
|
|
96
|
+
@if (selected()) {<svg class="ui-icon" viewBox="0 0 16 16"><path class="ui-draw" pathLength="1" d="M3.5 8.5l3 3 6-7" /></svg>}
|
|
97
|
+
</span>
|
|
98
|
+
`,
|
|
99
|
+
styles: `
|
|
100
|
+
:host {
|
|
101
|
+
--_t: var(--ui-look-tray, 0);
|
|
102
|
+
--_ic: var(--ui-icon);
|
|
103
|
+
position: relative;
|
|
104
|
+
display: flex;
|
|
105
|
+
align-items: center;
|
|
106
|
+
gap: var(--ui-space-2);
|
|
107
|
+
box-sizing: border-box;
|
|
108
|
+
min-block-size: calc(var(--ui-space-8) + 2px * var(--_t));
|
|
109
|
+
padding: calc(var(--ui-space-1-5) + 1px * var(--_t)) calc(var(--ui-space-2) + 2px * var(--_t));
|
|
110
|
+
border-radius: calc(var(--ui-radius-sm) + 3px * var(--_t));
|
|
111
|
+
color: var(--ui-text);
|
|
112
|
+
font-size: calc(var(--ui-text-base) + 0.5px * var(--_t));
|
|
113
|
+
line-height: var(--ui-leading-base);
|
|
114
|
+
cursor: pointer;
|
|
115
|
+
user-select: none;
|
|
116
|
+
-webkit-tap-highlight-color: transparent;
|
|
117
|
+
}
|
|
118
|
+
.text { display: flex; flex: 1; flex-direction: column; min-inline-size: 0; }
|
|
119
|
+
.label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
120
|
+
:host([aria-selected='true']) .label { font-weight: var(--ui-weight-medium); }
|
|
121
|
+
.desc { color: var(--ui-text-muted); font-size: var(--ui-text-sm); line-height: var(--ui-leading-xs); }
|
|
122
|
+
.check { display: flex; flex: none; align-items: center; justify-content: center; inline-size: var(--ui-icon-md); block-size: var(--ui-icon-md); }
|
|
123
|
+
svg { inline-size: var(--ui-icon-sm); block-size: var(--ui-icon-sm); stroke-width: calc(var(--ui-icon-stroke) + 0.25px); }
|
|
124
|
+
:host([aria-disabled]) { --_ic: var(--ui-text-disabled); color: var(--ui-text-disabled); cursor: default; }
|
|
125
|
+
:host([aria-disabled]) .desc { color: inherit; }
|
|
126
|
+
@media (pointer: coarse) {
|
|
127
|
+
:host { min-block-size: var(--ui-space-10); }
|
|
128
|
+
.label { font-size: calc(var(--ui-text-md) + 1px); }
|
|
129
|
+
}
|
|
130
|
+
@media (forced-colors: active) {
|
|
131
|
+
:host([data-active]) { --_ic: HighlightText; forced-color-adjust: none; color: HighlightText; }
|
|
132
|
+
:host([aria-disabled]) { --_ic: GrayText; color: GrayText; }
|
|
133
|
+
}
|
|
134
|
+
`,
|
|
135
|
+
})
|
|
136
|
+
export class UiOption {
|
|
137
|
+
/** What the form gets when this option is picked. */
|
|
138
|
+
readonly value = input<unknown>();
|
|
139
|
+
/** Shown dimmed; the pointer, the keys and type-ahead skip it. */
|
|
140
|
+
readonly disabled = input(false, { transform: booleanAttribute });
|
|
141
|
+
/** A quieter second line, heard after a pause. */
|
|
142
|
+
readonly description = input<string>();
|
|
143
|
+
/** What type-ahead matches and the field shows. Default: the option's text. */
|
|
144
|
+
readonly textValue = input<string>();
|
|
145
|
+
|
|
146
|
+
readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
147
|
+
readonly icon = contentChild(UiOptionIcon);
|
|
148
|
+
protected readonly field = inject(UiSelect);
|
|
149
|
+
readonly id = `${this.field.listId}-${nextId++}`;
|
|
150
|
+
private readonly label = viewChild.required<ElementRef<HTMLElement>>('label');
|
|
151
|
+
private readonly content = signal('');
|
|
152
|
+
readonly text = computed(() => (this.textValue() ?? this.content()).trim());
|
|
153
|
+
readonly selected: Signal<boolean> = computed(() => this.field.selected() === this);
|
|
154
|
+
protected readonly active = computed(() => this.field.activeOption() === this);
|
|
155
|
+
|
|
156
|
+
constructor() {
|
|
157
|
+
afterEveryRender(() => this.content.set(this.label().nativeElement.textContent ?? ''));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Put it on the `<button>` inside a `<ui-select>`; leave the button empty. It owns the value, the ARIA and the keys. */
|
|
162
|
+
@Directive({
|
|
163
|
+
selector: 'button[uiSelect]',
|
|
164
|
+
exportAs: 'uiSelect',
|
|
165
|
+
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiSelectTrigger), multi: true }],
|
|
166
|
+
host: {
|
|
167
|
+
type: 'button',
|
|
168
|
+
role: 'combobox',
|
|
169
|
+
'aria-haspopup': 'listbox',
|
|
170
|
+
style:
|
|
171
|
+
'position: absolute; inset: 0; box-sizing: border-box; margin: 0; padding: 0 var(--_pe) 0 var(--_ps); border: 0; border-radius: inherit; outline: 0; background: none; color: var(--_fg); font: inherit; text-align: start; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: inherit; touch-action: manipulation; -webkit-tap-highlight-color: transparent',
|
|
172
|
+
'[id]': 'id()',
|
|
173
|
+
'[attr.name]': 'null',
|
|
174
|
+
'[attr.required]': 'null',
|
|
175
|
+
'[attr.aria-expanded]': 'open()',
|
|
176
|
+
'[attr.aria-controls]': 'open() ? field.listId : null',
|
|
177
|
+
'[attr.aria-activedescendant]': 'open() ? (field.activeOption()?.id ?? null) : null',
|
|
178
|
+
'[attr.aria-describedby]': 'describedBy()',
|
|
179
|
+
'[attr.aria-invalid]': 'field.invalid() || null',
|
|
180
|
+
'[attr.aria-required]': 'required() || null',
|
|
181
|
+
'[textContent]': 'field.display()',
|
|
182
|
+
'(click)': 'field.toggle()',
|
|
183
|
+
'(keydown)': 'field.key($event)',
|
|
184
|
+
'(keyup)': 'keyup($event)',
|
|
185
|
+
'(blur)': 'blurred()',
|
|
186
|
+
'(document:reset)': 'formReset($event)',
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
export class UiSelectTrigger<T = unknown> implements ControlValueAccessor {
|
|
190
|
+
/** The chosen value; `null` shows the placeholder. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
|
|
191
|
+
readonly value = model<T | null>(null);
|
|
192
|
+
/** Whether the list is showing. Bind `[(open)]` to open it from code. */
|
|
193
|
+
readonly open = model(false);
|
|
194
|
+
/** Shown in grey while nothing is chosen. Default: `labels.placeholder`. */
|
|
195
|
+
readonly placeholder = input<string>();
|
|
196
|
+
/** How a value is matched to an option, for object values. Default: `Object.is`. */
|
|
197
|
+
readonly compareWith = input<(a: T, b: T) => boolean>(Object.is);
|
|
198
|
+
/** Submits the value as text with a native form, through a hidden input. */
|
|
199
|
+
readonly name = input<string>();
|
|
200
|
+
/** Sets `aria-required`. Signal forms set it from the schema. */
|
|
201
|
+
readonly required = input(false, { transform: booleanAttribute });
|
|
202
|
+
readonly id = input(`ui-select-${nextId++}`);
|
|
203
|
+
/** Your own descriptions, kept alongside the field's. Set it as `aria-describedby="…"`, not `[attr.aria-describedby]`. */
|
|
204
|
+
readonly ariaDescribedby = input<string>(undefined, { alias: 'aria-describedby' });
|
|
205
|
+
|
|
206
|
+
readonly el = inject<ElementRef<HTMLButtonElement>>(ElementRef).nativeElement;
|
|
207
|
+
protected readonly field = inject(UiSelect);
|
|
208
|
+
readonly disabled = signal(false);
|
|
209
|
+
private onChange: (v: T | null) => void = () => {};
|
|
210
|
+
private onTouched: () => void = () => {};
|
|
211
|
+
private initial: T | null = null;
|
|
212
|
+
|
|
213
|
+
protected readonly describedBy = computed(
|
|
214
|
+
() => [this.ariaDescribedby(), ...this.field.describers()].filter(Boolean).join(' ') || null,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
constructor() {
|
|
218
|
+
const destroy = inject(DestroyRef);
|
|
219
|
+
afterNextRender(() => {
|
|
220
|
+
const el = this.el;
|
|
221
|
+
this.initial = this.value();
|
|
222
|
+
const read = () => this.disabled.set(el.disabled);
|
|
223
|
+
read();
|
|
224
|
+
const view = el.ownerDocument.defaultView;
|
|
225
|
+
if (!view?.MutationObserver) return;
|
|
226
|
+
const watch = new view.MutationObserver(read);
|
|
227
|
+
watch.observe(el, { attributes: true, attributeFilter: ['disabled'] });
|
|
228
|
+
destroy.onDestroy(() => watch.disconnect());
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** True when `a` and `b` are the same value, through `compareWith`. */
|
|
233
|
+
matches(a: unknown, b: unknown) {
|
|
234
|
+
return a === null || b === null ? a === b : this.compareWith()(a as T, b as T);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Sets the value as a pick does: forms hear it, and nothing happens when it already is the value. */
|
|
238
|
+
choose(v: unknown) {
|
|
239
|
+
if (this.matches(v, this.value())) return;
|
|
240
|
+
this.value.set(v as T);
|
|
241
|
+
this.onChange(v as T);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
protected blurred() {
|
|
245
|
+
this.onTouched();
|
|
246
|
+
this.open.set(false);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Space would click the button again on release; the keydown already handled it. */
|
|
250
|
+
protected keyup(event: KeyboardEvent) {
|
|
251
|
+
if (event.key === ' ') event.preventDefault();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** With a name, a native form reset puts back the value it started with. */
|
|
255
|
+
protected formReset(event: Event) {
|
|
256
|
+
if (!this.name() || event.target !== this.el.form) return;
|
|
257
|
+
setTimeout(() => {
|
|
258
|
+
if (event.defaultPrevented) return;
|
|
259
|
+
this.open.set(false);
|
|
260
|
+
this.choose(this.initial);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
writeValue(v: T | null | undefined) {
|
|
265
|
+
this.value.set(v ?? null);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
registerOnChange(fn: (v: T | null) => void) {
|
|
269
|
+
this.onChange = fn;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
registerOnTouched(fn: () => void) {
|
|
273
|
+
this.onTouched = fn;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
setDisabledState(disabled: boolean) {
|
|
277
|
+
this.el.disabled = disabled;
|
|
278
|
+
this.disabled.set(disabled);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const space = (n: string) => `var(--ui-space-${n})`;
|
|
283
|
+
const SIZES: Record<SelectSize, Record<string, string>> = {
|
|
284
|
+
sm: { '--_h0': 'var(--ui-control-h-sm)', '--_ht': `calc(${space('8')} + ${space('0-5')})` },
|
|
285
|
+
md: { '--_h0': 'var(--ui-control-h-md)', '--_ht': `calc(${space('8')} + ${space('1-5')})` },
|
|
286
|
+
lg: { '--_h0': `calc(var(--ui-control-h-md) + ${space('1')})`, '--_ht': `calc(${space('10')} + ${space('0-5')})` },
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/** The overlay layer, placed at the field's width: below while 160px of room is left there, else where there is more. */
|
|
290
|
+
class SelectOverlay extends OverlayRef {
|
|
291
|
+
constructor(
|
|
292
|
+
el: HTMLElement,
|
|
293
|
+
doc: Document,
|
|
294
|
+
private readonly opts: OverlayOptions,
|
|
295
|
+
) {
|
|
296
|
+
super(el, doc, opts);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
override place() {
|
|
300
|
+
const { el } = this;
|
|
301
|
+
const anchor = this.opts.anchor();
|
|
302
|
+
if (!anchor?.isConnected || !el.hasAttribute('data-open')) return;
|
|
303
|
+
const r = anchor.getBoundingClientRect();
|
|
304
|
+
const root = el.ownerDocument.documentElement;
|
|
305
|
+
el.style.inlineSize = `${r.width}px`;
|
|
306
|
+
el.style.setProperty('--_room', '100vh');
|
|
307
|
+
const height = el.offsetHeight;
|
|
308
|
+
const rtl = el.ownerDocument.defaultView?.getComputedStyle(anchor).direction === 'rtl';
|
|
309
|
+
const view = { width: root.clientWidth, height: root.clientHeight };
|
|
310
|
+
const spot = placeOverlay(r, { width: r.width, height: Math.min(height, MIN_ROOM) }, view, 'bottom', 'start', OFFSET, rtl);
|
|
311
|
+
const shown = Math.min(height, spot.maxHeight);
|
|
312
|
+
el.style.setProperty('--_room', `${spot.maxHeight}px`);
|
|
313
|
+
Object.assign(el.style, {
|
|
314
|
+
left: `${spot.x}px`,
|
|
315
|
+
top: `${spot.side === 'top' ? r.top - OFFSET - shown : spot.y}px`,
|
|
316
|
+
transformOrigin: spot.origin,
|
|
317
|
+
});
|
|
318
|
+
el.dataset['side'] = spot.side;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The list panel of a `<ui-select>`, in the top layer while open. Used by `UiSelect` only. */
|
|
323
|
+
@Component({
|
|
324
|
+
selector: 'ui-select-list',
|
|
325
|
+
template: `<span #glow class="glow" aria-hidden="true"></span><ng-content />`,
|
|
326
|
+
styles: `
|
|
327
|
+
:host {
|
|
328
|
+
--_p: calc(var(--ui-space-1) + 1px * var(--_t));
|
|
329
|
+
--_r: calc(var(--ui-radius-sm) + 3px * var(--_t));
|
|
330
|
+
--_y: 0;
|
|
331
|
+
position: fixed;
|
|
332
|
+
inset: auto;
|
|
333
|
+
z-index: var(--ui-z-popover);
|
|
334
|
+
box-sizing: border-box;
|
|
335
|
+
max-block-size: min(var(--ui-select-panel-max-height, 288px), var(--_room, 288px));
|
|
336
|
+
margin: 0;
|
|
337
|
+
padding: var(--_p);
|
|
338
|
+
overflow-y: auto;
|
|
339
|
+
overscroll-behavior: contain;
|
|
340
|
+
scrollbar-width: thin;
|
|
341
|
+
border: 0;
|
|
342
|
+
border-radius: calc(var(--_r) + var(--_p));
|
|
343
|
+
background: var(--ui-bg-overlay);
|
|
344
|
+
box-shadow: var(--ui-shadow-md);
|
|
345
|
+
color: var(--ui-text);
|
|
346
|
+
text-align: start;
|
|
347
|
+
transition: var(--ui-duration-3) var(--ui-ease-out);
|
|
348
|
+
transition-property: opacity, scale, translate;
|
|
349
|
+
}
|
|
350
|
+
@container style(--ui-look-tray: 1) { :host { box-shadow: var(--ui-shadow-lg); } }
|
|
351
|
+
:host(:not([data-open])) { display: none !important; }
|
|
352
|
+
:host([data-side='top']) { --_y: var(--ui-space-1); }
|
|
353
|
+
:host([data-side='bottom']) { --_y: calc(var(--ui-space-1) * -1); }
|
|
354
|
+
:host([data-enter]) { opacity: 0; scale: 0.98; translate: 0 var(--_y); transition: none; }
|
|
355
|
+
.glow {
|
|
356
|
+
position: absolute;
|
|
357
|
+
inset-inline: var(--_p);
|
|
358
|
+
top: 0;
|
|
359
|
+
border-radius: var(--_r);
|
|
360
|
+
background: color-mix(in srgb, var(--ui-text) 6%, var(--ui-bg-overlay));
|
|
361
|
+
pointer-events: none;
|
|
362
|
+
transition: calc(var(--ui-duration-3) * 0.9) var(--ui-ease-out);
|
|
363
|
+
transition-property: translate, block-size;
|
|
364
|
+
}
|
|
365
|
+
@media (forced-colors: active) {
|
|
366
|
+
:host { border: 1px solid CanvasText; }
|
|
367
|
+
.glow { forced-color-adjust: none; background: Highlight; }
|
|
368
|
+
}
|
|
369
|
+
`,
|
|
370
|
+
})
|
|
371
|
+
export class UiSelectList {
|
|
372
|
+
readonly glow = viewChild.required<ElementRef<HTMLElement>>('glow');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
@Component({
|
|
376
|
+
selector: 'ui-select',
|
|
377
|
+
imports: [UiSelectList],
|
|
378
|
+
host: {
|
|
379
|
+
'[attr.data-size]': 'size()',
|
|
380
|
+
'[attr.data-open]': 'open() || null',
|
|
381
|
+
'[attr.data-invalid]': 'invalid() || null',
|
|
382
|
+
'[attr.data-disabled]': 'trigger()?.disabled() || null',
|
|
383
|
+
'[attr.data-empty]': '!selected() || null',
|
|
384
|
+
'[attr.data-icon]': '!!selected()?.icon() || null',
|
|
385
|
+
'[style]': 'sizes[size()]',
|
|
386
|
+
},
|
|
387
|
+
template: `
|
|
388
|
+
@if (label()) {
|
|
389
|
+
<div class="top">
|
|
390
|
+
<label [id]="labelId" [for]="trigger()?.id()" (click)="focusTrigger($event)">{{ label() }}@if (optional()) {<span class="ui-sr"> ({{ t().optional }})</span>}</label>
|
|
391
|
+
@if (optional()) {<span class="opt" aria-hidden="true">{{ t().optional }}</span>}
|
|
392
|
+
</div>
|
|
393
|
+
}
|
|
394
|
+
<div #box class="box" [attr.title]="clipped() ? display() : null" [attr.data-nudge]="nudge() || null" (animationend)="$event.target === $event.currentTarget && nudge.set(false)" (animationcancel)="$event.target === $event.currentTarget && nudge.set(false)">
|
|
395
|
+
<ng-content select="button" />
|
|
396
|
+
<span #icon class="icon" aria-hidden="true"></span>
|
|
397
|
+
<svg class="chev ui-icon" viewBox="0 0 16 16" aria-hidden="true"><path d="M4.5 6.25 8 9.75l3.5-3.5" /></svg>
|
|
398
|
+
</div>
|
|
399
|
+
<ui-select-list #panel popover="manual" role="listbox" [id]="listId" [attr.aria-labelledby]="label() ? labelId : null"
|
|
400
|
+
[attr.aria-label]="label() ? null : trigger()?.el?.getAttribute('aria-label')" [attr.data-kbd]="kbd() || null" (pointerdown)="$event.preventDefault()">
|
|
401
|
+
<ng-content select="ui-option" />
|
|
402
|
+
</ui-select-list>
|
|
403
|
+
<div class="foot" [class.ui-sr]="!message()">
|
|
404
|
+
<p #msg [id]="messageId" dir="auto" aria-live="polite">{{ message() }}</p>
|
|
405
|
+
</div>
|
|
406
|
+
@if (trigger()?.name(); as name) {<input type="hidden" [name]="name" [value]="submitted()" />}
|
|
407
|
+
`,
|
|
408
|
+
styles: `
|
|
409
|
+
:host {
|
|
410
|
+
--_t: var(--ui-look-tray, 0);
|
|
411
|
+
--_h: calc(var(--_h0) + var(--ui-space-1) * var(--_t));
|
|
412
|
+
--_e: var(--ui-text-field-edge, var(--ui-hairline));
|
|
413
|
+
--_w: calc(0.5px + 0.5px * var(--_t));
|
|
414
|
+
--_fill: var(--ui-bg-surface);
|
|
415
|
+
--_s: calc(var(--ui-space-2-5) + 2px * var(--_t) - 1px);
|
|
416
|
+
--_x: calc(var(--ui-space-2) + 2px * var(--_t) - 1px);
|
|
417
|
+
--_ic: var(--ui-icon);
|
|
418
|
+
--_ps: var(--_s);
|
|
419
|
+
--_pe: calc(var(--_x) + var(--ui-icon-md) + var(--ui-space-2));
|
|
420
|
+
--_fg: var(--ui-text);
|
|
421
|
+
display: flex;
|
|
422
|
+
flex-direction: column;
|
|
423
|
+
gap: calc(var(--ui-space-1-5) + 1px * var(--_t));
|
|
424
|
+
min-inline-size: 0;
|
|
425
|
+
color: var(--ui-text);
|
|
426
|
+
font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans);
|
|
427
|
+
}
|
|
428
|
+
:host([data-icon]) { --_ps: calc(var(--_s) + var(--ui-icon-md) + var(--ui-space-2)); }
|
|
429
|
+
:host([data-empty]) { --_fg: var(--ui-text-placeholder); }
|
|
430
|
+
.top { display: flex; align-items: baseline; gap: var(--ui-space-1-5); padding-inline: calc(2px * var(--_t)); }
|
|
431
|
+
label { font-weight: var(--ui-weight-medium); }
|
|
432
|
+
.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)); }
|
|
433
|
+
.box {
|
|
434
|
+
position: relative;
|
|
435
|
+
box-sizing: border-box;
|
|
436
|
+
block-size: var(--_h);
|
|
437
|
+
border: 1px solid transparent;
|
|
438
|
+
border-radius: var(--ui-text-field-radius, calc(var(--ui-radius-sm) + 1px + 3px * var(--_t)));
|
|
439
|
+
background: var(--_fill);
|
|
440
|
+
box-shadow: 0 0 0 var(--_w) var(--_e), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) 6%, transparent);
|
|
441
|
+
font-size: calc(var(--ui-text-base) + 0.5px * var(--_t));
|
|
442
|
+
cursor: pointer;
|
|
443
|
+
transition: var(--ui-duration-2) var(--ui-ease-out);
|
|
444
|
+
transition-property: box-shadow, background;
|
|
445
|
+
}
|
|
446
|
+
@media (hover: hover) { .box:hover { --_e: var(--ui-text-field-edge-hover, color-mix(in srgb, var(--ui-text) 18%, transparent)); } }
|
|
447
|
+
.box:has(:focus-visible) { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
|
|
448
|
+
:host([data-open]) .box { --_w: 1px; --_e: var(--ui-text-field-edge-focus, var(--ui-border-input)); }
|
|
449
|
+
:host([data-invalid]) .box {
|
|
450
|
+
--_w: 1px;
|
|
451
|
+
--_e: color-mix(in srgb, var(--ui-danger) 55%, transparent);
|
|
452
|
+
--_fill: color-mix(in srgb, var(--ui-danger-soft) calc(50% * var(--_t)), var(--ui-bg-surface));
|
|
453
|
+
}
|
|
454
|
+
:host([data-invalid][data-open]) .box { --_e: var(--ui-danger); }
|
|
455
|
+
:host([data-disabled]) { opacity: var(--ui-disabled-opacity); }
|
|
456
|
+
:host([data-disabled]) .box { cursor: not-allowed; --_e: var(--ui-hairline); }
|
|
457
|
+
.box[data-nudge] { animation: ui-nudge calc(var(--ui-duration-4) * 1.07) var(--ui-ease-out); }
|
|
458
|
+
.icon, .chev { position: absolute; inset-block: 0; margin-block: auto; color: var(--ui-icon); pointer-events: none; }
|
|
459
|
+
.icon { display: flex; align-items: center; inset-inline-start: var(--_s); }
|
|
460
|
+
.chev { inset-inline-end: calc(var(--_x) + 1px); inline-size: var(--ui-icon-sm); block-size: var(--ui-icon-sm); transition: rotate calc(var(--ui-duration-4) * 0.8) var(--ui-ease-out); }
|
|
461
|
+
:host([data-open]) .chev { rotate: 180deg; }
|
|
462
|
+
.foot { padding-inline: calc(2px * var(--_t)); }
|
|
463
|
+
p { margin: 0; }
|
|
464
|
+
:host([data-invalid]) p { color: var(--ui-danger); }
|
|
465
|
+
@media (pointer: coarse) {
|
|
466
|
+
:host { --_h: calc(var(--_ht) + var(--ui-space-1) * var(--_t)); }
|
|
467
|
+
.box { font-size: var(--ui-text-field-touch); }
|
|
468
|
+
}
|
|
469
|
+
@media (forced-colors: active) {
|
|
470
|
+
.box { border-color: CanvasText; }
|
|
471
|
+
:host([data-invalid]) .box { outline: 1px solid CanvasText; }
|
|
472
|
+
:host([data-open]) .box { outline: 2px solid Highlight; }
|
|
473
|
+
}
|
|
474
|
+
`,
|
|
475
|
+
})
|
|
476
|
+
export class UiSelect {
|
|
477
|
+
/** Shown above the field, and the name of the field and of its list. Clicking it focuses the field. */
|
|
478
|
+
readonly label = input<string>();
|
|
479
|
+
/** A quiet line under the field. The error takes its place while there is one. */
|
|
480
|
+
readonly hint = input<string>();
|
|
481
|
+
/** Marks the field invalid and shows this message where the hint was. */
|
|
482
|
+
readonly error = input<string | null | undefined>();
|
|
483
|
+
/** 28, 32 or 36px tall; Classic 32, 36 or 40px. Taller, with 16px text, on touch screens. */
|
|
484
|
+
readonly size = input<SelectSize>('md');
|
|
485
|
+
/** Adds "Optional" after the label. */
|
|
486
|
+
readonly optional = input(false, { transform: booleanAttribute });
|
|
487
|
+
/** Words to translate: the "Optional" tag and the default placeholder. */
|
|
488
|
+
readonly labels = input<Partial<SelectLabels>>({});
|
|
489
|
+
|
|
490
|
+
readonly trigger = contentChild(UiSelectTrigger);
|
|
491
|
+
readonly options = contentChildren(UiOption);
|
|
492
|
+
protected readonly sizes = SIZES;
|
|
493
|
+
protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
494
|
+
private readonly uid = nextId++;
|
|
495
|
+
readonly listId = `ui-select-list-${this.uid}`;
|
|
496
|
+
protected readonly labelId = `ui-select-label-${this.uid}`;
|
|
497
|
+
protected readonly messageId = `ui-select-msg-${this.uid}`;
|
|
498
|
+
|
|
499
|
+
readonly invalid = computed(() => !!this.error());
|
|
500
|
+
protected readonly message = computed(() => this.error() || this.hint() || '');
|
|
501
|
+
readonly describers = computed(() => (this.message() ? [this.messageId] : []));
|
|
502
|
+
readonly open = computed(() => !!this.trigger()?.open());
|
|
503
|
+
readonly selected: Signal<UiOption | undefined> = computed(() => {
|
|
504
|
+
const t = this.trigger();
|
|
505
|
+
const v = t?.value() ?? null;
|
|
506
|
+
return t ? this.options().find((o) => t.matches(o.value(), v)) : undefined;
|
|
507
|
+
});
|
|
508
|
+
readonly display = computed(() => this.selected()?.text() || this.trigger()?.placeholder() || this.t().placeholder);
|
|
509
|
+
protected readonly submitted = computed(() => String(this.trigger()?.value() ?? ''));
|
|
510
|
+
private readonly active = signal(-1);
|
|
511
|
+
readonly activeOption = computed(() => this.options()[this.active()]);
|
|
512
|
+
protected readonly kbd = signal(false);
|
|
513
|
+
protected readonly clipped = signal(false);
|
|
514
|
+
protected readonly nudge = signal(false);
|
|
515
|
+
|
|
516
|
+
private readonly box = viewChild.required<ElementRef<HTMLElement>>('box');
|
|
517
|
+
private readonly icon = viewChild.required<ElementRef<HTMLElement>>('icon');
|
|
518
|
+
private readonly panel = viewChild.required('panel', { read: ElementRef<HTMLElement> });
|
|
519
|
+
private readonly list = viewChild.required(UiSelectList);
|
|
520
|
+
private readonly msg = viewChild<ElementRef<HTMLElement>>('msg');
|
|
521
|
+
private readonly doc = inject(DOCUMENT);
|
|
522
|
+
private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
523
|
+
private readonly browser = isPlatformBrowser(inject(PLATFORM_ID));
|
|
524
|
+
private overlay?: SelectOverlay;
|
|
525
|
+
private fresh = false;
|
|
526
|
+
private primed = false;
|
|
527
|
+
private buffer = '';
|
|
528
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
529
|
+
private pointer = '';
|
|
530
|
+
private warned = false;
|
|
531
|
+
|
|
532
|
+
constructor() {
|
|
533
|
+
const destroy = inject(DestroyRef);
|
|
534
|
+
let was: boolean | undefined;
|
|
535
|
+
effect(() => {
|
|
536
|
+
const now = this.invalid();
|
|
537
|
+
if (now && was === false) this.nudge.set(true);
|
|
538
|
+
was = now;
|
|
539
|
+
});
|
|
540
|
+
effect(() => {
|
|
541
|
+
if (this.trigger()?.disabled()) untracked(() => this.close());
|
|
542
|
+
});
|
|
543
|
+
let shown: string | undefined;
|
|
544
|
+
afterRenderEffect(() => {
|
|
545
|
+
const text = this.message();
|
|
546
|
+
const p = this.msg()?.nativeElement;
|
|
547
|
+
const motion = this.motion();
|
|
548
|
+
if (shown !== undefined && text !== shown && p?.animate && motion) {
|
|
549
|
+
p.animate([{ opacity: 0, filter: 'blur(2px)', transform: 'translateY(-2px)' }, {}], { duration: 260 * motion, easing: 'ease-out' });
|
|
550
|
+
}
|
|
551
|
+
shown = text;
|
|
552
|
+
});
|
|
553
|
+
afterRenderEffect(() => {
|
|
554
|
+
const on = this.open();
|
|
555
|
+
untracked(() => this.sync(on));
|
|
556
|
+
});
|
|
557
|
+
afterRenderEffect(() => {
|
|
558
|
+
const option = this.activeOption();
|
|
559
|
+
if (this.open()) untracked(() => this.glide(option));
|
|
560
|
+
});
|
|
561
|
+
afterRenderEffect(() => {
|
|
562
|
+
const icon = this.selected()?.icon()?.el.cloneNode(true) as Element | undefined;
|
|
563
|
+
icon?.removeAttribute('id');
|
|
564
|
+
this.icon().nativeElement.replaceChildren(...(icon ? [icon] : []));
|
|
565
|
+
});
|
|
566
|
+
afterRenderEffect(() => {
|
|
567
|
+
this.display();
|
|
568
|
+
untracked(() => this.measure());
|
|
569
|
+
});
|
|
570
|
+
afterRenderEffect(() => {
|
|
571
|
+
const t = this.trigger();
|
|
572
|
+
const v = t?.value() ?? null;
|
|
573
|
+
if (isDevMode() && !this.warned && v !== null && this.options().length && !this.selected()) {
|
|
574
|
+
this.warned = true;
|
|
575
|
+
console.warn('ui-select: the value matches none of the options, so the placeholder shows.', v);
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
afterNextRender(() => {
|
|
579
|
+
const el = this.trigger()?.el;
|
|
580
|
+
if (isDevMode() && el && !this.label() && !el.hasAttribute('aria-label') && !el.hasAttribute('aria-labelledby')) {
|
|
581
|
+
console.warn('ui-select: give it a label, or an aria-label on the button.', el);
|
|
582
|
+
}
|
|
583
|
+
const view = this.doc.defaultView;
|
|
584
|
+
if (!el || !view?.ResizeObserver) return;
|
|
585
|
+
const watch = new view.ResizeObserver(() => this.measure());
|
|
586
|
+
watch.observe(el);
|
|
587
|
+
destroy.onDestroy(() => watch.disconnect());
|
|
588
|
+
});
|
|
589
|
+
destroy.onDestroy(() => {
|
|
590
|
+
clearTimeout(this.timer);
|
|
591
|
+
this.overlay?.hide(false);
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
toggle() {
|
|
596
|
+
if (this.open()) this.close();
|
|
597
|
+
else this.show(false);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** Opens the list with the chosen option active. `keyboard` shows the ring on the active option. */
|
|
601
|
+
show(keyboard: boolean) {
|
|
602
|
+
const t = this.trigger();
|
|
603
|
+
if (!t || t.disabled()) return;
|
|
604
|
+
this.kbd.set(keyboard);
|
|
605
|
+
if (!t.open()) this.prime();
|
|
606
|
+
t.open.set(true);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
close() {
|
|
610
|
+
this.trigger()?.open.set(false);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
pick(option: UiOption) {
|
|
614
|
+
if (option.disabled()) return;
|
|
615
|
+
this.trigger()?.choose(option.value());
|
|
616
|
+
this.close();
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
hover(option: UiOption, event: PointerEvent) {
|
|
620
|
+
const at = `${event.clientX},${event.clientY}`;
|
|
621
|
+
if (option.disabled() || at === this.pointer) return;
|
|
622
|
+
this.pointer = at;
|
|
623
|
+
this.kbd.set(false);
|
|
624
|
+
this.active.set(this.options().indexOf(option));
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
key(e: KeyboardEvent) {
|
|
628
|
+
const t = this.trigger();
|
|
629
|
+
if (!t || t.disabled() || e.isComposing) return;
|
|
630
|
+
const open = t.open();
|
|
631
|
+
const k = e.key;
|
|
632
|
+
const go = (fn: () => unknown) => {
|
|
633
|
+
e.preventDefault();
|
|
634
|
+
fn();
|
|
635
|
+
};
|
|
636
|
+
if (k.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && (k !== ' ' || this.buffer)) return go(() => this.type(k));
|
|
637
|
+
switch (k) {
|
|
638
|
+
case 'ArrowDown':
|
|
639
|
+
case 'ArrowUp':
|
|
640
|
+
if (!open) return go(() => this.show(true));
|
|
641
|
+
if (e.altKey && k === 'ArrowUp') return go(() => this.choose());
|
|
642
|
+
return go(() => this.move(this.next(this.active(), k === 'ArrowDown' ? 1 : -1)));
|
|
643
|
+
case 'Home':
|
|
644
|
+
case 'PageUp':
|
|
645
|
+
if (open) go(() => this.move(this.next(-1, 1)));
|
|
646
|
+
return;
|
|
647
|
+
case 'End':
|
|
648
|
+
case 'PageDown':
|
|
649
|
+
if (open) go(() => this.move(this.next(this.options().length, -1)));
|
|
650
|
+
return;
|
|
651
|
+
case 'Enter':
|
|
652
|
+
case ' ':
|
|
653
|
+
return go(() => (open ? this.choose() : this.show(true)));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
protected focusTrigger(event: MouseEvent) {
|
|
658
|
+
event.preventDefault();
|
|
659
|
+
this.trigger()?.el.focus();
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** The nearest enabled option from `from` in `dir`, or `from` itself when there is none. */
|
|
663
|
+
private next(from: number, dir: 1 | -1) {
|
|
664
|
+
const list = this.options();
|
|
665
|
+
for (let i = from + dir; i >= 0 && i < list.length; i += dir) if (!list[i].disabled()) return i;
|
|
666
|
+
return from >= 0 && from < list.length ? from : -1;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/** Makes the chosen option active, else the first enabled one. */
|
|
670
|
+
private prime() {
|
|
671
|
+
const selected = this.selected();
|
|
672
|
+
this.active.set(selected && !selected.disabled() ? this.options().indexOf(selected) : this.next(-1, 1));
|
|
673
|
+
this.primed = true;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
private move(i: number) {
|
|
677
|
+
this.kbd.set(true);
|
|
678
|
+
this.active.set(i);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
private choose() {
|
|
682
|
+
const option = this.activeOption();
|
|
683
|
+
if (option) this.pick(option);
|
|
684
|
+
else this.close();
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** Type-ahead: letters typed within 650ms join; one letter again cycles through the matches. Closed, it picks. */
|
|
688
|
+
private type(char: string) {
|
|
689
|
+
clearTimeout(this.timer);
|
|
690
|
+
this.buffer += char.toLowerCase();
|
|
691
|
+
this.timer = setTimeout(() => (this.buffer = ''), TYPEAHEAD_MS);
|
|
692
|
+
const list = this.options();
|
|
693
|
+
const open = this.open();
|
|
694
|
+
const from = open ? this.active() : list.indexOf(this.selected()!);
|
|
695
|
+
const all = this.buffer;
|
|
696
|
+
const same = [...all].every((c) => c === all[0]);
|
|
697
|
+
const find = (needle: string, start: number) => {
|
|
698
|
+
for (let i = 0; i < list.length; i++) {
|
|
699
|
+
const o = list[(start + i + list.length) % list.length];
|
|
700
|
+
if (!o.disabled() && o.text().toLowerCase().startsWith(needle)) return o;
|
|
701
|
+
}
|
|
702
|
+
return undefined;
|
|
703
|
+
};
|
|
704
|
+
const hit = all.length > 1 ? (find(all, Math.max(from, 0)) ?? (same ? find(all[0], from + 1) : undefined)) : find(all, from + 1);
|
|
705
|
+
if (!hit) return;
|
|
706
|
+
if (open) this.move(list.indexOf(hit));
|
|
707
|
+
else this.trigger()?.choose(hit.value());
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
private sync(on: boolean) {
|
|
711
|
+
if (!this.browser) return;
|
|
712
|
+
this.overlay ??= new SelectOverlay(this.panel().nativeElement, this.doc, {
|
|
713
|
+
anchor: () => this.box().nativeElement,
|
|
714
|
+
trigger: () => this.trigger()?.el ?? null,
|
|
715
|
+
side: () => 'bottom',
|
|
716
|
+
align: () => 'start',
|
|
717
|
+
offset: () => OFFSET,
|
|
718
|
+
dismiss: () => this.close(),
|
|
719
|
+
});
|
|
720
|
+
if (!on) {
|
|
721
|
+
this.overlay.hide(false);
|
|
722
|
+
this.kbd.set(false);
|
|
723
|
+
this.primed = false;
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
if (!this.primed) this.prime();
|
|
727
|
+
this.primed = false;
|
|
728
|
+
this.fresh = true;
|
|
729
|
+
this.overlay.show(false);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/** Slides the soft highlight to the active row, and keeps that row in view. Placed at once on open. */
|
|
733
|
+
private glide(option?: UiOption) {
|
|
734
|
+
const glow = this.list().glow().nativeElement;
|
|
735
|
+
const panel = this.panel().nativeElement;
|
|
736
|
+
const fresh = this.fresh;
|
|
737
|
+
this.fresh = false;
|
|
738
|
+
glow.style.opacity = option ? '' : '0';
|
|
739
|
+
if (!option) return;
|
|
740
|
+
const row = option.el;
|
|
741
|
+
if (fresh) glow.style.transition = 'none';
|
|
742
|
+
glow.style.translate = `0 ${row.offsetTop}px`;
|
|
743
|
+
glow.style.blockSize = `${row.offsetHeight}px`;
|
|
744
|
+
if (fresh) {
|
|
745
|
+
void glow.offsetWidth;
|
|
746
|
+
glow.style.transition = '';
|
|
747
|
+
}
|
|
748
|
+
if (!fresh && !this.kbd()) return;
|
|
749
|
+
const pad = parseFloat(this.doc.defaultView?.getComputedStyle(panel).paddingTop ?? '') || 0;
|
|
750
|
+
const top = row.offsetTop - pad;
|
|
751
|
+
const bottom = row.offsetTop + row.offsetHeight + pad;
|
|
752
|
+
if (top < panel.scrollTop) panel.scrollTop = top;
|
|
753
|
+
else if (bottom > panel.scrollTop + panel.clientHeight) panel.scrollTop = bottom - panel.clientHeight;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/** A `title` with the whole choice, only while it is cut off. */
|
|
757
|
+
private measure() {
|
|
758
|
+
const el = this.trigger()?.el;
|
|
759
|
+
this.clipped.set(!!el && el.scrollWidth > el.clientWidth);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** 0 under reduced motion, otherwise the app's motion scale (1 by default). */
|
|
763
|
+
private motion() {
|
|
764
|
+
const view = this.doc.defaultView;
|
|
765
|
+
if (!view?.matchMedia || view.matchMedia('(prefers-reduced-motion: reduce)').matches) return 0;
|
|
766
|
+
const scale = parseFloat(view.getComputedStyle(this.host).getPropertyValue('--ui-motion-scale'));
|
|
767
|
+
return Number.isNaN(scale) ? 1 : scale;
|
|
768
|
+
}
|
|
769
|
+
}
|