@cameronsjo/artificer 0.12.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,47 @@
1
+ /* Artificer · focus-trap helper for modals & popovers.
2
+ Usage:
3
+ const trap = ArtificerFocus.trap(modalEl);
4
+ // ... when closing:
5
+ trap.release();
6
+ Behavior: tab/shift-tab cycles within element; Esc fires onEscape if provided;
7
+ focus is restored to the previously-focused element on release.
8
+ SPA note: focusables are recomputed on EVERY Tab, so controls added while
9
+ open are reachable automatically — no re-trap needed. Only INITIAL focus is
10
+ set at trap() time; if you swap content and want focus reset, release() then
11
+ trap() again. */
12
+ (function () {
13
+ 'use strict';
14
+ const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
15
+
16
+ function trap(el, opts) {
17
+ opts = opts || {};
18
+ const previouslyFocused = document.activeElement;
19
+ const focusables = () => Array.from(el.querySelectorAll(FOCUSABLE)).filter(n => !n.hasAttribute('inert'));
20
+
21
+ // Move focus to first focusable, or the container itself
22
+ const first = focusables()[0];
23
+ if (first) first.focus();
24
+ else { el.tabIndex = -1; el.focus(); }
25
+
26
+ function onKey(e) {
27
+ if (e.key === 'Escape' && opts.onEscape) { opts.onEscape(e); return; }
28
+ if (e.key !== 'Tab') return;
29
+ const items = focusables();
30
+ if (items.length === 0) { e.preventDefault(); return; }
31
+ const firstEl = items[0];
32
+ const lastEl = items[items.length - 1];
33
+ if (e.shiftKey && document.activeElement === firstEl) { e.preventDefault(); lastEl.focus(); }
34
+ else if (!e.shiftKey && document.activeElement === lastEl) { e.preventDefault(); firstEl.focus(); }
35
+ }
36
+ el.addEventListener('keydown', onKey);
37
+
38
+ return {
39
+ release() {
40
+ el.removeEventListener('keydown', onKey);
41
+ if (previouslyFocused && previouslyFocused.focus) previouslyFocused.focus();
42
+ }
43
+ };
44
+ }
45
+
46
+ window.ArtificerFocus = { trap };
47
+ })();
@@ -0,0 +1,163 @@
1
+ /* ═══════════════════════════════════════════════════════════════════════════
2
+ ARTIFICER · Icons
3
+ Minimal Lucide-rooted set, hand-tuned to system stroke weight.
4
+ - 16px viewBox, 1.5 stroke (matches body type ascender weight)
5
+ - Inherit color via stroke="currentColor" — never hardcode
6
+ - Use semantic icons sparingly: max 1 per "row" of UI
7
+ ═══════════════════════════════════════════════════════════════════════════ */
8
+
9
+ (function () {
10
+ 'use strict';
11
+
12
+ // 16×16 paths. Stroke-only (no fills) so they read on any surface.
13
+ // Sourced from Lucide (lucide.dev, ISC license) and lightly tuned.
14
+ const PATHS = {
15
+ // Navigation / chrome
16
+ 'menu': 'M3 6h10M3 8h10M3 10h10',
17
+ 'close': 'M4 4l8 8M12 4l-8 8',
18
+ 'chevron-r': 'M6 4l4 4-4 4',
19
+ 'chevron-l': 'M10 4l-4 4 4 4',
20
+ 'chevron-d': 'M4 6l4 4 4-4',
21
+ 'chevron-u': 'M4 10l4-4 4 4',
22
+ 'arrow-r': 'M3 8h10M9 4l4 4-4 4',
23
+ 'arrow-l': 'M13 8H3M7 4L3 8l4 4',
24
+ 'external': 'M10 3h3v3M13 3l-6 6M11 9v3H4V5h3',
25
+
26
+ // Status / semantic
27
+ 'check': 'M3 8l3 3 7-7',
28
+ 'x': 'M4 4l8 8M12 4l-8 8',
29
+ 'alert': 'M8 2L1.5 13h13L8 2zM8 6v3M8 11.5v.01',
30
+ 'info': 'M8 1a7 7 0 100 14A7 7 0 008 1zM8 7v4M8 4.5v.01',
31
+ 'circle-dot': 'M8 1a7 7 0 100 14A7 7 0 008 1zM8 6a2 2 0 100 4 2 2 0 000-4z',
32
+ 'spinner': 'M8 1.5v2.5M8 12v2.5M3.4 3.4l1.8 1.8M10.8 10.8l1.8 1.8M1.5 8H4M12 8h2.5M3.4 12.6l1.8-1.8M10.8 5.2l1.8-1.8',
33
+
34
+ // Actions
35
+ 'plus': 'M8 3v10M3 8h10',
36
+ 'minus': 'M3 8h10',
37
+ 'search': 'M11.5 11.5L14 14M2.5 7a4.5 4.5 0 109 0 4.5 4.5 0 00-9 0z',
38
+ 'edit': 'M11 2l3 3-8 8H3v-3l8-8zM10 3l3 3',
39
+ 'trash': 'M3 4h10M5.5 4V2.5h5V4M4.5 4l.5 9.5h6L11.5 4M6.5 7v4M9.5 7v4',
40
+ 'copy': 'M5 5V3a1 1 0 011-1h7a1 1 0 011 1v7a1 1 0 01-1 1h-2M3 5h7a1 1 0 011 1v7a1 1 0 01-1 1H3a1 1 0 01-1-1V6a1 1 0 011-1z',
41
+ 'download': 'M8 2v8M4 7l4 4 4-4M2 13h12',
42
+ 'upload': 'M8 11V3M4 6l4-4 4 4M2 13h12',
43
+ 'send': 'M14 2L1 8l5 2 8-8zM14 2l-6 12-2-5',
44
+ 'refresh': 'M14 4v3.5h-3.5M2 12V8.5h3.5M3 6a5.5 5.5 0 0110.5-1M13 10a5.5 5.5 0 01-10.5 1',
45
+
46
+ // Files & system
47
+ 'file': 'M4 1h6l3 3v10a1 1 0 01-1 1H4a1 1 0 01-1-1V2a1 1 0 011-1zM10 1v3h3',
48
+ 'folder': 'M2 4a1 1 0 011-1h3l1.5 1.5H13a1 1 0 011 1V13a1 1 0 01-1 1H3a1 1 0 01-1-1V4z',
49
+ 'terminal': 'M2 3h12a1 1 0 011 1v8a1 1 0 01-1 1H2a1 1 0 01-1-1V4a1 1 0 011-1zM4 6l2 2-2 2M8 10h4',
50
+ 'code': 'M5 4L1 8l4 4M11 4l4 4-4 4',
51
+ 'settings': 'M8 5.5a2.5 2.5 0 100 5 2.5 2.5 0 000-5zM8 1v1.5M8 13.5V15M2.5 8H1M15 8h-1.5M3.5 3.5l1 1M11.5 11.5l1 1M3.5 12.5l1-1M11.5 4.5l1-1',
52
+ 'theme': 'M8 1a7 7 0 100 14V1z',
53
+
54
+ // Person
55
+ 'user': 'M8 8a3 3 0 100-6 3 3 0 000 6zM2 14c.5-3 3-5 6-5s5.5 2 6 5',
56
+ 'logout': 'M10 4V3a1 1 0 00-1-1H3a1 1 0 00-1 1v10a1 1 0 001 1h6a1 1 0 001-1v-1M7 8h7M11 5l3 3-3 3',
57
+ };
58
+
59
+ const SVG_NS = 'http://www.w3.org/2000/svg';
60
+
61
+ function build(name, opts) {
62
+ const d = PATHS[name];
63
+ if (!d) {
64
+ console.warn('[artificer/icons] Unknown icon: ' + name);
65
+ return null;
66
+ }
67
+ const svg = document.createElementNS(SVG_NS, 'svg');
68
+ const size = (opts && opts.size) || 16;
69
+ svg.setAttribute('viewBox', '0 0 16 16');
70
+ svg.setAttribute('width', size);
71
+ svg.setAttribute('height', size);
72
+ svg.setAttribute('fill', 'none');
73
+ svg.setAttribute('stroke', 'currentColor');
74
+ svg.setAttribute('stroke-width', (opts && opts.strokeWidth) || 1.5);
75
+ svg.setAttribute('stroke-linecap', 'round');
76
+ svg.setAttribute('stroke-linejoin', 'round');
77
+ svg.setAttribute('aria-hidden', 'true');
78
+ svg.setAttribute('focusable', 'false');
79
+ svg.classList.add('icon');
80
+ if (opts && opts.className) {
81
+ svg.classList.add(...opts.className.split(/\s+/).filter(Boolean));
82
+ }
83
+
84
+ // Split path data on M commands so each subpath is its own <path>;
85
+ // keeps stroke joins clean across disconnected strokes.
86
+ const subpaths = d.split(/(?=M)/g).filter(Boolean);
87
+ subpaths.forEach(p => {
88
+ const path = document.createElementNS(SVG_NS, 'path');
89
+ path.setAttribute('d', p.trim());
90
+ svg.appendChild(path);
91
+ });
92
+ return svg;
93
+ }
94
+
95
+ // Replace <i data-icon="name"></i> placeholders in the DOM
96
+ function hydrate(root) {
97
+ const scope = root || document;
98
+ scope.querySelectorAll('[data-icon]').forEach(el => {
99
+ if (el.dataset.iconHydrated === '1') return;
100
+ const name = el.getAttribute('data-icon');
101
+ const svg = build(name, {
102
+ size: el.getAttribute('data-icon-size') || 16,
103
+ strokeWidth: el.getAttribute('data-icon-stroke') || 1.5,
104
+ });
105
+ if (!svg) return;
106
+ el.innerHTML = '';
107
+ el.appendChild(svg);
108
+ el.dataset.iconHydrated = '1';
109
+ });
110
+ }
111
+
112
+ // Public API
113
+ // SPA lifecycle — auto-hydrate nodes inserted after first paint, and
114
+ // re-hydrate existing nodes when their data-icon* attributes change.
115
+ // Returns a disconnect fn. Idempotent guards make the re-scan cheap.
116
+ function observe(root) {
117
+ root = root || document.body;
118
+ hydrate(root);
119
+ if (typeof MutationObserver === 'undefined') return function () {};
120
+ var scheduled = false;
121
+ var mo = new MutationObserver(function (mutations) {
122
+ // Clear the hydration lock on any node whose icon attrs changed so
123
+ // hydrate() rebuilds it rather than skipping it as already-done.
124
+ mutations.forEach(function (m) {
125
+ if (m.type === 'attributes' && m.target.hasAttribute('data-icon')) {
126
+ delete m.target.dataset.iconHydrated;
127
+ }
128
+ });
129
+ if (scheduled) return;
130
+ scheduled = true;
131
+ (window.requestAnimationFrame || window.setTimeout)(function () { scheduled = false; hydrate(root); }, 0);
132
+ });
133
+ mo.observe(root, {
134
+ childList: true,
135
+ subtree: true,
136
+ attributes: true,
137
+ attributeFilter: ['data-icon', 'data-icon-size', 'data-icon-stroke'],
138
+ });
139
+ return function () { mo.disconnect(); };
140
+ }
141
+
142
+ window.ArtificerIcons = {
143
+ build: build,
144
+ hydrate: hydrate,
145
+ observe: observe,
146
+ list: () => Object.keys(PATHS),
147
+ };
148
+
149
+ // Auto-hydrate on DOM ready, then arm the MutationObserver for SPA use.
150
+ // observe() calls hydrate() internally, so no double-scan on static pages.
151
+ function autoInit() {
152
+ if (typeof MutationObserver !== 'undefined') {
153
+ observe(document.body);
154
+ } else {
155
+ hydrate();
156
+ }
157
+ }
158
+ if (document.readyState === 'loading') {
159
+ document.addEventListener('DOMContentLoaded', autoInit);
160
+ } else {
161
+ autoInit();
162
+ }
163
+ })();
@@ -0,0 +1,169 @@
1
+ /* Artificer · tablist keyboard helper (WAI-ARIA APG tabs pattern).
2
+ The .tabs primitive ships the LOOK (active underline keyed off
3
+ [aria-selected]); this ships the unavoidable-JS BEHAVIOR — roving
4
+ tabindex, arrow-key movement, Home/End, selection, and panel toggling
5
+ via aria-controls — the same way artificer-focus.js ships focus-trapping.
6
+
7
+ Usage (in-page view-switching tabs — see .tabs doctrine, "switch the VIEW"):
8
+ <div class="tabs" data-tabs>
9
+ <button aria-controls="p1" aria-selected="true">Overview</button>
10
+ <button aria-controls="p2">Activity</button>
11
+ </div>
12
+ <section id="p1">…</section>
13
+ <section id="p2">…</section>
14
+
15
+ ArtificerTabs.enhance(tablistEl, { onSelect(tab, i){…} }); // one element
16
+ ArtificerTabs.observe(document); // auto-enhance every [data-tabs] (SPA)
17
+
18
+ Orientation follows aria-orientation (default "horizontal"): horizontal uses
19
+ ←/→, vertical uses ↑/↓. enhance() is idempotent and returns a handle with
20
+ { select(i), destroy() }. Anchors used as tabs are treated as in-page
21
+ (default click prevented) — for cross-page navigation use .sidenav, not .tabs. */
22
+ (function () {
23
+ 'use strict';
24
+
25
+ // --- pure: the roving-tabindex state machine (unit-tested, no DOM) ---------
26
+ // Returns the index the focus/selection should move to for `key`, or null if
27
+ // the key isn't a tab-navigation key (caller does nothing, no preventDefault).
28
+ function nextIndex(key, current, count, opts) {
29
+ if (!(count > 0)) return null;
30
+ var vertical = opts && opts.orientation === 'vertical';
31
+ var fwd = vertical ? 'ArrowDown' : 'ArrowRight';
32
+ var back = vertical ? 'ArrowUp' : 'ArrowLeft';
33
+ switch (key) {
34
+ case fwd:
35
+ return (current + 1) % count;
36
+ case back:
37
+ return (current - 1 + count) % count;
38
+ case 'Home':
39
+ return 0;
40
+ case 'End':
41
+ return count - 1;
42
+ default:
43
+ return null;
44
+ }
45
+ }
46
+
47
+ var noop = { select: function () {}, destroy: function () {} };
48
+
49
+ // --- DOM: wire one tablist (verified in live-spec/navigation.html) ---------
50
+ function enhance(tablist, opts) {
51
+ opts = opts || {};
52
+ if (!tablist) return noop;
53
+ if (tablist.__artificerTabs) return tablist.__artificerTabs;
54
+
55
+ var tabs = Array.prototype.filter.call(tablist.children, function (n) {
56
+ return n.matches && n.matches('button, a, [role="tab"]');
57
+ });
58
+ if (!tabs.length) return noop;
59
+
60
+ var orientation =
61
+ opts.orientation || tablist.getAttribute('aria-orientation') || 'horizontal';
62
+
63
+ tablist.setAttribute('role', 'tablist');
64
+ tablist.setAttribute('aria-orientation', orientation);
65
+
66
+ function panelOf(tab) {
67
+ var id = tab.getAttribute('aria-controls');
68
+ return id ? document.getElementById(id) : null;
69
+ }
70
+
71
+ // Initial selection: an explicit aria-selected / aria-current wins, else first.
72
+ var selected = -1;
73
+ tabs.forEach(function (t, j) {
74
+ if (
75
+ selected < 0 &&
76
+ (t.getAttribute('aria-selected') === 'true' ||
77
+ t.getAttribute('aria-current') === 'page')
78
+ ) {
79
+ selected = j;
80
+ }
81
+ });
82
+ if (selected < 0) selected = 0;
83
+
84
+ function render(i, focus) {
85
+ tabs.forEach(function (tab, j) {
86
+ var on = j === i;
87
+ tab.setAttribute('role', 'tab');
88
+ tab.setAttribute('aria-selected', on ? 'true' : 'false');
89
+ tab.tabIndex = on ? 0 : -1;
90
+ var panel = panelOf(tab);
91
+ if (panel) {
92
+ panel.setAttribute('role', 'tabpanel');
93
+ if (tab.id) panel.setAttribute('aria-labelledby', tab.id);
94
+ panel.hidden = !on;
95
+ if (on) panel.tabIndex = 0;
96
+ }
97
+ });
98
+ selected = i;
99
+ if (focus) tabs[i].focus();
100
+ if (typeof opts.onSelect === 'function') opts.onSelect(tabs[i], i);
101
+ }
102
+
103
+ function onKeydown(e) {
104
+ var target = nextIndex(e.key, selected, tabs.length, { orientation: orientation });
105
+ if (target === null) return;
106
+ e.preventDefault();
107
+ render(target, true);
108
+ }
109
+ function onClick(e) {
110
+ var tab = e.target.closest('button, a, [role="tab"]');
111
+ if (!tab || !tablist.contains(tab)) return;
112
+ var i = tabs.indexOf(tab);
113
+ if (i < 0) return;
114
+ if (tab.tagName === 'A') e.preventDefault(); // in-page tab, not navigation
115
+ render(i, true);
116
+ }
117
+
118
+ tablist.addEventListener('keydown', onKeydown);
119
+ tablist.addEventListener('click', onClick);
120
+ render(selected, false); // sync ARIA/tabindex/panels without stealing focus
121
+
122
+ var handle = {
123
+ select: function (i) {
124
+ render(i, false);
125
+ },
126
+ destroy: function () {
127
+ tablist.removeEventListener('keydown', onKeydown);
128
+ tablist.removeEventListener('click', onClick);
129
+ delete tablist.__artificerTabs;
130
+ },
131
+ };
132
+ tablist.__artificerTabs = handle;
133
+ return handle;
134
+ }
135
+
136
+ // Auto-enhance every [data-tabs] now, and any inserted later (SPA lifecycle,
137
+ // matching ArtificerIcons.observe / Whimsy.observe). Returns a disconnect fn.
138
+ function observe(root) {
139
+ root = root || (typeof document !== 'undefined' ? document : null);
140
+ if (!root) return function () {};
141
+ function enhanceIn(scope) {
142
+ if (scope.matches && scope.matches('[data-tabs]')) enhance(scope);
143
+ if (scope.querySelectorAll) {
144
+ Array.prototype.forEach.call(scope.querySelectorAll('[data-tabs]'), function (el) {
145
+ enhance(el);
146
+ });
147
+ }
148
+ }
149
+ enhanceIn(root);
150
+ if (typeof MutationObserver === 'undefined') return function () {};
151
+ var mo = new MutationObserver(function (muts) {
152
+ muts.forEach(function (m) {
153
+ Array.prototype.forEach.call(m.addedNodes || [], function (n) {
154
+ if (n.nodeType === 1) enhanceIn(n);
155
+ });
156
+ });
157
+ });
158
+ mo.observe(root === document ? document.body : root, { childList: true, subtree: true });
159
+ return function () {
160
+ mo.disconnect();
161
+ };
162
+ }
163
+
164
+ var api = { enhance: enhance, observe: observe, nextIndex: nextIndex };
165
+ // globalThis === window in a browser (so window.ArtificerTabs works);
166
+ // in Node it lets the unit test import this file and exercise nextIndex.
167
+ if (typeof window !== 'undefined') window.ArtificerTabs = api;
168
+ else if (typeof globalThis !== 'undefined') globalThis.ArtificerTabs = api;
169
+ })();
@@ -0,0 +1,63 @@
1
+ // Artificer · theme handling (persisted in localStorage, shared across pages)
2
+ (function () {
3
+ var KEY = 'artificer.theme';
4
+ var stored = null;
5
+ try { stored = localStorage.getItem(KEY); } catch (e) {}
6
+ if (stored === 'light' || stored === 'dark') {
7
+ document.documentElement.setAttribute('data-theme', stored);
8
+ }
9
+
10
+ function apply(next) {
11
+ document.documentElement.setAttribute('data-theme', next);
12
+ try { localStorage.setItem(KEY, next); } catch (e) {}
13
+ document.querySelectorAll('[data-theme-toggle]').forEach(function (b) {
14
+ var label = b.querySelector('[data-theme-label]');
15
+ if (label) label.textContent = next === 'light' ? 'Light' : 'Dark';
16
+ });
17
+ }
18
+
19
+ function bind() {
20
+ var initial = document.documentElement.getAttribute('data-theme') ||
21
+ (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark');
22
+ document.querySelectorAll('[data-theme-toggle]').forEach(function (b) {
23
+ if (b.dataset.themeBound === '1') return;
24
+ b.dataset.themeBound = '1';
25
+ var label = b.querySelector('[data-theme-label]');
26
+ if (label) label.textContent = initial === 'light' ? 'Light' : 'Dark';
27
+ b.addEventListener('click', function () {
28
+ var cur = document.documentElement.getAttribute('data-theme') ||
29
+ (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark');
30
+ apply(cur === 'light' ? 'dark' : 'light');
31
+ });
32
+ });
33
+ }
34
+
35
+ function toggle() {
36
+ var cur = document.documentElement.getAttribute('data-theme') ||
37
+ (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark');
38
+ apply(cur === 'light' ? 'dark' : 'light');
39
+ }
40
+ // SPA lifecycle — auto-hydrate nodes inserted after first paint. Returns a
41
+ // disconnect fn. Idempotent guards make the re-scan cheap (done nodes skip).
42
+ function observe(root) {
43
+ root = root || document.body;
44
+ bind();
45
+ if (typeof MutationObserver === 'undefined') return function () {};
46
+ var scheduled = false;
47
+ var mo = new MutationObserver(function () {
48
+ if (scheduled) return;
49
+ scheduled = true;
50
+ (window.requestAnimationFrame || window.setTimeout)(function () { scheduled = false; bind(); }, 0);
51
+ });
52
+ mo.observe(root, { childList: true, subtree: true });
53
+ return function () { mo.disconnect(); };
54
+ }
55
+ window.ArtificerTheme = { apply: apply, toggle: toggle, bind: bind, observe: observe };
56
+
57
+ // Runs whether the script lands before or after DOMContentLoaded.
58
+ if (document.readyState === 'loading') {
59
+ document.addEventListener('DOMContentLoaded', bind);
60
+ } else {
61
+ bind();
62
+ }
63
+ })();