@cameronsjo/artificer 0.12.0 → 0.18.0

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,227 @@
1
+ /* Artificer · tree keyboard helper (WAI-ARIA APG tree pattern).
2
+ The .tree primitive ships the LOOK (twisty, rows, indent); this ships the
3
+ unavoidable-JS BEHAVIOR — roving tabindex on the [role="treeitem"] li,
4
+ clamped ↑/↓ over the VISIBLE items (an APG tree never wraps), Home/End,
5
+ →/← expand/collapse/first-child/parent, Enter/Space activate, and click
6
+ parity — the same way artificer-tabs.js ships the tabs pattern.
7
+
8
+ Usage:
9
+ <ul class="tree" role="tree" data-tree>
10
+ <li role="treeitem" aria-expanded="true" tabindex="0"> <div class="tree__row">…</div>
11
+ <ul class="tree__group" role="group"> <li role="treeitem" tabindex="-1">…</li> </ul>
12
+ </li>
13
+ </ul>
14
+ ArtificerTree.enhance(treeEl, { onSelect(item){…}, onToggle(item, open){…} });
15
+ ArtificerTree.observe(document); // auto-enhance every [data-tree] (SPA)
16
+
17
+ Mechanics ONLY: expand/collapse toggles aria-expanded + hides the item's
18
+ own .tree__group; activating a leaf moves aria-selected (single-select);
19
+ side-effects (loading children, navigation) belong in onSelect/onToggle.
20
+ Focus (and the ring — see the [role="treeitem"]:focus-visible rule) rides
21
+ the li host; visibility = no [hidden] ancestor group. enhance() is
22
+ idempotent and returns a cached handle. */
23
+ (function () {
24
+ 'use strict';
25
+
26
+ // --- pure: vertical movement over the visible list (unit-tested, no DOM) ---
27
+ // Returns the visible-index to move to, or null for non-movement keys.
28
+ // Clamped at both ends — an APG tree never wraps.
29
+ function nextVisible(key, current, count) {
30
+ if (!(count > 0)) return null;
31
+ switch (key) {
32
+ case 'ArrowDown':
33
+ return Math.min(current + 1, count - 1);
34
+ case 'ArrowUp':
35
+ return Math.max(current - 1, 0);
36
+ case 'Home':
37
+ return 0;
38
+ case 'End':
39
+ return count - 1;
40
+ default:
41
+ return null;
42
+ }
43
+ }
44
+
45
+ // --- pure: the horizontal/activation action table (unit-tested, no DOM) ----
46
+ // state.expanded: true (open parent) | false (closed parent) | null (leaf).
47
+ // Returns 'expand' | 'first-child' | 'collapse' | 'parent' | 'activate' | null.
48
+ function treeAction(key, state) {
49
+ var expanded = state ? state.expanded : null;
50
+ var hasParent = !!(state && state.hasParent);
51
+ switch (key) {
52
+ case 'ArrowRight':
53
+ if (expanded === false) return 'expand';
54
+ if (expanded === true) return 'first-child';
55
+ return null; // leaf: APG right-arrow does nothing
56
+ case 'ArrowLeft':
57
+ if (expanded === true) return 'collapse';
58
+ return hasParent ? 'parent' : null;
59
+ case 'Enter':
60
+ case ' ':
61
+ return 'activate';
62
+ default:
63
+ return null;
64
+ }
65
+ }
66
+
67
+ var noop = { select: function () {}, toggle: function () {}, destroy: function () {} };
68
+
69
+ // --- DOM: wire one tree (verified in live-spec/components-extended.html) ---
70
+ function enhance(treeEl, opts) {
71
+ opts = opts || {};
72
+ if (!treeEl) return noop;
73
+ if (treeEl.__artificerTree) return treeEl.__artificerTree;
74
+
75
+ function allItems() {
76
+ return Array.prototype.slice.call(treeEl.querySelectorAll('[role="treeitem"]'));
77
+ }
78
+ function visibleItems() {
79
+ // visible = no [hidden] ancestor group (closest() from the li sees them all)
80
+ return allItems().filter(function (it) {
81
+ return !it.closest('[hidden]');
82
+ });
83
+ }
84
+ function expandedState(item) {
85
+ var v = item.getAttribute('aria-expanded');
86
+ return v === null ? null : v === 'true'; // null = leaf
87
+ }
88
+ function parentItem(item) {
89
+ var p = item.parentElement && item.parentElement.closest('[role="treeitem"]');
90
+ return p && treeEl.contains(p) ? p : null;
91
+ }
92
+
93
+ function setCurrent(item, focus) {
94
+ allItems().forEach(function (t) {
95
+ t.setAttribute('tabindex', t === item ? '0' : '-1');
96
+ });
97
+ if (focus) item.focus();
98
+ }
99
+
100
+ function select(item) {
101
+ // aria-selected belongs on the [role="treeitem"], not the row (single-select)
102
+ allItems().forEach(function (t) {
103
+ if (t === item) t.setAttribute('aria-selected', 'true');
104
+ else t.removeAttribute('aria-selected');
105
+ });
106
+ if (typeof opts.onSelect === 'function') opts.onSelect(item);
107
+ }
108
+
109
+ function toggle(item, open) {
110
+ if (typeof open !== 'boolean') open = expandedState(item) !== true;
111
+ item.setAttribute('aria-expanded', String(open));
112
+ var group = item.querySelector(':scope > .tree__group');
113
+ if (group) group.hidden = !open;
114
+ if (typeof opts.onToggle === 'function') opts.onToggle(item, open);
115
+ }
116
+
117
+ // activate = the click path: parents toggle, leaves select
118
+ function activate(item) {
119
+ if (expandedState(item) === null) select(item);
120
+ else toggle(item);
121
+ }
122
+
123
+ function onClick(e) {
124
+ var row = e.target.closest('.tree__row');
125
+ if (!row) return;
126
+ var item = row.closest('[role="treeitem"]');
127
+ if (!item || !treeEl.contains(item)) return;
128
+ setCurrent(item, false); // click also moves the roving tabindex
129
+ activate(item);
130
+ }
131
+
132
+ function onKeydown(e) {
133
+ var item = e.target.closest('[role="treeitem"]');
134
+ if (!item || !treeEl.contains(item)) return;
135
+
136
+ var items = visibleItems();
137
+ var move = nextVisible(e.key, items.indexOf(item), items.length);
138
+ if (move !== null) {
139
+ e.preventDefault();
140
+ setCurrent(items[move], true);
141
+ return;
142
+ }
143
+
144
+ var action = treeAction(e.key, {
145
+ expanded: expandedState(item),
146
+ hasParent: !!parentItem(item),
147
+ });
148
+ if (!action) return;
149
+ e.preventDefault();
150
+ if (action === 'expand') toggle(item, true);
151
+ else if (action === 'collapse') toggle(item, false);
152
+ else if (action === 'first-child') {
153
+ var child = item.querySelector(':scope > .tree__group > [role="treeitem"]');
154
+ if (child) setCurrent(child, true);
155
+ } else if (action === 'parent') {
156
+ setCurrent(parentItem(item), true);
157
+ } else if (action === 'activate') {
158
+ activate(item);
159
+ }
160
+ }
161
+
162
+ // Initial roving tabindex: an existing tabindex="0" wins (idempotent over
163
+ // markup that already roves), else the selected item, else the first.
164
+ var items = allItems();
165
+ if (items.length) {
166
+ var keep = -1;
167
+ items.forEach(function (t, j) {
168
+ if (keep < 0 && t.getAttribute('tabindex') === '0') keep = j;
169
+ });
170
+ if (keep < 0) {
171
+ items.forEach(function (t, j) {
172
+ if (keep < 0 && t.getAttribute('aria-selected') === 'true') keep = j;
173
+ });
174
+ }
175
+ setCurrent(items[keep < 0 ? 0 : keep], false);
176
+ }
177
+
178
+ treeEl.addEventListener('click', onClick);
179
+ treeEl.addEventListener('keydown', onKeydown);
180
+
181
+ var handle = {
182
+ select: select,
183
+ toggle: toggle,
184
+ destroy: function () {
185
+ treeEl.removeEventListener('click', onClick);
186
+ treeEl.removeEventListener('keydown', onKeydown);
187
+ delete treeEl.__artificerTree;
188
+ },
189
+ };
190
+ treeEl.__artificerTree = handle;
191
+ return handle;
192
+ }
193
+
194
+ // Auto-enhance every [data-tree] now and on insert (SPA lifecycle, matching
195
+ // ArtificerTabs.observe). Returns a disconnect fn.
196
+ function observe(root) {
197
+ root = root || (typeof document !== 'undefined' ? document : null);
198
+ if (!root) return function () {};
199
+ function enhanceIn(scope) {
200
+ if (scope.matches && scope.matches('[data-tree]')) enhance(scope);
201
+ if (scope.querySelectorAll) {
202
+ Array.prototype.forEach.call(scope.querySelectorAll('[data-tree]'), function (el) {
203
+ enhance(el);
204
+ });
205
+ }
206
+ }
207
+ enhanceIn(root);
208
+ if (typeof MutationObserver === 'undefined') return function () {};
209
+ var mo = new MutationObserver(function (muts) {
210
+ muts.forEach(function (m) {
211
+ Array.prototype.forEach.call(m.addedNodes || [], function (n) {
212
+ if (n.nodeType === 1) enhanceIn(n);
213
+ });
214
+ });
215
+ });
216
+ mo.observe(root === document ? document.body : root, { childList: true, subtree: true });
217
+ return function () {
218
+ mo.disconnect();
219
+ };
220
+ }
221
+
222
+ var api = { enhance: enhance, observe: observe, nextVisible: nextVisible, treeAction: treeAction };
223
+ // globalThis === window in a browser (so window.ArtificerTree works);
224
+ // in Node it lets the unit test import this file and exercise the pure fns.
225
+ if (typeof window !== 'undefined') window.ArtificerTree = api;
226
+ else if (typeof globalThis !== 'undefined') globalThis.ArtificerTree = api;
227
+ })();
@@ -133,6 +133,26 @@
133
133
  to { background-position: 200% 50%; } /* exactly one tile → seamless loop */
134
134
  }
135
135
 
136
+ /* ─── Wordmark integration: carry the gradient through to the burnished stop ─
137
+ When .wordmark and .whimsy share one element, -webkit-text-fill-color:
138
+ transparent is inherited by ::after but background-image is not, so the
139
+ accent stop vanishes behind a transparent fill with no gradient to clip to.
140
+ Re-establish the same gradient plumbing on ::after here so the stop flows
141
+ with the mark. The :not(.wordmark--stop-none) guard ensures period-bearing
142
+ marks that suppress the pseudo are unaffected even when .whimsy is present.
143
+ animation-delay: 0 is implicit (matches the parent's start position) so
144
+ the stop hue stays continuous with the flowing mark. */
145
+ .wordmark.whimsy:not(.wordmark--stop-none)::after {
146
+ background-image: var(--whimsy-gradient);
147
+ background-size: 200% 100%;
148
+ background-repeat: repeat;
149
+ -webkit-background-clip: text;
150
+ background-clip: text;
151
+ color: transparent;
152
+ -webkit-text-fill-color: transparent;
153
+ animation: whimsy-flow var(--whimsy-speed) linear infinite;
154
+ }
155
+
136
156
  /* Vivid — for the rare moment you want full saturation. Dial consciously. */
137
157
  .whimsy--vivid { --whimsy-c: 0.21; }
138
158
 
@@ -315,3 +335,65 @@
315
335
  .whimsy-focus:focus-visible::after,
316
336
  .whimsy-focus:hover::after { animation: none; opacity: 1; }
317
337
  }
338
+
339
+
340
+ /* ═══ Word vs character gradient — granularity (v0.11.0) ══════════════════════
341
+ Flow has two granularities. DEFAULT = WORD gradient: one continuous ramp
342
+ across the whole element (bare .whimsy) — colour ramps through and between
343
+ letters. OPT-IN = CHARACTER gradient: the gradient is mapped per glyph
344
+ (Whimsy.hydrate() / data-whimsy="wave" → .whimsy-char cells, each painting its
345
+ own slice). Word is the default; character is the opt-in capability the
346
+ travelling wave/bob rides on. See CLAUDE.md § Whimsy. ════════════════════════════════════════════════════════ */
347
+
348
+ /* Tiling sine for rainbow underlines — one clean period per 40px; start/end at
349
+ the same phase so repeat-x butts seamlessly. */
350
+ :root { --whimsy-sine: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='10' viewBox='0 0 40 10'><path d='M0 5 Q 10 1.5 20 5 T 40 5' fill='none' stroke='black' stroke-width='2.4' stroke-linecap='round'/></svg>"); }
351
+
352
+ /* .whimsy--gold — a metal palette, cousin of .whimsy--silver. Slow (16s) so it
353
+ reads as near-static metal; add .whimsy--no-flow for a still gold fill. Metal
354
+ palettes pair best with a CHARACTER gradient (per-glyph metal = brushed/faceted). */
355
+ .whimsy--gold {
356
+ --whimsy-speed: 16s;
357
+ --whimsy-gradient: linear-gradient(var(--whimsy-angle),
358
+ oklch(0.72 0.12 82), oklch(0.90 0.10 92), oklch(0.66 0.13 72),
359
+ oklch(0.95 0.06 96), oklch(0.72 0.12 82));
360
+ }
361
+ :root[data-theme="light"] .whimsy--gold {
362
+ --whimsy-gradient: linear-gradient(var(--whimsy-angle),
363
+ oklch(0.56 0.12 72), oklch(0.68 0.13 84), oklch(0.47 0.12 66),
364
+ oklch(0.73 0.11 88), oklch(0.56 0.12 72));
365
+ }
366
+
367
+ /* .whimsy-underline — a flowing rainbow sine that draws in under a link, in
368
+ lockstep with the mark's own flow. Contiguous tiling sine, faded ends,
369
+ travels; reveals L→R. Put it on the text element (made inline-block) so it
370
+ shares the mark's exact width + gradient. The sanctioned replacement for a
371
+ flat link underline on a .whimsy wordmark — see CLAUDE.md § Whimsy + § Brand. */
372
+ .whimsy-underline { position: relative; display: inline-block; }
373
+ .whimsy-underline::after {
374
+ content: ""; position: absolute; left: 0; right: 0; bottom: -0.3em; height: 10px;
375
+ background-image: var(--whimsy-gradient); background-size: 200% 100%;
376
+ animation: whimsy-flow var(--whimsy-speed) linear infinite, whimsy-wave-travel 3.2s linear infinite;
377
+ -webkit-mask-image: var(--whimsy-sine), linear-gradient(90deg, transparent, #000 14%, #000 86%, transparent);
378
+ mask-image: var(--whimsy-sine), linear-gradient(90deg, transparent, #000 14%, #000 86%, transparent);
379
+ -webkit-mask-repeat: repeat-x, no-repeat; mask-repeat: repeat-x, no-repeat;
380
+ -webkit-mask-size: 40px 10px, 100% 100%; mask-size: 40px 10px, 100% 100%;
381
+ -webkit-mask-position: left center, center; mask-position: left center, center;
382
+ -webkit-mask-composite: source-in; mask-composite: intersect;
383
+ clip-path: inset(0 100% 0 0); opacity: 0;
384
+ transition: clip-path var(--dur-max) var(--ease), opacity var(--dur-fast) var(--ease);
385
+ }
386
+ .whimsy-underline:hover::after,
387
+ .whimsy-underline:focus-visible::after { clip-path: inset(0 0 0 0); opacity: 1; }
388
+ /* --on — latch the underline drawn-in permanently (the standing ".poke"). For
389
+ whimsy that doesn't wait for a pointer: the Pride greeting wears it all month.
390
+ Same revealed end-state as :hover; reduced-motion still freezes the flow
391
+ (the rule below) while leaving it visible. */
392
+ .whimsy-underline--on::after { clip-path: inset(0 0 0 0); opacity: 1; }
393
+ @keyframes whimsy-wave-travel {
394
+ from { -webkit-mask-position: 0 center, center; mask-position: 0 center, center; }
395
+ to { -webkit-mask-position: -40px center, center; mask-position: -40px center, center; }
396
+ }
397
+ @media (prefers-reduced-motion: reduce) {
398
+ .whimsy-underline::after { animation: none !important; background-position: 30% 50% !important; }
399
+ }
@@ -1,5 +1,5 @@
1
1
  /* ═══════════════════════════════════════════════════════════════════════════
2
- ARTIFICER · Whimsy helper · v0.12.0
2
+ ARTIFICER · Whimsy helper · v0.18.0
3
3
  ─────────────────────────────────────────────────────────────────────────
4
4
  Tiny, dependency-free. Pairs with artificer-whimsy.css. Exposes window.Whimsy.
5
5
 
@@ -12,6 +12,14 @@
12
12
  Whimsy.celebrate(el, ms?) one-shot: light el up for ms (default 2600)
13
13
  then remove. For whimsical operations
14
14
  (deploy succeeded, streak hit, etc).
15
+ Whimsy.greeting(root?) swap [data-whimsy-greeting] elements to the
16
+ seasonal footer line — June → "happy pride"
17
+ + vivid flow; off-season → the inline text
18
+ + glacial flow. Graceful without JS.
19
+ Whimsy.greetingFor(date?, opts) PURE — the greeting spec for a date
20
+ (date defaults to today when omitted):
21
+ { season, text, classes }. opts =
22
+ { default?, defaultClass? }. Unit-tested.
15
23
  Whimsy.run(el, opts) ignite el, then settle after opts.loops
16
24
  hue-cycles. For long "thinking" states.
17
25
  opts = { loops?, settle? }
@@ -147,27 +155,91 @@
147
155
  window.setTimeout(function () { clearEl(el); }, dur);
148
156
  }
149
157
 
158
+ /* ── Seasonal greeting ── pure spec + DOM application ──────────────────── */
159
+
160
+ /* PURE — no DOM. Given a date, return the footer-greeting spec for its
161
+ season. June (getMonth() === 5) is Pride: "happy pride" (no trailing
162
+ period — explicit) with always-on vivid flow — intentionally the one view
163
+ that does NOT settle, the single logged exception to Whimsy doctrine #7
164
+ ("whimsy rests"); every other long-lived whimsy still settles. Off-season
165
+ returns the caller's own line + class so every consumer keeps its voice.
166
+ opts = { default?, defaultClass? }. */
167
+ function greetingFor(date, opts) {
168
+ opts = opts || {};
169
+ var month = (date || new Date()).getMonth(); // 5 === June
170
+ if (month === 5) {
171
+ // Pride wears the FULL-ATTENTION treatment, latched on (the "clementine"
172
+ // combo from Lane-1's whimsy exploration): a per-character faceted
173
+ // gradient (--wave + splitWave), the bob frozen so it reads as flow not
174
+ // bounce (--no-bob), vivid saturation (the "bloom"), and the rainbow
175
+ // underline drawn in permanently (--on). The one view that never settles
176
+ // — doctrine #7's single logged exception. greeting() runs splitWave.
177
+ return { season: "pride", text: "happy pride",
178
+ classes: ["whimsy", "whimsy--wave", "whimsy--no-bob",
179
+ "whimsy--vivid", "whimsy-underline",
180
+ "whimsy-underline--on"] };
181
+ }
182
+ // Off-season fallback. The line is the consumer's to set — via the
183
+ // element's inline text or opts.default. Lines that fit the calm glacial
184
+ // drift: "kindness is free" (default) or "abide no hatred".
185
+ // (Future idea: rotate one per day/month via a modulus on the date; for
186
+ // now it's a deliberately stable per-surface choice, not a slot machine.)
187
+ return { season: "default",
188
+ text: opts.default || "kindness is free",
189
+ classes: ["whimsy", opts.defaultClass || "whimsy--glacial"] };
190
+ }
191
+
192
+ /* DOM — scan [data-whimsy-greeting]; the element's inline text IS the
193
+ off-season line (so the markup renders gracefully with JS disabled). Swap
194
+ in the seasonal text + apply the whimsy classes. Idempotent: a done flag
195
+ keeps observe() re-scans from re-reading the swapped text. */
196
+ function greeting(root) {
197
+ root = root || document;
198
+ var els = root.querySelectorAll("[data-whimsy-greeting]");
199
+ for (var i = 0; i < els.length; i++) {
200
+ var el = els[i];
201
+ if (el.dataset.whimsyGreetingDone === "1") continue;
202
+ var spec = greetingFor(new Date(), {
203
+ default: el.textContent.trim(),
204
+ defaultClass: el.dataset.whimsyGreetingClass
205
+ });
206
+ el.textContent = spec.text;
207
+ for (var c = 0; c < spec.classes.length; c++) el.classList.add(spec.classes[c]);
208
+ // A --wave spec needs its text shattered into staggered .whimsy-char
209
+ // cells (per-char faceted gradient) — same hydration data-whimsy="wave"
210
+ // gets. Run AFTER the text is set so splitWave reads the seasonal line.
211
+ if (spec.classes.indexOf("whimsy--wave") !== -1) splitWave(el);
212
+ el.dataset.whimsyGreetingDone = "1";
213
+ }
214
+ }
215
+
150
216
  // SPA lifecycle — auto-hydrate nodes inserted after first paint. Returns a
151
217
  // disconnect fn. Idempotent guards make the re-scan cheap (done nodes skip).
152
218
  function observe(root) {
153
219
  root = root || document.body;
154
220
  hydrate(root);
221
+ greeting(root);
155
222
  if (typeof MutationObserver === 'undefined') return function () {};
223
+ var schedule = window.requestAnimationFrame
224
+ ? function (cb) { window.requestAnimationFrame(cb); }
225
+ : function (cb) { window.setTimeout(cb, 0); };
156
226
  var scheduled = false;
157
227
  var mo = new MutationObserver(function () {
158
228
  if (scheduled) return;
159
229
  scheduled = true;
160
- (window.requestAnimationFrame || window.setTimeout)(function () { scheduled = false; hydrate(root); }, 0);
230
+ schedule(function () { scheduled = false; hydrate(root); greeting(root); });
161
231
  });
162
232
  mo.observe(root, { childList: true, subtree: true });
163
233
  return function () { mo.disconnect(); };
164
234
  }
165
235
 
166
- window.Whimsy = {
236
+ var api = {
167
237
  hydrate: hydrate,
168
238
  observe: observe,
169
239
  watch: watch,
170
240
  celebrate: celebrate,
241
+ greeting: greeting,
242
+ greetingFor: greetingFor,
171
243
  run: run,
172
244
  settle: settle,
173
245
  unsettle: unsettle,
@@ -175,10 +247,19 @@
175
247
  ignite: igniteEl,
176
248
  clear: clearEl
177
249
  };
250
+ // globalThis === window in a browser (so window.Whimsy works); in Node it
251
+ // lets the unit test import this file and exercise greetingFor.
252
+ if (typeof window !== "undefined") window.Whimsy = api;
253
+ else if (typeof globalThis !== "undefined") globalThis.Whimsy = api;
178
254
 
179
- if (document.readyState === "loading") {
180
- document.addEventListener("DOMContentLoaded", function () { hydrate(); });
181
- } else {
182
- hydrate();
255
+ // DOM auto-run — guarded so `import`ing the module in Node (the unit test)
256
+ // doesn't touch a document that isn't there.
257
+ if (typeof document !== "undefined") {
258
+ if (document.readyState === "loading") {
259
+ document.addEventListener("DOMContentLoaded", function () { hydrate(); greeting(); });
260
+ } else {
261
+ hydrate();
262
+ greeting();
263
+ }
183
264
  }
184
265
  })();