@joewinke/jatui 0.1.19 → 0.1.21

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,520 @@
1
+ <script lang="ts" generics="L extends { id: string | number }, R extends { id: string | number }">
2
+ /**
3
+ * LinkedColumns — generic two-column ink-up ribbon primitive.
4
+ *
5
+ * Renders two lists (LEFT = "many", RIGHT = "anchors") connected by bezier
6
+ * ribbons that ink up when active. The many-to-one mapping is declared by the
7
+ * caller via `links`: each link says which right item owns which range of left
8
+ * items. Ribbons animate on hover and on scroll (scroll-center activation).
9
+ *
10
+ * Extracted from SessionNavigator (jat-jj79f.7) so any coverage/traceability
11
+ * view — PRD↔tasks, files↔agents, plan sections↔work — can reuse the primitive
12
+ * without coupling to transcript/signal domain types.
13
+ *
14
+ * ## Props
15
+ * leftItems — ordered list of "many" items
16
+ * rightItems — ordered list of "anchor" items (one per group)
17
+ * links — for each right item: which leftItem id range it covers
18
+ * leftKey — fn that returns the data-left-id attribute value for a left item
19
+ * rightKey — fn that returns the data-right-id value for a right item
20
+ * accentColor — fn that returns an oklch color string for a right item (ribbon color)
21
+ * activeId — currently highlighted right item id (controlled); null = auto from scroll
22
+ * onActiveChange — called when hover/scroll changes the active right item
23
+ * compact — reduce outer padding (for embedding in panels)
24
+ *
25
+ * ## Slots / snippets
26
+ * leftItem — renders each left item; receives `{ item, isActive }` context
27
+ * rightItem — renders each right item; receives `{ item, isActive }` context
28
+ * leftHeader — optional column heading (default "Left")
29
+ * rightHeader — optional column heading (default "Right")
30
+ * empty — shown in right column when rightItems is empty
31
+ *
32
+ * ## Usage
33
+ * ```svelte
34
+ * <LinkedColumns
35
+ * {leftItems}
36
+ * {rightItems}
37
+ * {links}
38
+ * leftKey={i => i.id}
39
+ * rightKey={r => r.id}
40
+ * accentColor={r => r.color}
41
+ * >
42
+ * {#snippet leftItem({ item, isActive })}
43
+ * <div class:active={isActive}>{item.text}</div>
44
+ * {/snippet}
45
+ * {#snippet rightItem({ item, isActive })}
46
+ * <div class:active={isActive}>{item.title}</div>
47
+ * {/snippet}
48
+ * </LinkedColumns>
49
+ * ```
50
+ */
51
+
52
+ import type { Snippet } from 'svelte';
53
+
54
+ /** Describes which left items a single right (anchor) item covers. */
55
+ export interface ColumnLink {
56
+ /** The right item's id this link belongs to. */
57
+ rightId: string | number;
58
+ /** First left item id in the covered range (inclusive). */
59
+ leftStartId: string | number;
60
+ /** Last left item id in the covered range (inclusive). */
61
+ leftEndId: string | number;
62
+ }
63
+
64
+ let {
65
+ leftItems,
66
+ rightItems,
67
+ links,
68
+ leftKey,
69
+ rightKey,
70
+ accentColor,
71
+ activeId = $bindable(null),
72
+ onActiveChange,
73
+ compact = false,
74
+ leftItem,
75
+ rightItem,
76
+ leftHeader,
77
+ rightHeader,
78
+ empty
79
+ }: {
80
+ leftItems: L[];
81
+ rightItems: R[];
82
+ links: ColumnLink[];
83
+ leftKey: (item: L) => string | number;
84
+ rightKey: (item: R) => string | number;
85
+ accentColor: (item: R, isActive: boolean) => string;
86
+ activeId?: string | number | null;
87
+ onActiveChange?: (id: string | number | null) => void;
88
+ compact?: boolean;
89
+ leftItem: Snippet<[{ item: L; isActive: boolean }]>;
90
+ rightItem: Snippet<[{ item: R; isActive: boolean }]>;
91
+ leftHeader?: Snippet;
92
+ rightHeader?: Snippet;
93
+ empty?: Snippet;
94
+ } = $props();
95
+
96
+ // ── State ──────────────────────────────────────────────────────────────────
97
+
98
+ /** Hover overrides scroll-center activation. */
99
+ let hoverActiveId = $state<string | number | null>(null);
100
+ /** Scroll-center derived active id. */
101
+ let scrollActiveId = $state<string | number | null>(null);
102
+
103
+ /** The "inked up" right item id: hover > controlled activeId > scroll. */
104
+ const resolvedActiveId = $derived(
105
+ hoverActiveId ?? activeId ?? scrollActiveId
106
+ );
107
+
108
+ // ── DOM refs ───────────────────────────────────────────────────────────────
109
+
110
+ let containerEl = $state<HTMLElement | null>(null);
111
+ let leftScrollEl = $state<HTMLElement | null>(null);
112
+ let svgW = $state(0);
113
+ let svgH = $state(0);
114
+
115
+ // ── Ribbon geometry ────────────────────────────────────────────────────────
116
+
117
+ interface Ribbon {
118
+ id: string | number;
119
+ accent: string;
120
+ path: string;
121
+ active: boolean;
122
+ }
123
+ let ribbons = $state<Ribbon[]>([]);
124
+
125
+ function relRect(el: Element | null): { top: number; bottom: number; left: number; right: number } | null {
126
+ if (!el || !containerEl) return null;
127
+ const c = containerEl.getBoundingClientRect();
128
+ const r = el.getBoundingClientRect();
129
+ return { top: r.top - c.top, bottom: r.bottom - c.top, left: r.left - c.left, right: r.right - c.left };
130
+ }
131
+
132
+ function ribbonPath(lTop: number, lBot: number, lx: number, rTop: number, rBot: number, rx: number): string {
133
+ const midX = (lx + rx) / 2;
134
+ return [
135
+ `M ${lx} ${lTop}`,
136
+ `C ${midX} ${lTop} ${midX} ${rTop} ${rx} ${rTop}`,
137
+ `L ${rx} ${rBot}`,
138
+ `C ${midX} ${rBot} ${midX} ${lBot} ${lx} ${lBot}`,
139
+ 'Z'
140
+ ].join(' ');
141
+ }
142
+
143
+ /** Build a lookup: left item id → its index in leftItems. */
144
+ const leftIdxMap = $derived(
145
+ (() => {
146
+ const m = new Map<string | number, number>();
147
+ leftItems.forEach((it, i) => m.set(leftKey(it), i));
148
+ return m;
149
+ })()
150
+ );
151
+
152
+ function recompute() {
153
+ if (!containerEl) return;
154
+ svgW = containerEl.clientWidth;
155
+ svgH = containerEl.clientHeight;
156
+
157
+ const lv = relRect(leftScrollEl);
158
+ const next: Ribbon[] = [];
159
+
160
+ for (const link of links) {
161
+ const rightId = link.rightId;
162
+ const rightItemData = rightItems.find((r) => rightKey(r) === rightId);
163
+ if (!rightItemData) continue;
164
+
165
+ const active = rightId === resolvedActiveId;
166
+ const accent = accentColor(rightItemData, active);
167
+
168
+ const startEl = containerEl.querySelector(`[data-left-id="${link.leftStartId}"]`);
169
+ const endEl = containerEl.querySelector(`[data-left-id="${link.leftEndId}"]`);
170
+ const anchorEl = containerEl.querySelector(`[data-right-id="${rightId}"]`);
171
+
172
+ if (!anchorEl) continue;
173
+
174
+ const cR = relRect(anchorEl);
175
+ if (!cR) continue;
176
+
177
+ const sR = relRect(startEl);
178
+ const eR = relRect(endEl);
179
+
180
+ let lTop = sR ? sR.top : (lv ? lv.top : 0);
181
+ let lBot = eR ? eR.bottom : (lv ? lv.bottom : svgH);
182
+
183
+ // Clamp to the visible scroll area
184
+ if (lv) {
185
+ lTop = Math.max(lv.top, Math.min(lv.bottom, lTop));
186
+ lBot = Math.max(lv.top, Math.min(lv.bottom, lBot));
187
+ }
188
+ if (lBot - lTop < 4) lBot = lTop + 4;
189
+
190
+ const lx = lv ? lv.right : svgW * 0.5;
191
+ next.push({
192
+ id: rightId,
193
+ accent,
194
+ path: ribbonPath(lTop, lBot, lx, cR.top, cR.bottom, cR.left),
195
+ active
196
+ });
197
+ }
198
+
199
+ ribbons = next;
200
+ }
201
+
202
+ function updateScrollActive() {
203
+ if (!leftScrollEl || !containerEl) return;
204
+ const lv = relRect(leftScrollEl);
205
+ if (!lv) return;
206
+ const centerY = (lv.top + lv.bottom) / 2;
207
+
208
+ let best: string | number | null = null;
209
+ let bestDist = Infinity;
210
+
211
+ for (const link of links) {
212
+ const startEl = containerEl.querySelector(`[data-left-id="${link.leftStartId}"]`);
213
+ const endEl = containerEl.querySelector(`[data-left-id="${link.leftEndId}"]`);
214
+ if (!startEl && !endEl) continue;
215
+ const top = (startEl ? relRect(startEl)?.top : null) ?? (relRect(endEl)?.top ?? 0);
216
+ const bot = (endEl ? relRect(endEl)?.bottom : null) ?? (relRect(startEl)?.bottom ?? 0);
217
+ const mid = (top + bot) / 2;
218
+ const dist = Math.abs(mid - centerY);
219
+ if (dist < bestDist) { bestDist = dist; best = link.rightId; }
220
+ }
221
+
222
+ if (best !== scrollActiveId) {
223
+ scrollActiveId = best;
224
+ onActiveChange?.(resolvedActiveId);
225
+ }
226
+ }
227
+
228
+ let raf = 0;
229
+ function schedule() {
230
+ if (raf) return;
231
+ raf = requestAnimationFrame(() => {
232
+ raf = 0;
233
+ updateScrollActive();
234
+ recompute();
235
+ });
236
+ }
237
+
238
+ $effect(() => {
239
+ // Re-run geometry whenever these reactive dependencies change
240
+ resolvedActiveId; leftItems; rightItems; links;
241
+ schedule();
242
+ });
243
+
244
+ $effect(() => {
245
+ if (!containerEl) return;
246
+ const ro = new ResizeObserver(schedule);
247
+ ro.observe(containerEl);
248
+ const onScroll = () => schedule();
249
+ leftScrollEl?.addEventListener('scroll', onScroll, { passive: true });
250
+ window.addEventListener('resize', schedule);
251
+ schedule();
252
+ return () => {
253
+ ro.disconnect();
254
+ leftScrollEl?.removeEventListener('scroll', onScroll);
255
+ window.removeEventListener('resize', schedule);
256
+ };
257
+ });
258
+
259
+ // ── Interaction ────────────────────────────────────────────────────────────
260
+
261
+ function onLeftHover(leftId: string | number | null) {
262
+ if (leftId === null) { hoverActiveId = null; return; }
263
+ const link = links.find((l) => {
264
+ // Check if leftId falls within this link's range
265
+ const startIdx = leftIdxMap.get(l.leftStartId) ?? -1;
266
+ const endIdx = leftIdxMap.get(l.leftEndId) ?? -1;
267
+ const thisIdx = leftIdxMap.get(leftId) ?? -1;
268
+ return thisIdx >= startIdx && thisIdx <= endIdx;
269
+ });
270
+ hoverActiveId = link?.rightId ?? null;
271
+ }
272
+
273
+ function onRightHover(rightId: string | number | null) {
274
+ hoverActiveId = rightId;
275
+ }
276
+
277
+ /** Scroll the left column so the start of this right item's range is visible. */
278
+ export function scrollToRight(rightId: string | number) {
279
+ const link = links.find((l) => l.rightId === rightId);
280
+ if (!link || !containerEl) return;
281
+ const startEl = containerEl.querySelector(`[data-left-id="${link.leftStartId}"]`) as HTMLElement | null;
282
+ startEl?.scrollIntoView({ behavior: 'smooth', block: 'center' });
283
+ }
284
+
285
+ /** Scroll the left column to center the start of this right item's range. */
286
+ export function scrollToLeft(leftId: string | number) {
287
+ if (!containerEl) return;
288
+ const el = containerEl.querySelector(`[data-left-id="${leftId}"]`) as HTMLElement | null;
289
+ el?.scrollIntoView({ behavior: 'smooth', block: 'center' });
290
+ }
291
+ </script>
292
+
293
+ <div
294
+ class="lc-wrap"
295
+ class:lc-compact={compact}
296
+ bind:this={containerEl}
297
+ >
298
+ <!-- SVG ribbon layer (behind both columns) -->
299
+ <svg
300
+ class="lc-svg"
301
+ width={svgW}
302
+ height={svgH}
303
+ viewBox="0 0 {svgW} {svgH}"
304
+ aria-hidden="true"
305
+ >
306
+ <defs>
307
+ {#each ribbons as r (r.id)}
308
+ <linearGradient id="lc-grad-{r.id}" x1="0" y1="0" x2="1" y2="0">
309
+ <stop
310
+ offset="0%"
311
+ stop-color={r.accent}
312
+ stop-opacity={r.active ? 0.55 : 0.10}
313
+ />
314
+ <stop
315
+ offset="100%"
316
+ stop-color={r.accent}
317
+ stop-opacity={r.active ? 0.30 : 0.06}
318
+ />
319
+ </linearGradient>
320
+ {/each}
321
+ </defs>
322
+ {#each ribbons as r (r.id)}
323
+ <path
324
+ d={r.path}
325
+ fill="url(#lc-grad-{r.id})"
326
+ stroke={r.accent}
327
+ stroke-opacity={r.active ? 0.8 : 0.12}
328
+ stroke-width={r.active ? 1.5 : 0.75}
329
+ class="lc-ribbon"
330
+ class:lc-ribbon-active={r.active}
331
+ />
332
+ {/each}
333
+ </svg>
334
+
335
+ <!-- LEFT column (the "many") -->
336
+ <div class="lc-col lc-left" bind:this={leftScrollEl}>
337
+ <div class="lc-col-head">
338
+ {#if leftHeader}
339
+ {@render leftHeader()}
340
+ {:else}
341
+ Left
342
+ {/if}
343
+ </div>
344
+ {#each leftItems as item (leftKey(item))}
345
+ {@const itemId = leftKey(item)}
346
+ {@const link = links.find((l) => {
347
+ const startIdx = leftIdxMap.get(l.leftStartId) ?? -1;
348
+ const endIdx = leftIdxMap.get(l.leftEndId) ?? -1;
349
+ const thisIdx = leftIdxMap.get(itemId) ?? -1;
350
+ return thisIdx >= startIdx && thisIdx <= endIdx;
351
+ })}
352
+ {@const isActive = link ? link.rightId === resolvedActiveId : false}
353
+ <div
354
+ data-left-id={itemId}
355
+ class="lc-left-item"
356
+ class:lc-left-item-active={isActive}
357
+ role="presentation"
358
+ onmouseenter={() => onLeftHover(itemId)}
359
+ onmouseleave={() => onLeftHover(null)}
360
+ >
361
+ {@render leftItem({ item, isActive })}
362
+ </div>
363
+ {/each}
364
+ </div>
365
+
366
+ <!-- Gutter (ribbon breathing room) -->
367
+ <div class="lc-gutter"></div>
368
+
369
+ <!-- RIGHT column (the anchors) -->
370
+ <div class="lc-col lc-right">
371
+ <div class="lc-col-head">
372
+ {#if rightHeader}
373
+ {@render rightHeader()}
374
+ {:else}
375
+ Right
376
+ {/if}
377
+ </div>
378
+ {#if rightItems.length === 0}
379
+ {#if empty}
380
+ <div class="lc-empty">{@render empty()}</div>
381
+ {:else}
382
+ <div class="lc-empty">No items.</div>
383
+ {/if}
384
+ {:else}
385
+ {#each rightItems as item (rightKey(item))}
386
+ {@const rightId = rightKey(item)}
387
+ {@const isActive = rightId === resolvedActiveId}
388
+ {@const accent = accentColor(item, isActive)}
389
+ <div
390
+ data-right-id={rightId}
391
+ class="lc-right-item"
392
+ class:lc-right-item-active={isActive}
393
+ style="--lc-accent:{accent}; --lc-tint:color-mix(in oklch, {accent} 10%, transparent); --lc-glow:color-mix(in oklch, {accent} 40%, transparent);"
394
+ role="presentation"
395
+ onmouseenter={() => onRightHover(rightId)}
396
+ onmouseleave={() => onRightHover(null)}
397
+ onclick={() => scrollToRight(rightId)}
398
+ >
399
+ {@render rightItem({ item, isActive })}
400
+ </div>
401
+ {/each}
402
+ {/if}
403
+ </div>
404
+ </div>
405
+
406
+ <style>
407
+ .lc-wrap {
408
+ position: relative;
409
+ display: flex;
410
+ height: 100%;
411
+ min-height: 0;
412
+ background: var(--color-base-100, oklch(0.16 0.01 250));
413
+ border-radius: 0.75rem;
414
+ overflow: hidden;
415
+ }
416
+
417
+ /* SVG ribbon layer */
418
+ .lc-svg {
419
+ position: absolute;
420
+ inset: 0;
421
+ pointer-events: none;
422
+ z-index: 0;
423
+ }
424
+ .lc-ribbon {
425
+ transition: stroke-opacity 0.25s ease, stroke-width 0.25s ease;
426
+ }
427
+ .lc-ribbon-active {
428
+ filter: drop-shadow(0 0 6px var(--color-success, oklch(0.7 0.18 145)));
429
+ }
430
+
431
+ /* Columns */
432
+ .lc-col {
433
+ position: relative;
434
+ z-index: 1;
435
+ display: flex;
436
+ flex-direction: column;
437
+ min-height: 0;
438
+ }
439
+ .lc-left {
440
+ flex: 1 1 0;
441
+ overflow-y: auto;
442
+ padding: 0 1rem 40vh 1rem;
443
+ }
444
+ .lc-compact .lc-left {
445
+ padding: 0 0.5rem 20vh 0.5rem;
446
+ }
447
+ .lc-right {
448
+ flex: 0 0 320px;
449
+ overflow-y: auto;
450
+ padding: 0 0.75rem 1rem 0.75rem;
451
+ display: flex;
452
+ flex-direction: column;
453
+ gap: 0.5rem;
454
+ }
455
+ .lc-gutter {
456
+ flex: 0 0 90px;
457
+ }
458
+
459
+ /* Column headings */
460
+ .lc-col-head {
461
+ position: sticky;
462
+ top: 0;
463
+ z-index: 2;
464
+ padding: 0.5rem 0.25rem;
465
+ font-family: ui-sans-serif, system-ui, sans-serif;
466
+ font-size: 0.65rem;
467
+ font-weight: 700;
468
+ letter-spacing: 0.08em;
469
+ text-transform: uppercase;
470
+ color: var(--color-base-content, oklch(0.85 0.02 250));
471
+ opacity: 0.55;
472
+ background: linear-gradient(
473
+ var(--color-base-100, oklch(0.16 0.01 250)) 70%,
474
+ transparent
475
+ );
476
+ }
477
+
478
+ /* Left item wrapper — consumers style via snippet */
479
+ .lc-left-item {
480
+ border-radius: 0.3rem;
481
+ padding: 0.05rem 0.15rem;
482
+ margin: 0.02rem 0;
483
+ transition: background-color 0.2s ease, box-shadow 0.2s ease;
484
+ }
485
+ .lc-left-item-active {
486
+ background: color-mix(in oklch, var(--color-success, oklch(0.7 0.18 145)) 8%, transparent);
487
+ box-shadow: inset 2px 0 0 var(--color-success, oklch(0.7 0.18 145));
488
+ }
489
+
490
+ /* Right item wrapper — consumers style via snippet; provides CSS vars */
491
+ .lc-right-item {
492
+ position: relative;
493
+ border-radius: 0.5rem;
494
+ border: 1px solid color-mix(in oklch, var(--lc-accent, currentColor) 22%, transparent);
495
+ background: var(--lc-tint, transparent);
496
+ cursor: pointer;
497
+ transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
498
+ }
499
+ .lc-right-item:hover {
500
+ transform: translateX(-2px);
501
+ }
502
+ .lc-right-item-active {
503
+ border-color: var(--lc-accent, currentColor);
504
+ box-shadow: 0 0 0 1px var(--lc-accent, currentColor), 0 0 18px var(--lc-glow, transparent);
505
+ }
506
+
507
+ .lc-empty {
508
+ padding: 1rem;
509
+ font-size: 0.78rem;
510
+ opacity: 0.6;
511
+ }
512
+
513
+ @media (prefers-reduced-motion: reduce) {
514
+ .lc-ribbon,
515
+ .lc-left-item,
516
+ .lc-right-item {
517
+ transition: none;
518
+ }
519
+ }
520
+ </style>