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,172 @@
|
|
|
1
|
+
# Tree View
|
|
2
|
+
|
|
3
|
+
Folders and files, or any hierarchy, where one row is picked: chevron folders that glide open, indent guides that darken around the picked row, a quiet meta per row, and the full tree keyboard with typeahead. The pick works with signal forms, reactive forms or ngModel.
|
|
4
|
+
|
|
5
|
+
Add: `npx ochreui@latest add tree-view`
|
|
6
|
+
Import: `import { UiTreeView, UiTreeIcon } from './ui/tree-view';`
|
|
7
|
+
Selector: `<ui-tree-view>`
|
|
8
|
+
Docs: /components/tree-view
|
|
9
|
+
|
|
10
|
+
## When to use
|
|
11
|
+
- Folders and files: repositories, drives, knowledge bases, job outputs.
|
|
12
|
+
- Any hierarchy browsed to pick one item: projects, docs outlines, nested categories.
|
|
13
|
+
- A side panel whose picked row drives a viewer.
|
|
14
|
+
|
|
15
|
+
## When not to use
|
|
16
|
+
- Many columns, sorting or multi-select: use a data table.
|
|
17
|
+
- Where a page sits in a hierarchy: use Breadcrumb.
|
|
18
|
+
- A flat list of choices: use Select or Dropdown Menu.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { Component, signal } from '@angular/core';
|
|
24
|
+
import { TreeNode, UiTreeIcon, UiTreeView } from './ui/tree-view';
|
|
25
|
+
|
|
26
|
+
@Component({
|
|
27
|
+
selector: 'app-handbook-files',
|
|
28
|
+
imports: [UiTreeView, UiTreeIcon],
|
|
29
|
+
template: `
|
|
30
|
+
<ui-tree-view aria-label="Handbook" [items]="files" [expanded]="['guides']" [(value)]="selected" (picked)="open($event.node)">
|
|
31
|
+
<ng-template uiTreeIcon let-node let-open="open">
|
|
32
|
+
<svg class="ui-icon" viewBox="0 0 16 16" aria-hidden="true">
|
|
33
|
+
@if (!node.children) {
|
|
34
|
+
<path d="M9 2H4.5A1.5 1.5 0 0 0 3 3.5v9A1.5 1.5 0 0 0 4.5 14h7a1.5 1.5 0 0 0 1.5-1.5V6zM9 2v4h4" />
|
|
35
|
+
} @else if (open) {
|
|
36
|
+
<path d="M2 11.5v-7A1.5 1.5 0 0 1 3.5 3h2.4l1.6 1.5h4A1.5 1.5 0 0 1 13 6v1M2 11.5 3.7 7.9A1.5 1.5 0 0 1 5 7h8.3a.8.8 0 0 1 .7 1.1l-1.5 3.8a1.5 1.5 0 0 1-1.4 1.1h-7.6A1.5 1.5 0 0 1 2 11.5z" />
|
|
37
|
+
} @else {
|
|
38
|
+
<path d="M2 4.5A1.5 1.5 0 0 1 3.5 3h2.4l1.6 1.5h5A1.5 1.5 0 0 1 14 6v5.5a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 2 11.5z" />
|
|
39
|
+
}
|
|
40
|
+
</svg>
|
|
41
|
+
</ng-template>
|
|
42
|
+
</ui-tree-view>
|
|
43
|
+
`,
|
|
44
|
+
})
|
|
45
|
+
export class HandbookFiles {
|
|
46
|
+
protected readonly files: TreeNode[] = [
|
|
47
|
+
{
|
|
48
|
+
id: 'guides',
|
|
49
|
+
label: 'guides',
|
|
50
|
+
children: [
|
|
51
|
+
{ id: 'onboarding', label: 'onboarding.md', meta: '4 KB' },
|
|
52
|
+
{ id: 'on-call', label: 'on-call.md', meta: '9 KB' },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
{ id: 'policies', label: 'policies', children: [{ id: 'expenses', label: 'expenses.md' }] },
|
|
56
|
+
{ id: 'contacts', label: 'contacts.csv', meta: '1 KB' },
|
|
57
|
+
];
|
|
58
|
+
protected readonly selected = signal<string | null>('onboarding');
|
|
59
|
+
protected readonly reading = signal<TreeNode | null>(null);
|
|
60
|
+
|
|
61
|
+
protected open(node: TreeNode) {
|
|
62
|
+
if (!node.children) this.reading.set(node);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Rules
|
|
68
|
+
- Give every node a unique id; a children array (even empty) makes a folder.
|
|
69
|
+
- Name the tree with aria-label or aria-labelledby.
|
|
70
|
+
- Draw icons with <ng-template uiTreeIcon let-node let-open="open"> and currentColor.
|
|
71
|
+
- Bind the pick with [(value)], [formField], formControl or ngModel; use (picked) to act on every pick.
|
|
72
|
+
- With [formField], don't set disabled or name: the schema does.
|
|
73
|
+
- Arrow keys move focus only; selection follows Enter, Space or a click.
|
|
74
|
+
|
|
75
|
+
## Inputs
|
|
76
|
+
|
|
77
|
+
| Name | Type | Default | Description |
|
|
78
|
+
| --- | --- | --- | --- |
|
|
79
|
+
| `items` | `readonly TreeNode[]` | required | Top-level nodes; ids unique across the tree. |
|
|
80
|
+
| `value` | `string \| null (model)` | null | Picked id; [(value)] or a form binding. |
|
|
81
|
+
| `expanded` | `readonly string[] (model)` | [] | Open folder ids; [(expanded)] to control, [expanded] for the start. |
|
|
82
|
+
| `guides` | `boolean` | true | Lines under open folders; darker around the picked row. |
|
|
83
|
+
| `disabled` | `boolean` | false | Whole tree off; forms set it. |
|
|
84
|
+
| `invalid` | `boolean` | false | aria-invalid; signal forms set it. |
|
|
85
|
+
| `labels` | `Partial<TreeViewLabels>` | English | Translations: empty. |
|
|
86
|
+
| `aria-label / aria-labelledby` | `string` | none | Names the tree; one is required. |
|
|
87
|
+
|
|
88
|
+
## Outputs
|
|
89
|
+
|
|
90
|
+
| Name | Payload | Description |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| `picked` | `TreePick` | { id, node } after click, Enter or Space, also on re-picks; not on form writes. |
|
|
93
|
+
| `valueChange` | `string \| null` | A pick changed the value. |
|
|
94
|
+
| `expandedChange` | `readonly string[]` | A folder opened or closed. |
|
|
95
|
+
| `touch` | `void` | Focus left the tree. |
|
|
96
|
+
|
|
97
|
+
## TreeNode
|
|
98
|
+
|
|
99
|
+
| Field | Type | Description |
|
|
100
|
+
| --- | --- | --- |
|
|
101
|
+
| `id` | `string` | Unique across the tree. |
|
|
102
|
+
| `label` | `string` | Row text and accessible name. |
|
|
103
|
+
| `meta` | `string` | Quiet end detail; the row description. |
|
|
104
|
+
| `disabled` | `boolean` | Faded, skipped, inert; folders stay closed. |
|
|
105
|
+
| `children` | `readonly TreeNode[]` | Makes a folder, even when empty. |
|
|
106
|
+
| `icon` | `string` | Key for the uiTreeIcon template. |
|
|
107
|
+
| `data` | `T` | Your payload, returned in picked. |
|
|
108
|
+
|
|
109
|
+
## TreePick
|
|
110
|
+
|
|
111
|
+
| Field | Type | Description |
|
|
112
|
+
| --- | --- | --- |
|
|
113
|
+
| `id` | `string` | Picked id. |
|
|
114
|
+
| `node` | `TreeNode` | Picked node. |
|
|
115
|
+
|
|
116
|
+
## TreeIconContext
|
|
117
|
+
|
|
118
|
+
| Field | Type | Description |
|
|
119
|
+
| --- | --- | --- |
|
|
120
|
+
| `$implicit` | `TreeNode` | The node. |
|
|
121
|
+
| `open` | `boolean` | Folder is open. |
|
|
122
|
+
| `selected` | `boolean` | Row is picked. |
|
|
123
|
+
|
|
124
|
+
## TreeViewLabels
|
|
125
|
+
|
|
126
|
+
| Field | Type | Description |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| `empty` | `string` | Line in an open empty folder. |
|
|
129
|
+
|
|
130
|
+
## Template: uiTreeIcon
|
|
131
|
+
|
|
132
|
+
Draws the 16px icon before each label. Use currentColor: muted, full ink on the picked row. Context: `{ $implicit: TreeNode; open: boolean; selected: boolean }`.
|
|
133
|
+
|
|
134
|
+
```html
|
|
135
|
+
<ng-template uiTreeIcon let-node let-open="open">
|
|
136
|
+
<svg class="ui-icon" viewBox="0 0 16 16" aria-hidden="true"><path [attr.d]="node.children ? (open ? folderOpen : folder) : file" /></svg>
|
|
137
|
+
</ng-template>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## CSS variables
|
|
141
|
+
|
|
142
|
+
| Variable | Default | Description |
|
|
143
|
+
| --- | --- | --- |
|
|
144
|
+
| `--ui-tree-row-h` | `calc(var(--ui-control-h-sm) + 4px * tray)` | Row height: 28, Classic 32; on touch screens 36, Classic 40. |
|
|
145
|
+
| `--ui-tree-indent` | `calc(var(--ui-space-4) + 2px * tray)` | Indent per level: 16, Classic and touch 18; capped at 45%. |
|
|
146
|
+
| `--ui-tree-hover-bg` | `text at about 3%` | Hovered row. |
|
|
147
|
+
| `--ui-tree-selected-bg` | `text at about 6%` | Picked row. |
|
|
148
|
+
| `--ui-tree-chevron` | `muted at 45%, Classic 59% (about 51% in dark)` | Resting chevrons; var(--ui-text-muted) for 3:1. The Empty line is var(--ui-text-muted). |
|
|
149
|
+
| `--ui-tree-guide` | `text at about 8%` | Guides. |
|
|
150
|
+
| `--ui-tree-guide-active` | `text at about 22%` | Guide around the picked row. |
|
|
151
|
+
|
|
152
|
+
## Keyboard
|
|
153
|
+
|
|
154
|
+
| Keys | Action |
|
|
155
|
+
| --- | --- |
|
|
156
|
+
| Tab | Enter on the last-used, picked or first row; Tab again leaves. |
|
|
157
|
+
| ↓ / ↑ | Next / previous visible row; disabled skipped; no wrap. |
|
|
158
|
+
| → | Open a folder; on an open one, go to its first row. |
|
|
159
|
+
| ← | Close an open folder; else go to the parent. |
|
|
160
|
+
| Home / End | First / last visible row. |
|
|
161
|
+
| Enter | Pick; on a folder also toggle. |
|
|
162
|
+
| Space | Pick. |
|
|
163
|
+
| * | Open every folder at this level. |
|
|
164
|
+
| a–z, 0–9 | Typeahead over visible enabled rows; repeat a letter to cycle. |
|
|
165
|
+
|
|
166
|
+
## Accessibility
|
|
167
|
+
- role=tree with treeitems carrying aria-level, posinset, setsize, expanded, selected and disabled.
|
|
168
|
+
- Rows named by the label (aria-labelledby), described by meta (aria-describedby).
|
|
169
|
+
- Roving tabindex; focus ring on :focus-visible only.
|
|
170
|
+
- Closed folders are visibility: hidden; closing around focus moves focus to the folder.
|
|
171
|
+
- Labels use dir=auto aligned to the row direction.
|
|
172
|
+
- Forced colors: SelectedItem for the picked row, dashed outline on hover, guides kept as borders.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Theming Ochre
|
|
2
|
+
|
|
3
|
+
Components read only `--ui-*` CSS variables, so a theme is a stylesheet. Put overrides in their own file, listed after `ochre.css` in the `styles` of `angular.json`.
|
|
4
|
+
|
|
5
|
+
## Brand in five variables
|
|
6
|
+
|
|
7
|
+
Most brands need the accent trio, the font and the radius:
|
|
8
|
+
|
|
9
|
+
```css
|
|
10
|
+
/* src/styles/theme.css */
|
|
11
|
+
:root {
|
|
12
|
+
/* light-dark(light value, dark value): covers a set theme and the system setting */
|
|
13
|
+
--ui-accent: light-dark(#0f766e, #2dd4bf);
|
|
14
|
+
--ui-accent-hover: light-dark(#115e59, #5eead4);
|
|
15
|
+
--ui-accent-soft: light-dark(rgb(15 118 110 / 0.1), rgb(45 212 191 / 0.16));
|
|
16
|
+
--ui-font-sans: 'Inter Variable', Inter, system-ui, sans-serif;
|
|
17
|
+
--ui-radius-md: 10px;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- Font and radius need one value. Colors need a light and a dark value, so write them with `light-dark()`: it follows `data-ui-theme`, the `.dark` class and the system setting alike.
|
|
22
|
+
- `--ui-accent-hover` is a step darker in light mode and a step lighter in dark mode.
|
|
23
|
+
- `--ui-accent-soft` is the accent at about 10% (light) or 16% (dark) opacity, for selected rows and chips.
|
|
24
|
+
- If the font isn't installed on every machine, load it in the app (for example with `@fontsource-variable/<font>`).
|
|
25
|
+
|
|
26
|
+
## Contrast
|
|
27
|
+
|
|
28
|
+
Check before you finish:
|
|
29
|
+
|
|
30
|
+
- `--ui-text-on-accent` on `--ui-accent`: at least 4.5:1. If the brand color is light, set `--ui-text-on-accent` to a dark value.
|
|
31
|
+
- `--ui-text-link` on `--ui-bg-surface`: at least 4.5:1, in both themes.
|
|
32
|
+
- `--ui-accent` on `--ui-bg-surface`: at least 3:1, so selection and focus stay visible.
|
|
33
|
+
|
|
34
|
+
## Taking a brand from somewhere
|
|
35
|
+
|
|
36
|
+
- **A website**: take the main button color as the accent, the body font, and the corner radius of its buttons and cards.
|
|
37
|
+
- **A logo**: take its strongest color as the accent. Leave the neutrals alone.
|
|
38
|
+
- **Figma**: use the primary color style, the text style of body copy, and the radius of the main components.
|
|
39
|
+
|
|
40
|
+
Never change the neutral text, background and border tokens unless the user asks for it. They are tuned for contrast.
|
|
41
|
+
|
|
42
|
+
## Scoped themes
|
|
43
|
+
|
|
44
|
+
Any container can carry its own tokens and scopes, and everything inside follows:
|
|
45
|
+
|
|
46
|
+
```html
|
|
47
|
+
<section class="danger-zone" data-ui-density="compact">…</section>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```css
|
|
51
|
+
.danger-zone {
|
|
52
|
+
--ui-accent: var(--ui-danger);
|
|
53
|
+
--ui-accent-soft: var(--ui-danger-soft);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Scopes: `data-ui-theme="light|dark"` (no attribute follows the system), `data-ui-density="comfortable|compact"`, `data-ui-look="flat|tray"`.
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
|
|
2
|
+
/**
|
|
3
|
+
* Smart Recommendation, from Ochre (https://ochreui.dev).
|
|
4
|
+
* Needs ochre.css (tokens, .ui-sr, .ui-icon and ui-rise), added once by `npx ochreui init`.
|
|
5
|
+
*
|
|
6
|
+
* <ui-smart-recommendation [options]="modes" (accept)="save($event.id)" (undo)="revert($event.id)" />
|
|
7
|
+
* A suggestion card: what to do, why, and how sure the app is. The first option is the recommendation,
|
|
8
|
+
* the others are one menu away; Accept commits the option shown and offers Undo.
|
|
9
|
+
* For a reason with markup, add <ng-template uiRecommendationReason let-o>…</ng-template> inside.
|
|
10
|
+
*
|
|
11
|
+
* Public styling hooks (set them on the element or any ancestor):
|
|
12
|
+
* --ui-smart-recommendation-width widest the card grows (default 440px; it shrinks with its container)
|
|
13
|
+
* --ui-smart-recommendation-radius corners (default 10px; Classic a 16px card in a 22px tray)
|
|
14
|
+
*/
|
|
15
|
+
import { NgTemplateOutlet } from '@angular/common';
|
|
16
|
+
import {
|
|
17
|
+
Component,
|
|
18
|
+
DOCUMENT,
|
|
19
|
+
Directive,
|
|
20
|
+
ElementRef,
|
|
21
|
+
Injector,
|
|
22
|
+
TemplateRef,
|
|
23
|
+
afterNextRender,
|
|
24
|
+
computed,
|
|
25
|
+
contentChild,
|
|
26
|
+
effect,
|
|
27
|
+
inject,
|
|
28
|
+
input,
|
|
29
|
+
isDevMode,
|
|
30
|
+
model,
|
|
31
|
+
output,
|
|
32
|
+
signal,
|
|
33
|
+
} from '@angular/core';
|
|
34
|
+
|
|
35
|
+
export type RecommendationSignal = 0 | 1 | 2 | 3;
|
|
36
|
+
|
|
37
|
+
export interface Recommendation {
|
|
38
|
+
id: string;
|
|
39
|
+
/** The option's name, in the menu, the Accept button and the status. */
|
|
40
|
+
label: string;
|
|
41
|
+
/** Why: one or two sentences. Use a `uiRecommendationReason` template for markup. */
|
|
42
|
+
reason: string;
|
|
43
|
+
/** How sure the app is, from 0 (no signal) to 3 (strong). */
|
|
44
|
+
signal: RecommendationSignal;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every visible and spoken string, for translation. */
|
|
48
|
+
export interface SmartRecommendationLabels {
|
|
49
|
+
alternatives: string;
|
|
50
|
+
accept: (label: string) => string;
|
|
51
|
+
accepted: (label: string) => string;
|
|
52
|
+
undo: string;
|
|
53
|
+
/** The badge on the first option in the menu. */
|
|
54
|
+
recommended: string;
|
|
55
|
+
/** The header badge while another option is shown. */
|
|
56
|
+
alternative: string;
|
|
57
|
+
/** The words for signal 0, 1, 2 and 3. */
|
|
58
|
+
signal: readonly [string, string, string, string];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const LABELS: SmartRecommendationLabels = {
|
|
62
|
+
alternatives: 'Alternatives',
|
|
63
|
+
accept: (l) => `Accept ${l}`,
|
|
64
|
+
accepted: (l) => `${l} accepted`,
|
|
65
|
+
undo: 'Undo',
|
|
66
|
+
recommended: 'Recommended',
|
|
67
|
+
alternative: 'Alternative',
|
|
68
|
+
signal: ['No signal', 'Weak signal', 'Moderate signal', 'Strong signal'],
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** A reason with markup: `<ng-template uiRecommendationReason let-o>…</ng-template>`. */
|
|
72
|
+
@Directive({ selector: 'ng-template[uiRecommendationReason]' })
|
|
73
|
+
export class UiRecommendationReason {
|
|
74
|
+
readonly template = inject<TemplateRef<{ $implicit: Recommendation }>>(TemplateRef);
|
|
75
|
+
|
|
76
|
+
static ngTemplateContextGuard(_dir: UiRecommendationReason, ctx: unknown): ctx is { $implicit: Recommendation } {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const ICON = 'flex: none; inline-size: var(--ui-icon-md); block-size: var(--ui-icon-md)';
|
|
82
|
+
const BTN =
|
|
83
|
+
'position: relative; display: inline-flex; align-items: center; gap: var(--ui-space-1-5); box-sizing: border-box; block-size: var(--_bh); margin: 0; padding: 0 calc(9px + 2px * var(--_t) + var(--_bt)); border: 1px solid var(--_bc); border-radius: calc(var(--ui-radius-sm) + 4px * var(--_t)); background: var(--_g) var(--_bg); box-shadow: var(--_sh); color: var(--_fg); font: inherit; font-weight: calc(500 - 100 * var(--_t)); white-space: nowrap; cursor: pointer; -webkit-tap-highlight-color: transparent; --_d: calc((140ms + 20ms * var(--_t)) * var(--_ms)) var(--ui-ease-out); transition: background-color var(--_d), box-shadow var(--_d), transform calc(160ms * var(--_ms)) var(--ui-ease-out);';
|
|
84
|
+
const SEC =
|
|
85
|
+
BTN +
|
|
86
|
+
' flex: none; --_bg: color-mix(in srgb, var(--_tb) calc(100% * var(--_t)), var(--ui-bg-surface)); --_sh: 0 0 0 calc(.5px + .5px * var(--_t)) color-mix(in srgb, var(--ui-border) calc(100% * var(--_t)), var(--_e)), 0 var(--_y) var(--_y) calc(-1 * var(--_y)) var(--_s), 0 1px var(--_bl) var(--_s)';
|
|
87
|
+
const BADGE = 'flex: none; font-weight: var(--ui-weight-medium); white-space: nowrap';
|
|
88
|
+
|
|
89
|
+
const S = {
|
|
90
|
+
card: 'box-sizing: border-box; padding: calc(6px * var(--_t)) calc(8px * var(--_t)) calc(8px * var(--_t)); border: calc(1px * var(--_t)) solid color-mix(in srgb, var(--ui-border) calc(100% * var(--_t)), transparent); border-radius: var(--_r); background: var(--ui-bg-surface); box-shadow: 0 calc(1px * var(--_t)) calc(2px * var(--_t)) var(--_s)',
|
|
91
|
+
head: 'display: flex; align-items: center; gap: calc(var(--ui-space-2) + 2px * var(--_t)); box-sizing: border-box; inline-size: 100%; block-size: var(--_hh); margin: 0; padding: 0 calc(12px - 2px * var(--_t)) 0 calc(16px - 6px * var(--_t)); border: 0; border-radius: calc(var(--_r) - 4px * var(--_t)) calc(var(--_r) - 4px * var(--_t)) calc(12px * var(--_t)) calc(12px * var(--_t)); background: var(--_hb); color: var(--ui-text); font: inherit; font-size: calc(var(--ui-text-base) + 2px * var(--_t)); font-weight: var(--ui-weight-medium); text-align: start; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background-color calc(140ms * var(--_ms)) var(--ui-ease-out)',
|
|
92
|
+
spark: `${ICON}; fill: var(--ui-hue-blue)`,
|
|
93
|
+
box: `${ICON}; color: light-dark(color-mix(in oklab, color-mix(in srgb, var(--ui-hue-blue), var(--ui-hue-cyan)) 75%, var(--ui-bg-surface)), var(--ui-hue-blue))`,
|
|
94
|
+
title: 'flex: 0 1 auto; min-inline-size: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap',
|
|
95
|
+
alt: `${BADGE}; padding: calc(1px * var(--_t)) calc(6px + 2px * var(--_t)); border-radius: calc(5px + 99px * var(--_t)); background: var(--ui-purple-soft); color: var(--_pu); font-size: calc(var(--ui-text-sm) - .5px * var(--_t)); line-height: calc(20px - 1.43px * var(--_t)); --ui-rise-distance: 2px`,
|
|
96
|
+
chev: 'display: grid; place-items: center; flex: none; inline-size: calc(20px - 4px * var(--_t)); block-size: calc(20px - 4px * var(--_t)); margin-inline-start: auto; color: var(--_cc); transition: color calc(140ms * var(--_ms)), rotate calc(280ms * var(--_ms)) var(--ui-ease-out)',
|
|
97
|
+
chevIcon: `${ICON}; stroke-width: 1.25px`,
|
|
98
|
+
body: 'display: grid; transition: grid-template-rows calc(340ms * var(--_ms)) var(--ui-ease-out), visibility 0s',
|
|
99
|
+
clip: 'min-block-size: 0; overflow: hidden',
|
|
100
|
+
reason: 'margin: 0; padding: 0 calc(16px - 6px * var(--_t)) calc(14px - 6px * var(--_t)); color: color-mix(in srgb, var(--ui-text-muted) calc(100% - 45% * var(--_t)), var(--ui-text)); font-size: calc(var(--ui-text-base) + 1px * var(--_t)); line-height: calc(20px + 1px * var(--_t)); overflow-wrap: anywhere; --ui-rise-distance: 2px',
|
|
101
|
+
foot: 'display: flex; flex-wrap: wrap; align-items: center; gap: var(--ui-space-2) var(--ui-space-3); box-sizing: border-box; min-block-size: calc(50px * var(--_t)); padding-block: calc(10px - 2px * var(--_t)); padding-inline: var(--ui-space-4) calc(10px - 2px * var(--_t)); border-block-start: calc(1px * var(--_f)) solid var(--ui-border)',
|
|
102
|
+
sig: 'display: inline-flex; align-items: center; gap: var(--ui-space-2); min-inline-size: 0; color: var(--ui-text-muted); font-size: calc(var(--ui-text-sm) + 1px * var(--_t)); line-height: calc(16px + 2.57px * var(--_t)); white-space: nowrap',
|
|
103
|
+
bars: 'display: inline-flex; flex: none; direction: ltr; align-items: flex-end; gap: 2px; block-size: calc(11px + 1px * var(--_t))',
|
|
104
|
+
acts: 'display: flex; flex: 0 1 auto; align-items: center; gap: calc(var(--ui-space-2) - 2px * var(--_t)); min-inline-size: 0; margin-inline-start: auto',
|
|
105
|
+
anchor: 'position: relative; display: flex; flex: none',
|
|
106
|
+
sec: SEC,
|
|
107
|
+
secIcon: `${SEC}; padding-inline-start: calc(7px + 4px * var(--_t) + var(--_bt))`,
|
|
108
|
+
icon: `${ICON}; color: var(--_ic); stroke-width: 1.25px`,
|
|
109
|
+
pri:
|
|
110
|
+
BTN +
|
|
111
|
+
' flex: 0 1 auto; min-inline-size: 0; --_fg: var(--ui-text-on-neutral); --_bg: color-mix(in srgb, var(--ui-bg-neutral-solid) calc(var(--_a) + (100% - var(--_a)) * var(--_t)), var(--ui-bg-surface)); --_g: linear-gradient(color-mix(in srgb, var(--ui-gray-0) calc(var(--_hi) * var(--_t)), transparent), color-mix(in srgb, var(--ui-gray-1000) calc(5% * var(--_t)), transparent)); --_sh: 0 0 0 calc(1px * var(--_t)) var(--ui-gray-1000), 0 calc(2px * var(--_t)) calc(4px * var(--_t)) color-mix(in srgb, var(--ui-gray-1000) calc(16% * var(--_t)), transparent), inset 0 calc(1px * var(--_t)) color-mix(in srgb, var(--ui-gray-0) calc(14% * var(--_t)), transparent)',
|
|
112
|
+
cut: 'min-inline-size: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap',
|
|
113
|
+
ok: 'display: inline-flex; align-items: center; gap: var(--ui-space-1-5); box-sizing: border-box; min-inline-size: 0; block-size: calc(28px - 9.43px * var(--_t)); padding: 0 calc(10px - 6px * var(--_t)) 0 calc(8px - 8px * var(--_t)); border-radius: var(--ui-radius-sm); background: color-mix(in srgb, var(--ui-success-soft) calc(100% * var(--_f)), transparent); color: var(--_ok); font-weight: var(--ui-weight-medium); white-space: nowrap; --ui-rise-distance: 2px',
|
|
114
|
+
okIcon: 'flex: none; inline-size: calc(16px - 2px * var(--_t)); block-size: calc(16px - 2px * var(--_t))',
|
|
115
|
+
menu: 'position: absolute; z-index: var(--ui-z-popover); display: flex; flex-direction: column; gap: 2px; box-sizing: border-box; inline-size: calc(248px + 12px * var(--_t)); padding: var(--ui-space-1); border-radius: calc(10px + 2px * var(--_t)); background: color-mix(in srgb, var(--ui-bg-overlay) calc(100% * var(--_t)), var(--ui-bg-surface)); box-shadow: 0 0 0 calc(.5px + .5px * var(--_t)) color-mix(in srgb, var(--ui-border) calc(100% * var(--_t)), var(--_e)), 0 12px calc(28px + 2px * var(--_t)) calc(-10px + 2px * var(--_t)) var(--_md), 0 1px 2px color-mix(in srgb, var(--_s) calc(100% * var(--_f)), transparent)',
|
|
116
|
+
item: 'display: flex; align-items: center; gap: 10px; box-sizing: border-box; inline-size: 100%; block-size: var(--_ih); margin: 0; padding: 0 10px 0 calc(8px + 2px * var(--_t)); border: 0; border-radius: calc(var(--ui-radius-sm) + 2px * var(--_t)); background: var(--_ib); box-shadow: var(--_ik); color: var(--ui-text); font: inherit; text-align: start; cursor: pointer; -webkit-tap-highlight-color: transparent; transition: background-color calc(90ms * var(--_f) * var(--_ms))',
|
|
117
|
+
lab: 'display: flex; flex: 1; align-items: center; gap: var(--ui-space-2); min-inline-size: 0',
|
|
118
|
+
rec: `${BADGE}; padding: 0 calc(5px + 1px * var(--_t)); border-radius: calc(5px + 99px * var(--_t)); background: var(--_rb); color: var(--_rc); font-size: calc(var(--ui-text-xs) - .5px * var(--_t)); line-height: calc(18px - 2px * var(--_t))`,
|
|
119
|
+
} as const;
|
|
120
|
+
|
|
121
|
+
let nextId = 0;
|
|
122
|
+
|
|
123
|
+
@Component({
|
|
124
|
+
selector: 'ui-smart-recommendation',
|
|
125
|
+
imports: [NgTemplateOutlet],
|
|
126
|
+
host: {
|
|
127
|
+
role: 'region',
|
|
128
|
+
'[attr.aria-label]': 'title()',
|
|
129
|
+
'[attr.title]': 'null',
|
|
130
|
+
'[attr.data-accepted]': 'accepted() || null',
|
|
131
|
+
'(document:pointerdown)': 'outside($event)',
|
|
132
|
+
style:
|
|
133
|
+
'--_t: var(--ui-look-tray, 0); --_f: calc(1 - var(--_t)); --_y: calc(1px * var(--_f)); --_ms: var(--ui-motion-scale); --_s: color-mix(in srgb, var(--ui-gray-1000) 6%, transparent); --_e: color-mix(in srgb, var(--ui-bg-muted) 88%, var(--ui-bg-surface)); --_r: var(--ui-smart-recommendation-radius, calc(10px + 6px * var(--_t))); --_hh: calc(44px - 4px * var(--_t)); --_bh: calc(28px + 4px * var(--_t)); --_ih: calc(32px + 2px * var(--_t)); --_ok: light-dark(oklch(from var(--ui-hue-green) calc(l - .14) c h), var(--ui-hue-green)); --_g3: light-dark(oklch(from var(--ui-hue-green) calc(l - .08) c h), oklch(from var(--ui-hue-green) calc(l - .1) c h)); --_pu: light-dark(oklch(from var(--ui-hue-purple) calc(l - .01) c h), var(--ui-hue-purple)); --_bl2: light-dark(oklch(from var(--ui-hue-blue) calc(l - .08) c h), oklch(from var(--ui-hue-blue) calc(l + .08) c h)); --_rb: light-dark(color-mix(in srgb, var(--ui-hue-blue) 10%, transparent), color-mix(in srgb, var(--ui-hue-blue) 20%, transparent)); --_rc: var(--_bl2); --_hw: light-dark(color-mix(in srgb, var(--ui-text) 3.5%, transparent), color-mix(in srgb, var(--ui-text) 6%, transparent)); --_hc: light-dark(color-mix(in srgb, var(--ui-text) 4%, transparent), color-mix(in srgb, var(--ui-text) 9%, transparent)); --_iw: light-dark(color-mix(in srgb, var(--ui-text) 4%, transparent), color-mix(in srgb, var(--ui-text) 7%, transparent)); --_ct: light-dark(color-mix(in srgb, var(--ui-hue-blue) 6%, transparent), color-mix(in srgb, var(--ui-hue-blue) 14%, transparent)); --_md: light-dark(color-mix(in srgb, var(--ui-gray-1000) 18%, transparent), color-mix(in srgb, var(--ui-gray-1000) 60%, transparent)); --_ic: light-dark(var(--ui-gray-500), color-mix(in srgb, var(--ui-gray-500), var(--ui-gray-600))); position: relative; display: block; box-sizing: border-box; inline-size: min(var(--ui-smart-recommendation-width, 440px), 100%); min-inline-size: 0; padding: calc(6px * var(--_t)); border-radius: calc(var(--_r) + 6px * var(--_t)); background: color-mix(in srgb, var(--ui-tray-bg) calc(100% * var(--_t)), var(--ui-bg-surface)); box-shadow: 0 0 0 calc(.5px * var(--_f)) var(--_e), 0 var(--_y) var(--_y) calc(-1 * var(--_y)) var(--_s), 0 var(--_y) calc(2 * var(--_y)) var(--_s); color: var(--ui-text); font: var(--ui-text-base) / calc(20px - 1.43px * var(--_t)) var(--ui-font-sans)',
|
|
134
|
+
},
|
|
135
|
+
template: `
|
|
136
|
+
@if (shown(); as o) {
|
|
137
|
+
<div [style]="s.card">
|
|
138
|
+
<button type="button" class="head" [attr.aria-expanded]="open()" [attr.aria-controls]="uid + '-b'" [style]="s.head" (click)="open.set(!open())">
|
|
139
|
+
<svg class="fl" viewBox="0 0 16 16" aria-hidden="true" [style]="s.spark"><path d="M6.4.7c.55 3.3 2.25 5 5.6 5.6-3.35.55-5.05 2.25-5.6 5.6C5.85 8.55 4.15 6.85.8 6.3 4.15 5.7 5.85 4 6.4.7zM12.2 9.3c.25 1.35.95 2.05 2.3 2.3-1.35.25-2.05.95-2.3 2.3-.25-1.35-.95-2.05-2.3-2.3 1.35-.25 2.05-.95 2.3-2.3z" /></svg>
|
|
140
|
+
<svg class="cl ui-icon" viewBox="0 0 16 16" aria-hidden="true" [style]="s.box"><path d="M8.5 3H4.5A1.5 1.5 0 0 0 3 4.5v7A1.5 1.5 0 0 0 4.5 13h7a1.5 1.5 0 0 0 1.5-1.5V7.5M12.5 1.5v4M10.5 3.5h4" /></svg>
|
|
141
|
+
<span dir="auto" [title]="title()" [style]="s.title">{{ title() }}</span>
|
|
142
|
+
@if (isAlt()) {
|
|
143
|
+
<span class="badge" [style]="s.alt" [style.animation]="live() ? 'ui-rise calc(300ms * var(--_f) * var(--_ms)) var(--ui-ease-out)' : null">{{ l().alternative }}</span>
|
|
144
|
+
}
|
|
145
|
+
<span class="chev" aria-hidden="true" [style]="s.chev" [style.rotate]="open() ? '0deg' : 'calc(90deg * var(--_dir))'">
|
|
146
|
+
<svg class="ui-icon" viewBox="0 0 16 16" [style]="s.chevIcon"><path d="M4.5 6.5L8 10l3.5-3.5" /></svg>
|
|
147
|
+
</span>
|
|
148
|
+
</button>
|
|
149
|
+
<div class="body" [id]="uid + '-b'" [inert]="!open()" [attr.data-closed]="open() ? null : ''" [style]="s.body" [style.grid-template-rows]="open() ? '1fr' : '0fr'">
|
|
150
|
+
<div [style]="s.clip">
|
|
151
|
+
@for (k of [o]; track k.id) {
|
|
152
|
+
<p class="reason" dir="auto" [style]="s.reason" [style.animation]="live() ? 'ui-rise calc(360ms * var(--_ms)) var(--ui-ease-out)' : null">
|
|
153
|
+
@if (reasonTpl(); as r) {
|
|
154
|
+
<ng-container *ngTemplateOutlet="r.template; context: { $implicit: k }" />
|
|
155
|
+
} @else {
|
|
156
|
+
{{ k.reason }}
|
|
157
|
+
}
|
|
158
|
+
</p>
|
|
159
|
+
}
|
|
160
|
+
</div>
|
|
161
|
+
</div>
|
|
162
|
+
</div>
|
|
163
|
+
<footer [style]="s.foot">
|
|
164
|
+
<span dir="auto" [title]="words(o.signal)" [style]="s.sig">
|
|
165
|
+
<ng-container *ngTemplateOutlet="bars; context: { $implicit: o.signal, menu: false }" />
|
|
166
|
+
{{ words(o.signal) }}
|
|
167
|
+
</span>
|
|
168
|
+
<div class="acts" [style]="s.acts">
|
|
169
|
+
@if (accepted()) {
|
|
170
|
+
<span class="ok" dir="auto" [title]="l().accepted(o.label)" [style]="s.ok" [style.animation]="live() ? 'ui-rise calc(300ms * var(--_ms)) var(--ui-ease-out)' : null">
|
|
171
|
+
<svg class="ui-icon" viewBox="0 0 16 16" aria-hidden="true" [style]="s.okIcon"><path d="M3.5 8.5l3 3 6-7" /></svg>
|
|
172
|
+
<span [style]="s.cut">{{ l().accepted(o.label) }}</span>
|
|
173
|
+
</span>
|
|
174
|
+
<button type="button" class="btn sec undo" [style]="s.secIcon" (click)="revert(o)">
|
|
175
|
+
<svg class="fl ui-icon" viewBox="0 0 16 16" aria-hidden="true" [style]="s.icon"><path d="M6 3.5L3 6.5l3 3M3.5 6.5h6a3.5 3.5 0 0 1 0 7H8" /></svg>
|
|
176
|
+
{{ l().undo }}
|
|
177
|
+
</button>
|
|
178
|
+
} @else {
|
|
179
|
+
@if (options().length > 1) {
|
|
180
|
+
<div class="anchor" [style]="s.anchor" (keydown)="onKey($event)">
|
|
181
|
+
<button type="button" class="btn sec trigger" [id]="uid + '-t'" aria-haspopup="menu" [attr.aria-expanded]="menuOpen()" [style]="s.secIcon" (click)="toggleMenu()">
|
|
182
|
+
<svg class="fl ui-icon" viewBox="0 0 16 16" aria-hidden="true" [style]="s.icon"><path d="M5 13V3M2.5 5.5L5 3l2.5 2.5M11 3v10M8.5 10.5L11 13l2.5-2.5" /></svg>
|
|
183
|
+
{{ l().alternatives }}
|
|
184
|
+
</button>
|
|
185
|
+
@if (menuOpen()) {
|
|
186
|
+
<div class="menu" role="menu" [attr.aria-labelledby]="uid + '-t'" [attr.data-below]="below() || null" [style]="s.menu" [style.inset-inline-end.px]="shift()" (focusout)="leave($event)">
|
|
187
|
+
@for (m of options(); track m.id; let i = $index) {
|
|
188
|
+
<button type="button" class="item" role="menuitemradio" [attr.aria-checked]="m.id === o.id" [tabindex]="m.id === o.id ? 0 : -1" [style]="s.item" (click)="pick(m)">
|
|
189
|
+
<span [style]="s.lab">
|
|
190
|
+
<span dir="auto" [title]="m.label" [style]="s.cut">{{ m.label }}</span>
|
|
191
|
+
@if (i === 0) {
|
|
192
|
+
<span class="badge" [style]="s.rec">{{ l().recommended }}</span>
|
|
193
|
+
}
|
|
194
|
+
</span>
|
|
195
|
+
<ng-container *ngTemplateOutlet="bars; context: { $implicit: m.signal, menu: true }" />
|
|
196
|
+
</button>
|
|
197
|
+
}
|
|
198
|
+
</div>
|
|
199
|
+
}
|
|
200
|
+
</div>
|
|
201
|
+
}
|
|
202
|
+
<button type="button" class="btn pri" [title]="l().accept(o.label)" [style]="s.pri" (click)="commit(o)">
|
|
203
|
+
<span [style]="s.cut">{{ l().accept(o.label) }}</span>
|
|
204
|
+
</button>
|
|
205
|
+
}
|
|
206
|
+
</div>
|
|
207
|
+
</footer>
|
|
208
|
+
<span class="ui-sr" aria-live="polite">{{ said() }}</span>
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
<ng-template #bars let-level let-menu="menu">
|
|
212
|
+
<span class="bars" [attr.data-level]="level" [attr.role]="menu ? 'img' : null" [attr.aria-label]="menu ? words(level) : null" [attr.aria-hidden]="menu ? null : 'true'" [title]="menu ? words(level) : ''" [style]="s.bars">
|
|
213
|
+
@for (h of heights; track $index) {
|
|
214
|
+
<span class="bar" [attr.data-on]="$index < (level || 1) ? '' : null" [style.block-size]="h"></span>
|
|
215
|
+
}
|
|
216
|
+
</span>
|
|
217
|
+
</ng-template>
|
|
218
|
+
`,
|
|
219
|
+
styles: `
|
|
220
|
+
:host { --_dir: -1; --_a: 100%; --_bc: transparent; --_bl: 2px; --_bt: 0px; --_cc: var(--_ic); --_fg: var(--ui-text); --_g: none; --_hb: transparent; --_hi: 12%; --_ib: transparent; --_ik: none; --_tb: light-dark(var(--ui-bg-surface), color-mix(in srgb, var(--ui-text) 5%, var(--ui-bg-surface))); }
|
|
221
|
+
:host(:dir(rtl)) { --_dir: 1; }
|
|
222
|
+
.cl { display: none; }
|
|
223
|
+
@container style(--ui-look-tray: 1) {
|
|
224
|
+
.cl { display: block; }
|
|
225
|
+
.fl { display: none; }
|
|
226
|
+
}
|
|
227
|
+
.body[data-closed] { visibility: hidden; transition-delay: 0s, calc(340ms * var(--_ms)); }
|
|
228
|
+
.reason ::ng-deep :is(strong, b) { color: var(--ui-text); font-weight: var(--ui-weight-medium); }
|
|
229
|
+
.bar { inline-size: 3px; border-radius: 1px; background: light-dark(var(--ui-border), var(--ui-border-strong)); transition: background-color calc(260ms * var(--_ms)); }
|
|
230
|
+
.menu .bar[data-on] { background: var(--_on); }
|
|
231
|
+
.bars { --_on: var(--ui-text-disabled); }
|
|
232
|
+
[data-level='1'] { --_on: var(--ui-hue-amber); }
|
|
233
|
+
[data-level='2'] { --_on: color-mix(in oklch, var(--ui-hue-amber) 70%, var(--ui-hue-green)); }
|
|
234
|
+
[data-level='3'] { --_on: var(--_g3); }
|
|
235
|
+
.menu { inset-block-end: calc(100% + 6px + 2px * var(--_t)); transform-origin: 50% 100%; --_my: 4px; animation: menu-in calc(180ms * var(--_ms)) var(--ui-ease-out); }
|
|
236
|
+
.menu[data-below] { inset-block: calc(100% + 6px + 2px * var(--_t)) auto; transform-origin: 50% 0; --_my: -4px; }
|
|
237
|
+
@keyframes menu-in { from { opacity: 0; transform: translateY(var(--_my)) scale(.98); } }
|
|
238
|
+
.item[aria-checked='true'] { --_ib: var(--_ct); --_ik: inset 0 0 0 calc(.5px * var(--_f)) color-mix(in srgb, var(--ui-hue-blue) 55%, transparent); }
|
|
239
|
+
@media (hover: hover) {
|
|
240
|
+
.head:hover { --_hb: var(--_hw); --_cc: var(--ui-icon); }
|
|
241
|
+
@container style(--ui-look-tray: 1) { .head:hover { --_hb: var(--_hc); --_cc: var(--_ic); } }
|
|
242
|
+
.item:hover:not([aria-checked='true']) { --_ib: var(--_iw); }
|
|
243
|
+
.pri:hover { --_a: 88%; --_hi: 20%; }
|
|
244
|
+
.sec:hover { --_bl: 3px; --_tb: light-dark(color-mix(in srgb, var(--ui-text) 4%, var(--ui-bg-surface)), color-mix(in srgb, var(--ui-text) 10%, var(--ui-bg-surface))); }
|
|
245
|
+
}
|
|
246
|
+
@media (pointer: fine) {
|
|
247
|
+
.btn:active { transform: scale(calc(.97 + .01 * var(--_t))); }
|
|
248
|
+
}
|
|
249
|
+
:is(.head, .item):focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset-inset); }
|
|
250
|
+
.btn:focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
|
|
251
|
+
@media (pointer: coarse) {
|
|
252
|
+
* { --_hh: calc(48px - 8px * var(--_t)); --_bh: calc(36px - 4px * var(--_t)); --_ih: calc(40px - 6px * var(--_t)); --_bt: calc(4px * var(--_f)); }
|
|
253
|
+
.btn::after { content: ''; position: absolute; inset: calc(-6px * var(--_t)); }
|
|
254
|
+
}
|
|
255
|
+
@media (forced-colors: active) {
|
|
256
|
+
:host { outline: calc(1px * var(--_f)) solid CanvasText; }
|
|
257
|
+
.btn { --_bc: ButtonText; }
|
|
258
|
+
.ok, .menu, .badge { outline: 1px solid CanvasText; }
|
|
259
|
+
.bar { forced-color-adjust: none; background: GrayText; }
|
|
260
|
+
.bars .bar[data-on] { background: CanvasText; }
|
|
261
|
+
.bars[data-level='0'] .bar { background: GrayText; }
|
|
262
|
+
.item[aria-checked='true'] { outline: 1px solid Highlight; outline-offset: -1px; }
|
|
263
|
+
}
|
|
264
|
+
`,
|
|
265
|
+
})
|
|
266
|
+
export class UiSmartRecommendation {
|
|
267
|
+
/** The first is the recommendation, the rest are alternatives. */
|
|
268
|
+
readonly options = input.required<readonly Recommendation[]>();
|
|
269
|
+
/** The card heading, and the name of its region. */
|
|
270
|
+
readonly title = input('Smart recommendation');
|
|
271
|
+
/** Every visible and spoken string. */
|
|
272
|
+
readonly labels = input<Partial<SmartRecommendationLabels>>({});
|
|
273
|
+
/** Whether the reason shows. The header folds and unfolds it. */
|
|
274
|
+
readonly open = model(true);
|
|
275
|
+
/** The id of the option shown. Unset, or an id not in `options`, shows the first. */
|
|
276
|
+
readonly selected = model<string | undefined>(undefined);
|
|
277
|
+
/** True while the shown option is accepted: the status and Undo replace the buttons. */
|
|
278
|
+
readonly accepted = model(false);
|
|
279
|
+
/** The option, when Accept is pressed. */
|
|
280
|
+
readonly accept = output<Recommendation>();
|
|
281
|
+
/** The option, when its accept is undone. */
|
|
282
|
+
readonly undo = output<Recommendation>();
|
|
283
|
+
|
|
284
|
+
protected readonly s = S;
|
|
285
|
+
protected readonly heights = [0, 3, 6].map((n) => `calc(${5 + n}px + 1px * var(--_t))`);
|
|
286
|
+
protected readonly uid = `ui-smart-recommendation-${nextId++}`;
|
|
287
|
+
protected readonly l = computed(() => ({ ...LABELS, ...this.labels() }));
|
|
288
|
+
protected readonly reasonTpl = contentChild(UiRecommendationReason);
|
|
289
|
+
protected readonly shown = computed(() => {
|
|
290
|
+
const list = this.options();
|
|
291
|
+
return list.find((o) => o.id === this.selected()) ?? list[0];
|
|
292
|
+
});
|
|
293
|
+
protected readonly isAlt = computed(() => this.shown() !== this.options()[0]);
|
|
294
|
+
protected readonly menuOpen = signal(false);
|
|
295
|
+
protected readonly shift = signal(0);
|
|
296
|
+
protected readonly below = signal(false);
|
|
297
|
+
protected readonly live = signal(false);
|
|
298
|
+
protected readonly said = signal('');
|
|
299
|
+
|
|
300
|
+
private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
|
|
301
|
+
private readonly doc = inject(DOCUMENT);
|
|
302
|
+
private readonly injector = inject(Injector);
|
|
303
|
+
|
|
304
|
+
constructor() {
|
|
305
|
+
if (isDevMode()) {
|
|
306
|
+
effect(() => {
|
|
307
|
+
const list = this.options();
|
|
308
|
+
if (!list.length) console.warn('ui-smart-recommendation: pass at least one option; the card renders nothing without one.', this.el);
|
|
309
|
+
if (new Set(list.map((o) => o.id)).size !== list.length) console.warn('ui-smart-recommendation: every option needs its own id.', this.el);
|
|
310
|
+
if (!this.title().trim()) console.warn('ui-smart-recommendation: give it a title; it names the region and the header button.', this.el);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
protected words(level: number) {
|
|
316
|
+
return this.l().signal[Math.max(0, Math.min(3, level))];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
protected toggleMenu(at: 'checked' | 'first' | 'last' = 'checked') {
|
|
320
|
+
if (this.menuOpen()) return this.closeMenu(true);
|
|
321
|
+
this.shift.set(0);
|
|
322
|
+
this.below.set(false);
|
|
323
|
+
this.menuOpen.set(true);
|
|
324
|
+
afterNextRender(
|
|
325
|
+
() => {
|
|
326
|
+
const menu = this.menu();
|
|
327
|
+
if (!menu) return;
|
|
328
|
+
const trigger = menu.parentElement!;
|
|
329
|
+
const foot = this.el.querySelector('footer')!;
|
|
330
|
+
const view = this.doc.defaultView!;
|
|
331
|
+
const rtl = view.getComputedStyle(this.el).direction === 'rtl';
|
|
332
|
+
const t = trigger.getBoundingClientRect();
|
|
333
|
+
if (rtl ? t.left + menu.offsetWidth > this.doc.documentElement.clientWidth : t.right - menu.offsetWidth < 0) {
|
|
334
|
+
const f = foot.getBoundingClientRect();
|
|
335
|
+
const cs = view.getComputedStyle(foot);
|
|
336
|
+
this.shift.set(-Math.round(rtl ? t.left - f.left - parseFloat(cs.paddingLeft) : f.right - parseFloat(cs.paddingRight) - t.right));
|
|
337
|
+
}
|
|
338
|
+
if (t.top - menu.offsetHeight - 8 < 0) this.below.set(true);
|
|
339
|
+
const items = this.items();
|
|
340
|
+
const i = at === 'first' ? 0 : at === 'last' ? items.length - 1 : items.findIndex((b) => b.tabIndex === 0);
|
|
341
|
+
items[Math.max(0, i)]?.focus();
|
|
342
|
+
},
|
|
343
|
+
{ injector: this.injector },
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
protected onKey(e: KeyboardEvent) {
|
|
348
|
+
const items = this.items();
|
|
349
|
+
const i = items.indexOf(this.doc.activeElement as HTMLElement);
|
|
350
|
+
if (e.key === 'Escape' && this.menuOpen()) {
|
|
351
|
+
e.preventDefault();
|
|
352
|
+
this.closeMenu(true);
|
|
353
|
+
} else if (e.key === 'Tab') {
|
|
354
|
+
this.menuOpen.set(false);
|
|
355
|
+
} else if (i < 0 && !this.menuOpen() && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
356
|
+
e.preventDefault();
|
|
357
|
+
this.toggleMenu(e.key === 'ArrowDown' ? 'first' : 'last');
|
|
358
|
+
} else if (i >= 0) {
|
|
359
|
+
const n = items.length;
|
|
360
|
+
const next = { ArrowDown: i + 1, ArrowUp: i - 1 + n, Home: 0, End: n - 1 }[e.key];
|
|
361
|
+
if (next === undefined) return;
|
|
362
|
+
e.preventDefault();
|
|
363
|
+
items[next % n].focus();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
protected leave(e: FocusEvent) {
|
|
368
|
+
const to = e.relatedTarget as Node | null;
|
|
369
|
+
if (to && !this.menu()?.parentElement?.contains(to)) this.menuOpen.set(false);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
protected outside(e: Event) {
|
|
373
|
+
if (this.menuOpen() && !this.menu()?.parentElement?.contains(e.target as Node)) this.menuOpen.set(false);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
protected pick(o: Recommendation) {
|
|
377
|
+
if (o !== this.shown()) {
|
|
378
|
+
this.live.set(true);
|
|
379
|
+
this.selected.set(o.id);
|
|
380
|
+
}
|
|
381
|
+
this.closeMenu(true);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
protected commit(o: Recommendation) {
|
|
385
|
+
this.live.set(true);
|
|
386
|
+
this.accepted.set(true);
|
|
387
|
+
this.said.set(this.l().accepted(o.label));
|
|
388
|
+
this.accept.emit(o);
|
|
389
|
+
this.focusLater('.undo');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
protected revert(o: Recommendation) {
|
|
393
|
+
this.accepted.set(false);
|
|
394
|
+
this.said.set('');
|
|
395
|
+
this.undo.emit(o);
|
|
396
|
+
this.focusLater('.pri');
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
private closeMenu(refocus: boolean) {
|
|
400
|
+
this.menuOpen.set(false);
|
|
401
|
+
if (refocus) this.el.querySelector<HTMLElement>('.trigger')?.focus();
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
private focusLater(sel: string) {
|
|
405
|
+
afterNextRender(() => this.el.querySelector<HTMLElement>(sel)?.focus(), { injector: this.injector });
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private menu() {
|
|
409
|
+
return this.el.querySelector<HTMLElement>('.menu');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
private items() {
|
|
413
|
+
return [...this.el.querySelectorAll<HTMLElement>('.item')];
|
|
414
|
+
}
|
|
415
|
+
}
|