@ruckc/truss-chart 1.52.0 → 1.53.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.
@@ -298,7 +298,7 @@
298
298
  svg { display: block; overflow: visible; }
299
299
  .grid { stroke: var(--tr-chart-grid-color, var(--tr-outline-variant)); stroke-opacity: var(--tr-chart-grid-opacity, 0.6); stroke-dasharray: var(--tr-chart-grid-dash, none); }
300
300
  .grid[data-zero='true'] { stroke-opacity: 1; }
301
- .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
301
+ .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-family: var(--tr-chart-tick-font, inherit); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
302
302
  .val { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); font-weight: 600; font-variant-numeric: tabular-nums; }
303
303
  .bar { outline: none; transition: filter var(--tr-dur-fast) var(--tr-ease); }
304
304
  .bar.act { cursor: pointer; }
@@ -89,7 +89,7 @@
89
89
  <script lang="ts">
90
90
  import { useLabels } from './labels.js';
91
91
  import type { Snippet } from 'svelte';
92
- import { startDrag } from '@ruckc/truss-ui';
92
+ import { startDrag, readColorTokens, onThemeChange } from '@ruckc/truss-ui';
93
93
  import ChartFrame from './ChartFrame.svelte';
94
94
  import { cssvars, csstext } from './cssvars.js';
95
95
  import SsrStyle from './SsrStyle.svelte';
@@ -249,17 +249,16 @@
249
249
  schedule();
250
250
  return () => { cancelAnimationFrame(raf); raf = 0; };
251
251
  });
252
- // colours come from CSS custom properties, so follow theme switches (explicit data-theme, a data-truss-theme theme, or the OS preference)
252
+ // colours come from CSS custom properties, so follow theme switches (data-theme or data-truss-theme on any ancestor, or the OS preference)
253
253
  $effect(() => {
254
- const mo = new MutationObserver(schedule);
255
- mo.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'data-truss-theme', 'class'] });
256
- // the default theme follows the OS, which changes without touching any attribute
257
- const os = matchMedia('(prefers-color-scheme: dark)');
258
- os.addEventListener('change', schedule);
259
- return () => { mo.disconnect(); os.removeEventListener('change', schedule); };
254
+ if (canvas) return onThemeChange(canvas, schedule);
260
255
  });
261
256
 
262
257
  const cssVar = (st: CSSStyleDeclaration, name: string, fallback: string) => st.getPropertyValue(name).trim() || fallback;
258
+ const COLOR_TOKENS = [
259
+ '--tr-on-surface', '--tr-on-surface-variant', '--tr-outline-variant', '--tr-surface', '--tr-chart-tick-color', '--tr-chart-grid-color',
260
+ ...Array.from({ length: 8 }, (_, i) => `--trc-s${i + 1}`), ...Array.from({ length: 9 }, (_, i) => `--trc-seq-${i + 1}`)
261
+ ];
263
262
 
264
263
  function draw() {
265
264
  raf = 0;
@@ -273,13 +272,16 @@
273
272
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
274
273
  ctx.clearRect(0, 0, W, H);
275
274
  const st = getComputedStyle(c);
275
+ // every colour the canvas uses, resolved by the browser at the canvas (any CSS colour syntax: oklch(), light-dark()…)
276
+ const col = readColorTokens(c, COLOR_TOKENS);
276
277
  const v = cur;
277
278
  const X = linear(v.x0, v.x1, m.left, m.left + pw), Y = linear(v.y0, v.y1, m.top + ph, m.top);
278
279
 
279
280
  // axes
280
- ctx.font = `11px ${cssVar(st, '--tr-font-sans', 'sans-serif')}`;
281
- ctx.fillStyle = cssVar(st, '--tr-on-surface-variant', '#666');
282
- ctx.strokeStyle = cssVar(st, '--tr-outline-variant', '#ccc');
281
+ ctx.font = `11px ${cssVar(st, '--tr-chart-tick-font', cssVar(st, '--tr-font-sans', 'sans-serif'))}`;
282
+ ctx.fillStyle = col['--tr-chart-tick-color'] ?? col['--tr-on-surface-variant'] ?? '#666';
283
+ ctx.strokeStyle = col['--tr-chart-grid-color'] ?? col['--tr-outline-variant'] ?? '#ccc';
284
+ const gridAlpha = Number.parseFloat(cssVar(st, '--tr-chart-grid-opacity', '0.6'));
283
285
  ctx.globalAlpha = 1;
284
286
  ctx.lineWidth = 1;
285
287
  ctx.textBaseline = 'middle';
@@ -287,7 +289,7 @@
287
289
  for (const t of niceTicks(v.y0, v.y1, 5).ticks) {
288
290
  if (t < v.y0 || t > v.y1) continue;
289
291
  const y = Math.round(Y(t)) + 0.5;
290
- ctx.globalAlpha = 0.6;
292
+ ctx.globalAlpha = gridAlpha;
291
293
  ctx.beginPath(); ctx.moveTo(m.left, y); ctx.lineTo(m.left + pw, y); ctx.stroke();
292
294
  ctx.globalAlpha = 1;
293
295
  ctx.fillText(fmtAxis(t), m.left - 8, y);
@@ -315,11 +317,7 @@
315
317
  ctx.rect(m.left, m.top, pw, ph);
316
318
  ctx.clip();
317
319
  if (markers) {
318
- const slots = Array.from({ length: 8 }, (_, i) => {
319
- ctx.fillStyle = '#000';
320
- ctx.fillStyle = cssVar(st, `--trc-s${i + 1}`, '#2a78d6');
321
- return ctx.fillStyle as string;
322
- });
320
+ const slots = Array.from({ length: 8 }, (_, i) => col[`--trc-s${i + 1}`] ?? '#2a78d6');
323
321
  const { xs: dx, ys: dy, g, n: dn } = data;
324
322
  const half = markerSize / 2;
325
323
  const x0 = v.x0, x1 = v.x1, y0 = v.y0, y1 = v.y1;
@@ -336,11 +334,9 @@
336
334
  } else {
337
335
  const stops: [number, number, number][] = [];
338
336
  for (let i = 1; i <= 9; i++) {
339
- const raw = st.getPropertyValue(`--trc-seq-${i}`).trim();
340
- if (!raw) break;
341
- ctx.fillStyle = '#000';
342
- ctx.fillStyle = raw;
343
- stops.push(parseColor(ctx.fillStyle as string));
337
+ const c = col[`--trc-seq-${i}`];
338
+ if (!c) break;
339
+ stops.push(parseColor(c));
344
340
  }
345
341
  const colors = stops.length > 1 ? stops : DEFAULT_RAMP.map(parseColor);
346
342
  let max = 1;
@@ -359,7 +355,7 @@
359
355
  }
360
356
  }
361
357
  if (hl && !hlHidden) {
362
- const core = cssVar(st, '--tr-on-surface', '#111'), halo = cssVar(st, '--tr-surface', '#fff');
358
+ const core = col['--tr-on-surface'] ?? '#111', halo = col['--tr-surface'] ?? '#fff';
363
359
  const r = highlightSize / 2;
364
360
  ctx.globalAlpha = 1;
365
361
  for (let i = 0; i < hl.n; i++) {
@@ -230,7 +230,7 @@
230
230
  svg { display: block; outline: none; overflow: visible; }
231
231
  svg:focus-visible { border-radius: var(--tr-radius-sm); box-shadow: var(--tr-focus-ring); }
232
232
  .grid { stroke: var(--tr-chart-grid-color, var(--tr-outline-variant)); stroke-opacity: var(--tr-chart-grid-opacity, 0.6); stroke-dasharray: var(--tr-chart-grid-dash, none); }
233
- .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
233
+ .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-family: var(--tr-chart-tick-font, inherit); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
234
234
  .bar { transition: opacity var(--tr-dur-fast) var(--tr-ease), filter var(--tr-dur-fast) var(--tr-ease); }
235
235
  .bar[data-hover='true'] { filter: brightness(1.12) saturate(1.1); }
236
236
  .bar[data-dim='true'] { opacity: 0.3; }
@@ -291,7 +291,7 @@
291
291
  d={areaPath(seg, X, Y, si)} />
292
292
  {/each}
293
293
  {/if}
294
- <path class="line" stroke={s.color} d={segments(s.values).map((seg) => seg.map((i, k) => `${k ? 'L' : 'M'}${X(xs[i]).toFixed(1)},${Y(bands[si][i]!.hi).toFixed(1)}`).join(' ')).join(' ')} />
294
+ <path class="line" stroke={s.color} color={s.color} d={segments(s.values).map((seg) => seg.map((i, k) => `${k ? 'L' : 'M'}${X(xs[i]).toFixed(1)},${Y(bands[si][i]!.hi).toFixed(1)}`).join(' ')).join(' ')} />
295
295
  {/each}
296
296
 
297
297
  {#each refs as r}
@@ -362,10 +362,11 @@
362
362
  svg.act { cursor: pointer; }
363
363
  svg:focus-visible { border-radius: var(--tr-radius-sm); box-shadow: var(--tr-focus-ring); }
364
364
  .grid { stroke: var(--tr-chart-grid-color, var(--tr-outline-variant)); stroke-opacity: var(--tr-chart-grid-opacity, 0.6); stroke-dasharray: var(--tr-chart-grid-dash, none); stroke-width: 1; }
365
- .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
366
- .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
367
- .area { fill-opacity: 0.14; }
368
- .area.stack { fill-opacity: 0.55; }
365
+ .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-family: var(--tr-chart-tick-font, inherit); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
366
+ .line { fill: none; stroke-width: var(--tr-chart-line-width, 2); stroke-linejoin: round; stroke-linecap: round; filter: var(--tr-chart-line-glow, none); }
367
+ /* an unstacked area: its tint, and an optional fade toward the baseline (a mask image, e.g. linear-gradient(#000, transparent)) */
368
+ .area { fill-opacity: var(--tr-chart-area-opacity, 0.14); -webkit-mask-image: var(--tr-chart-area-fade, none); mask-image: var(--tr-chart-area-fade, none); }
369
+ .area.stack { fill-opacity: 0.55; -webkit-mask-image: none; mask-image: none; }
369
370
  .band { fill-opacity: 0.18; stroke: none; }
370
371
  .ref { stroke: var(--tr-on-surface); stroke-width: 1.5; stroke-dasharray: 6 3; }
371
372
  .reflabel { fill: var(--tr-on-surface); font-size: var(--tr-text-xs); font-weight: 600; }
@@ -238,7 +238,7 @@
238
238
  svg.act { cursor: pointer; }
239
239
  svg:focus-visible { border-radius: var(--tr-radius-sm); box-shadow: var(--tr-focus-ring); }
240
240
  .grid { stroke: var(--tr-chart-grid-color, var(--tr-outline-variant)); stroke-opacity: var(--tr-chart-grid-opacity, 0.6); stroke-dasharray: var(--tr-chart-grid-dash, none); stroke-width: 1; }
241
- .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
241
+ .tick { fill: var(--tr-chart-tick-color, var(--tr-on-surface-variant)); font-family: var(--tr-chart-tick-font, inherit); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
242
242
  .axis { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-sm); font-weight: 600; }
243
243
  .pt { fill-opacity: 0.85; stroke: var(--tr-surface-container-low); stroke-width: 2; }
244
244
  .pt.on { fill-opacity: 1; }
@@ -34,6 +34,7 @@
34
34
  deltaGoodDirection = 'up',
35
35
  spark,
36
36
  size = 'md',
37
+ featured = false,
37
38
  labels
38
39
  }: {
39
40
  label: string;
@@ -50,6 +51,8 @@
50
51
  spark?: number[];
51
52
  /** `'auto'` makes the tile a width-based size container: the value size and padding follow the tile's width (`cqi`, clamped to 20-56px), so it needs no fixed size in a grid cell; it ignores height so it never collapses. `'sm'` uses `--tr-text-xl` for the value and tighter padding. The value size can also be set from outside with the `--trc-stat-value-size` custom property (default 28px). */
52
53
  size?: 'sm' | 'md' | 'auto';
54
+ /** The headline number of a row of tiles: a tinted tile (the primary container in classic; a theme may give it a gradient and a glow through the `--tr-stat-featured-*` hooks) whose trend line follows its text colour. */
55
+ featured?: boolean;
53
56
  /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
54
57
  labels?: Partial<StatTileLabels>;
55
58
  } = $props();
@@ -75,11 +78,11 @@
75
78
  </script>
76
79
 
77
80
  <div class="host" class:auto={size === 'auto'}>
78
- <div class="tile" class:sm={size === 'sm'} role="group" aria-label={summary}>
81
+ <div class="tile" class:sm={size === 'sm'} class:featured role="group" aria-label={summary}>
79
82
  <div class="label" aria-hidden="true">{label}</div>
80
83
  <div class="row" aria-hidden="true">
81
84
  <div class="value">{shown}</div>
82
- {#if spark && spark.length > 1}<Sparkline values={spark} {label} />{/if}
85
+ {#if spark && spark.length > 1}<Sparkline values={spark} {label} color={featured ? 'currentColor' : undefined} />{/if}
83
86
  </div>
84
87
  {#if delta != null}
85
88
  <div class={deltaClass} data-dir={dir} aria-hidden="true">
@@ -97,7 +100,14 @@
97
100
  .host.auto { display: block; container-type: inline-size; height: 100%; min-width: 0; }
98
101
  .auto .tile { box-sizing: border-box; height: 100%; padding: clamp(8px, 4cqi, 20px) clamp(12px, 5cqi, 24px); gap: clamp(2px, 1.5cqi, 8px); }
99
102
  .auto .value { font-size: var(--trc-stat-value-size, clamp(20px, 11cqi, 56px)); }
100
- .tile { display: flex; flex-direction: column; gap: 4px; padding: 12px 16px; background: var(--tr-surface-container-low); border: 1px solid var(--tr-outline-variant); border-radius: var(--tr-radius-md); color: var(--tr-on-surface); font-family: var(--tr-font-sans); }
103
+ .tile { display: flex; flex-direction: column; gap: 4px; padding: 12px 16px; background: var(--tr-stat-tile-bg, var(--tr-surface-container-low)); border: 1px solid var(--tr-stat-tile-border-color, var(--tr-outline-variant)); border-radius: var(--tr-radius-card, var(--tr-radius-md)); box-shadow: var(--tr-stat-tile-shadow, none); color: var(--tr-on-surface); font-family: var(--tr-font-sans); }
104
+ .tile.featured {
105
+ background-color: var(--tr-stat-featured-bg, var(--tr-primary-container)); background-image: var(--tr-stat-featured-image, none);
106
+ border-color: var(--tr-stat-featured-border-color, transparent); box-shadow: var(--tr-stat-featured-shadow, none); color: var(--tr-stat-featured-color, var(--tr-on-primary-container));
107
+ }
108
+ .featured .label, .featured .vs, .featured .delta.good, .featured .delta.bad, .featured .delta.neutral { color: inherit; }
109
+ .tile.featured:not(.sm) .value { font-size: var(--tr-stat-featured-value-size, var(--trc-stat-value-size, 28px)); }
110
+ .auto .tile.featured .value { font-size: var(--tr-stat-featured-value-size, var(--trc-stat-value-size, clamp(20px, 11cqi, 56px))); }
101
111
  .tile.sm { padding: 8px 12px; gap: 2px; }
102
112
  .tile.sm .value { font-size: var(--trc-stat-value-size, var(--tr-text-xl)); }
103
113
  .label { color: var(--tr-on-surface-variant); font-size: var(--tr-text-sm); font-weight: 600; }
@@ -25,6 +25,8 @@ type $$ComponentProps = {
25
25
  spark?: number[];
26
26
  /** `'auto'` makes the tile a width-based size container: the value size and padding follow the tile's width (`cqi`, clamped to 20-56px), so it needs no fixed size in a grid cell; it ignores height so it never collapses. `'sm'` uses `--tr-text-xl` for the value and tighter padding. The value size can also be set from outside with the `--trc-stat-value-size` custom property (default 28px). */
27
27
  size?: 'sm' | 'md' | 'auto';
28
+ /** The headline number of a row of tiles: a tinted tile (the primary container in classic; a theme may give it a gradient and a glow through the `--tr-stat-featured-*` hooks) whose trend line follows its text colour. */
29
+ featured?: boolean;
28
30
  /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
29
31
  labels?: Partial<StatTileLabels>;
30
32
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-chart",
3
- "version": "1.52.0",
3
+ "version": "1.53.0",
4
4
  "description": "Zero-dependency SVG charts for Truss UI",
5
5
  "type": "module",
6
6
  "license": "MIT",