@ruckc/truss-code 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.
@@ -14,6 +14,7 @@
14
14
 
15
15
  <script lang="ts">
16
16
  import { useLabels } from './labels.js';
17
+ import { scrollFocus } from './scrollfocus.js';
17
18
  import { csstext } from './cssvars.js';
18
19
  import SsrStyle from './SsrStyle.svelte';
19
20
  import { prepareMonacoCsp } from './monaco-csp.js';
@@ -164,8 +165,7 @@
164
165
 
165
166
  {#snippet block()}
166
167
  <SsrStyle {uid} rules={{ '': style }} />
167
- <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
168
- <pre {...rest} bind:this={el} data-sv={uid} role="region" class={['cb', klass]} class:wrap class:nums={lineNumbers} {@attach csstext(() => style)} {onscroll} tabindex="0" aria-label={label ?? L.code}><code>{#each lines as segs, i (i)}<span class="ln">{#if lineNumbers}<span class="no" aria-hidden="true">{i + 1}</span>{/if}<span class="tx">{#each segs as s}<span class={s.role ? `s-${s.role}` : undefined}>{s.text}</span>{/each}{'\n'}</span></span>{/each}</code></pre>
168
+ <pre {...rest} bind:this={el} data-sv={uid} class={['cb', klass]} class:wrap class:nums={lineNumbers} {@attach csstext(() => style)} {onscroll} {@attach scrollFocus(() => ({ label: label ?? L.code }))}><code>{#each lines as segs, i (i)}<span class="ln">{#if lineNumbers}<span class="no" aria-hidden="true">{i + 1}</span>{/if}<span class="tx">{#each segs as s}<span class={s.role ? `s-${s.role}` : undefined}>{s.text}</span>{/each}{'\n'}</span></span>{/each}</code></pre>
169
169
  {/snippet}
170
170
 
171
171
  {#if copy}
@@ -215,6 +215,7 @@
215
215
 
216
216
  <script lang="ts">
217
217
  import { useLabels } from './labels.js';
218
+ import { scrollFocus } from './scrollfocus.js';
218
219
  import { csstext } from './cssvars.js';
219
220
  import SsrStyle from './SsrStyle.svelte';
220
221
  import { prepareMonacoCsp } from './monaco-csp.js';
@@ -615,6 +616,7 @@
615
616
  {/if}
616
617
 
617
618
  {#if showTable && !whole}
619
+ <!-- the keyboard entry point of the merge (n / p / F7 are handled for focus anywhere inside it), so it stays a tab stop even when it does not scroll -->
618
620
  <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
619
621
  <div class="scroll" role="region" aria-label={L.versionsRegion} tabindex="0">
620
622
  <table class="tbl" data-show={narrow ? tab : 'all'} aria-label={name ? L.versionsOf(name) : L.versions}>
@@ -670,8 +672,7 @@
670
672
  {/if}
671
673
 
672
674
  {#if showResult && !whole}
673
- <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
674
- <div class="result" role="region" aria-label={L.result} tabindex="0">
675
+ <div class="result" {@attach scrollFocus(() => ({ label: L.result }))}>
675
676
  <div class="rhead" aria-hidden="true">{L.result}</div>
676
677
  <table class="tbl res" aria-label={name ? L.mergedOf(name) : L.merged}>
677
678
  <colgroup><col class="c-no" /><col class="c-sym" /><col class="c-code" /></colgroup>
@@ -729,7 +730,7 @@
729
730
  .mv[data-height='fill'], .mv[data-height='fixed'] { display: flex; flex-direction: column; min-height: 0; }
730
731
  .mv[data-height='fill'] { height: 100%; }
731
732
  .mv[data-height='fill'] .scroll, .mv[data-height='fixed'] .scroll, .mv[data-height='fill'] .result, .mv[data-height='fixed'] .result { max-height: none; min-height: 0; flex: 1 1 auto; }
732
- .scroll:focus-visible, .result:focus-visible { outline: none; box-shadow: inset var(--tr-focus-ring); }
733
+ .scroll:focus-visible, .result:focus-visible { outline: none; box-shadow: var(--tr-focus-ring-inset); }
733
734
  .tbl { border-collapse: collapse; width: 100%; table-layout: fixed; font-family: var(--tr-font-mono); font-size: var(--mv-font, var(--tr-text-sm)); line-height: 1.5; tab-size: 4; }
734
735
  .c-no { width: 5ch; } .c-sym { width: 3ch; }
735
736
  th { position: sticky; top: 0; z-index: 1; text-align: left; padding: var(--tr-space-1) var(--tr-space-2); background: var(--tr-surface-container); border-bottom: 1px solid var(--tr-outline-variant); font-family: var(--tr-font-sans); font-size: var(--tr-text-sm); }
@@ -12,6 +12,7 @@
12
12
 
13
13
  <script lang="ts">
14
14
  import { useLabels } from './labels.js';
15
+ import { scrollFocus } from './scrollfocus.js';
15
16
  import { csstext } from './cssvars.js';
16
17
  import SsrStyle from './SsrStyle.svelte';
17
18
  const uid = $props.id();
@@ -48,8 +49,7 @@
48
49
  </script>
49
50
 
50
51
  <SsrStyle {uid} rules={{ '': style ?? '' }} />
51
- <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
52
- <pre {...rest} data-sv={uid} role="region" class="pt" class:wrap class:nums={lineNumbers} {@attach csstext(() => style)} tabindex="0" aria-label={label ?? L.patchText}><code>{#each lines as l, i (i)}<span class={"ln " + l.kind} data-kind={l.kind}>{#if lineNumbers}<span class="no" aria-hidden="true">{i + 1}</span>{/if}<span class="sym">{#if l.mark}<span aria-hidden="true">{l.mark.sym}</span><span class="vh">{l.mark.sr + ": "}</span>{/if}</span><span class="tx">{l.body + '\n'}</span></span>{/each}</code></pre>
52
+ <pre {...rest} data-sv={uid} class="pt" class:wrap class:nums={lineNumbers} {@attach csstext(() => style)} {@attach scrollFocus(() => ({ label: label ?? L.patchText }))}><code>{#each lines as l, i (i)}<span class={"ln " + l.kind} data-kind={l.kind}>{#if lineNumbers}<span class="no" aria-hidden="true">{i + 1}</span>{/if}<span class="sym">{#if l.mark}<span aria-hidden="true">{l.mark.sym}</span><span class="vh">{l.mark.sr + ": "}</span>{/if}</span><span class="tx">{l.body + '\n'}</span></span>{/each}</code></pre>
53
53
 
54
54
  <style>
55
55
  .pt {
@@ -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/dist/theme.d.ts CHANGED
@@ -78,5 +78,5 @@ export interface Theme {
78
78
  }
79
79
  /** The theme in effect at `el`: nearest `data-theme`, else the OS preference; tokens and density as computed there. */
80
80
  export declare function readTheme(el: Element): Theme;
81
- /** Calls `cb` whenever the theme or density can have changed (`data-theme`, `data-density`, class or style on any ancestor, or the OS scheme). Returns a stop function. */
81
+ /** Calls `cb` whenever the theme or density can have changed (`data-theme`, `data-truss-theme`, `data-density`, class or style on any ancestor, or the OS scheme). Returns a stop function. */
82
82
  export declare function watchTheme(el: Element, cb: () => void): () => void;
package/dist/theme.js CHANGED
@@ -208,11 +208,11 @@ export function readTheme(el) {
208
208
  metrics: fontMetrics(cs.getPropertyValue('--tr-text-md'), cs.getPropertyValue('--tr-leading-normal'), cs.getPropertyValue('--tr-font-mono'))
209
209
  };
210
210
  }
211
- /** Calls `cb` whenever the theme or density can have changed (`data-theme`, `data-density`, class or style on any ancestor, or the OS scheme). Returns a stop function. */
211
+ /** Calls `cb` whenever the theme or density can have changed (`data-theme`, `data-truss-theme`, `data-density`, class or style on any ancestor, or the OS scheme). Returns a stop function. */
212
212
  export function watchTheme(el, cb) {
213
213
  const mo = new MutationObserver(cb);
214
214
  for (let n = el; n; n = n.parentElement)
215
- mo.observe(n, { attributes: true, attributeFilter: ['data-theme', 'data-density', 'class', 'style'] });
215
+ mo.observe(n, { attributes: true, attributeFilter: ['data-theme', 'data-truss-theme', 'data-density', 'class', 'style'] });
216
216
  const mq = matchMedia('(prefers-color-scheme: dark)');
217
217
  mq.addEventListener('change', cb);
218
218
  return () => { mo.disconnect(); mq.removeEventListener('change', cb); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-code",
3
- "version": "1.51.0",
3
+ "version": "1.52.0",
4
4
  "description": "Read-only code blocks, diff and merge views, plus an optional Monaco code editor, styled with Truss UI",
5
5
  "type": "module",
6
6
  "license": "MIT",