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.
Files changed (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -2
  3. package/cli/ochreui.mjs +601 -0
  4. package/mcp/core.mjs +126 -0
  5. package/mcp/docs.json +906 -0
  6. package/mcp/server.mjs +23 -0
  7. package/package.json +393 -3
  8. package/registry/agent-team.ts +350 -0
  9. package/registry/avatar.ts +300 -0
  10. package/registry/badge.ts +207 -0
  11. package/registry/breadcrumb.ts +435 -0
  12. package/registry/button.ts +238 -0
  13. package/registry/card.ts +221 -0
  14. package/registry/change-summary.ts +200 -0
  15. package/registry/checkbox.ts +204 -0
  16. package/registry/code-block.ts +332 -0
  17. package/registry/color-picker.ts +720 -0
  18. package/registry/comment-thread.ts +503 -0
  19. package/registry/context-menu.ts +281 -0
  20. package/registry/cover-flow.ts +623 -0
  21. package/registry/date-picker.ts +799 -0
  22. package/registry/decision-inbox.ts +340 -0
  23. package/registry/dialog.ts +577 -0
  24. package/registry/dropdown-menu.ts +864 -0
  25. package/registry/email-draft.ts +916 -0
  26. package/registry/empty-calendar.ts +199 -0
  27. package/registry/empty-state.ts +164 -0
  28. package/registry/enrichment-orbit.ts +164 -0
  29. package/registry/filter-menu.ts +541 -0
  30. package/registry/form-field.ts +308 -0
  31. package/registry/frost-cloud.ts +381 -0
  32. package/registry/ghosted-chat.ts +210 -0
  33. package/registry/glow-cloud.ts +402 -0
  34. package/registry/grid-background.ts +164 -0
  35. package/registry/hover-card.ts +401 -0
  36. package/registry/icon-button.ts +133 -0
  37. package/registry/index.json +2124 -0
  38. package/registry/integration-beam.ts +588 -0
  39. package/registry/integration-grid.ts +209 -0
  40. package/registry/kbd.ts +176 -0
  41. package/registry/layer-stack.ts +351 -0
  42. package/registry/lead-sourcing.ts +285 -0
  43. package/registry/logo-cluster.ts +239 -0
  44. package/registry/logo-marquee.ts +505 -0
  45. package/registry/message-actions.ts +833 -0
  46. package/registry/model-orbit.ts +583 -0
  47. package/registry/model-wheel.ts +372 -0
  48. package/registry/notification-stack.ts +301 -0
  49. package/registry/number-input.ts +552 -0
  50. package/registry/number-ticker.ts +261 -0
  51. package/registry/ochre.css +917 -0
  52. package/registry/otp-input.ts +704 -0
  53. package/registry/overlay.ts +312 -0
  54. package/registry/pagination.ts +362 -0
  55. package/registry/popover.ts +296 -0
  56. package/registry/profile-menu.ts +332 -0
  57. package/registry/progress-tracker.ts +291 -0
  58. package/registry/rating.ts +461 -0
  59. package/registry/reaction-bar.ts +322 -0
  60. package/registry/region-map.ts +537 -0
  61. package/registry/resizable.ts +489 -0
  62. package/registry/scroll-area.ts +271 -0
  63. package/registry/segmented-control.ts +433 -0
  64. package/registry/select.ts +769 -0
  65. package/registry/separator.ts +97 -0
  66. package/registry/shimmer-loader.ts +211 -0
  67. package/registry/side-folder.ts +152 -0
  68. package/registry/skeleton.ts +136 -0
  69. package/registry/skill/INSTRUCTIONS.md +11 -0
  70. package/registry/skill/SKILL.md +65 -0
  71. package/registry/skill/checklist.md +45 -0
  72. package/registry/skill/references/agent-team.md +105 -0
  73. package/registry/skill/references/avatar.md +88 -0
  74. package/registry/skill/references/badge.md +92 -0
  75. package/registry/skill/references/breadcrumb.md +99 -0
  76. package/registry/skill/references/button.md +82 -0
  77. package/registry/skill/references/card.md +111 -0
  78. package/registry/skill/references/catalog.md +114 -0
  79. package/registry/skill/references/change-summary.md +103 -0
  80. package/registry/skill/references/checkbox.md +105 -0
  81. package/registry/skill/references/code-block.md +109 -0
  82. package/registry/skill/references/color-picker.md +138 -0
  83. package/registry/skill/references/comment-thread.md +164 -0
  84. package/registry/skill/references/context-menu.md +205 -0
  85. package/registry/skill/references/cover-flow.md +128 -0
  86. package/registry/skill/references/date-picker.md +154 -0
  87. package/registry/skill/references/decision-inbox.md +126 -0
  88. package/registry/skill/references/dialog.md +155 -0
  89. package/registry/skill/references/dropdown-menu.md +144 -0
  90. package/registry/skill/references/email-draft.md +182 -0
  91. package/registry/skill/references/empty-calendar.md +74 -0
  92. package/registry/skill/references/empty-state.md +118 -0
  93. package/registry/skill/references/enrichment-orbit.md +71 -0
  94. package/registry/skill/references/filter-menu.md +143 -0
  95. package/registry/skill/references/form-field.md +118 -0
  96. package/registry/skill/references/frost-cloud.md +80 -0
  97. package/registry/skill/references/ghosted-chat.md +75 -0
  98. package/registry/skill/references/glow-cloud.md +84 -0
  99. package/registry/skill/references/grid-background.md +76 -0
  100. package/registry/skill/references/hover-card.md +125 -0
  101. package/registry/skill/references/icon-button.md +99 -0
  102. package/registry/skill/references/integration-beam.md +107 -0
  103. package/registry/skill/references/integration-grid.md +100 -0
  104. package/registry/skill/references/kbd.md +65 -0
  105. package/registry/skill/references/layer-stack.md +115 -0
  106. package/registry/skill/references/lead-sourcing.md +92 -0
  107. package/registry/skill/references/logo-cluster.md +92 -0
  108. package/registry/skill/references/logo-marquee.md +123 -0
  109. package/registry/skill/references/message-actions.md +146 -0
  110. package/registry/skill/references/model-orbit.md +106 -0
  111. package/registry/skill/references/model-wheel.md +84 -0
  112. package/registry/skill/references/notification-stack.md +138 -0
  113. package/registry/skill/references/number-input.md +117 -0
  114. package/registry/skill/references/number-ticker.md +73 -0
  115. package/registry/skill/references/otp-input.md +142 -0
  116. package/registry/skill/references/pagination.md +112 -0
  117. package/registry/skill/references/popover.md +143 -0
  118. package/registry/skill/references/profile-menu.md +123 -0
  119. package/registry/skill/references/progress-tracker.md +122 -0
  120. package/registry/skill/references/rating.md +111 -0
  121. package/registry/skill/references/reaction-bar.md +128 -0
  122. package/registry/skill/references/region-map.md +109 -0
  123. package/registry/skill/references/resizable.md +127 -0
  124. package/registry/skill/references/scroll-area.md +113 -0
  125. package/registry/skill/references/segmented-control.md +96 -0
  126. package/registry/skill/references/select.md +122 -0
  127. package/registry/skill/references/separator.md +69 -0
  128. package/registry/skill/references/shimmer-loader.md +83 -0
  129. package/registry/skill/references/side-folder.md +75 -0
  130. package/registry/skill/references/skeleton.md +95 -0
  131. package/registry/skill/references/smart-recommendation.md +132 -0
  132. package/registry/skill/references/spinner.md +84 -0
  133. package/registry/skill/references/switch.md +79 -0
  134. package/registry/skill/references/tab-overload.md +70 -0
  135. package/registry/skill/references/tag-input.md +126 -0
  136. package/registry/skill/references/team-invite.md +94 -0
  137. package/registry/skill/references/testimonial-wall.md +110 -0
  138. package/registry/skill/references/text-area.md +115 -0
  139. package/registry/skill/references/text-field.md +126 -0
  140. package/registry/skill/references/text-reveal.md +71 -0
  141. package/registry/skill/references/thinking-loader.md +87 -0
  142. package/registry/skill/references/time-picker.md +122 -0
  143. package/registry/skill/references/tips-panel.md +115 -0
  144. package/registry/skill/references/toggle-group.md +98 -0
  145. package/registry/skill/references/tool-call-card.md +151 -0
  146. package/registry/skill/references/tooltip.md +94 -0
  147. package/registry/skill/references/tree-view.md +172 -0
  148. package/registry/skill/theming.md +57 -0
  149. package/registry/smart-recommendation.ts +415 -0
  150. package/registry/spinner.ts +67 -0
  151. package/registry/switch.ts +146 -0
  152. package/registry/tab-overload.ts +211 -0
  153. package/registry/tag-input.ts +620 -0
  154. package/registry/team-invite.ts +248 -0
  155. package/registry/testimonial-wall.ts +521 -0
  156. package/registry/text-area.ts +432 -0
  157. package/registry/text-field.ts +415 -0
  158. package/registry/text-reveal.ts +175 -0
  159. package/registry/theme.ts +68 -0
  160. package/registry/thinking-loader.ts +302 -0
  161. package/registry/time-picker.ts +828 -0
  162. package/registry/tips-panel.ts +312 -0
  163. package/registry/toggle-group.ts +357 -0
  164. package/registry/tool-call-card.ts +578 -0
  165. package/registry/tooltip.ts +486 -0
  166. package/registry/tree-view.ts +454 -0
  167. package/styles/base.css +500 -0
  168. package/styles/tokens.css +415 -0
@@ -0,0 +1,433 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Segmented Control, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (the --ui-* tokens, .ui-icon and .ui-badge), which `npx ochreui init` adds once.
5
+ *
6
+ * <ui-segmented-control aria-label="Layout" [(value)]="layout">
7
+ * <button uiSegment value="list">List</button>
8
+ * <button uiSegment value="board">Board <span uiSegmentCount>3</span></button>
9
+ * </ui-segmented-control>
10
+ * Two to six options, one picked, with a gliding indicator you can also drag.
11
+ * Works with [(value)], signal forms, reactive forms and ngModel.
12
+ *
13
+ * Public styling hooks (set them on the element or any ancestor):
14
+ * --ui-segmented-track tray track (default: ink at 6% over the page)
15
+ * --ui-segmented-pill tray pill (default: the surface; lighter than the track in dark mode)
16
+ * --ui-segmented-fill buttons indicator (default: ink at 8%)
17
+ * --ui-segmented-line underline indicator (default: ink)
18
+ */
19
+ import {
20
+ Component,
21
+ DOCUMENT,
22
+ DestroyRef,
23
+ Directive,
24
+ ElementRef,
25
+ OnInit,
26
+ afterNextRender,
27
+ afterRenderEffect,
28
+ booleanAttribute,
29
+ computed,
30
+ contentChildren,
31
+ forwardRef,
32
+ inject,
33
+ input,
34
+ isDevMode,
35
+ linkedSignal,
36
+ model,
37
+ signal,
38
+ viewChild,
39
+ } from '@angular/core';
40
+ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
41
+ import type { FormValueControl } from '@angular/forms/signals';
42
+
43
+ export type SegmentedVariant = 'tray' | 'buttons' | 'underline';
44
+ export type SegmentedSize = 'sm' | 'md' | 'lg';
45
+ export type SegmentedKind = 'radio' | 'tabs';
46
+
47
+ /** One option: `<button uiSegment value="board">Board</button>`. Native `disabled`, `aria-label`, `aria-controls` and `id` are yours. */
48
+ @Directive({
49
+ selector: 'button[uiSegment]',
50
+ host: {
51
+ type: 'button',
52
+ style:
53
+ 'position: relative; display: inline-flex; flex: none; align-items: center; justify-content: center; gap: var(--ui-space-1-5); box-sizing: border-box; block-size: max(var(--_oh), var(--_min)); margin: 0; padding: 0 var(--_px); border: 0; border-radius: var(--_rad); font: inherit; white-space: nowrap; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: color var(--ui-duration-1), background-color var(--ui-duration-1)',
54
+ '[attr.role]': 'group.kind() === "tabs" ? "tab" : "radio"',
55
+ '[attr.aria-checked]': 'group.kind() === "radio" ? active() : null',
56
+ '[attr.aria-selected]': 'group.kind() === "tabs" ? active() : null',
57
+ '[attr.tabindex]': 'group.stop() === this ? 0 : -1',
58
+ '[attr.data-active]': 'active() || null',
59
+ '[disabled]': 'off()',
60
+ },
61
+ })
62
+ export class UiSegment<T = string> implements OnInit {
63
+ readonly value = input.required<T>();
64
+ readonly disabled = input(false, { transform: booleanAttribute });
65
+ /** False until the inputs are in: `@for` sets each option's value after the previous option has rendered. */
66
+ readonly ready = signal(false);
67
+
68
+ readonly el = inject<ElementRef<HTMLButtonElement>>(ElementRef).nativeElement;
69
+ protected readonly group = inject<UiSegmentedControl<T>>(forwardRef(() => UiSegmentedControl));
70
+ readonly active = computed(() => Object.is(this.group.current(), this.value()));
71
+ readonly off = computed(() => this.disabled() || this.group.off());
72
+
73
+ ngOnInit() {
74
+ this.ready.set(true);
75
+ }
76
+ }
77
+
78
+ /** A small count after the label: `<span uiSegmentCount>3</span>`. It is part of the option's name. */
79
+ @Directive({
80
+ selector: '[uiSegmentCount]',
81
+ host: {
82
+ class: 'ui-seg-n',
83
+ style:
84
+ 'display: inline-grid; place-items: center; box-sizing: border-box; min-inline-size: calc(16px + 2px * var(--ui-look-tray, 0)); block-size: calc(16px + 2px * var(--ui-look-tray, 0)); padding: 0 calc(4px + 1px * var(--ui-look-tray, 0)); border-radius: calc(5px + 4px * var(--ui-look-tray, 0)); font-size: var(--ui-text-xs); line-height: 1; font-weight: calc(500 + 100 * var(--ui-look-tray, 0)); font-variant-numeric: tabular-nums',
85
+ },
86
+ })
87
+ export class UiSegmentCount {}
88
+
89
+ const space = (n: string) => `var(--ui-space-${n})`;
90
+ const SIZES: Record<SegmentedSize, Record<string, string>> = {
91
+ sm: { '--_z': space('6'), '--_zt': '2px', '--_sp': space('2'), '--_fs': 'var(--ui-text-sm)', '--_lh': 'var(--ui-leading-xs)', '--_ic': 'var(--ui-icon-sm)' },
92
+ md: { '--_z': `calc(${space('6')} + ${space('1')})`, '--_zt': '2px', '--_sp': space('2-5'), '--_fs': 'var(--ui-text-base)', '--_lh': 'var(--ui-leading-base)', '--_ic': 'var(--ui-icon-md)' },
93
+ lg: { '--_z': space('8'), '--_zt': '4px', '--_sp': space('3'), '--_fs': 'var(--ui-text-base)', '--_lh': 'var(--ui-leading-base)', '--_ic': 'var(--ui-icon-md)' },
94
+ };
95
+
96
+ @Component({
97
+ selector: 'ui-segmented-control',
98
+ providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UiSegmentedControl), multi: true }],
99
+ host: {
100
+ '[attr.role]': 'kind() === "tabs" ? "tablist" : "radiogroup"',
101
+ '[attr.aria-disabled]': 'off() || null',
102
+ '[attr.data-variant]': 'variant()',
103
+ '[attr.data-size]': 'size()',
104
+ '[attr.data-full]': 'fullWidth() || null',
105
+ '[attr.data-pressed]': 'pressed() || null',
106
+ '[style]': 'sizes[size()]',
107
+ '(keydown)': 'key($event)',
108
+ '(click)': 'clicked($event)',
109
+ '(pointerdown)': 'down($event)',
110
+ '(pointermove)': 'move($event)',
111
+ '(pointerup)': 'up()',
112
+ '(pointercancel)': 'end()',
113
+ '(focusout)': 'left($event)',
114
+ '(document:reset)': 'formReset($event)',
115
+ },
116
+ template: `
117
+ <div class="sc" #sc (scroll)="fade()" [style]="scStyle">
118
+ <div class="row" #row [style]="rowStyle">
119
+ <span class="ind" #ind aria-hidden="true" [style]="indStyle"></span>
120
+ <ng-content />
121
+ </div>
122
+ </div>
123
+ @if (name()) {<input type="hidden" [name]="name()" [value]="current() ?? ''" />}
124
+ `,
125
+ styles: `
126
+ :host {
127
+ --_t: var(--ui-look-tray, 0);
128
+ --_H: calc(var(--_z) + var(--_zt) * var(--_t));
129
+ --_rad: calc(var(--ui-radius-sm) + 2px * var(--_t));
130
+ --_lead: calc((150ms + 120ms * var(--_t)) * var(--ui-motion-scale));
131
+ --_trail: calc((220ms + 180ms * var(--_t)) * var(--ui-motion-scale));
132
+ --_wash: color-mix(in srgb, var(--ui-text) calc(5% - 5% * var(--_t)), transparent);
133
+ --_p: 0; --_g: 0; --_bg: none; --_base: none; --_fl: 0px; --_fr: 0px; --_rd: flex; --_it: 0; --_ih: auto; --_im: 0; --_ir: var(--_rad); --_is: none; --_ib: none; --_oh: var(--_H); --_min: 0px; --_px: calc(var(--_sp) + 2px * var(--_t)); --_dl: var(--ui-duration-3); --_dr: var(--ui-duration-3); --_fo: var(--ui-focus-offset-inset); --_do: var(--ui-disabled-opacity);
134
+ display: block;
135
+ inline-size: fit-content;
136
+ min-inline-size: 0;
137
+ max-inline-size: 100%;
138
+ font: var(--ui-weight-medium) calc(var(--_fs) + 0.5px * var(--_t)) / var(--_lh) var(--ui-font-sans);
139
+ }
140
+ :host([data-full]) { --_rd: grid; inline-size: 100%; }
141
+ .row { grid-auto-flow: column; grid-auto-columns: 1fr; }
142
+ :host([aria-disabled]) { opacity: var(--ui-disabled-opacity); }
143
+ .ind[data-ready] {
144
+ transition: left var(--_dl) var(--ui-ease-out), right var(--_dr) var(--ui-ease-out),
145
+ scale var(--ui-duration-1), opacity var(--ui-duration-1);
146
+ }
147
+ .ind[data-dir='r'] { --_dl: var(--_trail); --_dr: var(--_lead); }
148
+ .ind[data-dir='l'] { --_dl: var(--_lead); --_dr: var(--_trail); }
149
+ :host([data-pressed]) .ind { scale: calc(1 - 0.03 * var(--ui-motion-scale)); }
150
+ :host([data-variant='buttons']) {
151
+ --_p: var(--ui-space-1);
152
+ --_g: var(--ui-space-2);
153
+ --_ib: var(--ui-segmented-fill, color-mix(in srgb, var(--ui-text) 8%, transparent));
154
+ --_fo: calc(2px - 4px * var(--_t));
155
+ }
156
+ :host([data-variant='buttons']:not([data-full])) { margin-inline: calc(var(--_p) * -1); max-inline-size: calc(100% + var(--_p) * 2); }
157
+ :host([data-variant='tray']) {
158
+ --_p: calc(2px + 1px * var(--_t));
159
+ --_oh: calc(var(--_H) - 4px + 4px * var(--_t));
160
+ --_bg: var(--ui-segmented-track, color-mix(in srgb, var(--ui-text) 6%, transparent));
161
+ --_ib: var(--ui-segmented-pill, light-dark(var(--ui-bg-surface), var(--ui-bg-muted)));
162
+ --_is: var(--ui-shadow-sm);
163
+ }
164
+ :host([data-variant='underline']) {
165
+ --_oh: calc(var(--_H) + 6px);
166
+ --_g: var(--ui-space-5);
167
+ --_px: 2px;
168
+ --_wash: none;
169
+ --_base: inset 0 calc(-0.5px - 0.5px * var(--_t)) var(--ui-hairline);
170
+ --_ib: var(--ui-segmented-line, var(--ui-text));
171
+ --_it: auto;
172
+ --_ih: calc(1.5px + 0.5px * var(--_t));
173
+ --_im: 2px;
174
+ --_ir: 2px 2px 0 0;
175
+ }
176
+ :host ::ng-deep [uiSegment] { background: none; color: var(--ui-text-muted); }
177
+ :host ::ng-deep [uiSegment] > svg { inline-size: var(--_ic); block-size: var(--_ic); }
178
+ :host([data-variant='buttons']) ::ng-deep [uiSegment] { box-shadow: inset 0 0 0 0.5px var(--ui-hairline); }
179
+ :host ::ng-deep [data-active] { color: var(--ui-text); }
180
+ :host ::ng-deep .ui-seg-n { background: color-mix(in srgb, var(--ui-text) 6%, transparent); }
181
+ :host ::ng-deep :focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--_fo); }
182
+ :host ::ng-deep :disabled { opacity: var(--_do); cursor: not-allowed; }
183
+ :host([aria-disabled]) { --_do: 1; }
184
+ @media (hover: hover) {
185
+ :host ::ng-deep [uiSegment]:not(:disabled, [data-active]):hover { color: var(--ui-text); background: var(--_wash); }
186
+ }
187
+ @media (pointer: coarse) {
188
+ :host { --_min: var(--ui-space-10); }
189
+ }
190
+ @media (forced-colors: active) {
191
+ :host([data-variant]) { --_ib: Highlight; --_is: none; }
192
+ :host([data-variant='underline']) { --_base: inset 0 -1px CanvasText; }
193
+ :host([data-variant='tray']) .sc { outline: 1px solid CanvasText; outline-offset: -1px; }
194
+ .ind { forced-color-adjust: none; }
195
+ :host(:not([data-variant='underline'])) ::ng-deep [data-active] { forced-color-adjust: none; color: HighlightText; }
196
+ }
197
+ `,
198
+ })
199
+ export class UiSegmentedControl<T = string> implements ControlValueAccessor, FormValueControl<T | null> {
200
+ /** The picked option's value; `null` until one is picked. Bind `[(value)]`, `[formField]`, `formControlName` or `ngModel`. */
201
+ readonly value = model<T | null>(null);
202
+ /** The picked value now. Form writes land here without emitting `valueChange`. */
203
+ readonly current = linkedSignal(() => this.value());
204
+ /** `tray`: a track with a raised pill. `buttons`: separate chips with a sliding fill. `underline`: text tabs with a sliding line. */
205
+ readonly variant = input<SegmentedVariant>('tray');
206
+ /** 24, 28 or 32px tall; Classic 26, 30 or 36px. At least 40px on touch screens. */
207
+ readonly size = input<SegmentedSize>('md');
208
+ /** Fills its container with equal options, each as wide as the widest needs. */
209
+ readonly fullWidth = input(false, { transform: booleanAttribute });
210
+ /** `radio` picks a value (a radiogroup). `tabs` switches panels (a tablist): add `aria-controls` to each option. */
211
+ readonly kind = input<SegmentedKind>('radio');
212
+ /** Disables every option. Forms set it too. */
213
+ readonly disabled = input(false, { transform: booleanAttribute });
214
+ /** Submits the value with a native form, through a hidden input. Signal forms set it from the field. */
215
+ readonly name = input('');
216
+
217
+ protected readonly sizes = SIZES;
218
+ protected readonly scStyle =
219
+ 'display: flex; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain; touch-action: pan-x; padding: var(--_p); border-radius: calc(var(--ui-radius-md) + 3px * var(--_t)); background: var(--_bg); box-shadow: var(--_base); mask: linear-gradient(to right, #0000, #000 var(--_fl), #000 calc(100% - var(--_fr)), #0000)';
220
+ protected readonly rowStyle = 'position: relative; display: var(--_rd); flex: none; gap: var(--_g); min-inline-size: 100%; user-select: none';
221
+ protected readonly indStyle =
222
+ 'position: absolute; inset-block: var(--_it) 0; block-size: var(--_ih); margin-inline: var(--_im); border-radius: var(--_ir); background: var(--_ib); box-shadow: var(--_is); pointer-events: none';
223
+ private readonly options = contentChildren<UiSegment<T>>(UiSegment, { descendants: true });
224
+ protected readonly segments = computed(() => this.options().filter((s) => s.ready()));
225
+ protected readonly pressed = signal(false);
226
+ private readonly preview = signal<UiSegment<T> | null>(null);
227
+ private readonly formDisabled = signal(false);
228
+ readonly off = computed(() => this.disabled() || this.formDisabled());
229
+ private readonly selected = computed(() => this.segments().find((s) => s.active()));
230
+ private readonly enabled = computed(() => this.segments().filter((s) => !s.off()));
231
+ /** The option that Tab lands on: the picked one, or the first enabled one. */
232
+ readonly stop = computed(() => {
233
+ const s = this.selected();
234
+ return s && !s.off() ? s : this.enabled()[0];
235
+ });
236
+
237
+ private readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
238
+ private readonly doc = inject(DOCUMENT);
239
+ private readonly destroy = inject(DestroyRef);
240
+ private readonly sc = viewChild.required<ElementRef<HTMLElement>>('sc');
241
+ private readonly row = viewChild.required<ElementRef<HTMLElement>>('row');
242
+ private readonly ind = viewChild.required<ElementRef<HTMLElement>>('ind');
243
+ private readonly ro?: ResizeObserver;
244
+ private lastLeft?: number;
245
+ private initial: T | null = null;
246
+ private onChange: (v: T | null) => void = () => {};
247
+ private onTouched: () => void = () => {};
248
+
249
+ constructor() {
250
+ const view = this.doc.defaultView;
251
+ if (view?.ResizeObserver) this.ro = new view.ResizeObserver(() => this.place());
252
+ afterRenderEffect(() => {
253
+ const ro = this.ro;
254
+ if (!ro) return;
255
+ ro.disconnect();
256
+ ro.observe(this.row().nativeElement);
257
+ for (const s of this.segments()) ro.observe(s.el);
258
+ });
259
+ afterRenderEffect(() => {
260
+ this.variant();
261
+ this.size();
262
+ this.fullWidth();
263
+ this.place();
264
+ });
265
+ afterNextRender(() => {
266
+ this.initial = this.current();
267
+ const watch = view?.MutationObserver && new view.MutationObserver(() => this.place());
268
+ watch?.observe(this.doc.documentElement, { attributes: true, subtree: true, attributeFilter: ['dir'] });
269
+ if (isDevMode()) {
270
+ if (!this.host.hasAttribute('aria-label') && !this.host.hasAttribute('aria-labelledby')) {
271
+ console.warn('ui-segmented-control: give it an aria-label or aria-labelledby that names the choice.', this.host);
272
+ }
273
+ for (const s of this.segments()) {
274
+ if (!s.el.textContent?.trim() && !s.el.hasAttribute('aria-label')) console.warn('uiSegment: an option with only an icon needs an aria-label.', s.el);
275
+ }
276
+ }
277
+ this.destroy.onDestroy(() => {
278
+ watch?.disconnect();
279
+ this.ro?.disconnect();
280
+ });
281
+ });
282
+ }
283
+
284
+ /** Moves the indicator under the picked (or dragged-over) option, and updates the side fades. */
285
+ private place() {
286
+ const ind = this.ind().nativeElement;
287
+ const seg = (this.preview() ?? this.selected())?.el;
288
+ const row = this.row().nativeElement.getBoundingClientRect();
289
+ this.fade();
290
+ if (!seg || !row.width) {
291
+ ind.style.opacity = '0';
292
+ return;
293
+ }
294
+ const r = seg.getBoundingClientRect();
295
+ const k = this.row().nativeElement.offsetWidth / row.width || 1;
296
+ const left = (r.left - row.left) * k;
297
+ if (this.lastLeft !== undefined && Math.abs(left - this.lastLeft) > 0.5) ind.dataset['dir'] = left > this.lastLeft ? 'r' : 'l';
298
+ this.lastLeft = left;
299
+ ind.style.left = `${left}px`;
300
+ ind.style.right = `${(row.right - r.right) * k}px`;
301
+ ind.style.opacity = '';
302
+ if (!ind.hasAttribute('data-ready')) {
303
+ this.reveal(seg, true);
304
+ void ind.offsetWidth;
305
+ this.doc.defaultView?.requestAnimationFrame(() => ind.setAttribute('data-ready', ''));
306
+ }
307
+ }
308
+
309
+ protected fade() {
310
+ const sc = this.sc().nativeElement;
311
+ const max = sc.scrollWidth - sc.clientWidth;
312
+ const x = sc.scrollLeft;
313
+ const rtl = this.doc.defaultView?.getComputedStyle(sc).direction === 'rtl';
314
+ const [l, r] = rtl ? [max + x, -x] : [x, max - x];
315
+ sc.style.setProperty('--_fl', l > 1 ? '24px' : '0px');
316
+ sc.style.setProperty('--_fr', r > 1 ? '24px' : '0px');
317
+ }
318
+
319
+ /** Scrolls the row, never the page, until the option is in view. */
320
+ private reveal(el: HTMLElement, now = false) {
321
+ const sc = this.sc().nativeElement;
322
+ if (sc.scrollWidth <= sc.clientWidth) return;
323
+ const a = el.getBoundingClientRect();
324
+ const b = sc.getBoundingClientRect();
325
+ const pad = 24;
326
+ const d = a.left < b.left + pad ? a.left - b.left - pad : a.right > b.right - pad ? a.right - b.right + pad : 0;
327
+ const view = this.doc.defaultView;
328
+ const still = now || view?.matchMedia?.('(prefers-reduced-motion: reduce)').matches || parseFloat(view?.getComputedStyle(sc).getPropertyValue('--ui-motion-scale') ?? '') === 0;
329
+ if (d) sc.scrollBy({ left: (d * sc.offsetWidth) / b.width || d, behavior: still ? 'instant' : 'smooth' });
330
+ }
331
+
332
+ private select(seg: UiSegment<T>, focus = false) {
333
+ if (seg.off()) return;
334
+ if (focus) seg.el.focus();
335
+ this.reveal(seg.el);
336
+ this.pick(seg.value());
337
+ }
338
+
339
+ private pick(v: T | null) {
340
+ if (Object.is(v, this.current())) return;
341
+ this.current.set(v);
342
+ this.value.set(v);
343
+ this.onChange(v);
344
+ }
345
+
346
+ /** A native form reset puts back the value the control started with. */
347
+ protected formReset(e: Event) {
348
+ if (!this.name() || e.target !== this.host.closest('form')) return;
349
+ setTimeout(() => e.defaultPrevented || this.pick(this.initial));
350
+ }
351
+
352
+ private hit(target: EventTarget | null) {
353
+ return this.segments().find((s) => s.el.contains(target as Node));
354
+ }
355
+
356
+ private nearest(x: number) {
357
+ let best: UiSegment<T> | undefined;
358
+ let dist = Infinity;
359
+ for (const s of this.enabled()) {
360
+ const r = s.el.getBoundingClientRect();
361
+ const d = x < r.left ? r.left - x : x > r.right ? x - r.right : 0;
362
+ if (d < dist) [best, dist] = [s, d];
363
+ }
364
+ return best;
365
+ }
366
+
367
+ protected key(e: KeyboardEvent) {
368
+ const segs = this.enabled();
369
+ const cur = segs.findIndex((s) => s.el === e.target);
370
+ if (cur < 0 || e.altKey || e.ctrlKey || e.metaKey) return;
371
+ const rtl = this.doc.defaultView?.getComputedStyle(this.host).direction === 'rtl';
372
+ const tabs = this.kind() === 'tabs';
373
+ const step: Record<string, number> = { ArrowRight: rtl ? -1 : 1, ArrowLeft: rtl ? 1 : -1, ArrowDown: tabs ? 0 : 1, ArrowUp: tabs ? 0 : -1 };
374
+ const n = segs.length;
375
+ const i = e.key === 'Home' ? 0 : e.key === 'End' ? n - 1 : step[e.key] ? (cur + step[e.key] + n) % n : -1;
376
+ if (i < 0) return;
377
+ e.preventDefault();
378
+ this.select(segs[i], true);
379
+ }
380
+
381
+ /** Keyboard Enter / Space, a tap, or an assistive-technology click. A mouse press was already handled on release. */
382
+ protected clicked(e: MouseEvent) {
383
+ if ((e as PointerEvent).pointerType === 'mouse') return;
384
+ const s = this.hit(e.target);
385
+ if (s) this.select(s);
386
+ }
387
+
388
+ protected down(e: PointerEvent) {
389
+ const s = this.hit(e.target);
390
+ if (e.pointerType !== 'mouse' || e.button !== 0 || !s || s.off()) return;
391
+ this.host.setPointerCapture?.(e.pointerId);
392
+ this.pressed.set(true);
393
+ this.preview.set(s);
394
+ }
395
+
396
+ protected move(e: PointerEvent) {
397
+ if (!this.pressed()) return;
398
+ const s = this.nearest(e.clientX);
399
+ if (s && s !== this.preview()) this.preview.set(s);
400
+ }
401
+
402
+ protected up() {
403
+ const s = this.preview();
404
+ if (!this.pressed()) return;
405
+ this.end();
406
+ if (s) this.select(s);
407
+ }
408
+
409
+ protected end() {
410
+ this.pressed.set(false);
411
+ this.preview.set(null);
412
+ }
413
+
414
+ protected left(e: FocusEvent) {
415
+ if (!this.host.contains(e.relatedTarget as Node)) this.onTouched();
416
+ }
417
+
418
+ writeValue(v: T | null) {
419
+ this.current.set(v ?? null);
420
+ }
421
+
422
+ registerOnChange(fn: (v: T | null) => void) {
423
+ this.onChange = fn;
424
+ }
425
+
426
+ registerOnTouched(fn: () => void) {
427
+ this.onTouched = fn;
428
+ }
429
+
430
+ setDisabledState(disabled: boolean) {
431
+ this.formDisabled.set(disabled);
432
+ }
433
+ }