@ruckc/truss-markdown 1.51.0 → 1.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@
5
5
  import MarkdownCode from './MarkdownCode.svelte';
6
6
  import { use, MARKDOWN_DEFAULTS } from './context.js';
7
7
  import { useLabels } from './labels.js';
8
+ import { scrollFocus } from './scrollfocus.js';
8
9
 
9
10
  /** Renders block nodes (paragraph, heading, list, code block, blockquote, rule) with plain elements. */
10
11
  let { nodes }: { nodes: BlockNode[] } = $props();
@@ -33,7 +34,7 @@
33
34
  {:else if n.type === 'code'}
34
35
  <MarkdownCode lang={n.lang} text={n.text} />
35
36
  {:else if n.type === 'table'}
36
- <div class="table" role="region" {@attach focusable} aria-label={L.table(n.head.length, n.rows.length)}>
37
+ <div class="table" {@attach scrollFocus(() => ({ label: L.table(n.head.length, n.rows.length) }))}>
37
38
  <table>
38
39
  <thead>
39
40
  <tr>{#each n.head as cell, k}<th scope="col" data-align={n.align[k] ?? undefined}><MarkdownInlines nodes={cell} /></th>{/each}</tr>
@@ -4,6 +4,7 @@
4
4
  import { highlight, resolveLanguage, type Token } from './highlight.js';
5
5
  import { use, MARKDOWN_DEFAULTS } from './context.js';
6
6
  import { useLabels } from './labels.js';
7
+ import { scrollFocus } from './scrollfocus.js';
7
8
 
8
9
  /** A fenced code block: highlighted tokens inside a scrollable `<pre>`, a language label and a Copy button, or the caller's `code` snippet. */
9
10
  let { lang, text }: { lang: string; text: string } = $props();
@@ -17,9 +18,6 @@
17
18
  let timer: ReturnType<typeof setTimeout>;
18
19
  onDestroy(() => clearTimeout(timer));
19
20
 
20
- /** A scrolling region must be reachable by keyboard so it can be scrolled with the arrow keys. */
21
- const focusable = (node: HTMLElement) => void (node.tabIndex = 0);
22
-
23
21
  async function copy() {
24
22
  try {
25
23
  await navigator.clipboard.writeText(text);
@@ -43,7 +41,7 @@
43
41
  </Tooltip>
44
42
  <span class="tr-visually-hidden" role="status" aria-live="polite">{status === 'copied' ? L.copied : status === 'failed' ? L.copyFailed : ''}</span>
45
43
  </figcaption>
46
- <pre role="region" {@attach focusable} aria-label={L.codeRegion(label)}><code class={lang ? `language-${lang}` : undefined}
44
+ <pre {@attach scrollFocus(() => ({ label: L.codeRegion(label) }))}><code class={lang ? `language-${lang}` : undefined}
47
45
  >{#each tokens as t}{#if t.type === 'plain'}{t.text}{:else}<span class="tok-{t.type}">{t.text}</span>{/if}{/each}</code></pre>
48
46
  </figure>
49
47
  {/if}
@@ -53,7 +51,7 @@
53
51
  figcaption { display: flex; align-items: center; justify-content: space-between; gap: var(--tr-space-2); padding: 0 var(--tr-space-1) 0 var(--tr-space-3); border-bottom: 1px solid var(--tr-outline-variant); color: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); }
54
52
  .lang { font-family: var(--tr-font-mono); }
55
53
  pre { margin: 0; max-width: 100%; overflow-x: auto; padding: var(--tr-space-3); border: 0; border-radius: 0; background: none; color: var(--tr-on-surface); font-family: var(--tr-font-mono); font-size: 0.875em; line-height: 1.5; tab-size: 2; }
56
- pre:focus-visible { outline: none; box-shadow: inset var(--tr-focus-ring); }
54
+ pre:focus-visible { outline: none; box-shadow: var(--tr-focus-ring-inset); }
57
55
  code { padding: 0; background: none; font: inherit; white-space: pre; }
58
56
  .tok-keyword { color: var(--tr-syntax-keyword); }
59
57
  .tok-string { color: var(--tr-syntax-string); }
@@ -0,0 +1,18 @@
1
+ import type { Attachment } from 'svelte/attachments';
2
+ /**
3
+ * @internal Makes a scroll container reachable by keyboard while (and only while) it scrolls and has nothing of its own to Tab to (axe `scrollable-region-focusable`, #269).
4
+ *
5
+ * A keyboard user can scroll a region only when it, or something inside it, can take focus. So while the content overflows and holds no tabbable control, the element gets
6
+ * `tabindex="0"`, `role="region"` and an accessible name, and a focus ring from its component's CSS (`:focus-visible`). With a tabbable control inside, the control is the tab stop
7
+ * (focusing it scrolls it into view, and axe accepts that), and an extra stop on the container would only make Tab slower. When nothing overflows there is no tab stop at all.
8
+ * Re-measured when the element or its children resize and when its content changes. It writes nothing on the server (attachments run in the browser), so the markup carries no
9
+ * tab stop until it has been measured and there is no hydration mismatch.
10
+ *
11
+ * `get` supplies the name: `labelledby` (an element id, preferred) or `label` (text). Without either, the role is left off (a nameless region is noise) and only the tab stop is added.
12
+ * `role: false` keeps the element's own role (`main`). Attributes the app set itself (`tabindex` other than `-1`, `role`, `aria-label`, `aria-labelledby`) are never overwritten.
13
+ */
14
+ export declare function scrollFocus(get?: () => {
15
+ labelledby?: string;
16
+ label?: string;
17
+ role?: boolean;
18
+ }): Attachment<HTMLElement>;
@@ -0,0 +1,92 @@
1
+ /** Elements a keyboard user can Tab to on their own. A `tabindex="-1"` element is only focusable by script, so it does not count. */
2
+ const TABBABLE = 'a[href],area[href],button,input:not([type="hidden"]),select,textarea,summary,iframe,audio[controls],video[controls],[contenteditable]:not([contenteditable="false"]),[tabindex]';
3
+ function tabbableWithin(el) {
4
+ for (const c of el.querySelectorAll(TABBABLE)) {
5
+ const t = c.getAttribute('tabindex');
6
+ if (t !== null && Number(t) < 0)
7
+ continue;
8
+ if (c.matches(':disabled') || c.closest('[hidden],[inert]') || c.getClientRects().length === 0)
9
+ continue;
10
+ return true;
11
+ }
12
+ return false;
13
+ }
14
+ /** Whether `el` scrolls right now: it clips its overflow (`auto`/`scroll`) and its content is larger than its box. */
15
+ function scrolls(el) {
16
+ const cs = getComputedStyle(el);
17
+ return (/auto|scroll/.test(cs.overflowY) && el.scrollHeight > el.clientHeight + 1) || (/auto|scroll/.test(cs.overflowX) && el.scrollWidth > el.clientWidth + 1);
18
+ }
19
+ /**
20
+ * @internal Makes a scroll container reachable by keyboard while (and only while) it scrolls and has nothing of its own to Tab to (axe `scrollable-region-focusable`, #269).
21
+ *
22
+ * A keyboard user can scroll a region only when it, or something inside it, can take focus. So while the content overflows and holds no tabbable control, the element gets
23
+ * `tabindex="0"`, `role="region"` and an accessible name, and a focus ring from its component's CSS (`:focus-visible`). With a tabbable control inside, the control is the tab stop
24
+ * (focusing it scrolls it into view, and axe accepts that), and an extra stop on the container would only make Tab slower. When nothing overflows there is no tab stop at all.
25
+ * Re-measured when the element or its children resize and when its content changes. It writes nothing on the server (attachments run in the browser), so the markup carries no
26
+ * tab stop until it has been measured and there is no hydration mismatch.
27
+ *
28
+ * `get` supplies the name: `labelledby` (an element id, preferred) or `label` (text). Without either, the role is left off (a nameless region is noise) and only the tab stop is added.
29
+ * `role: false` keeps the element's own role (`main`). Attributes the app set itself (`tabindex` other than `-1`, `role`, `aria-label`, `aria-labelledby`) are never overwritten.
30
+ */
31
+ export function scrollFocus(get) {
32
+ return (el) => {
33
+ let added = [];
34
+ let restore; // a programmatic-only `tabindex="-1"` (Shell's `main`) is replaced while the tab stop is on
35
+ let queued = false;
36
+ const clear = () => {
37
+ for (const a of added)
38
+ el.removeAttribute(a);
39
+ added = [];
40
+ if (restore !== undefined) {
41
+ el.setAttribute('tabindex', restore);
42
+ restore = undefined;
43
+ }
44
+ };
45
+ const set = (name, value) => { if (!el.hasAttribute(name)) {
46
+ el.setAttribute(name, value);
47
+ added.push(name);
48
+ } };
49
+ const measure = () => {
50
+ queued = false;
51
+ const on = scrolls(el) && !tabbableWithin(el);
52
+ if (!on) {
53
+ clear();
54
+ return;
55
+ }
56
+ if (added.length)
57
+ return;
58
+ const { labelledby, label, role = true } = get?.() ?? {};
59
+ const named = !!(labelledby || label) && !el.hasAttribute('aria-label') && !el.hasAttribute('aria-labelledby');
60
+ if (el.getAttribute('tabindex') === '-1') {
61
+ restore = '-1';
62
+ el.removeAttribute('tabindex');
63
+ }
64
+ set('tabindex', '0');
65
+ if (role && (named || el.hasAttribute('aria-label') || el.hasAttribute('aria-labelledby')))
66
+ set('role', 'region');
67
+ if (named) {
68
+ if (labelledby)
69
+ set('aria-labelledby', labelledby);
70
+ else
71
+ set('aria-label', label);
72
+ }
73
+ };
74
+ // coalesce bursts (a virtual list scrolling, many nodes added) into one measurement per microtask
75
+ const schedule = () => { if (!queued) {
76
+ queued = true;
77
+ queueMicrotask(measure);
78
+ } };
79
+ const ro = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(schedule);
80
+ const watchKids = () => { if (ro) {
81
+ ro.disconnect();
82
+ ro.observe(el);
83
+ for (const k of el.children)
84
+ ro.observe(k);
85
+ } };
86
+ const mo = new MutationObserver(() => { watchKids(); schedule(); });
87
+ mo.observe(el, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['tabindex', 'disabled', 'hidden', 'inert', 'href'] });
88
+ watchKids();
89
+ measure();
90
+ return () => { ro?.disconnect(); mo.disconnect(); clear(); };
91
+ };
92
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-markdown",
3
- "version": "1.51.0",
3
+ "version": "1.52.0",
4
4
  "description": "A safe Markdown renderer for Truss UI: a typed AST rendered with Svelte components, never HTML strings",
5
5
  "type": "module",
6
6
  "license": "MIT",