@aitrien/motifspec 1.27.0 → 1.27.2

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/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  Every release of MotifSpec (named brand-codex until 1.10.0), newest first. Entries before 1.10.0 keep the names they were released under. Versions follow the rules in `CONVENTIONS.md`: renaming or removing a semantic token is major, adding tokens, components or icons is minor, and changing a value or fixing a description is a patch. `npm run build` fails if the version in `package.json` has no entry here.
4
4
 
5
+ ## [1.27.2] - 2026-12-06
6
+
7
+ ### Fixed
8
+ - Header nav links are 40px tall (`size.target.control`), the same as the search field, so a docs header's links and search line up. Their side padding is unchanged.
9
+ - A focused nav link's focus ring is no longer cut off at the top and bottom. The nav list scrolls sideways when it has to, which clipped the ring; it now keeps room for the ring inside it, without moving the links or changing the header's height.
10
+
11
+ ## [1.27.1] - 2026-11-19
12
+
13
+ ### Fixed
14
+ - `codex.js` works with Turbo (Rails), or anything that changes the page without a reload. It sets each page's elements up again after every `turbo:load`, `turbo:frame-load` and `turbo:morph`, so the theme toggle, alert dismiss buttons, copy buttons, tabs, search, date pickers and the rest work after a visit or a redirect, not only on the first full load. Listeners on the document are set once, no element is set up twice, and a calendar, dialog or menu left open is closed before Turbo caches the page.
15
+
5
16
  ## [1.27.0] - 2026-11-19
6
17
 
7
18
  ### Added
package/README.md CHANGED
@@ -79,7 +79,7 @@ Pin an exact version, as a Hugo site pins a tag: the version you install is the
79
79
  |---|---|
80
80
  | `@aitrien/motifspec/codex.css` | Tokens, fonts and every component. Its font URLs are `../fonts/…`, so a bundler that copies assets (Vite, webpack, Parcel) resolves them from the package. Without a bundler, serve `dist/` and `fonts/` side by side. |
81
81
  | `@aitrien/motifspec/tokens.css`, `fonts.css`, `chroma.css` | The parts on their own: tokens only, `@font-face` only, syntax highlighting only. |
82
- | `@aitrien/motifspec/codex.js` | Behaviour: theme toggle, copy buttons, TOC, tooltips. Load with `defer`. |
82
+ | `@aitrien/motifspec/codex.js` | Behaviour: theme toggle, copy buttons, TOC, tooltips, alerts, toasts, date pickers, dialogs. Load with `defer`. With Turbo (Rails), it sets each new page up after every visit, so it needs nothing more. |
83
83
  | `@aitrien/motifspec/theme-init.js` | Inline it in `<head>` before the stylesheet so a stored theme applies before first paint. |
84
84
  | `@aitrien/motifspec/plate.js`, `backdrop.html`, `backdrop-foot.html`, `backdrop-side.html` | The contour backdrop and its motion (`backdrop.md`). |
85
85
  | `@aitrien/motifspec/icons.svg` | The icon sprite. Inline it once in `<body>`. |
@@ -169,7 +169,7 @@ layouts/ Hugo partials (_partials/motifspec/) and Markdown render hoo
169
169
  docs/syntax-highlighting.md
170
170
  fonts/ Self-hosted WOFF2 (Latin subsets) and their OFL licences
171
171
  scripts/ validate, build, audit, check (the rule checker projects run), pack-check, release, and docs with docs-reference (motifspec.com and its generated pages)
172
- js/codex.js Theme toggle, copy buttons, tooltip dismissal, TOC docking and active section, search, tabs and the component preview
172
+ js/codex.js Theme toggle, copy buttons, tooltip dismissal, TOC docking and active section, search, tabs, alerts, toasts, date pickers, dialogs and the component preview
173
173
  js/theme-init.js One-line <head> snippet that applies a stored theme before first paint
174
174
  js/plate.js Contour survey generator for the backdrop and the specimen plate: draws the static contours at build time and animates them in the browser
175
175
  site/ motifspec.com: a Hugo site whose pages are made from this README's sections, docs/, CONVENTIONS.md and CHANGELOG.md
package/dist/codex.css CHANGED
@@ -2,7 +2,7 @@
2
2
  @font-face { font-family: "Geist Mono"; src: url("../fonts/geist-mono-latin-wght.woff2") format("woff2"); font-weight: 100 900; font-style: normal; font-display: swap; }
3
3
  @font-face { font-family: "Yarndings 12"; src: url("../fonts/yarndings-12-latin.woff2") format("woff2"); font-weight: 400; font-style: normal; font-display: block; }
4
4
 
5
- /* MotifSpec 1.27.0 — generated from tokens/*.json. Do not edit.
5
+ /* MotifSpec 1.27.2 — generated from tokens/*.json. Do not edit.
6
6
  Themes: semantic colours use light-dark(); color-scheme follows the system unless
7
7
  <html data-theme="light|dark"> pins it. --cx-primitive-* are for this file only. */
8
8
  :root {
@@ -833,12 +833,18 @@ p, li, dd, figcaption { text-wrap: pretty; }
833
833
  /* A relative offset, not a margin: it moves the links without changing the row's width, so it can't change whether
834
834
  the header wraps. */
835
835
  .cx-header[data-wrapped] .cx-nav { position: relative; inset-inline-start: calc(var(--_nav-pad) * -1); }
836
- .cx-nav ul { display: flex; gap: 0 var(--cx-space-inline-xs); margin: 0; padding: 0; list-style: none; overflow-x: auto; scrollbar-width: thin; }
836
+ /* The list scrolls sideways when it has to, which clips whatever is drawn outside it. Its padding is as wide as the
837
+ focus ring and its offset, and a matching negative margin keeps it in place, so a focused link's ring shows whole. */
838
+ .cx-nav ul {
839
+ --_ring: calc(var(--cx-border-focus-width) + var(--cx-border-focus-offset));
840
+ display: flex; gap: 0 var(--cx-space-inline-xs); margin: calc(var(--_ring) * -1); padding: var(--_ring);
841
+ list-style: none; overflow-x: auto; scrollbar-width: thin;
842
+ }
837
843
  .cx-nav li { flex: none; }
838
844
  .cx-nav a {
839
845
  /* Halfway between inset-sm and inset-md, so Work, Writing, About, the pause control and the toggle fit one row on a
840
- 360px phone; narrower than that, the list scrolls inside its row. */
841
- display: inline-flex; align-items: center; min-height: var(--cx-size-target-touch); padding-inline: var(--_nav-pad);
846
+ 360px phone; narrower than that, the list scrolls inside its row. As tall as the search field beside it. */
847
+ display: inline-flex; align-items: center; min-height: var(--cx-size-target-control); padding-inline: var(--_nav-pad);
842
848
  border-radius: var(--cx-radius-pill); color: var(--cx-color-text-secondary); text-decoration: none; font-weight: var(--cx-font-weight-ui);
843
849
  transition: color var(--cx-motion-duration-fast) var(--cx-motion-easing-standard), background-color var(--cx-motion-duration-fast) var(--cx-motion-easing-standard);
844
850
  }
package/dist/codex.js CHANGED
@@ -4,6 +4,9 @@
4
4
  // Enhancement only. Without this script: themes follow the system, code is selectable, the TOC is a plain open list,
5
5
  // and every control that needs script stays hidden. Load it with `defer`; it also works inline at the end of <body>.
6
6
  // Pair it with dist/theme-init.js inline in <head>, which applies a stored theme before first paint.
7
+ // Pages that change without a reload (Turbo in Rails, or anything that fires turbo:load): listeners on the document
8
+ // are set once, and each page's elements are set up again after every visit, frame load and morph. An element is
9
+ // never set up twice, and anything left open is closed before Turbo caches the page.
7
10
  (function () {
8
11
  'use strict';
9
12
  var root = document.documentElement;
@@ -46,12 +49,358 @@
46
49
  apply(theme);
47
50
  if (system && system.addEventListener) system.addEventListener('change', function () { apply(theme); });
48
51
 
52
+ // Set-up bookkeeping, so it can run on every page a Turbo visit brings in. fresh() is true the first time an
53
+ // element is set up for a job (an element restored from Turbo's cache is a new element, so it's set up again).
54
+ // watch() and observe() tie a window listener or an observer to an element, and drop it once that element leaves
55
+ // the page, so visits don't pile them up.
56
+ var done = new WeakMap();
57
+ function fresh(el, job) {
58
+ var jobs = done.get(el);
59
+ if (!jobs) { jobs = {}; done.set(el, jobs); }
60
+ if (jobs[job]) return false;
61
+ jobs[job] = true; return true;
62
+ }
63
+ function each(selector, fn) { Array.prototype.forEach.call(document.querySelectorAll(selector), fn); }
64
+ var watchers = { resize: [], scroll: [] };
65
+ function watch(type, el, fn) { watchers[type].push({ el: el, fn: fn }); }
66
+ function run(type) {
67
+ watchers[type] = watchers[type].filter(function (w) { return w.el.isConnected; });
68
+ watchers[type].forEach(function (w) { w.fn(); });
69
+ }
70
+ window.addEventListener('resize', function () { run('resize'); });
71
+ window.addEventListener('scroll', function () { run('scroll'); }, { passive: true });
72
+ var observers = [];
73
+ function observe(el, observer, target) {
74
+ if (target !== undefined) observer.observe(target);
75
+ else if (observer.observe && !(observer instanceof IntersectionObserver)) observer.observe(el);
76
+ observers.push({ el: el, observer: observer });
77
+ return observer;
78
+ }
79
+ function sweep() {
80
+ observers = observers.filter(function (o) { if (o.el.isConnected) return true; o.observer.disconnect(); return false; });
81
+ }
82
+ var openers = new WeakMap();
83
+
49
84
  function ready(fn) { if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn); else fn(); }
50
85
 
51
- ready(function () {
86
+ // ------------------------------------------------------------ once: listeners on the document
87
+ // Escape hides any open tooltip without moving the pointer or focus (WCAG 1.4.13).
88
+ document.addEventListener('keydown', function (e) {
89
+ if (e.key !== 'Escape') return;
90
+ document.querySelectorAll('.cx-tooltip').forEach(function (t) { t.parentElement.classList.add('is-tooltip-dismissed'); });
91
+ });
92
+
93
+ // The docs header's panels close on Escape and on a click outside them, whichever page they're on.
94
+ var headerPanels = function () { return Array.prototype.slice.call(document.querySelectorAll('.cx-header__toc, .cx-header__menu')); };
95
+ document.addEventListener('keydown', function (e) {
96
+ if (e.key !== 'Escape') return;
97
+ headerPanels().forEach(function (d) { if (d.open) { d.open = false; d.querySelector('summary').focus(); } });
98
+ });
99
+ document.addEventListener('click', function (e) { headerPanels().forEach(function (d) { if (d.open && !d.contains(e.target)) d.open = false; }); });
100
+
101
+ // Inside a component preview, links don't navigate.
102
+ document.addEventListener('click', function (e) {
103
+ if (document.body && document.body.classList.contains('cx-preview-body') && e.target.closest && e.target.closest('a[href]')) e.preventDefault();
104
+ });
105
+
106
+ // Search's shortcut: Ctrl+K or Cmd+K, or "/" outside a text field, opens the page's search.
107
+ document.addEventListener('keydown', function (e) {
108
+ var dialog = document.querySelector('.cx-search[data-search]'), open = dialog && openers.get(dialog);
109
+ if (!open) return;
110
+ var typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName) || document.activeElement.isContentEditable;
111
+ if (((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') || (e.key === '/' && !typing && !e.ctrlKey && !e.metaKey && !e.altKey)) {
112
+ e.preventDefault(); open();
113
+ }
114
+ });
115
+
116
+ // Alert dismissal (alert.md): pressing the button hides its alert, and with a key keeps it hidden on later pages.
117
+ // Focus moves to the main column, since the button it was on has gone. One listener on the document, so an alert
118
+ // on any page, or added later, works the same way.
119
+ var dismissed = function (key) { try { return Boolean(key) && localStorage.getItem('cx-alert:' + key) === 'dismissed'; } catch (e) { return false; } };
120
+ document.addEventListener('click', function (e) {
121
+ var btn = e.target.closest && e.target.closest('.cx-alert [data-alert-dismiss]');
122
+ if (!btn) return;
123
+ var key = btn.dataset.alertDismiss;
124
+ btn.closest('.cx-alert').hidden = true;
125
+ if (key) { try { localStorage.setItem('cx-alert:' + key, 'dismissed'); } catch (err) { /* storage blocked: hidden for this view only */ } }
126
+ var main = document.getElementById('main');
127
+ if (main) main.focus({ preventScroll: true });
128
+ });
129
+
130
+
131
+ // Toasts (toast.md): document.dispatchEvent(new CustomEvent('cx-toast', { detail: { text: 'Saved', kind: 'success' } })).
132
+ // One polite live region per page, made here if the page has none, holds up to three; each goes after
133
+ // motion.duration.toast, a time that starts again while the pointer or focus is on it. Escape inside one, or its
134
+ // button, closes it. The text is set as text, never as HTML.
135
+ var toastRegion = function () {
136
+ var region = document.querySelector('.cx-toasts');
137
+ if (!region) {
138
+ region = document.createElement('div'); region.className = 'cx-toasts'; region.setAttribute('role', 'status');
139
+ document.body.appendChild(region);
140
+ }
141
+ return region;
142
+ };
143
+ var toastIcon = function (id, cls) {
144
+ var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
145
+ svg.setAttribute('class', 'cx-icon cx-icon--sm' + (cls ? ' ' + cls : '')); svg.setAttribute('aria-hidden', 'true'); svg.setAttribute('focusable', 'false');
146
+ var use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); use.setAttribute('href', '#cx-icon-' + id);
147
+ svg.appendChild(use); return svg;
148
+ };
149
+ var closeToast = function (toast) {
150
+ if (toast.classList.contains('is-leaving')) return;
151
+ clearTimeout(toast._cxTimer);
152
+ var hadFocus = toast.contains(document.activeElement);
153
+ toast.classList.add('is-leaving'); toast.classList.remove('is-shown');
154
+ setTimeout(function () { toast.remove(); }, ms('--cx-motion-duration-base'));
155
+ if (hadFocus) { var main = document.getElementById('main'); if (main) main.focus({ preventScroll: true }); }
156
+ };
157
+ document.addEventListener('cx-toast', function (e) {
158
+ var d = e.detail || {};
159
+ if (!d.text) return;
160
+ var toast = document.createElement('div'); toast.className = 'cx-toast';
161
+ if (d.kind === 'success') toast.appendChild(toastIcon('check'));
162
+ var p = document.createElement('p'); p.className = 'cx-toast__text'; p.textContent = d.text; toast.appendChild(p);
163
+ var btn = document.createElement('button'); btn.type = 'button'; btn.className = 'cx-toast__dismiss'; btn.setAttribute('aria-label', 'Dismiss');
164
+ btn.appendChild(toastIcon('close')); btn.addEventListener('click', function () { closeToast(toast); }); toast.appendChild(btn);
165
+ var wait = function () { clearTimeout(toast._cxTimer); toast._cxTimer = setTimeout(function () { closeToast(toast); }, ms('--cx-motion-duration-toast')); };
166
+ var hold = function () { clearTimeout(toast._cxTimer); };
167
+ toast.addEventListener('mouseenter', hold); toast.addEventListener('focusin', hold);
168
+ toast.addEventListener('mouseleave', function () { if (!toast.contains(document.activeElement)) wait(); });
169
+ toast.addEventListener('focusout', function (ev) { if (!toast.contains(ev.relatedTarget) && !toast.matches(':hover')) wait(); });
170
+ toast.addEventListener('keydown', function (ev) { if (ev.key === 'Escape') { ev.stopPropagation(); closeToast(toast); } });
171
+ var region = toastRegion();
172
+ var live = region.querySelectorAll('.cx-toast:not(.is-leaving)');
173
+ if (live.length >= 3) closeToast(live[0]);
174
+ region.appendChild(toast);
175
+ window.requestAnimationFrame(function () { window.requestAnimationFrame(function () { toast.classList.add('is-shown'); }); });
176
+ wait();
177
+ });
178
+
179
+ // Dialogs (dialog.md): [data-dialog-open="id"] opens a .cx-dialog with showModal(), which moves focus in, closes on
180
+ // Escape and returns focus when closed. [data-dialog-close] inside one closes it, and so does a click on the
181
+ // backdrop, unless the dialog has data-dialog-keep (a form, whose typing a stray click would lose).
182
+ document.addEventListener('click', function (e) {
183
+ var t = e.target.closest ? e.target : null;
184
+ if (!t) return;
185
+ var opener = t.closest('[data-dialog-open]');
186
+ if (opener) {
187
+ var d = document.getElementById(opener.dataset.dialogOpen);
188
+ if (d && d.showModal && !d.open) d.showModal();
189
+ return;
190
+ }
191
+ var dialog = t.closest('dialog.cx-dialog');
192
+ if (!dialog) return;
193
+ if (t.closest('[data-dialog-close]')) { dialog.close(); return; }
194
+ if (t === dialog && !dialog.hasAttribute('data-dialog-keep')) {
195
+ var r = dialog.getBoundingClientRect();
196
+ if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) dialog.close();
197
+ }
198
+ });
199
+
200
+ // Date pickers (date-picker.md): each [data-date-picker] box gets its calendar button, and the page gets one
201
+ // calendar, made the first time it's opened. Dates are day first (DD/MM/YYYY) and weeks start on Monday. The
202
+ // calendar sits under its field, or above it when there's more room there, in page coordinates so it scrolls
203
+ // with the field. Days, months and years are each a grid with one tab stop, moved by the arrow keys.
204
+ var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
205
+ var DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
206
+ var PAGE = 20;
207
+ var placing = false, cal = null, calBody, calDays, calList, calTitle, monthBtn, yearBtn, navs;
208
+ var field = null, opener = null, selected = null, focusDay = null, view = 'days';
209
+ var today = function () { var n = new Date(); return new Date(n.getFullYear(), n.getMonth(), n.getDate()); };
210
+ var same = function (a, b) { return a && b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); };
211
+ var pad = function (n) { return String(n).padStart(2, '0'); };
212
+ var parse = function (v) {
213
+ var m = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(v.trim());
214
+ if (!m) return null;
215
+ var d = new Date(+m[3], +m[2] - 1, +m[1]);
216
+ return d.getMonth() === +m[2] - 1 ? d : null;
217
+ };
218
+ var clampDay = function (y, m, d) { return new Date(y, m, Math.min(d, new Date(y, m + 1, 0).getDate())); };
219
+ var icon = function (id) {
220
+ var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
221
+ svg.setAttribute('class', 'cx-icon'); svg.setAttribute('aria-hidden', 'true'); svg.setAttribute('focusable', 'false');
222
+ var use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); use.setAttribute('href', '#cx-icon-' + id);
223
+ svg.appendChild(use); return svg;
224
+ };
225
+ var el = function (tag, cls, attrs) {
226
+ var n = document.createElement(tag); if (cls) n.className = cls;
227
+ Object.keys(attrs || {}).forEach(function (k) { n.setAttribute(k, attrs[k]); });
228
+ return n;
229
+ };
230
+ var build = function () {
231
+ // a page restored from Turbo's cache can bring a copy of the last calendar with it
232
+ each('.cx-calendar', function (old) { old.remove(); });
233
+ cal = el('div', 'cx-calendar', { popover: '', role: 'dialog', 'aria-label': 'Choose a date' });
234
+ var head = el('div', 'cx-calendar__head');
235
+ var prev = el('button', 'cx-calendar__nav', { type: 'button', 'data-step': '-1' }); prev.appendChild(icon('arrow-back'));
236
+ var next = el('button', 'cx-calendar__nav', { type: 'button', 'data-step': '1' }); next.appendChild(icon('arrow-forward'));
237
+ var pick = el('div', 'cx-calendar__pickers');
238
+ monthBtn = el('button', 'cx-calendar__pick', { type: 'button', 'aria-expanded': 'false' });
239
+ yearBtn = el('button', 'cx-calendar__pick', { type: 'button', 'aria-expanded': 'false' });
240
+ [monthBtn, yearBtn].forEach(function (b) { b.appendChild(document.createElement('span')); b.appendChild(icon('expand')); pick.appendChild(b); });
241
+ head.appendChild(prev); head.appendChild(pick); head.appendChild(next);
242
+ calTitle = el('p', 'cx-visually-hidden', { 'aria-live': 'polite' });
243
+ calDays = el('table', 'cx-calendar__grid', { role: 'grid' });
244
+ var thead = el('thead'), tr = el('tr');
245
+ [1, 2, 3, 4, 5, 6, 0].forEach(function (i) { var th = el('th', '', { scope: 'col', abbr: DAYS[i] }); th.textContent = DAYS[i].slice(0, 2); tr.appendChild(th); });
246
+ thead.appendChild(tr); calDays.appendChild(thead); calBody = el('tbody'); calDays.appendChild(calBody);
247
+ calList = el('div', 'cx-calendar__list'); calList.hidden = true;
248
+ var foot = el('div', 'cx-calendar__foot');
249
+ var todayBtn = el('button', '', { type: 'button' }); todayBtn.textContent = 'Today';
250
+ var closeBtn = el('button', '', { type: 'button' }); closeBtn.textContent = 'Close';
251
+ foot.appendChild(todayBtn); foot.appendChild(closeBtn);
252
+ [head, calTitle, calDays, calList, foot].forEach(function (n) { cal.appendChild(n); });
253
+ document.body.appendChild(cal);
254
+ navs = [prev, next];
255
+ var labelId = 'cx-calendar-title'; calTitle.id = labelId; calDays.setAttribute('aria-labelledby', labelId);
256
+ monthBtn.setAttribute('aria-controls', 'cx-calendar-list'); yearBtn.setAttribute('aria-controls', 'cx-calendar-list'); calList.id = 'cx-calendar-list';
257
+
258
+ calBody.addEventListener('click', function (e) {
259
+ var b = e.target.closest('.cx-calendar__day'); if (!b) return;
260
+ var v = b.dataset.value.split('-'); choose(new Date(+v[0], +v[1], +v[2]));
261
+ });
262
+ calList.addEventListener('click', function (e) {
263
+ var b = e.target.closest('.cx-calendar__cell'); if (!b) return;
264
+ var v = +b.dataset.value;
265
+ if (view === 'years') { focusDay = clampDay(v, focusDay.getMonth(), focusDay.getDate()); setView('months'); }
266
+ else { focusDay = clampDay(focusDay.getFullYear(), v, focusDay.getDate()); setView('days'); }
267
+ });
268
+ monthBtn.addEventListener('click', function () { setView(view === 'months' ? 'days' : 'months'); });
269
+ yearBtn.addEventListener('click', function () { setView(view === 'years' ? 'days' : 'years'); });
270
+ navs.forEach(function (n) {
271
+ n.addEventListener('click', function () {
272
+ var st = +n.dataset.step, y = focusDay.getFullYear(), m = focusDay.getMonth(), d = focusDay.getDate();
273
+ focusDay = view === 'days' ? clampDay(y, m + st, d) : view === 'months' ? clampDay(y + st, m, d) : clampDay(y + st * PAGE, m, d);
274
+ render(false);
275
+ });
276
+ });
277
+ todayBtn.addEventListener('click', function () { view = 'days'; focusDay = today(); render(true); });
278
+ closeBtn.addEventListener('click', close);
279
+ cal.addEventListener('toggle', function (e) { if (e.newState !== 'open') view = 'days'; });
280
+ calBody.addEventListener('keydown', function (e) {
281
+ var d = new Date(focusDay), k = e.key;
282
+ if (k === 'ArrowLeft') d.setDate(d.getDate() - 1);
283
+ else if (k === 'ArrowRight') d.setDate(d.getDate() + 1);
284
+ else if (k === 'ArrowUp') d.setDate(d.getDate() - 7);
285
+ else if (k === 'ArrowDown') d.setDate(d.getDate() + 7);
286
+ else if (k === 'PageUp') d = clampDay(d.getFullYear(), d.getMonth() - 1, d.getDate());
287
+ else if (k === 'PageDown') d = clampDay(d.getFullYear(), d.getMonth() + 1, d.getDate());
288
+ else if (k === 'Home') d.setDate(d.getDate() - ((d.getDay() + 6) % 7));
289
+ else if (k === 'End') d.setDate(d.getDate() + (6 - (d.getDay() + 6) % 7));
290
+ else return;
291
+ e.preventDefault(); focusDay = d; render(true);
292
+ });
293
+ calList.addEventListener('keydown', function (e) {
294
+ var cols = view === 'years' ? 4 : 3, k = e.key, y = focusDay.getFullYear(), m = focusDay.getMonth(), dd = focusDay.getDate(), delta = 0;
295
+ if (k === 'ArrowLeft') delta = -1; else if (k === 'ArrowRight') delta = 1;
296
+ else if (k === 'ArrowUp') delta = -cols; else if (k === 'ArrowDown') delta = cols;
297
+ else if (k === 'PageUp') delta = view === 'years' ? -PAGE : -12; else if (k === 'PageDown') delta = view === 'years' ? PAGE : 12;
298
+ else return;
299
+ e.preventDefault();
300
+ if (view === 'months' && Math.abs(delta) < 12) { if (m + delta < 0 || m + delta > 11) return; focusDay = clampDay(y, m + delta, dd); }
301
+ else focusDay = view === 'years' ? clampDay(y + delta, m, dd) : clampDay(y + delta / 12, m, dd);
302
+ render(true);
303
+ });
304
+ if (!placing) { placing = true; window.addEventListener('resize', place); document.addEventListener('scroll', place, true); }
305
+ };
306
+ var cell = function (cls, text, label, o) {
307
+ var b = el('button', cls, { type: 'button', 'aria-label': label, 'aria-pressed': o.pressed ? 'true' : 'false' });
308
+ if (o.current) b.setAttribute('aria-current', 'date');
309
+ b.textContent = text; b.tabIndex = o.focus ? 0 : -1; b.dataset.value = o.value;
310
+ return b;
311
+ };
312
+ var render = function (moveFocus) {
313
+ var y = focusDay.getFullYear(), m = focusDay.getMonth(), now = today();
314
+ monthBtn.firstChild.textContent = MONTHS[m]; yearBtn.firstChild.textContent = y;
315
+ monthBtn.setAttribute('aria-expanded', String(view === 'months')); yearBtn.setAttribute('aria-expanded', String(view === 'years'));
316
+ monthBtn.setAttribute('aria-label', 'Choose the month, ' + MONTHS[m]); yearBtn.setAttribute('aria-label', 'Choose the year, ' + y);
317
+ calDays.hidden = view !== 'days'; calList.hidden = view === 'days';
318
+ var labels = { days: ['Previous month', 'Next month'], months: ['Previous year', 'Next year'], years: ['Previous ' + PAGE + ' years', 'Next ' + PAGE + ' years'] }[view];
319
+ navs[0].setAttribute('aria-label', labels[0]); navs[1].setAttribute('aria-label', labels[1]);
320
+ if (view === 'days') {
321
+ calTitle.textContent = MONTHS[m] + ' ' + y;
322
+ var first = new Date(y, m, 1), start = new Date(y, m, 1 - ((first.getDay() + 6) % 7));
323
+ calBody.textContent = '';
324
+ for (var w = 0; w < 6; w++) {
325
+ var row = el('tr');
326
+ for (var i = 0; i < 7; i++) {
327
+ var d = new Date(start.getFullYear(), start.getMonth(), start.getDate() + w * 7 + i), td = el('td');
328
+ td.appendChild(cell('cx-calendar__day' + (d.getMonth() !== m ? ' is-other' : ''), d.getDate(),
329
+ DAYS[d.getDay()] + ' ' + d.getDate() + ' ' + MONTHS[d.getMonth()] + ' ' + d.getFullYear(),
330
+ { pressed: same(d, selected), current: same(d, now), focus: same(d, focusDay), value: d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() }));
331
+ row.appendChild(td);
332
+ }
333
+ calBody.appendChild(row);
334
+ }
335
+ } else if (view === 'months') {
336
+ calTitle.textContent = 'Months of ' + y; calList.className = 'cx-calendar__list cx-calendar__list--months'; calList.textContent = '';
337
+ MONTHS.forEach(function (name, i) {
338
+ calList.appendChild(cell('cx-calendar__cell', name.slice(0, 3), name + ' ' + y,
339
+ { pressed: selected && selected.getFullYear() === y && selected.getMonth() === i, current: now.getFullYear() === y && now.getMonth() === i, focus: i === m, value: i }));
340
+ });
341
+ } else {
342
+ var from = Math.floor(y / PAGE) * PAGE;
343
+ calTitle.textContent = from + ' to ' + (from + PAGE - 1); calList.className = 'cx-calendar__list cx-calendar__list--years'; calList.textContent = '';
344
+ for (var k = from; k < from + PAGE; k++) {
345
+ calList.appendChild(cell('cx-calendar__cell', k, String(k), { pressed: selected && selected.getFullYear() === k, current: now.getFullYear() === k, focus: k === y, value: k }));
346
+ }
347
+ }
348
+ place();
349
+ if (moveFocus) { var f = (view === 'days' ? calBody : calList).querySelector('[tabindex="0"]'); if (f) f.focus(); }
350
+ };
351
+ var setView = function (v) { view = v; render(true); };
352
+ var place = function () {
353
+ if (!cal || !field || !cal.matches(':popover-open')) return;
354
+ var r = field.getBoundingClientRect(), vw = document.documentElement.clientWidth, h = cal.offsetHeight, w = cal.offsetWidth, gap = px('--cx-space-inline-xs'), edge = px('--cx-space-inline-sm');
355
+ var below = window.innerHeight - r.bottom - gap, above = r.top - gap;
356
+ var top = (below < h && above > below) ? r.top - h - gap : r.bottom + gap;
357
+ cal.style.top = (top + window.scrollY) + 'px';
358
+ cal.style.left = (Math.max(edge, Math.min(r.left, vw - w - edge)) + window.scrollX) + 'px';
359
+ };
360
+ var close = function () { cal.hidePopover(); if (opener) opener.focus(); };
361
+ var choose = function (d) {
362
+ selected = d;
363
+ var input = field.querySelector('.cx-field__control');
364
+ input.value = pad(d.getDate()) + '/' + pad(d.getMonth() + 1) + '/' + d.getFullYear();
365
+ input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true }));
366
+ close();
367
+ };
368
+ var setUpPicker = function (box) {
369
+ var btn = box.querySelector('.cx-date__button');
370
+ if (!btn || !HTMLElement.prototype.hasOwnProperty('popover')) return;
371
+ btn.hidden = false;
372
+ if (!fresh(btn, 'date')) return;
373
+ btn.addEventListener('click', function () {
374
+ if (!cal || !cal.isConnected) build();
375
+ if (cal.matches(':popover-open') && opener === btn) { cal.hidePopover(); return; }
376
+ if (cal.matches(':popover-open')) cal.hidePopover();
377
+ opener = btn; field = box;
378
+ selected = parse(box.querySelector('.cx-field__control').value);
379
+ focusDay = new Date(selected || today()); view = 'days';
380
+ cal.setAttribute('aria-label', btn.getAttribute('aria-label') || 'Choose a date');
381
+ cal.showPopover(); render(true);
382
+ });
383
+ };
384
+
385
+ // Before Turbo caches a page for the back button, close what's open on it, so it isn't restored open.
386
+ document.addEventListener('turbo:before-cache', function () {
387
+ each('.cx-calendar', function (c) { if (c.matches(':popover-open')) c.hidePopover(); });
388
+ each('dialog[open]', function (dlg) { dlg.close(); });
389
+ each('.cx-toast', function (t) { t.remove(); });
390
+ each('.cx-header__toc[open], .cx-header__menu[open]', function (dt) { dt.open = false; });
391
+ });
392
+
393
+ // The first page now, and each page Turbo brings in after it.
394
+ ready(page);
395
+ ['turbo:load', 'turbo:frame-load', 'turbo:morph'].forEach(function (type) { document.addEventListener(type, page); });
396
+
397
+ // ------------------------------------------------------------ each page: its elements
398
+ function page() {
399
+ sweep();
52
400
  apply(theme);
53
- document.querySelectorAll('[data-theme-toggle]').forEach(function (b) {
401
+ each('[data-theme-toggle]', function (b) {
54
402
  b.hidden = false;
403
+ if (!fresh(b, 'theme')) return;
55
404
  b.addEventListener('click', function () {
56
405
  var next = shown(theme) === 'dark' ? 'light' : 'dark';
57
406
  theme = next === sys() ? 'auto' : next; // matching the system again means following it again
@@ -59,13 +408,10 @@
59
408
  });
60
409
  });
61
410
 
62
- // Escape hides any open tooltip without moving the pointer or focus (WCAG 1.4.13).
63
- document.addEventListener('keydown', function (e) {
64
- if (e.key !== 'Escape') return;
65
- document.querySelectorAll('.cx-tooltip').forEach(function (t) { t.parentElement.classList.add('is-tooltip-dismissed'); });
66
- });
67
- document.querySelectorAll('.cx-tooltip').forEach(function (t) {
411
+ // Tooltips: a dismissed one comes back once the pointer or focus leaves its host.
412
+ each('.cx-tooltip', function (t) {
68
413
  var host = t.parentElement;
414
+ if (!fresh(host, 'tooltip')) return;
69
415
  var reset = function () { host.classList.remove('is-tooltip-dismissed'); };
70
416
  host.addEventListener('mouseleave', reset);
71
417
  host.addEventListener('focusout', reset);
@@ -73,7 +419,7 @@
73
419
 
74
420
  // The sticky header's height, for scroll-padding-top and the docked TOC: it wraps to two rows on narrow screens.
75
421
  var header = document.querySelector('.cx-header');
76
- if (header) {
422
+ if (header && fresh(header, 'measure')) {
77
423
  var end = header.querySelector('.cx-header__end'), mark = header.querySelector('.cx-wordmark');
78
424
  var measure = function () {
79
425
  root.style.setProperty('--_header-height', header.offsetHeight + 'px');
@@ -82,7 +428,7 @@
82
428
  if (end && mark) header.toggleAttribute('data-wrapped', end.offsetTop > mark.offsetTop + mark.offsetHeight / 2);
83
429
  };
84
430
  measure();
85
- if ('ResizeObserver' in window) new ResizeObserver(measure).observe(header); else window.addEventListener('resize', measure);
431
+ if ('ResizeObserver' in window) observe(header, new ResizeObserver(measure)); else watch('resize', header, measure);
86
432
  }
87
433
 
88
434
  // Backdrop pages: the header starts on the frost and scrolls with the page. Once the script marks it, it sticks,
@@ -90,34 +436,30 @@
90
436
  // Without this script it keeps scrolling away, which is safe: it never sits see-through over the text.
91
437
  // A docs header (cx-header--docs) is marked the same way, so it shows the wordmark in full at the top of the page
92
438
  // and compacts once the page scrolls.
93
- document.querySelectorAll('.cx-backdrop-page .cx-header, .cx-header--docs').forEach(function (header) {
439
+ each('.cx-backdrop-page .cx-header, .cx-header--docs', function (header) {
440
+ if (!fresh(header, 'scrolled')) return;
94
441
  var queued = false;
95
442
  var set = function () { queued = false; header.setAttribute('data-scrolled', window.scrollY > 0 ? 'true' : 'false'); };
96
443
  set();
97
- window.addEventListener('scroll', function () { if (!queued) { queued = true; window.requestAnimationFrame(set); } }, { passive: true });
444
+ watch('scroll', header, function () { if (!queued) { queued = true; window.requestAnimationFrame(set); } });
98
445
  });
99
446
 
100
447
  // The docs header's "On this page" and menu are <details>, so they open and close without script. With it, one
101
448
  // opens at a time, and they close on Escape, on a click outside them, and when a link in them is followed.
102
449
  var panels = Array.prototype.slice.call(document.querySelectorAll('.cx-header__toc, .cx-header__menu'));
103
450
  panels.forEach(function (d) {
451
+ if (!fresh(d, 'panel')) return;
104
452
  d.addEventListener('toggle', function () { if (d.open) panels.forEach(function (o) { if (o !== d) o.open = false; }); });
105
453
  d.addEventListener('click', function (e) { if (e.target.closest('a[href]')) d.open = false; });
106
454
  });
107
- if (panels.length) {
108
- document.addEventListener('keydown', function (e) {
109
- if (e.key !== 'Escape') return;
110
- panels.forEach(function (d) { if (d.open) { d.open = false; d.querySelector('summary').focus(); } });
111
- });
112
- document.addEventListener('click', function (e) { panels.forEach(function (d) { if (d.open && !d.contains(e.target)) d.open = false; }); });
113
- }
114
455
 
115
456
  // Copy buttons on code blocks.
116
- document.querySelectorAll('.cx-code').forEach(function (block) {
457
+ each('.cx-code', function (block) {
117
458
  var btn = block.querySelector('.cx-code__copy');
118
459
  var code = block.querySelector('pre code');
119
460
  if (!btn || !code) return;
120
461
  btn.hidden = false;
462
+ if (!fresh(btn, 'copy')) return;
121
463
  btn.addEventListener('click', function () {
122
464
  var text = Array.prototype.map.call(code.querySelectorAll('.cl'), function (l) { return l.textContent; }).join('') || code.textContent;
123
465
  var label = btn.querySelector('.cx-code__copy-label');
@@ -139,7 +481,8 @@
139
481
  // Component previews: size each frame to its document's body (the root is never shorter than the frame), and keep
140
482
  // its theme. An open dialog or popover (a calendar) sits above the body, so while one is open the frame also grows
141
483
  // to hold it, and shrinks back when it closes. Inside a preview, links don't navigate.
142
- document.querySelectorAll('.cx-preview__frame').forEach(function (f) {
484
+ each('.cx-preview__frame', function (f) {
485
+ if (!fresh(f, 'preview')) return;
143
486
  var fit = function () {
144
487
  try {
145
488
  var d = f.contentDocument; if (!d || !d.body) return;
@@ -158,7 +501,7 @@
158
501
  };
159
502
  var start = function () {
160
503
  apply(theme); fit();
161
- try { if ('ResizeObserver' in window) new ResizeObserver(fit).observe(f.contentDocument.body); } catch (e) { /* keep the measured height */ }
504
+ try { if ('ResizeObserver' in window) observe(f, new ResizeObserver(fit), f.contentDocument.body); } catch (e) { /* keep the measured height */ }
162
505
  try {
163
506
  var d = f.contentDocument, later = function () { window.requestAnimationFrame(fit); };
164
507
  d.addEventListener('toggle', later, true);
@@ -169,13 +512,13 @@
169
512
  f.addEventListener('load', start);
170
513
  try { if (f.contentDocument && f.contentDocument.readyState === 'complete' && f.contentDocument.body && f.contentDocument.body.childNodes.length) start(); } catch (e) { /* wait for load */ }
171
514
  });
172
- if (document.body.classList.contains('cx-preview-body')) {
173
- document.addEventListener('click', function (e) { if (e.target.closest && e.target.closest('a[href]')) e.preventDefault(); });
174
- }
175
515
 
176
516
  // Tabs: turn the labelled panels into a tab list with one panel shown. Sets that share data-tabs stay in step,
177
517
  // and the choice is remembered for the next page (cx-tabs:<name> in localStorage).
178
- document.querySelectorAll('.cx-tabs').forEach(function (set, n) {
518
+ each('.cx-tabs', function (set, n) {
519
+ if (!fresh(set, 'tabs')) return;
520
+ // a page restored from Turbo's cache keeps the tab list it was given, but not its listeners: make it again
521
+ set.querySelectorAll(':scope > .cx-tabs__list').forEach(function (old) { old.remove(); });
179
522
  var panels = Array.prototype.slice.call(set.querySelectorAll(':scope > .cx-tabs__panel'));
180
523
  if (panels.length < 2) return;
181
524
  var list = document.createElement('div'); list.className = 'cx-tabs__list'; list.setAttribute('role', 'tablist');
@@ -219,7 +562,8 @@
219
562
 
220
563
  // Search: a dialog over a Pagefind index. The trigger is shown once this script runs; Pagefind's script loads on the
221
564
  // first open. Ctrl+K or Cmd+K, or "/" outside a text field, opens it; Escape closes it (native to <dialog>).
222
- document.querySelectorAll('.cx-search[data-search]').forEach(function (dialog) {
565
+ each('.cx-search[data-search]', function (dialog) {
566
+ if (!fresh(dialog, 'search')) return;
223
567
  var input = dialog.querySelector('.cx-search__input');
224
568
  var list = dialog.querySelector('.cx-search__results');
225
569
  var status = dialog.querySelector('.cx-search__status');
@@ -279,17 +623,13 @@
279
623
  e.preventDefault();
280
624
  if (next < 0) input.focus(); else links[next].focus();
281
625
  });
282
- document.addEventListener('keydown', function (e) {
283
- var typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement.tagName) || document.activeElement.isContentEditable;
284
- if (((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'k') || (e.key === '/' && !typing && !e.ctrlKey && !e.metaKey && !e.altKey)) {
285
- e.preventDefault(); open();
286
- }
287
- });
626
+ openers.set(dialog, open);
288
627
  });
289
628
 
290
629
  // Copy page: copies the page's Markdown (its index.md) as plain text. ClipboardItem takes a promise, so the copy
291
630
  // still counts as part of the click while the file loads; without it, the text is fetched first.
292
- document.querySelectorAll('[data-copy-page]').forEach(function (btn) {
631
+ each('[data-copy-page]', function (btn) {
632
+ if (!fresh(btn, 'copy-page')) return;
293
633
  var label = btn.querySelector('.cx-page-actions__label');
294
634
  var icon = btn.querySelector('use');
295
635
  var set = function (state, text, iconId) {
@@ -311,306 +651,48 @@
311
651
  });
312
652
  });
313
653
 
314
- // Alerts (alert.md): show each dismiss button, and hide its alert when pressed. With a key, data-alert-dismiss="x",
315
- // the alert stays hidden on later pages too (localStorage "cx-alert:x"). Focus moves to the main column, since the
316
- // button it was on has gone. One listener on the document, so an alert an app adds later works the same way.
317
- var dismissed = function (key) { try { return Boolean(key) && localStorage.getItem('cx-alert:' + key) === 'dismissed'; } catch (e) { return false; } };
318
- document.querySelectorAll('.cx-alert [data-alert-dismiss]').forEach(function (btn) {
654
+ // Alerts (alert.md): show each dismiss button, or keep its alert hidden if its key was dismissed on an earlier
655
+ // page (localStorage "cx-alert:x" for data-alert-dismiss="x"). The click is handled once, on the document.
656
+ each('.cx-alert [data-alert-dismiss]', function (btn) {
319
657
  var alert = btn.closest('.cx-alert');
320
658
  if (dismissed(btn.dataset.alertDismiss)) alert.hidden = true; else btn.hidden = false;
321
659
  });
322
- document.addEventListener('click', function (e) {
323
- var btn = e.target.closest && e.target.closest('.cx-alert [data-alert-dismiss]');
324
- if (!btn) return;
325
- var key = btn.dataset.alertDismiss;
326
- btn.closest('.cx-alert').hidden = true;
327
- if (key) { try { localStorage.setItem('cx-alert:' + key, 'dismissed'); } catch (err) { /* storage blocked: hidden for this view only */ } }
328
- var main = document.getElementById('main');
329
- if (main) main.focus({ preventScroll: true });
330
- });
331
-
332
- // Toasts (toast.md): document.dispatchEvent(new CustomEvent('cx-toast', { detail: { text: 'Saved', kind: 'success' } })).
333
- // One polite live region per page, made here if the page has none, holds up to three; each goes after
334
- // motion.duration.toast, a time that starts again while the pointer or focus is on it. Escape inside one, or its
335
- // button, closes it. The text is set as text, never as HTML.
336
- var region = document.querySelector('.cx-toasts');
337
- if (!region) {
338
- region = document.createElement('div'); region.className = 'cx-toasts'; region.setAttribute('role', 'status');
339
- document.body.appendChild(region);
340
- }
341
- var toastIcon = function (id, cls) {
342
- var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
343
- svg.setAttribute('class', 'cx-icon cx-icon--sm' + (cls ? ' ' + cls : '')); svg.setAttribute('aria-hidden', 'true'); svg.setAttribute('focusable', 'false');
344
- var use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); use.setAttribute('href', '#cx-icon-' + id);
345
- svg.appendChild(use); return svg;
346
- };
347
- var closeToast = function (toast) {
348
- if (toast.classList.contains('is-leaving')) return;
349
- clearTimeout(toast._cxTimer);
350
- var hadFocus = toast.contains(document.activeElement);
351
- toast.classList.add('is-leaving'); toast.classList.remove('is-shown');
352
- setTimeout(function () { toast.remove(); }, ms('--cx-motion-duration-base'));
353
- if (hadFocus) { var main = document.getElementById('main'); if (main) main.focus({ preventScroll: true }); }
354
- };
355
- document.addEventListener('cx-toast', function (e) {
356
- var d = e.detail || {};
357
- if (!d.text) return;
358
- var toast = document.createElement('div'); toast.className = 'cx-toast';
359
- if (d.kind === 'success') toast.appendChild(toastIcon('check'));
360
- var p = document.createElement('p'); p.className = 'cx-toast__text'; p.textContent = d.text; toast.appendChild(p);
361
- var btn = document.createElement('button'); btn.type = 'button'; btn.className = 'cx-toast__dismiss'; btn.setAttribute('aria-label', 'Dismiss');
362
- btn.appendChild(toastIcon('close')); btn.addEventListener('click', function () { closeToast(toast); }); toast.appendChild(btn);
363
- var wait = function () { clearTimeout(toast._cxTimer); toast._cxTimer = setTimeout(function () { closeToast(toast); }, ms('--cx-motion-duration-toast')); };
364
- var hold = function () { clearTimeout(toast._cxTimer); };
365
- toast.addEventListener('mouseenter', hold); toast.addEventListener('focusin', hold);
366
- toast.addEventListener('mouseleave', function () { if (!toast.contains(document.activeElement)) wait(); });
367
- toast.addEventListener('focusout', function (ev) { if (!toast.contains(ev.relatedTarget) && !toast.matches(':hover')) wait(); });
368
- toast.addEventListener('keydown', function (ev) { if (ev.key === 'Escape') { ev.stopPropagation(); closeToast(toast); } });
369
- var live = region.querySelectorAll('.cx-toast:not(.is-leaving)');
370
- if (live.length >= 3) closeToast(live[0]);
371
- region.appendChild(toast);
372
- window.requestAnimationFrame(function () { window.requestAnimationFrame(function () { toast.classList.add('is-shown'); }); });
373
- wait();
374
- });
660
+ // The toast region is made with the page, so it's already there when a toast is announced.
661
+ toastRegion();
375
662
 
376
- // Dialogs (dialog.md): [data-dialog-open="id"] opens a .cx-dialog with showModal(), which moves focus in, closes on
377
- // Escape and returns focus when closed. [data-dialog-close] inside one closes it, and so does a click on the
378
- // backdrop, unless the dialog has data-dialog-keep (a form, whose typing a stray click would lose).
379
- document.addEventListener('click', function (e) {
380
- var t = e.target.closest ? e.target : null;
381
- if (!t) return;
382
- var opener = t.closest('[data-dialog-open]');
383
- if (opener) {
384
- var d = document.getElementById(opener.dataset.dialogOpen);
385
- if (d && d.showModal && !d.open) d.showModal();
386
- return;
387
- }
388
- var dialog = t.closest('dialog.cx-dialog');
389
- if (!dialog) return;
390
- if (t.closest('[data-dialog-close]')) { dialog.close(); return; }
391
- if (t === dialog && !dialog.hasAttribute('data-dialog-keep')) {
392
- var r = dialog.getBoundingClientRect();
393
- if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) dialog.close();
394
- }
395
- });
396
-
397
- // Date pickers (date-picker.md): each [data-date-picker] box gets its calendar button, and the page gets one
398
- // calendar, made the first time it's opened. Dates are day first (DD/MM/YYYY) and weeks start on Monday. The
399
- // calendar sits under its field, or above it when there's more room there, in page coordinates so it scrolls
400
- // with the field. Days, months and years are each a grid with one tab stop, moved by the arrow keys.
401
- var pickers = document.querySelectorAll('[data-date-picker]');
402
- if (pickers.length) {
403
- var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
404
- var DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
405
- var PAGE = 20;
406
- var cal = null, calBody, calDays, calList, calTitle, monthBtn, yearBtn, navs;
407
- var field = null, opener = null, selected = null, focusDay = null, view = 'days';
408
- var today = function () { var n = new Date(); return new Date(n.getFullYear(), n.getMonth(), n.getDate()); };
409
- var same = function (a, b) { return a && b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); };
410
- var pad = function (n) { return String(n).padStart(2, '0'); };
411
- var parse = function (v) {
412
- var m = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.exec(v.trim());
413
- if (!m) return null;
414
- var d = new Date(+m[3], +m[2] - 1, +m[1]);
415
- return d.getMonth() === +m[2] - 1 ? d : null;
416
- };
417
- var clampDay = function (y, m, d) { return new Date(y, m, Math.min(d, new Date(y, m + 1, 0).getDate())); };
418
- var icon = function (id) {
419
- var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
420
- svg.setAttribute('class', 'cx-icon'); svg.setAttribute('aria-hidden', 'true'); svg.setAttribute('focusable', 'false');
421
- var use = document.createElementNS('http://www.w3.org/2000/svg', 'use'); use.setAttribute('href', '#cx-icon-' + id);
422
- svg.appendChild(use); return svg;
423
- };
424
- var el = function (tag, cls, attrs) {
425
- var n = document.createElement(tag); if (cls) n.className = cls;
426
- Object.keys(attrs || {}).forEach(function (k) { n.setAttribute(k, attrs[k]); });
427
- return n;
428
- };
429
- var build = function () {
430
- cal = el('div', 'cx-calendar', { popover: '', role: 'dialog', 'aria-label': 'Choose a date' });
431
- var head = el('div', 'cx-calendar__head');
432
- var prev = el('button', 'cx-calendar__nav', { type: 'button', 'data-step': '-1' }); prev.appendChild(icon('arrow-back'));
433
- var next = el('button', 'cx-calendar__nav', { type: 'button', 'data-step': '1' }); next.appendChild(icon('arrow-forward'));
434
- var pick = el('div', 'cx-calendar__pickers');
435
- monthBtn = el('button', 'cx-calendar__pick', { type: 'button', 'aria-expanded': 'false' });
436
- yearBtn = el('button', 'cx-calendar__pick', { type: 'button', 'aria-expanded': 'false' });
437
- [monthBtn, yearBtn].forEach(function (b) { b.appendChild(document.createElement('span')); b.appendChild(icon('expand')); pick.appendChild(b); });
438
- head.appendChild(prev); head.appendChild(pick); head.appendChild(next);
439
- calTitle = el('p', 'cx-visually-hidden', { 'aria-live': 'polite' });
440
- calDays = el('table', 'cx-calendar__grid', { role: 'grid' });
441
- var thead = el('thead'), tr = el('tr');
442
- [1, 2, 3, 4, 5, 6, 0].forEach(function (i) { var th = el('th', '', { scope: 'col', abbr: DAYS[i] }); th.textContent = DAYS[i].slice(0, 2); tr.appendChild(th); });
443
- thead.appendChild(tr); calDays.appendChild(thead); calBody = el('tbody'); calDays.appendChild(calBody);
444
- calList = el('div', 'cx-calendar__list'); calList.hidden = true;
445
- var foot = el('div', 'cx-calendar__foot');
446
- var todayBtn = el('button', '', { type: 'button' }); todayBtn.textContent = 'Today';
447
- var closeBtn = el('button', '', { type: 'button' }); closeBtn.textContent = 'Close';
448
- foot.appendChild(todayBtn); foot.appendChild(closeBtn);
449
- [head, calTitle, calDays, calList, foot].forEach(function (n) { cal.appendChild(n); });
450
- document.body.appendChild(cal);
451
- navs = [prev, next];
452
- var labelId = 'cx-calendar-title'; calTitle.id = labelId; calDays.setAttribute('aria-labelledby', labelId);
453
- monthBtn.setAttribute('aria-controls', 'cx-calendar-list'); yearBtn.setAttribute('aria-controls', 'cx-calendar-list'); calList.id = 'cx-calendar-list';
454
-
455
- calBody.addEventListener('click', function (e) {
456
- var b = e.target.closest('.cx-calendar__day'); if (!b) return;
457
- var v = b.dataset.value.split('-'); choose(new Date(+v[0], +v[1], +v[2]));
458
- });
459
- calList.addEventListener('click', function (e) {
460
- var b = e.target.closest('.cx-calendar__cell'); if (!b) return;
461
- var v = +b.dataset.value;
462
- if (view === 'years') { focusDay = clampDay(v, focusDay.getMonth(), focusDay.getDate()); setView('months'); }
463
- else { focusDay = clampDay(focusDay.getFullYear(), v, focusDay.getDate()); setView('days'); }
464
- });
465
- monthBtn.addEventListener('click', function () { setView(view === 'months' ? 'days' : 'months'); });
466
- yearBtn.addEventListener('click', function () { setView(view === 'years' ? 'days' : 'years'); });
467
- navs.forEach(function (n) {
468
- n.addEventListener('click', function () {
469
- var st = +n.dataset.step, y = focusDay.getFullYear(), m = focusDay.getMonth(), d = focusDay.getDate();
470
- focusDay = view === 'days' ? clampDay(y, m + st, d) : view === 'months' ? clampDay(y + st, m, d) : clampDay(y + st * PAGE, m, d);
471
- render(false);
472
- });
473
- });
474
- todayBtn.addEventListener('click', function () { view = 'days'; focusDay = today(); render(true); });
475
- closeBtn.addEventListener('click', close);
476
- cal.addEventListener('toggle', function (e) { if (e.newState !== 'open') view = 'days'; });
477
- calBody.addEventListener('keydown', function (e) {
478
- var d = new Date(focusDay), k = e.key;
479
- if (k === 'ArrowLeft') d.setDate(d.getDate() - 1);
480
- else if (k === 'ArrowRight') d.setDate(d.getDate() + 1);
481
- else if (k === 'ArrowUp') d.setDate(d.getDate() - 7);
482
- else if (k === 'ArrowDown') d.setDate(d.getDate() + 7);
483
- else if (k === 'PageUp') d = clampDay(d.getFullYear(), d.getMonth() - 1, d.getDate());
484
- else if (k === 'PageDown') d = clampDay(d.getFullYear(), d.getMonth() + 1, d.getDate());
485
- else if (k === 'Home') d.setDate(d.getDate() - ((d.getDay() + 6) % 7));
486
- else if (k === 'End') d.setDate(d.getDate() + (6 - (d.getDay() + 6) % 7));
487
- else return;
488
- e.preventDefault(); focusDay = d; render(true);
489
- });
490
- calList.addEventListener('keydown', function (e) {
491
- var cols = view === 'years' ? 4 : 3, k = e.key, y = focusDay.getFullYear(), m = focusDay.getMonth(), dd = focusDay.getDate(), delta = 0;
492
- if (k === 'ArrowLeft') delta = -1; else if (k === 'ArrowRight') delta = 1;
493
- else if (k === 'ArrowUp') delta = -cols; else if (k === 'ArrowDown') delta = cols;
494
- else if (k === 'PageUp') delta = view === 'years' ? -PAGE : -12; else if (k === 'PageDown') delta = view === 'years' ? PAGE : 12;
495
- else return;
496
- e.preventDefault();
497
- if (view === 'months' && Math.abs(delta) < 12) { if (m + delta < 0 || m + delta > 11) return; focusDay = clampDay(y, m + delta, dd); }
498
- else focusDay = view === 'years' ? clampDay(y + delta, m, dd) : clampDay(y + delta / 12, m, dd);
499
- render(true);
500
- });
501
- window.addEventListener('resize', place);
502
- document.addEventListener('scroll', place, true);
503
- };
504
- var cell = function (cls, text, label, o) {
505
- var b = el('button', cls, { type: 'button', 'aria-label': label, 'aria-pressed': o.pressed ? 'true' : 'false' });
506
- if (o.current) b.setAttribute('aria-current', 'date');
507
- b.textContent = text; b.tabIndex = o.focus ? 0 : -1; b.dataset.value = o.value;
508
- return b;
509
- };
510
- var render = function (moveFocus) {
511
- var y = focusDay.getFullYear(), m = focusDay.getMonth(), now = today();
512
- monthBtn.firstChild.textContent = MONTHS[m]; yearBtn.firstChild.textContent = y;
513
- monthBtn.setAttribute('aria-expanded', String(view === 'months')); yearBtn.setAttribute('aria-expanded', String(view === 'years'));
514
- monthBtn.setAttribute('aria-label', 'Choose the month, ' + MONTHS[m]); yearBtn.setAttribute('aria-label', 'Choose the year, ' + y);
515
- calDays.hidden = view !== 'days'; calList.hidden = view === 'days';
516
- var labels = { days: ['Previous month', 'Next month'], months: ['Previous year', 'Next year'], years: ['Previous ' + PAGE + ' years', 'Next ' + PAGE + ' years'] }[view];
517
- navs[0].setAttribute('aria-label', labels[0]); navs[1].setAttribute('aria-label', labels[1]);
518
- if (view === 'days') {
519
- calTitle.textContent = MONTHS[m] + ' ' + y;
520
- var first = new Date(y, m, 1), start = new Date(y, m, 1 - ((first.getDay() + 6) % 7));
521
- calBody.textContent = '';
522
- for (var w = 0; w < 6; w++) {
523
- var row = el('tr');
524
- for (var i = 0; i < 7; i++) {
525
- var d = new Date(start.getFullYear(), start.getMonth(), start.getDate() + w * 7 + i), td = el('td');
526
- td.appendChild(cell('cx-calendar__day' + (d.getMonth() !== m ? ' is-other' : ''), d.getDate(),
527
- DAYS[d.getDay()] + ' ' + d.getDate() + ' ' + MONTHS[d.getMonth()] + ' ' + d.getFullYear(),
528
- { pressed: same(d, selected), current: same(d, now), focus: same(d, focusDay), value: d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() }));
529
- row.appendChild(td);
530
- }
531
- calBody.appendChild(row);
532
- }
533
- } else if (view === 'months') {
534
- calTitle.textContent = 'Months of ' + y; calList.className = 'cx-calendar__list cx-calendar__list--months'; calList.textContent = '';
535
- MONTHS.forEach(function (name, i) {
536
- calList.appendChild(cell('cx-calendar__cell', name.slice(0, 3), name + ' ' + y,
537
- { pressed: selected && selected.getFullYear() === y && selected.getMonth() === i, current: now.getFullYear() === y && now.getMonth() === i, focus: i === m, value: i }));
538
- });
539
- } else {
540
- var from = Math.floor(y / PAGE) * PAGE;
541
- calTitle.textContent = from + ' to ' + (from + PAGE - 1); calList.className = 'cx-calendar__list cx-calendar__list--years'; calList.textContent = '';
542
- for (var k = from; k < from + PAGE; k++) {
543
- calList.appendChild(cell('cx-calendar__cell', k, String(k), { pressed: selected && selected.getFullYear() === k, current: now.getFullYear() === k, focus: k === y, value: k }));
544
- }
545
- }
546
- place();
547
- if (moveFocus) { var f = (view === 'days' ? calBody : calList).querySelector('[tabindex="0"]'); if (f) f.focus(); }
548
- };
549
- var setView = function (v) { view = v; render(true); };
550
- var place = function () {
551
- if (!cal || !field || !cal.matches(':popover-open')) return;
552
- var r = field.getBoundingClientRect(), vw = document.documentElement.clientWidth, h = cal.offsetHeight, w = cal.offsetWidth, gap = px('--cx-space-inline-xs'), edge = px('--cx-space-inline-sm');
553
- var below = window.innerHeight - r.bottom - gap, above = r.top - gap;
554
- var top = (below < h && above > below) ? r.top - h - gap : r.bottom + gap;
555
- cal.style.top = (top + window.scrollY) + 'px';
556
- cal.style.left = (Math.max(edge, Math.min(r.left, vw - w - edge)) + window.scrollX) + 'px';
557
- };
558
- var close = function () { cal.hidePopover(); if (opener) opener.focus(); };
559
- var choose = function (d) {
560
- selected = d;
561
- var input = field.querySelector('.cx-field__control');
562
- input.value = pad(d.getDate()) + '/' + pad(d.getMonth() + 1) + '/' + d.getFullYear();
563
- input.dispatchEvent(new Event('input', { bubbles: true })); input.dispatchEvent(new Event('change', { bubbles: true }));
564
- close();
565
- };
566
- pickers.forEach(function (box) {
567
- var btn = box.querySelector('.cx-date__button');
568
- if (!btn || !HTMLElement.prototype.hasOwnProperty('popover')) return;
569
- btn.hidden = false;
570
- btn.addEventListener('click', function () {
571
- if (!cal) build();
572
- if (cal.matches(':popover-open') && opener === btn) { cal.hidePopover(); return; }
573
- if (cal.matches(':popover-open')) cal.hidePopover();
574
- opener = btn; field = box;
575
- selected = parse(box.querySelector('.cx-field__control').value);
576
- focusDay = new Date(selected || today()); view = 'days';
577
- cal.setAttribute('aria-label', btn.getAttribute('aria-label') || 'Choose a date');
578
- cal.showPopover(); render(true);
579
- });
580
- });
581
- }
663
+ each('[data-date-picker]', setUpPicker);
582
664
 
583
665
  // Docs navigation: dock it (sticky, scrolling on its own) while it sits beside the main column, as the TOC does.
584
- document.querySelectorAll('.cx-docs').forEach(function (docs) {
666
+ each('.cx-docs', function (docs) {
585
667
  var nav = docs.querySelector('.cx-docs-nav');
586
668
  var main = docs.querySelector('.cx-docs__main');
587
- if (!nav || !main) return;
669
+ if (!nav || !main || !fresh(docs, 'dock')) return;
588
670
  var dock = function () {
589
671
  root.style.setProperty('--_viewport-height', window.innerHeight + 'px');
590
672
  nav.classList.toggle('is-docked', Math.abs(nav.offsetTop - main.offsetTop) < 1);
591
673
  };
592
- dock(); window.addEventListener('resize', dock);
674
+ dock(); watch('resize', docs, dock);
593
675
  // Bring the current page into view inside the docked navigation.
594
676
  var current = nav.querySelector('[aria-current="page"]');
595
677
  if (current && nav.classList.contains('is-docked') && current.offsetTop > nav.clientHeight / 2) nav.scrollTop = current.offsetTop - nav.clientHeight / 2;
596
678
  });
597
679
 
598
680
  // Table of contents: dock beside the article when there is room, and mark the current section.
599
- document.querySelectorAll('.cx-article').forEach(function (article) {
681
+ each('.cx-article', function (article) {
600
682
  var toc = article.querySelector('.cx-toc');
601
683
  var main = article.querySelector('.cx-article__main');
602
- if (!toc || !main) return;
684
+ if (!toc || !main || !fresh(article, 'toc')) return;
603
685
  var dock = function () { toc.classList.toggle('is-docked', Math.abs(toc.offsetTop - main.offsetTop) < 1); };
604
- dock(); window.addEventListener('resize', dock);
686
+ dock(); watch('resize', article, dock);
605
687
  // The docs header's "On this page" (site-header.md) lists the same sections, so it is marked too.
606
688
  var links = Array.prototype.slice.call(document.querySelectorAll('.cx-article .cx-toc a[href^="#"], .cx-header__toc li a[href^="#"]'));
607
689
  if (!('IntersectionObserver' in window)) return;
608
- var io = new IntersectionObserver(function (entries) {
690
+ var io = observe(article, new IntersectionObserver(function (entries) {
609
691
  entries.forEach(function (e) {
610
692
  if (!e.isIntersecting) return;
611
693
  links.forEach(function (a) { a.setAttribute('aria-current', a.getAttribute('href') === '#' + e.target.id ? 'true' : 'false'); });
612
694
  });
613
- }, { rootMargin: '0px 0px -70% 0px' });
695
+ }, { rootMargin: '0px 0px -70% 0px' }));
614
696
  links.forEach(function (a) { var t = document.getElementById(a.getAttribute('href').slice(1)); if (t) io.observe(t); });
615
697
  });
616
698
 
@@ -619,7 +701,8 @@
619
701
  // scripted drawing listens for the "cx-plate-pause" event. Under reduced motion nothing plays and the control
620
702
  // stays hidden. The contour survey (data-plate-seed) is run by dist/plate.js instead.
621
703
  var still = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
622
- document.querySelectorAll('.cx-plate[data-plate-motion]').forEach(function (plate) {
704
+ each('.cx-plate[data-plate-motion]', function (plate) {
705
+ if (!fresh(plate, 'plate')) return;
623
706
  var frame = plate.closest('.cx-plate-frame') || plate.parentElement;
624
707
  var toggle = frame.querySelector('.cx-plate__pause');
625
708
  var input = toggle && toggle.querySelector('input');
@@ -634,5 +717,5 @@
634
717
  if (input) input.addEventListener('change', function () { set(input.checked); });
635
718
  set(Boolean(input && input.checked));
636
719
  });
637
- });
720
+ }
638
721
  })();
package/dist/tokens.css CHANGED
@@ -1,4 +1,4 @@
1
- /* MotifSpec 1.27.0 — generated from tokens/*.json. Do not edit.
1
+ /* MotifSpec 1.27.2 — generated from tokens/*.json. Do not edit.
2
2
  Themes: semantic colours use light-dark(); color-scheme follows the system unless
3
3
  <html data-theme="light|dark"> pins it. --cx-primitive-* are for this file only. */
4
4
  :root {
package/dist/tokens.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Generated by npm run build. Do not edit.
2
- export const version = "1.27.0";
2
+ export const version = "1.27.2";
3
3
  export const changelog = "https://github.com/Aitrien/motifspec/blob/main/CHANGELOG.md";
4
4
  export const showcase = "https://motifspec.com";
5
5
  export const tokens = [
package/dist/tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.27.0",
2
+ "version": "1.27.2",
3
3
  "changelog": "https://github.com/Aitrien/motifspec/blob/main/CHANGELOG.md",
4
4
  "showcase": "https://motifspec.com",
5
5
  "tokens": [
@@ -188,7 +188,8 @@ Copy the snippet as it is: the build fails if this doc and `js/theme-init.js` di
188
188
 
189
189
  - The skip link is the first focusable element and targets `<main id="main">`.
190
190
  - `<nav aria-label="Main">`; only one nav per page uses that label.
191
- - Nav links, the toggle and the menu control are 44px (`size.target.touch`); the toggle is a pill with equal sides, so it renders as a circle.
191
+ - Nav links are 40px (`size.target.control`), the height of the search field, so the two line up in a docs header. The toggle and the menu control are 44px (`size.target.touch`); the toggle is a pill with equal sides, so it renders as a circle.
192
+ - The nav list scrolls sideways when it's too long for its row, and keeps room for the focus ring inside it, so a focused link's ring isn't cut off.
192
193
  - The menu's control is named "Menu" (`aria-label` on its summary), and its icon changes to a cross while it is open; "On this page" is named by its text. Both are `<details>`, so Enter or Space opens them without script, and the browser announces whether they are expanded.
193
194
  - While the menu is open, the page behind it doesn't scroll. With `dist/codex.js`, Escape closes an open menu or panel and returns focus to its control.
194
195
  - The toggle's `aria-label` is its action ("Switch to light theme"); the visible tooltip repeats it and is `aria-hidden`. Escape hides the tooltip without moving the pointer (see `tooltip.md`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aitrien/motifspec",
3
- "version": "1.27.0",
3
+ "version": "1.27.2",
4
4
  "description": "Liam Marshall's design system: DTCG tokens and layered component rules that AI assistants read a layer at a time, with generated CSS, Hugo templates and a rule checker.",
5
5
  "keywords": [
6
6
  "design-system",