@adia-ai/web-components 0.0.27 → 0.0.28

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.
@@ -0,0 +1,276 @@
1
+ /**
2
+ * <feed-ui> + <feed-item-ui> — Shared top-layer feed channel.
3
+ *
4
+ * Per docs/specs/feed-channel.md (SPEC-FEED-CHANNEL-001).
5
+ *
6
+ * Phase 1 (skeleton) ships here:
7
+ * - Per-position singletons (`<feed-ui position="bottom-right">`)
8
+ * resolving OD-FEED-1; mounted lazily into document.body via
9
+ * Popover API for top-layer placement.
10
+ * - Auto-fade `<feed-item-ui>` policy. Sticky-dismissible and
11
+ * action-required policies are stubs that respect the contract
12
+ * (no auto-dismiss when `duration` is null/0; close button
13
+ * surfaces when `dismissible` is set) but the action-required
14
+ * focus-trap is deferred.
15
+ * - Static API: AdiaFeed.post() / .get() / .clear() / .purge()
16
+ * — `purge()` directly addresses the audit's L-B4 container-leak
17
+ * finding by giving consumers a tear-down hook.
18
+ * - Global event channel: window.dispatchEvent(new CustomEvent(
19
+ * 'feed', { detail: { text, position, ... } })) — same shape as
20
+ * AdiaFeed.post() so any code can post without importing the
21
+ * module directly. Idempotent listener install (HMR-safe).
22
+ *
23
+ * Toast migration (spec § 2.5) is deferred — toast-ui keeps its own
24
+ * container + API for now; the migration happens once the feed-ui
25
+ * surface has soaked.
26
+ */
27
+
28
+ import { AdiaElement, html } from '../../core/element.js';
29
+
30
+ /* ── Container — one per position, mounted into document.body ── */
31
+
32
+ class AdiaFeedContainer extends AdiaElement {
33
+ static properties = {
34
+ position: { type: String, default: 'bottom-right', reflect: true },
35
+ max: { type: Number, default: 5, reflect: true },
36
+ };
37
+ static template = () => html``;
38
+
39
+ connected() {
40
+ if (!this.hasAttribute('role')) this.setAttribute('role', 'region');
41
+ if (!this.hasAttribute('aria-label')) this.setAttribute('aria-label', 'Feed');
42
+ }
43
+ }
44
+
45
+ customElements.define('feed-ui', AdiaFeedContainer);
46
+
47
+ /* ── Item — one per posted message ── */
48
+
49
+ class AdiaFeedItem extends AdiaElement {
50
+ #timer = null;
51
+ #removing = false;
52
+ #closeTimer = null;
53
+ #openRaf = null;
54
+
55
+ static properties = {
56
+ text: { type: String, default: '', reflect: true },
57
+ heading: { type: String, default: '', reflect: true },
58
+ icon: { type: String, default: '', reflect: true },
59
+ variant: { type: String, default: 'default', reflect: true },
60
+ duration: { type: Number, default: 4000, reflect: true },
61
+ dismissible: { type: Boolean, default: false, reflect: true },
62
+ };
63
+
64
+ static parts = {
65
+ body: '<div slot="body"></div>',
66
+ };
67
+ static template = () => html``;
68
+
69
+ #onPress = (e) => {
70
+ if (e.target.closest('[data-feed-close]')) this.dismiss();
71
+ };
72
+
73
+ connected() {
74
+ this.addEventListener('press', this.#onPress);
75
+ }
76
+
77
+ #getDuration() {
78
+ const raw = getComputedStyle(this).getPropertyValue('--feed-item-duration').trim();
79
+ return parseFloat(raw) || 200;
80
+ }
81
+
82
+ render() {
83
+ const body = this.ensure('body');
84
+ body.textContent = '';
85
+ if (this.heading) {
86
+ const h = document.createElement('strong');
87
+ h.textContent = this.heading;
88
+ h.style.display = 'block';
89
+ body.appendChild(h);
90
+ }
91
+ if (this.text) {
92
+ const t = document.createElement('span');
93
+ t.textContent = this.text;
94
+ body.appendChild(t);
95
+ }
96
+
97
+ /* Inferred policy roles per spec §2.4. Auto-fade gets `status`;
98
+ sticky danger/warning gets `alert`; action-required gets
99
+ `alertdialog` (a future phase wires the focus trap). */
100
+ const isSticky = !this.duration || this.duration <= 0;
101
+ const isLoud = this.variant === 'danger' || this.variant === 'warning';
102
+ let role = 'status';
103
+ if (isSticky && isLoud) role = 'alert';
104
+ this.setAttribute('role', role);
105
+ this.setAttribute('aria-live', role === 'alert' ? 'assertive' : 'polite');
106
+
107
+ /* Render dismiss button for sticky items (spec §2.2 — default true
108
+ for sticky, false for auto-fade). */
109
+ const wantsClose = this.dismissible || isSticky;
110
+ let close = this.querySelector(':scope > [data-feed-close]');
111
+ if (wantsClose && !close) {
112
+ close = document.createElement('button-ui');
113
+ close.setAttribute('data-feed-close', '');
114
+ close.setAttribute('icon', 'x');
115
+ close.setAttribute('variant', 'ghost');
116
+ close.setAttribute('size', 'sm');
117
+ close.setAttribute('aria-label', 'Dismiss');
118
+ this.appendChild(close);
119
+ } else if (!wantsClose && close) {
120
+ close.remove();
121
+ }
122
+
123
+ if (!this.hasAttribute('data-open') && !this.#removing) {
124
+ this.#openRaf = requestAnimationFrame(() => {
125
+ this.#openRaf = null;
126
+ this.setAttribute('data-open', '');
127
+ });
128
+ }
129
+ this.#scheduleAutoDismiss();
130
+ }
131
+
132
+ #scheduleAutoDismiss() {
133
+ if (this.#timer) clearTimeout(this.#timer);
134
+ if (this.duration && this.duration > 0) {
135
+ this.#timer = setTimeout(() => this.dismiss(), this.duration);
136
+ }
137
+ }
138
+
139
+ dismiss() {
140
+ if (this.#removing) return;
141
+ this.#removing = true;
142
+ if (this.#timer) { clearTimeout(this.#timer); this.#timer = null; }
143
+ this.removeAttribute('data-open');
144
+ this.setAttribute('data-closing', '');
145
+ this.#closeTimer = setTimeout(() => {
146
+ this.#closeTimer = null;
147
+ const container = this.parentElement;
148
+ this.dispatchEvent(new Event('close', { bubbles: true }));
149
+ this.remove();
150
+ if (container?.matches?.('feed-ui')) AdiaFeed.releaseContainerIfEmpty(container);
151
+ }, this.#getDuration());
152
+ }
153
+
154
+ update(patch) {
155
+ if (!patch || typeof patch !== 'object') return;
156
+ for (const k of Object.keys(patch)) {
157
+ if (k in AdiaFeedItem.properties) this[k] = patch[k];
158
+ }
159
+ this.#scheduleAutoDismiss();
160
+ }
161
+
162
+ disconnected() {
163
+ this.removeEventListener('press', this.#onPress);
164
+ if (this.#timer) { clearTimeout(this.#timer); this.#timer = null; }
165
+ if (this.#closeTimer) { clearTimeout(this.#closeTimer); this.#closeTimer = null; }
166
+ if (this.#openRaf != null) { cancelAnimationFrame(this.#openRaf); this.#openRaf = null; }
167
+ }
168
+ }
169
+
170
+ customElements.define('feed-item-ui', AdiaFeedItem);
171
+
172
+ /* ── Static API — AdiaFeed ── */
173
+
174
+ class AdiaFeed {
175
+ static #containers = new Map();
176
+
177
+ /**
178
+ * Get (or lazily create) the per-position lane. Each lane is a
179
+ * manual Popover-API container — `[popover="manual"]` puts it in
180
+ * the browser's top-layer with no z-index wars and the native
181
+ * popover stack lets multiple lanes coexist (e.g. one feed in
182
+ * top-right + another in bottom-center) without collision.
183
+ */
184
+ static get(position = 'bottom-right') {
185
+ let el = AdiaFeed.#containers.get(position);
186
+ if (el && el.isConnected) return el;
187
+ el = document.createElement('feed-ui');
188
+ el.setAttribute('position', position);
189
+ if ('popover' in HTMLElement.prototype) el.setAttribute('popover', 'manual');
190
+ document.body.appendChild(el);
191
+ try { el.showPopover?.(); } catch { /* graceful fallback */ }
192
+ AdiaFeed.#containers.set(position, el);
193
+ return el;
194
+ }
195
+
196
+ /**
197
+ * Post a feed item. Returns a `FeedHandle` (`{id, dismiss, update}`).
198
+ *
199
+ * @param {Object} opts
200
+ * @param {string} [opts.text]
201
+ * @param {string} [opts.heading]
202
+ * @param {string} [opts.icon]
203
+ * @param {string} [opts.variant='default'] default | info | success | warning | danger
204
+ * @param {number|null} [opts.duration=4000] ms; null/0 = sticky (requires close click)
205
+ * @param {string} [opts.position='bottom-right']
206
+ * @param {boolean} [opts.dismissible] override default (true for sticky, false for auto)
207
+ * @param {string} [opts.id]
208
+ */
209
+ static post(opts = {}) {
210
+ const {
211
+ text = '',
212
+ heading = '',
213
+ icon = '',
214
+ variant = 'default',
215
+ duration = 4000,
216
+ position = 'bottom-right',
217
+ dismissible,
218
+ id,
219
+ } = opts;
220
+ const v = variant === 'error' ? 'danger' : variant; // documented alias
221
+ const container = AdiaFeed.get(position);
222
+ const item = document.createElement('feed-item-ui');
223
+ if (id) item.setAttribute('data-id', id);
224
+ item.text = text;
225
+ item.heading = heading;
226
+ item.icon = icon;
227
+ item.variant = v;
228
+ item.duration = duration;
229
+ if (dismissible != null) item.dismissible = !!dismissible;
230
+ container.appendChild(item);
231
+ return {
232
+ id: id ?? null,
233
+ dismiss: () => item.dismiss(),
234
+ update: (patch) => item.update(patch),
235
+ };
236
+ }
237
+
238
+ /** Clear all items in a single lane. */
239
+ static clear(position = 'bottom-right') {
240
+ const el = AdiaFeed.#containers.get(position);
241
+ if (!el) return;
242
+ for (const item of [...el.children]) {
243
+ if (item.tagName === 'FEED-ITEM-UI') item.dismiss?.();
244
+ }
245
+ }
246
+
247
+ /** Tear down ALL containers. Test cleanup; addresses audit L-B4. */
248
+ static purge() {
249
+ for (const el of AdiaFeed.#containers.values()) {
250
+ try { el.hidePopover?.(); } catch { /* noop */ }
251
+ el.remove();
252
+ }
253
+ AdiaFeed.#containers.clear();
254
+ }
255
+
256
+ /** Internal: drop a lane when its last item exits. */
257
+ static releaseContainerIfEmpty(container) {
258
+ if (!container || container.children.length > 0) return;
259
+ try { container.hidePopover?.(); } catch { /* noop */ }
260
+ container.remove();
261
+ for (const [pos, el] of AdiaFeed.#containers) {
262
+ if (el === container) AdiaFeed.#containers.delete(pos);
263
+ }
264
+ }
265
+ }
266
+
267
+ /* Global 'feed' CustomEvent listener — same shape as AdiaFeed.post().
268
+ Idempotent (HMR-safe via window flag). */
269
+ if (typeof window !== 'undefined' && !window.__adiaFeedListenerInstalled) {
270
+ window.__adiaFeedListenerInstalled = true;
271
+ window.addEventListener('feed', (e) => {
272
+ if (e?.detail && typeof e.detail === 'object') AdiaFeed.post(e.detail);
273
+ });
274
+ }
275
+
276
+ export { AdiaFeedContainer, AdiaFeedItem, AdiaFeed };
@@ -0,0 +1,33 @@
1
+ # Generated alongside feed.js — kept in sync by hand for now (feed
2
+ # is a Phase-1 skeleton; once it stabilizes, run the regen pipeline).
3
+ $schema: ../../../../scripts/schemas/component.yaml.schema.json
4
+ name: AdiaFeedContainer
5
+ tag: feed-ui
6
+ component: Feed
7
+ category: container
8
+ version: 1
9
+ description: >-
10
+ Shared top-layer feed channel. Per docs/specs/feed-channel.md
11
+ (SPEC-FEED-CHANNEL-001). Per-position singletons mounted lazily into
12
+ document.body via Popover API; consumers post via the static API
13
+ (`AdiaFeed.post()`) or the global 'feed' CustomEvent.
14
+ props:
15
+ position:
16
+ description: Lane the feed renders into
17
+ type: string
18
+ default: bottom-right
19
+ enum:
20
+ - top-left
21
+ - top-center
22
+ - top-right
23
+ - bottom-left
24
+ - bottom-center
25
+ - bottom-right
26
+ - inline
27
+ max:
28
+ description: Cap on simultaneously visible items per lane
29
+ type: number
30
+ default: 5
31
+ events: {}
32
+ slots: {}
33
+ states: {}
@@ -30,6 +30,7 @@ export { AdiaChat } from './chat/chat.js';
30
30
  export { AdiaDrawer } from './drawer/drawer.js';
31
31
  export { AdiaModal } from './modal/modal.js';
32
32
  export { AdiaToast } from './toast/toast.js';
33
+ export { AdiaFeedContainer, AdiaFeedItem, AdiaFeed } from './feed/feed.js';
33
34
  export { AdiaTabs } from './tabs/tabs.js';
34
35
  export { AdiaTab } from './tabs/tab.js';
35
36
  export { AdiaTooltip } from './tooltip/tooltip.js';
@@ -51,6 +52,7 @@ export { AdiaSkeleton } from './skeleton/skeleton.js';
51
52
  export { AdiaAlert } from './alert/alert.js';
52
53
  export { AdiaKbd } from './kbd/kbd.js';
53
54
  export { AdiaTag } from './tag/tag.js';
55
+ export { AdiaSwatch } from './swatch/swatch.js';
54
56
  export { AdiaCol } from './col/col.js';
55
57
  export { AdiaField } from './field/field.js';
56
58
  export { AdiaRow } from './row/row.js';
@@ -0,0 +1,116 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://adiaui.dev/a2ui/v0_9/components/Swatch.json",
4
+ "title": "Swatch",
5
+ "description": "Color/style swatch primitive — a small inline tile representing a single\nvisual sample. Renders as block, dot, square, line, or dashed depending on\nshape; pairs with an optional label slot or attribute. Composed by chart-\nlegend-ui's per-row swatch and consumed directly by the token-colors /\nspacing demo pages.\n",
6
+ "type": "object",
7
+ "allOf": [
8
+ {
9
+ "$ref": "common_types.json#/$defs/ComponentCommon"
10
+ },
11
+ {
12
+ "$ref": "common_types.json#/$defs/CatalogComponentCommon"
13
+ }
14
+ ],
15
+ "properties": {
16
+ "color": {
17
+ "description": "Swatch color. Accepts any CSS color value or var() reference. Sets the internal --swatch-color custom property; consumers can also set --swatch-color via inline style.",
18
+ "type": "string",
19
+ "default": ""
20
+ },
21
+ "component": {
22
+ "const": "Swatch"
23
+ },
24
+ "label": {
25
+ "description": "Optional label rendered next to (or below, for shape=\"block\") the swatch. Use the default slot for richer content.",
26
+ "type": "string",
27
+ "default": ""
28
+ },
29
+ "shape": {
30
+ "description": "Visual shape — block (filled tile), dot (small circle), square (small filled square), line (solid hairline), dashed (dashed hairline).",
31
+ "type": "string",
32
+ "enum": [
33
+ "block",
34
+ "dot",
35
+ "square",
36
+ "line",
37
+ "dashed"
38
+ ],
39
+ "default": "square"
40
+ },
41
+ "size": {
42
+ "description": "Size preset — sm / md / lg. Drives the swatch's intrinsic dimensions; lg is reserved for the token-scale demo (40 px tall).",
43
+ "type": "string",
44
+ "enum": [
45
+ "sm",
46
+ "md",
47
+ "lg"
48
+ ],
49
+ "default": "md"
50
+ }
51
+ },
52
+ "required": [
53
+ "component"
54
+ ],
55
+ "unevaluatedProperties": false,
56
+ "x-adiaui": {
57
+ "anti_patterns": [],
58
+ "category": "display",
59
+ "events": {},
60
+ "examples": [
61
+ {
62
+ "description": "A single dot swatch with text label, e.g. as a chart-legend row.",
63
+ "a2ui": "[\n {\n \"id\": \"root\",\n \"component\": \"Swatch\",\n \"shape\": \"dot\",\n \"color\": \"var(--a-data-0)\",\n \"label\": \"Revenue\"\n }\n]",
64
+ "name": "basic-swatch"
65
+ },
66
+ {
67
+ "description": "Block swatch for a token-scale step.",
68
+ "a2ui": "[\n {\n \"id\": \"root\",\n \"component\": \"Swatch\",\n \"shape\": \"block\",\n \"size\": \"lg\",\n \"color\": \"var(--a-neutral-50)\",\n \"label\": \"50\"\n }\n]",
69
+ "name": "token-block"
70
+ }
71
+ ],
72
+ "keywords": [
73
+ "swatch",
74
+ "color",
75
+ "chip",
76
+ "sample",
77
+ "palette",
78
+ "legend",
79
+ "step",
80
+ "token"
81
+ ],
82
+ "name": "AdiaSwatch",
83
+ "related": [
84
+ "chart-legend-ui",
85
+ "tag-ui"
86
+ ],
87
+ "slots": {
88
+ "default": {
89
+ "description": "Optional rich label content. When present, replaces the [label] attribute's text."
90
+ }
91
+ },
92
+ "states": [
93
+ {
94
+ "description": "Default, ready for display.",
95
+ "name": "idle"
96
+ }
97
+ ],
98
+ "synonyms": {
99
+ "color-chip": [
100
+ "swatch",
101
+ "chip"
102
+ ],
103
+ "color-sample": [
104
+ "swatch"
105
+ ]
106
+ },
107
+ "tag": "swatch-ui",
108
+ "tokens": {
109
+ "--swatch-color": {
110
+ "description": "Resolved color of the swatch fill / line. Wins over the [color] attr if set inline."
111
+ }
112
+ },
113
+ "traits": [],
114
+ "version": 1
115
+ }
116
+ }
@@ -0,0 +1,141 @@
1
+ @scope (swatch-ui) {
2
+ :where(:scope) {
3
+ /* ── Layout ── */
4
+ --swatch-gap: var(--a-space-2);
5
+
6
+ /* ── Sizes ──
7
+ The "tile" is the coloured shape; the label is plain text rendered
8
+ beside (or below, for shape="block") the tile. Tile dimensions are
9
+ size-driven; line shapes use a fixed thickness regardless of size. */
10
+ --swatch-tile-sm: var(--a-space-2); /* 8px ~ */
11
+ --swatch-tile-md: var(--a-space-2-5); /* 10px ~ */
12
+ --swatch-tile-lg: 40px; /* token-scale block */
13
+ --swatch-line-thickness: 2px;
14
+ --swatch-line-length: calc(var(--swatch-tile-md) * 1.75);
15
+
16
+ /* ── Colors ──
17
+ Default colour falls back through the data palette so a bare
18
+ <swatch-ui> still renders something visible; consumers usually set
19
+ --swatch-color via [color] attr or inline style. */
20
+ --swatch-color: var(--a-data-0);
21
+ --swatch-label-fg: var(--a-fg-subtle);
22
+
23
+ /* ── Shape ── */
24
+ --swatch-radius-block: var(--a-radius-sm);
25
+ --swatch-radius-square: var(--a-radius-sm);
26
+
27
+ /* ── Block-shape extras (token-scale) ── */
28
+ --swatch-block-shadow: inset 0 0 0 1px color-mix(in oklab, currentColor 8%, transparent);
29
+
30
+ /* ── Typography ── */
31
+ --swatch-label-size: var(--a-ui-size);
32
+ --swatch-label-step-size: 10px;
33
+ --swatch-label-step-mt: 4px;
34
+ }
35
+
36
+ :scope {
37
+ /* Default: inline-flex row — tile + label side-by-side, vertically
38
+ centered. shape="block" overrides to flex-column (label sits below
39
+ the tile, mimicking the token-scale step layout). */
40
+ box-sizing: border-box;
41
+ display: inline-flex;
42
+ align-items: center;
43
+ gap: var(--swatch-gap);
44
+ min-width: 0;
45
+ color: var(--swatch-label-fg);
46
+ font-size: var(--swatch-label-size);
47
+ line-height: 1.2;
48
+ vertical-align: baseline;
49
+ }
50
+
51
+ /* The colored tile — sized by [size] + [shape] combinations below. */
52
+ :scope > [data-tile] {
53
+ box-sizing: border-box;
54
+ display: block;
55
+ flex-shrink: 0;
56
+ background: var(--swatch-color);
57
+ line-height: 0;
58
+ }
59
+
60
+ :scope > [data-label] {
61
+ min-width: 0;
62
+ overflow: hidden;
63
+ text-overflow: ellipsis;
64
+ white-space: nowrap;
65
+ }
66
+
67
+ :scope > [data-label][hidden] { display: none; }
68
+
69
+ /* ═══════ Shape: dot ═══════ */
70
+ :scope[shape="dot"] > [data-tile] {
71
+ width: var(--_size, var(--swatch-tile-md));
72
+ height: var(--_size, var(--swatch-tile-md));
73
+ border-radius: 50%;
74
+ }
75
+
76
+ /* ═══════ Shape: square ═══════ */
77
+ :scope[shape="square"] > [data-tile] {
78
+ width: var(--_size, var(--swatch-tile-md));
79
+ height: var(--_size, var(--swatch-tile-md));
80
+ border-radius: var(--swatch-radius-square);
81
+ }
82
+
83
+ /* ═══════ Shape: line (solid hairline) ═══════ */
84
+ :scope[shape="line"] > [data-tile] {
85
+ width: var(--swatch-line-length);
86
+ height: var(--swatch-line-thickness);
87
+ border-radius: var(--swatch-line-thickness);
88
+ }
89
+
90
+ /* ═══════ Shape: dashed (dashed hairline) ═══════ */
91
+ :scope[shape="dashed"] > [data-tile] {
92
+ width: var(--swatch-line-length);
93
+ height: var(--swatch-line-thickness);
94
+ background: transparent;
95
+ border-top: var(--swatch-line-thickness) dashed var(--swatch-color);
96
+ }
97
+
98
+ /* ═══════ Shape: block (token-scale tile) ═══════
99
+ Filled rectangle whose height grows with [size]; label sits BELOW
100
+ the tile (flex-column) so we can use it as a token-step swatch
101
+ without any extra wrapper. */
102
+ :scope[shape="block"] {
103
+ flex-direction: column;
104
+ align-items: stretch;
105
+ gap: 0;
106
+ }
107
+
108
+ :scope[shape="block"] > [data-tile] {
109
+ width: 100%;
110
+ height: var(--_block-h, var(--swatch-tile-lg));
111
+ border-radius: var(--swatch-radius-block);
112
+ box-shadow: var(--swatch-block-shadow);
113
+ }
114
+
115
+ :scope[shape="block"] > [data-label] {
116
+ text-align: center;
117
+ font-family: var(--a-font-family-code);
118
+ font-size: var(--swatch-label-step-size);
119
+ font-variant-numeric: tabular-nums;
120
+ color: var(--a-fg-muted);
121
+ margin-block-start: var(--swatch-label-step-mt);
122
+ white-space: nowrap;
123
+ }
124
+
125
+ /* ═══════ Sizes ═══════
126
+ Override the dot/square diameter (`--_size`) and the block-shape
127
+ height (`--_block-h`) for each size preset. Line/dashed widths use
128
+ --swatch-line-length so they stay consistent at all sizes. */
129
+ :scope[size="sm"] {
130
+ --_size: var(--swatch-tile-sm);
131
+ --_block-h: calc(var(--swatch-tile-lg) * 0.6);
132
+ }
133
+ :scope[size="md"] {
134
+ --_size: var(--swatch-tile-md);
135
+ --_block-h: calc(var(--swatch-tile-lg) * 0.75);
136
+ }
137
+ :scope[size="lg"] {
138
+ --_size: calc(var(--swatch-tile-md) * 1.4);
139
+ --_block-h: var(--swatch-tile-lg);
140
+ }
141
+ }