@tjakoen/grain 0.1.28 → 0.1.29

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tjakoen/grain",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "GRAIN — an AI-interaction design system where every surface is operable by both a human and an AI through one shared vocabulary.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -10,8 +10,10 @@
10
10
  // open/close hooks, the lazy iframe load (base, unconditional), and the hover/tap bridge (enhanced,
11
11
  // gated on the demo root existing).
12
12
  //
13
- // It maps each rendered CSS class to its catalog section slug (the .md title slug — stable for
14
- // grain's components). The catalog exposes a "single" mode (data-peek-single + .is-peek-active,
13
+ // It maps each rendered CSS class to its catalog section slug. The lookup is read from the loaded
14
+ // catalog itself: a class matches an entry whose slug equals it, or whose title does once a
15
+ // trailing "(source)" is dropped, which is how a consumer's own components ("cv-entry (portfolio)")
16
+ // get found. The hand MAP below only holds the aliases where the class and the slug differ. The catalog exposes a "single" mode (data-peek-single + .is-peek-active,
15
17
  // driven here via window.__catSetActive). The bridge adapts to the pointer: with a HOVERING
16
18
  // pointer it's a hover-reveal in single mode (one entry at a time, cross-fading). On a COARSE
17
19
  // pointer (touch, no hover) a finger can't hover, so the same usage→specimen link becomes a TAP:
@@ -34,10 +36,12 @@
34
36
  // on touch it would be undrivable, so there we leave the full scrollable catalog.
35
37
  const canHover = matchMedia("(hover: hover) and (pointer: fine)").matches;
36
38
 
37
- // rendered CSS class → catalog slug. A hand-maintained BRIDGE (the rendered class often differs
38
- // from the slug: "icon-btn" → "icon-button", "field" → "input"), so keep it in sync when a
39
- // component's class or title changes. A drift test (catalog-peek.test.ts) fails if a slug here
40
- // stops matching a real component. Innermost match wins (hover the glyph → Icon; the row → Nav item).
39
+ // ALIASES: rendered CSS class → catalog slug, only where the two differ ("icon-btn" →
40
+ // "icon-button", "field" → "input"). Every other class resolves through the index built from the
41
+ // loaded catalog (indexCatalog below), so a new component reveals on hover with no edit here.
42
+ // Identity rows are kept so the bridge still works in the moment before the catalog loads. A
43
+ // drift test (catalog-peek.test.ts) fails if a slug here stops matching a real component.
44
+ // Innermost match wins (hover the glyph → Icon; the row → Nav item).
41
45
  const MAP = {
42
46
  "action-badge": "action-badge", "icon-btn": "icon-button", "nav-item": "nav-item",
43
47
  "tab-bar": "tab-bar", "side-rail": "side-rail", "app-shell": "app-shell",
@@ -46,6 +50,28 @@
46
50
  "icon": "icon", "t": "typography",
47
51
  };
48
52
 
53
+ // class → slug, filled from the embedded catalog's own entries once it loads.
54
+ const INDEX = new Map();
55
+ const titleKey = (t) => t.replace(/\s*\([^)]*\)\s*$/, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
56
+ function indexCatalog(doc) {
57
+ for (const d of doc.querySelectorAll(".cat-doc[id]")) {
58
+ if (!INDEX.has(d.id)) INDEX.set(d.id, d.id);
59
+ const h = d.querySelector("h1, h2");
60
+ const key = h && titleKey(h.textContent || "");
61
+ if (key && !INDEX.has(key)) INDEX.set(key, d.id);
62
+ }
63
+ }
64
+ // the slug for one element, or null: an alias first, then the catalog's own index
65
+ const slugFor = (el) => {
66
+ if (!el.classList) return null;
67
+ for (const cls of el.classList) {
68
+ if (Object.prototype.hasOwnProperty.call(MAP, cls)) return MAP[cls];
69
+ const hit = INDEX.get(cls);
70
+ if (hit) return hit;
71
+ }
72
+ return null;
73
+ };
74
+
49
75
  const isOpen = () => !pane.hidden;
50
76
  // open/close = switch the sidebar-panel's mode — REUSE the shell's one mechanic (click its mode
51
77
  // tab; shell.js flips data-mode/hidden/aria-selected), never a parallel show/hide path. On the
@@ -74,6 +100,7 @@
74
100
  // on touch we skip single mode and leave the full scrollable catalog.
75
101
  frame.addEventListener("load", () => {
76
102
  // touch: no single mode — honour a tap that fired before the frame finished loading.
103
+ try { if (frame.contentDocument) indexCatalog(frame.contentDocument); } catch { /* cross-origin — aliases only */ }
77
104
  if (!canHover) { if (pendingTouch) { scrollToInFrame(pendingTouch); pendingTouch = null; } return; }
78
105
  try {
79
106
  const doc = frame.contentDocument;
@@ -119,7 +146,8 @@
119
146
  if (!canHover || !isOpen()) return;
120
147
  let el = e.target;
121
148
  while (el && el !== demo) {
122
- if (el.classList) for (const cls in MAP) if (el.classList.contains(cls)) { peek(MAP[cls]); return; }
149
+ const slug = slugFor(el);
150
+ if (slug) { peek(slug); return; }
123
151
  el = el.parentElement;
124
152
  }
125
153
  // No component under the pointer → HOLD the current reveal (don't switch on prose/gaps). This
@@ -144,7 +172,7 @@
144
172
  if (e.target.closest("a,button,input,textarea,select,label,[data-action],[data-peek]")) return;
145
173
  let el = e.target, slug = null;
146
174
  while (el && el !== demo && !slug) {
147
- if (el.classList) for (const cls in MAP) if (el.classList.contains(cls)) { slug = MAP[cls]; break; }
175
+ slug = slugFor(el);
148
176
  el = el.parentElement;
149
177
  }
150
178
  if (!slug) return;