@hypermedia-components/core 0.1.1 → 0.1.3
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/dist/code-editor.d.ts +13 -0
- package/dist/code-editor.js +121 -0
- package/dist/csrf-header.d.ts +25 -0
- package/dist/csrf-header.js +83 -0
- package/dist/datagrid.js +58 -11
- package/dist/field-errors.js +8 -3
- package/dist/hc-code.css +275 -0
- package/dist/hc-sparkline.css +62 -0
- package/dist/hc.behaviors.d.ts +4 -1
- package/dist/hc.behaviors.js +9 -0
- package/dist/hc.behaviors.min.js +4 -2
- package/dist/hc.core.css +38 -0
- package/dist/hc.core.min.css +1 -1
- package/dist/hc.css +421 -0
- package/dist/hc.min.css +1 -1
- package/dist/hc.min.js +4 -2
- package/dist/hc.tokens.color-amber.css +2 -0
- package/dist/hc.tokens.color-emerald.css +2 -0
- package/dist/hc.tokens.color-indigo.css +2 -0
- package/dist/hc.tokens.color-rose.css +2 -0
- package/dist/hc.tokens.core.css +38 -0
- package/dist/hc.tokens.css +82 -0
- package/dist/hc.tokens.neutral-neutral.css +9 -0
- package/dist/hc.tokens.neutral-slate.css +9 -0
- package/dist/hc.tokens.neutral-stone.css +9 -0
- package/dist/hc.tokens.neutral-zinc.css +9 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/sparkline.d.ts +13 -0
- package/dist/sparkline.js +169 -0
- package/package.json +1 -1
- package/src/tokens/component.tokens.json +32 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Install the editable-code behavior on the given root.
|
|
3
|
+
*
|
|
4
|
+
* Enhances every `.hc-code[data-editable]` once and re-scans subtrees
|
|
5
|
+
* delivered by htmx (`htmx:load`). Repeated calls on the same root return the
|
|
6
|
+
* same uninstaller.
|
|
7
|
+
*
|
|
8
|
+
* @param {Document|Element} [root=document]
|
|
9
|
+
* The scope to scan. Defaults to the global document when available.
|
|
10
|
+
* @returns {() => void} an idempotent uninstaller that removes the synced
|
|
11
|
+
* gutters and listeners it added.
|
|
12
|
+
*/
|
|
13
|
+
export function installCodeEditor(root?: Document | Element): () => void;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// installCodeEditor — upgrade an editable `hc-code` field with a synced
|
|
2
|
+
// line-number gutter (issue #255).
|
|
3
|
+
//
|
|
4
|
+
// <div class="hc-code" data-editable data-gutter="line-numbers">
|
|
5
|
+
// <textarea class="hc-code__input" name="content" spellcheck="false">SELECT 1</textarea>
|
|
6
|
+
// </div>
|
|
7
|
+
//
|
|
8
|
+
// The value lives in a real <textarea name>, so it submits in forms, works
|
|
9
|
+
// with htmx (hx-post / hx-include / hx-vals), and degrades to a plain
|
|
10
|
+
// monospace textarea when this script is absent. When `data-gutter="line-numbers"`
|
|
11
|
+
// is set, the behavior inserts a `.hc-code__gutter` element before the
|
|
12
|
+
// textarea and keeps it in sync: it re-numbers on input and matches the
|
|
13
|
+
// textarea's vertical scroll. To keep the numbers aligned with the lines it
|
|
14
|
+
// sets the textarea to not soft-wrap (`wrap="off"`), so long lines scroll
|
|
15
|
+
// horizontally rather than pushing the numbers out of step.
|
|
16
|
+
//
|
|
17
|
+
// Syntax highlighting is out of scope (a CSP-safe overlay is a possible
|
|
18
|
+
// follow-up). installCodeEditor(root = document) is idempotent and returns an
|
|
19
|
+
// uninstaller; fields swapped in by htmx are enhanced on `htmx:load`.
|
|
20
|
+
|
|
21
|
+
const INSTALL_KEY = '__hcCodeEditorUninstall';
|
|
22
|
+
|
|
23
|
+
function lineNumbers(count) {
|
|
24
|
+
let out = '1';
|
|
25
|
+
for (let i = 2; i <= count; i += 1) out += '\n' + i;
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function enhance(container) {
|
|
30
|
+
const textarea = container.querySelector('.hc-code__input');
|
|
31
|
+
if (!textarea) return null;
|
|
32
|
+
if (container.dataset.gutter !== 'line-numbers') return () => {};
|
|
33
|
+
if (container.querySelector('.hc-code__gutter')) return () => {};
|
|
34
|
+
|
|
35
|
+
// Keep line numbers aligned: a soft-wrapped line would span several rows
|
|
36
|
+
// while the gutter counts one. Horizontal scroll instead.
|
|
37
|
+
const prevWrap = textarea.getAttribute('wrap');
|
|
38
|
+
textarea.setAttribute('wrap', 'off');
|
|
39
|
+
|
|
40
|
+
const gutter = container.ownerDocument.createElement('div');
|
|
41
|
+
gutter.className = 'hc-code__gutter';
|
|
42
|
+
gutter.setAttribute('aria-hidden', 'true');
|
|
43
|
+
container.insertBefore(gutter, textarea);
|
|
44
|
+
|
|
45
|
+
let lastCount = 0;
|
|
46
|
+
const renumber = () => {
|
|
47
|
+
const count = Math.max(1, textarea.value.split('\n').length);
|
|
48
|
+
if (count !== lastCount) {
|
|
49
|
+
gutter.textContent = lineNumbers(count);
|
|
50
|
+
lastCount = count;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const syncScroll = () => {
|
|
54
|
+
gutter.scrollTop = textarea.scrollTop;
|
|
55
|
+
};
|
|
56
|
+
const onInput = () => {
|
|
57
|
+
renumber();
|
|
58
|
+
syncScroll();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
textarea.addEventListener('input', onInput);
|
|
62
|
+
textarea.addEventListener('scroll', syncScroll);
|
|
63
|
+
renumber();
|
|
64
|
+
syncScroll();
|
|
65
|
+
|
|
66
|
+
return () => {
|
|
67
|
+
textarea.removeEventListener('input', onInput);
|
|
68
|
+
textarea.removeEventListener('scroll', syncScroll);
|
|
69
|
+
gutter.remove();
|
|
70
|
+
if (prevWrap == null) textarea.removeAttribute('wrap');
|
|
71
|
+
else textarea.setAttribute('wrap', prevWrap);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Install the editable-code behavior on the given root.
|
|
77
|
+
*
|
|
78
|
+
* Enhances every `.hc-code[data-editable]` once and re-scans subtrees
|
|
79
|
+
* delivered by htmx (`htmx:load`). Repeated calls on the same root return the
|
|
80
|
+
* same uninstaller.
|
|
81
|
+
*
|
|
82
|
+
* @param {Document|Element} [root=document]
|
|
83
|
+
* The scope to scan. Defaults to the global document when available.
|
|
84
|
+
* @returns {() => void} an idempotent uninstaller that removes the synced
|
|
85
|
+
* gutters and listeners it added.
|
|
86
|
+
*/
|
|
87
|
+
export function installCodeEditor(root = (typeof document !== 'undefined' ? document : null)) {
|
|
88
|
+
if (!root) return () => {};
|
|
89
|
+
if (root[INSTALL_KEY]) return root[INSTALL_KEY];
|
|
90
|
+
|
|
91
|
+
const enhanced = new WeakSet();
|
|
92
|
+
const detachers = [];
|
|
93
|
+
|
|
94
|
+
const scan = (scope) => {
|
|
95
|
+
if (!scope || !scope.querySelectorAll) return;
|
|
96
|
+
scope.querySelectorAll('.hc-code[data-editable]').forEach((el) => {
|
|
97
|
+
if (enhanced.has(el)) return;
|
|
98
|
+
const detach = enhance(el);
|
|
99
|
+
if (detach) {
|
|
100
|
+
enhanced.add(el);
|
|
101
|
+
detachers.push(detach);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
scan(root);
|
|
107
|
+
|
|
108
|
+
const target = root.body || root;
|
|
109
|
+
const onLoad = (event) => scan(event && event.target);
|
|
110
|
+
target.addEventListener('htmx:load', onLoad);
|
|
111
|
+
|
|
112
|
+
const uninstall = () => {
|
|
113
|
+
if (root[INSTALL_KEY] !== uninstall) return;
|
|
114
|
+
target.removeEventListener('htmx:load', onLoad);
|
|
115
|
+
detachers.forEach((fn) => fn());
|
|
116
|
+
detachers.length = 0;
|
|
117
|
+
delete root[INSTALL_KEY];
|
|
118
|
+
};
|
|
119
|
+
root[INSTALL_KEY] = uninstall;
|
|
120
|
+
return uninstall;
|
|
121
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Install the csrf-header behavior: attach the page's CSRF token
|
|
3
|
+
* (`<meta name="csrf-token" content="…">`, header name from the meta's
|
|
4
|
+
* `data-header` attribute, default `X-CSRF-Token`) to every htmx
|
|
5
|
+
* request via `htmx:configRequest`.
|
|
6
|
+
*
|
|
7
|
+
* The meta tag is read at request time, so a rotated token is picked
|
|
8
|
+
* up automatically. A header already set on the request (e.g. via
|
|
9
|
+
* `data-hx-headers`) is left untouched. Without the meta tag the
|
|
10
|
+
* behavior is inert.
|
|
11
|
+
*
|
|
12
|
+
* @param {Document} [root]
|
|
13
|
+
* The root to listen on. Defaults to the global document when
|
|
14
|
+
* available.
|
|
15
|
+
* @returns {() => void} an idempotent uninstaller.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // <head>
|
|
19
|
+
* // <meta name="csrf-token" content="3x4mpl3…">
|
|
20
|
+
* // </head>
|
|
21
|
+
*
|
|
22
|
+
* import { installCsrfHeader } from '@hypermedia-components/core';
|
|
23
|
+
* installCsrfHeader();
|
|
24
|
+
*/
|
|
25
|
+
export function installCsrfHeader(root?: Document): () => void;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// csrf-header behavior — the blessed CSRF token delivery convention for
|
|
2
|
+
// htmx requests (#246).
|
|
3
|
+
//
|
|
4
|
+
// Contract:
|
|
5
|
+
// - The server's layout renders the token into the page head:
|
|
6
|
+
//
|
|
7
|
+
// <meta name="csrf-token" content="…">
|
|
8
|
+
//
|
|
9
|
+
// The header name defaults to `X-CSRF-Token` and is configurable on
|
|
10
|
+
// the carrier for stacks that expect a different one:
|
|
11
|
+
//
|
|
12
|
+
// <meta name="csrf-token" content="…" data-header="X-CSRFToken">
|
|
13
|
+
//
|
|
14
|
+
// - On every `htmx:configRequest` the behavior reads the meta tag —
|
|
15
|
+
// at request time, so server-side token rotation needs no
|
|
16
|
+
// re-install — and adds the header to the outgoing request.
|
|
17
|
+
// - A header already present in `event.detail.headers` is never
|
|
18
|
+
// overwritten: a per-request `data-hx-headers` (or an earlier
|
|
19
|
+
// listener) wins over the page-level convention.
|
|
20
|
+
// - No meta tag, or an empty `content` → strict no-op.
|
|
21
|
+
//
|
|
22
|
+
// The behavior never makes a request — htmx owns the network. Plain
|
|
23
|
+
// `<form method="post">` submissions never fire `htmx:configRequest`;
|
|
24
|
+
// no-JS degradation needs the framework's hidden-field mechanism.
|
|
25
|
+
//
|
|
26
|
+
// installCsrfHeader() returns an `uninstall` function. Idempotent.
|
|
27
|
+
|
|
28
|
+
const INSTALL_KEY = '__hcCsrfHeaderUninstall';
|
|
29
|
+
|
|
30
|
+
const DEFAULT_HEADER = 'X-CSRF-Token';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Install the csrf-header behavior: attach the page's CSRF token
|
|
34
|
+
* (`<meta name="csrf-token" content="…">`, header name from the meta's
|
|
35
|
+
* `data-header` attribute, default `X-CSRF-Token`) to every htmx
|
|
36
|
+
* request via `htmx:configRequest`.
|
|
37
|
+
*
|
|
38
|
+
* The meta tag is read at request time, so a rotated token is picked
|
|
39
|
+
* up automatically. A header already set on the request (e.g. via
|
|
40
|
+
* `data-hx-headers`) is left untouched. Without the meta tag the
|
|
41
|
+
* behavior is inert.
|
|
42
|
+
*
|
|
43
|
+
* @param {Document} [root]
|
|
44
|
+
* The root to listen on. Defaults to the global document when
|
|
45
|
+
* available.
|
|
46
|
+
* @returns {() => void} an idempotent uninstaller.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // <head>
|
|
50
|
+
* // <meta name="csrf-token" content="3x4mpl3…">
|
|
51
|
+
* // </head>
|
|
52
|
+
*
|
|
53
|
+
* import { installCsrfHeader } from '@hypermedia-components/core';
|
|
54
|
+
* installCsrfHeader();
|
|
55
|
+
*/
|
|
56
|
+
export function installCsrfHeader(root = (typeof document !== 'undefined' ? document : null)) {
|
|
57
|
+
if (!root) return () => {};
|
|
58
|
+
if (root[INSTALL_KEY]) return root[INSTALL_KEY];
|
|
59
|
+
|
|
60
|
+
function onConfigRequest(event) {
|
|
61
|
+
const headers = event?.detail?.headers;
|
|
62
|
+
if (!headers || typeof headers !== 'object') return;
|
|
63
|
+
|
|
64
|
+
const doc = root.nodeType === 9 ? root : root.ownerDocument;
|
|
65
|
+
const meta = doc.querySelector('meta[name="csrf-token"]');
|
|
66
|
+
const token = meta?.getAttribute('content');
|
|
67
|
+
if (!token) return;
|
|
68
|
+
|
|
69
|
+
const header = meta.getAttribute('data-header') || DEFAULT_HEADER;
|
|
70
|
+
if (header in headers) return; // an explicit per-request header wins
|
|
71
|
+
headers[header] = token;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
root.addEventListener('htmx:configRequest', onConfigRequest);
|
|
75
|
+
|
|
76
|
+
const uninstall = () => {
|
|
77
|
+
if (root[INSTALL_KEY] !== uninstall) return;
|
|
78
|
+
root.removeEventListener('htmx:configRequest', onConfigRequest);
|
|
79
|
+
delete root[INSTALL_KEY];
|
|
80
|
+
};
|
|
81
|
+
root[INSTALL_KEY] = uninstall;
|
|
82
|
+
return uninstall;
|
|
83
|
+
}
|
package/dist/datagrid.js
CHANGED
|
@@ -80,6 +80,32 @@ function rowCells(row) {
|
|
|
80
80
|
);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
// The navigation matrix as a VISUAL grid: a cell with rowspan/colspan is
|
|
84
|
+
// entered into every (row, column) slot it covers, so arrow keys move by
|
|
85
|
+
// visual position and multi-row records stay column-aligned (the lead
|
|
86
|
+
// rowspan cell is reachable from every sub-row it spans).
|
|
87
|
+
function buildMatrix(grid) {
|
|
88
|
+
const rows = bodyRows(grid);
|
|
89
|
+
const out = rows.map(() => []);
|
|
90
|
+
rows.forEach((row, r) => {
|
|
91
|
+
let c = 0;
|
|
92
|
+
for (const cell of rowCells(row)) {
|
|
93
|
+
while (out[r][c] !== undefined) c += 1; // slot taken by a rowspan above
|
|
94
|
+
const cs = cell.colSpan || 1;
|
|
95
|
+
// rowspan="0" = "to the end of the row group" (HTML spec); either way
|
|
96
|
+
// a span never crosses into the next record's rows.
|
|
97
|
+
const rs = cell.rowSpan === 0 ? rows.length - r : cell.rowSpan || 1;
|
|
98
|
+
for (let dr = 0; dr < rs; dr += 1) {
|
|
99
|
+
const target = rows[r + dr];
|
|
100
|
+
if (!target || target.parentNode !== row.parentNode) break;
|
|
101
|
+
for (let dc = 0; dc < cs; dc += 1) out[r + dr][c + dc] = cell;
|
|
102
|
+
}
|
|
103
|
+
c += cs;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
83
109
|
/** Measure header heights + frozen widths → sticky offset variables. */
|
|
84
110
|
function measure(grid) {
|
|
85
111
|
const headTrs = ownedBy(grid, '.hc-datagrid__head > tr');
|
|
@@ -149,7 +175,8 @@ function attach(grid, detachers) {
|
|
|
149
175
|
for (const h of ownedBy(grid, '.hc-datagrid__headcell')) {
|
|
150
176
|
if (!h.getAttribute('role')) h.setAttribute('role', 'columnheader');
|
|
151
177
|
}
|
|
152
|
-
matrix
|
|
178
|
+
// A spanning cell occupies several matrix slots — visit each cell once.
|
|
179
|
+
new Set(matrix.flat()).forEach((cell) => {
|
|
153
180
|
cell.setAttribute('role', cell.tagName === 'TH' ? 'rowheader' : 'gridcell');
|
|
154
181
|
cell.tabIndex = -1;
|
|
155
182
|
// Widgets in cells are not separate tab stops — the grid manages focus.
|
|
@@ -160,15 +187,16 @@ function attach(grid, detachers) {
|
|
|
160
187
|
}
|
|
161
188
|
|
|
162
189
|
function rebuild() {
|
|
163
|
-
matrix =
|
|
190
|
+
matrix = buildMatrix(grid);
|
|
164
191
|
applyRoles();
|
|
165
192
|
applyResizedWidths(); // re-apply column widths to swapped-in rows
|
|
166
|
-
|
|
167
|
-
if (cur) {
|
|
168
|
-
cur
|
|
169
|
-
const pos = locate(cur);
|
|
193
|
+
let cur = matrix[active.r]?.[active.c];
|
|
194
|
+
if (!cur) {
|
|
195
|
+
cur = matrix[0]?.[0];
|
|
196
|
+
const pos = cur && locate(cur);
|
|
170
197
|
if (pos) active = pos;
|
|
171
198
|
}
|
|
199
|
+
if (cur) cur.tabIndex = 0;
|
|
172
200
|
}
|
|
173
201
|
|
|
174
202
|
// ---- Column resize ----
|
|
@@ -331,6 +359,23 @@ function attach(grid, detachers) {
|
|
|
331
359
|
cell.scrollIntoView?.({ block: 'nearest', inline: 'nearest' });
|
|
332
360
|
}
|
|
333
361
|
|
|
362
|
+
// Arrow movement: walk from the active slot in direction (dr, dc),
|
|
363
|
+
// skipping further slots of the same spanning cell so a rowspan/colspan
|
|
364
|
+
// cell counts as a single stop. The active slot (not the cell's top-left)
|
|
365
|
+
// is the walk origin, so the entry row/column is kept while crossing a
|
|
366
|
+
// span — ↓ then ↑ round-trips.
|
|
367
|
+
function step(dr, dc) {
|
|
368
|
+
const { r, c } = active;
|
|
369
|
+
const cur = matrix[r]?.[c];
|
|
370
|
+
let nr = r + dr;
|
|
371
|
+
let nc = c + dc;
|
|
372
|
+
while (cur && matrix[nr]?.[nc] === cur) {
|
|
373
|
+
nr += dr;
|
|
374
|
+
nc += dc;
|
|
375
|
+
}
|
|
376
|
+
setActive(nr, nc);
|
|
377
|
+
}
|
|
378
|
+
|
|
334
379
|
function toggleRow(r) {
|
|
335
380
|
const row = bodyRows(grid)[r];
|
|
336
381
|
if (!row) return;
|
|
@@ -386,10 +431,10 @@ function attach(grid, detachers) {
|
|
|
386
431
|
else if (key === 'ArrowLeft') key = 'ArrowRight';
|
|
387
432
|
}
|
|
388
433
|
switch (key) {
|
|
389
|
-
case 'ArrowDown':
|
|
390
|
-
case 'ArrowUp':
|
|
391
|
-
case 'ArrowRight':
|
|
392
|
-
case 'ArrowLeft':
|
|
434
|
+
case 'ArrowDown': step(1, 0); break;
|
|
435
|
+
case 'ArrowUp': step(-1, 0); break;
|
|
436
|
+
case 'ArrowRight': step(0, 1); break;
|
|
437
|
+
case 'ArrowLeft': step(0, -1); break;
|
|
393
438
|
case 'Home': setActive(event.ctrlKey ? 0 : r, 0); break;
|
|
394
439
|
case 'End':
|
|
395
440
|
if (event.ctrlKey) setActive(matrix.length - 1, Infinity);
|
|
@@ -419,9 +464,11 @@ function attach(grid, detachers) {
|
|
|
419
464
|
if (!cell || !grid.contains(cell)) return;
|
|
420
465
|
const pos = locate(cell);
|
|
421
466
|
if (!pos) return;
|
|
422
|
-
if (
|
|
467
|
+
if (matrix[active.r]?.[active.c] !== cell) {
|
|
423
468
|
setActive(pos.r, pos.c, false); // don't re-focus; focus is already here
|
|
424
469
|
} else {
|
|
470
|
+
// Already the active cell — keep the active slot as-is so a spanning
|
|
471
|
+
// cell remembers which sub-row/column it was entered from.
|
|
425
472
|
cell.setAttribute('data-active', '');
|
|
426
473
|
}
|
|
427
474
|
}
|
package/dist/field-errors.js
CHANGED
|
@@ -68,16 +68,21 @@ function scopeOf(alert, root) {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
// First control in the scope whose `name` matches. `form.elements`
|
|
71
|
-
// handles radio/checkbox groups natively (RadioNodeList
|
|
71
|
+
// handles radio/checkbox groups natively (RadioNodeList). Hidden inputs
|
|
72
|
+
// are skipped when the group has a visible member: the blessed boolean
|
|
73
|
+
// idiom pairs `<input type="hidden" value="false">` with the real
|
|
74
|
+
// checkbox under one name, and the ARIA wiring, focus, and edit-to-clear
|
|
75
|
+
// belong on the control the user can operate.
|
|
72
76
|
function controlFor(scope, name) {
|
|
73
77
|
let found;
|
|
74
78
|
if (scope.elements && typeof scope.elements.namedItem === 'function') {
|
|
75
79
|
found = scope.elements.namedItem(name);
|
|
76
80
|
} else {
|
|
77
|
-
found = scope.
|
|
81
|
+
found = scope.querySelectorAll(`[name="${escapeName(name)}"]`);
|
|
78
82
|
}
|
|
79
83
|
if (found && found.tagName == null && typeof found.length === 'number') {
|
|
80
|
-
|
|
84
|
+
const members = Array.from(found); // RadioNodeList / NodeList
|
|
85
|
+
found = members.find((el) => el.type !== 'hidden') ?? members[0];
|
|
81
86
|
}
|
|
82
87
|
return found ?? null;
|
|
83
88
|
}
|
package/dist/hc-code.css
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/* hc-code — code surface (issues #253, #256, #255).
|
|
2
|
+
*
|
|
3
|
+
* A monospace surface styled from the kit's tokens. The read-only modes
|
|
4
|
+
* need no script (CSP `default-src 'self'` safe); the editable mode is a
|
|
5
|
+
* real <textarea> that `installCodeEditor()` upgrades with a synced
|
|
6
|
+
* line-number gutter. Syntax highlighting is out of scope — the lines are
|
|
7
|
+
* plain text (a server-tokenized or behavior-driven follow-up may add it
|
|
8
|
+
* additively later).
|
|
9
|
+
*
|
|
10
|
+
* 1. Plain block — apply to a <pre>:
|
|
11
|
+
*
|
|
12
|
+
* <pre class="hc-code"><code>SELECT 1
|
|
13
|
+
* FROM t</code></pre>
|
|
14
|
+
*
|
|
15
|
+
* Overflow is a horizontal scroll by default; `data-wrap="on"` soft-wraps
|
|
16
|
+
* long lines instead.
|
|
17
|
+
*
|
|
18
|
+
* 2. Line-numbered / decorable — apply to an <ol>, one <li class="hc-code__line">
|
|
19
|
+
* per line, with `data-gutter="line-numbers"`:
|
|
20
|
+
*
|
|
21
|
+
* <ol class="hc-code" data-gutter="line-numbers">
|
|
22
|
+
* <li class="hc-code__line">SELECT *</li>
|
|
23
|
+
* <li class="hc-code__line" data-state="covered"> FROM orders</li>
|
|
24
|
+
* <li class="hc-code__line" data-state="missed"> WHERE total > 0</li>
|
|
25
|
+
* </ol>
|
|
26
|
+
*
|
|
27
|
+
* 3. Unified diff — apply `data-mode="diff"` to the <ol>; each line carries a
|
|
28
|
+
* `data-state` of added | removed | context and `data-old` / `data-new`
|
|
29
|
+
* line numbers (the server computes the hunks — the kit only styles them):
|
|
30
|
+
*
|
|
31
|
+
* <ol class="hc-code" data-mode="diff">
|
|
32
|
+
* <li class="hc-code__line" data-state="context" data-old="12" data-new="12"> SELECT id</li>
|
|
33
|
+
* <li class="hc-code__line" data-state="removed" data-old="13"> FROM users</li>
|
|
34
|
+
* <li class="hc-code__line" data-state="added" data-new="13"> FROM app_users</li>
|
|
35
|
+
* </ol>
|
|
36
|
+
*
|
|
37
|
+
* 4. Editable — a <div class="hc-code" data-editable> wrapping a real
|
|
38
|
+
* <textarea class="hc-code__input" name="…">. The value submits in forms
|
|
39
|
+
* and with htmx; without JS it is a plain monospace textarea.
|
|
40
|
+
* `installCodeEditor()` adds a synced line-number gutter when
|
|
41
|
+
* `data-gutter="line-numbers"` is set:
|
|
42
|
+
*
|
|
43
|
+
* <div class="hc-code" data-editable data-gutter="line-numbers">
|
|
44
|
+
* <textarea class="hc-code__input" name="content" spellcheck="false">SELECT 1</textarea>
|
|
45
|
+
* </div>
|
|
46
|
+
*
|
|
47
|
+
* Per-line `data-state` colours from semantic status tokens, so the same
|
|
48
|
+
* markup themes correctly in light and dark:
|
|
49
|
+
* added / covered → success removed / missed → error context → muted
|
|
50
|
+
*
|
|
51
|
+
* Legend: `.hc-code__swatch[data-state]` is a small colour chip the consumer
|
|
52
|
+
* composes into a legend next to a coverage block.
|
|
53
|
+
*
|
|
54
|
+
* Accessibility: a horizontally scrollable block must be keyboard-reachable,
|
|
55
|
+
* so make it a focusable, labelled region — `tabindex="0"` plus
|
|
56
|
+
* `role="region"` and an `aria-label`. Colour is never the only diff cue: the
|
|
57
|
+
* unified-diff gutter prints a `+` / `-` sign glyph alongside the tint.
|
|
58
|
+
*/
|
|
59
|
+
@layer hc.components {
|
|
60
|
+
.hc-code {
|
|
61
|
+
margin: 0;
|
|
62
|
+
border: 1px solid var(--hc-code-border);
|
|
63
|
+
border-radius: var(--hc-code-radius);
|
|
64
|
+
background: var(--hc-code-bg);
|
|
65
|
+
color: var(--hc-code-fg);
|
|
66
|
+
font-family: var(--hc-code-font-family);
|
|
67
|
+
font-size: var(--hc-code-font-size);
|
|
68
|
+
line-height: var(--hc-code-line-height);
|
|
69
|
+
overflow-x: auto;
|
|
70
|
+
tab-size: 2;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Plain block on a <pre>. */
|
|
74
|
+
.hc-code:where(pre) {
|
|
75
|
+
padding: var(--hc-code-padding-block) var(--hc-code-padding-inline);
|
|
76
|
+
white-space: pre;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.hc-code:where(pre)[data-wrap="on"] {
|
|
80
|
+
white-space: pre-wrap;
|
|
81
|
+
overflow-wrap: anywhere;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/* Line-based modes on an <ol>: reset the list, keep the inline padding on
|
|
85
|
+
* the lines so a line's background tint spans the full block width. */
|
|
86
|
+
.hc-code:where(ol) {
|
|
87
|
+
padding-block: var(--hc-code-padding-block);
|
|
88
|
+
padding-inline: 0;
|
|
89
|
+
list-style: none;
|
|
90
|
+
counter-reset: hc-code-line;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.hc-code__line {
|
|
94
|
+
padding-inline: var(--hc-code-padding-inline);
|
|
95
|
+
white-space: pre;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.hc-code[data-wrap="on"] .hc-code__line {
|
|
99
|
+
white-space: pre-wrap;
|
|
100
|
+
overflow-wrap: anywhere;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* --- Line-number gutter ------------------------------------------------ */
|
|
104
|
+
|
|
105
|
+
.hc-code[data-gutter="line-numbers"] .hc-code__line {
|
|
106
|
+
position: relative;
|
|
107
|
+
counter-increment: hc-code-line;
|
|
108
|
+
padding-inline-start: var(--hc-code-gutter-width);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.hc-code[data-gutter="line-numbers"] .hc-code__line::before {
|
|
112
|
+
content: counter(hc-code-line);
|
|
113
|
+
position: absolute;
|
|
114
|
+
inset-inline-start: 0;
|
|
115
|
+
inline-size: calc(var(--hc-code-gutter-width) - var(--hc-code-gutter-gap));
|
|
116
|
+
color: var(--hc-code-gutter-fg);
|
|
117
|
+
text-align: end;
|
|
118
|
+
user-select: none;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* --- Unified diff ------------------------------------------------------ */
|
|
122
|
+
|
|
123
|
+
.hc-code[data-mode="diff"] .hc-code__line {
|
|
124
|
+
position: relative;
|
|
125
|
+
|
|
126
|
+
/* Reserve two right-aligned number columns; the sign + code follow. */
|
|
127
|
+
padding-inline-start: calc(var(--hc-code-num-width) * 2 + var(--hc-code-gutter-gap));
|
|
128
|
+
list-style-position: inside;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.hc-code[data-mode="diff"] .hc-code__line::before {
|
|
132
|
+
content: attr(data-old);
|
|
133
|
+
position: absolute;
|
|
134
|
+
inset-inline-start: 0;
|
|
135
|
+
inline-size: var(--hc-code-num-width);
|
|
136
|
+
color: var(--hc-code-gutter-fg);
|
|
137
|
+
text-align: end;
|
|
138
|
+
user-select: none;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.hc-code[data-mode="diff"] .hc-code__line::after {
|
|
142
|
+
content: attr(data-new);
|
|
143
|
+
position: absolute;
|
|
144
|
+
inset-inline-start: var(--hc-code-num-width);
|
|
145
|
+
inline-size: var(--hc-code-num-width);
|
|
146
|
+
color: var(--hc-code-gutter-fg);
|
|
147
|
+
text-align: end;
|
|
148
|
+
user-select: none;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* The +/- sign rides the list marker, so it is never selected when the
|
|
152
|
+
* code text is copied. A neutral two-space marker keeps context lines and
|
|
153
|
+
* the code column aligned. */
|
|
154
|
+
.hc-code[data-mode="diff"] .hc-code__line::marker {
|
|
155
|
+
content: " ";
|
|
156
|
+
color: var(--hc-code-gutter-fg);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.hc-code[data-mode="diff"] .hc-code__line[data-state="added"]::marker {
|
|
160
|
+
content: "+ ";
|
|
161
|
+
color: var(--hc-code-added-marker);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.hc-code[data-mode="diff"] .hc-code__line[data-state="removed"]::marker {
|
|
165
|
+
content: "- ";
|
|
166
|
+
color: var(--hc-code-removed-marker);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/* --- Per-line state (shared by gutter + diff modes) -------------------- */
|
|
170
|
+
|
|
171
|
+
.hc-code__line[data-state="added"],
|
|
172
|
+
.hc-code__line[data-state="covered"] {
|
|
173
|
+
background: var(--hc-code-added-bg);
|
|
174
|
+
box-shadow: inset 0.1875rem 0 0 var(--hc-code-added-marker);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.hc-code__line[data-state="removed"],
|
|
178
|
+
.hc-code__line[data-state="missed"] {
|
|
179
|
+
background: var(--hc-code-removed-bg);
|
|
180
|
+
box-shadow: inset 0.1875rem 0 0 var(--hc-code-removed-marker);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.hc-code__line[data-state="context"] {
|
|
184
|
+
color: var(--hc-code-context-fg);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* --- Legend swatch ----------------------------------------------------- */
|
|
188
|
+
|
|
189
|
+
.hc-code__swatch {
|
|
190
|
+
display: inline-block;
|
|
191
|
+
inline-size: 0.75em;
|
|
192
|
+
block-size: 0.75em;
|
|
193
|
+
border: 1px solid var(--hc-code-border);
|
|
194
|
+
border-radius: 2px;
|
|
195
|
+
vertical-align: middle;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.hc-code__swatch[data-state="added"],
|
|
199
|
+
.hc-code__swatch[data-state="covered"] {
|
|
200
|
+
background: var(--hc-code-added-bg);
|
|
201
|
+
border-color: var(--hc-code-added-marker);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.hc-code__swatch[data-state="removed"],
|
|
205
|
+
.hc-code__swatch[data-state="missed"] {
|
|
206
|
+
background: var(--hc-code-removed-bg);
|
|
207
|
+
border-color: var(--hc-code-removed-marker);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/* --- Editable field (#255) --------------------------------------------- */
|
|
211
|
+
|
|
212
|
+
/* A real <textarea> on the same surface. The textarea drives the height;
|
|
213
|
+
* installCodeEditor() overlays the line-number gutter as an absolutely
|
|
214
|
+
* positioned column so it tracks the textarea's visible box (not its own
|
|
215
|
+
* content height) and scrolls in sync. Without JS it is just the textarea,
|
|
216
|
+
* full width. */
|
|
217
|
+
.hc-code[data-editable] {
|
|
218
|
+
position: relative;
|
|
219
|
+
display: block;
|
|
220
|
+
padding: 0;
|
|
221
|
+
overflow: hidden;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.hc-code[data-editable]:focus-within {
|
|
225
|
+
border-color: var(--hc-code-focus-border);
|
|
226
|
+
box-shadow: inset 0 0 0 1px var(--hc-code-focus-border);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/* The synced line-number gutter (created by the behavior, decorative).
|
|
230
|
+
* inset-block:0 ties its height to the textarea's visible box; overflow is
|
|
231
|
+
* hidden and the behavior matches its scrollTop to the textarea. */
|
|
232
|
+
.hc-code__gutter {
|
|
233
|
+
position: absolute;
|
|
234
|
+
inset-block: 0;
|
|
235
|
+
inset-inline-start: 0;
|
|
236
|
+
inline-size: var(--hc-code-gutter-width);
|
|
237
|
+
box-sizing: border-box;
|
|
238
|
+
padding-block: var(--hc-code-padding-block);
|
|
239
|
+
padding-inline-end: var(--hc-code-gutter-gap);
|
|
240
|
+
overflow: hidden;
|
|
241
|
+
color: var(--hc-code-gutter-fg);
|
|
242
|
+
text-align: end;
|
|
243
|
+
white-space: pre;
|
|
244
|
+
pointer-events: none;
|
|
245
|
+
user-select: none;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
.hc-code__input {
|
|
249
|
+
display: block;
|
|
250
|
+
box-sizing: border-box;
|
|
251
|
+
inline-size: 100%;
|
|
252
|
+
min-block-size: var(--hc-code-input-min-height);
|
|
253
|
+
margin: 0;
|
|
254
|
+
padding-block: var(--hc-code-padding-block);
|
|
255
|
+
padding-inline: var(--hc-code-padding-inline);
|
|
256
|
+
border: 0;
|
|
257
|
+
background: transparent;
|
|
258
|
+
color: inherit;
|
|
259
|
+
font-family: inherit;
|
|
260
|
+
font-size: inherit;
|
|
261
|
+
line-height: inherit;
|
|
262
|
+
resize: vertical;
|
|
263
|
+
tab-size: inherit;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/* Make room for the gutter overlay. */
|
|
267
|
+
.hc-code[data-editable][data-gutter="line-numbers"] .hc-code__input {
|
|
268
|
+
padding-inline-start: var(--hc-code-gutter-width);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/* The container shows the :focus-within ring instead. */
|
|
272
|
+
.hc-code__input:focus {
|
|
273
|
+
outline: none;
|
|
274
|
+
}
|
|
275
|
+
}
|