@human-kit/ui 1.6.0 → 1.7.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.
@@ -28,13 +28,13 @@ The charts are a preview. Their API can change in a minor version of the package
28
28
  </Chart.Root>
29
29
  ```
30
30
 
31
- | Group | Parts |
32
- | ------ | ----------------------------------------------------------------- |
33
- | Root | `Chart.Root`, `Chart.Title`, `Chart.Plot` |
34
- | Marks | `Chart.Line`, `Chart.Area`, `Chart.Bar`, `Chart.Dot`, `Chart.Arc` |
35
- | Guides | `Chart.Axis`, `Chart.Grid`, `Chart.Legend` |
36
- | Access | `Chart.Tooltip`, `Chart.DataTable` |
37
- | Scales | `scaleLinear`, `scaleTime`, `scaleBand` (one subpath for each) |
31
+ | Group | Parts |
32
+ | ------ | ------------------------------------------------------------------------------- |
33
+ | Root | `Chart.Root`, `Chart.Title`, `Chart.Plot` |
34
+ | Marks | `Chart.Line`, `Chart.Area`, `Chart.Bar`, `Chart.Dot`, `Chart.Arc`, `Chart.Rule` |
35
+ | Guides | `Chart.Axis`, `Chart.Grid`, `Chart.Legend` |
36
+ | Access | `Chart.Tooltip`, `Chart.DataTable` |
37
+ | Scales | `scaleLinear`, `scaleTime`, `scaleBand` (one subpath for each) |
38
38
 
39
39
  ## Rules
40
40
 
@@ -123,4 +123,12 @@
123
123
  </tr>
124
124
  {/each}
125
125
  </tbody>
126
+ <!-- A rule is not a row of the data: its row is in the footer, and its value spans the columns. -->
127
+ <tfoot>
128
+ {#each ctx.rules as rule, i (i)}
129
+ <tr data-rule=""
130
+ ><th scope="row">{rule.label}</th><td colspan={table.head.length - 1}>{rule.text}</td></tr
131
+ >
132
+ {/each}
133
+ </tfoot>
126
134
  </table>
@@ -12,5 +12,6 @@ export { default as ChartDataTable } from './data-table/chart-data-table.svelte'
12
12
  export { default as ChartTooltip } from './tooltip/chart-tooltip.svelte';
13
13
  export { default as ChartAxis } from './axis/chart-axis.svelte';
14
14
  export { default as ChartGrid } from './grid/chart-grid.svelte';
15
- export type { Channel, ChartContext, ChartMargin, ChartPoint, ChartRootProps, ChartTitleProps, ChartPlotProps, ChartLineProps, ChartMarkData, ChartAxisProps, ChartBarProps, ChartAreaProps, ChartDotProps, ChartArcProps, ChartSymbol, ChartLegendProps, ChartDataTableProps, ChartTooltipProps, ChartTooltipState, ChartGridProps, ChartTick, FormatOptions, Scale, ScaleOptions, ScaleType, ValueFormat, ChartValue } from './types.js';
15
+ export { default as ChartRule } from './rule/chart-rule.svelte';
16
+ export type { Channel, ChartContext, ChartMargin, ChartPoint, ChartRootProps, ChartTitleProps, ChartPlotProps, ChartLineProps, ChartMarkData, ChartAxisProps, ChartBarProps, ChartAreaProps, ChartDotProps, ChartArcProps, ChartSymbol, ChartLegendProps, ChartDataTableProps, ChartTooltipProps, ChartTooltipState, ChartGridProps, ChartRuleProps, ChartTick, FormatOptions, Scale, ScaleOptions, ScaleType, ValueFormat, ChartValue } from './types.js';
16
17
  export { getChartContext, useChartContext } from './root/context.js';
@@ -12,4 +12,5 @@ export { default as ChartDataTable } from './data-table/chart-data-table.svelte'
12
12
  export { default as ChartTooltip } from './tooltip/chart-tooltip.svelte';
13
13
  export { default as ChartAxis } from './axis/chart-axis.svelte';
14
14
  export { default as ChartGrid } from './grid/chart-grid.svelte';
15
+ export { default as ChartRule } from './rule/chart-rule.svelte';
15
16
  export { getChartContext, useChartContext } from './root/context.js';
@@ -11,3 +11,4 @@ export { default as DataTable } from './data-table/chart-data-table.svelte';
11
11
  export { default as Tooltip } from './tooltip/chart-tooltip.svelte';
12
12
  export { default as Axis } from './axis/chart-axis.svelte';
13
13
  export { default as Grid } from './grid/chart-grid.svelte';
14
+ export { default as Rule } from './rule/chart-rule.svelte';
@@ -11,3 +11,4 @@ export { default as DataTable } from './data-table/chart-data-table.svelte';
11
11
  export { default as Tooltip } from './tooltip/chart-tooltip.svelte';
12
12
  export { default as Axis } from './axis/chart-axis.svelte';
13
13
  export { default as Grid } from './grid/chart-grid.svelte';
14
+ export { default as Rule } from './rule/chart-rule.svelte';
@@ -16,6 +16,7 @@
16
16
  type ChartMargin,
17
17
  type ChartMark,
18
18
  type ChartPoint,
19
+ type ChartRule,
19
20
  type ChartTick,
20
21
  type ChartValue
21
22
  } from './context.js';
@@ -82,6 +83,13 @@
82
83
  const marks = new SvelteMap<string, ChartMark>();
83
84
  let markCount = 0;
84
85
 
86
+ // The rules, in mount order. A rule is not a mark: it is not in `entries`, thus the keyboard
87
+ // does not stop on it, and the parts that read the first mark do not see it.
88
+ const rules = new SvelteMap<ChartRule, 0>();
89
+ // The rules that make a line. A function and not a `$derived`: on a server, a `$derived` keeps
90
+ // the value of its first read, and the plot reads it before the rules in it start.
91
+ const ruleList = () => [...rules.keys()].filter((rule) => rule.text);
92
+
85
93
  const entries = $derived.by(() => {
86
94
  const out: ChartEntry[] = [];
87
95
  for (const [mark, m] of marks) {
@@ -143,7 +151,9 @@
143
151
  }
144
152
  return { xs, ys };
145
153
  });
146
- const extents = $derived([...marks.values()].flatMap((m) => (m.extent ? [m.extent()] : [])));
154
+ const extents = $derived(
155
+ [...marks.values(), ...rules.keys()].flatMap((m) => (m.extent ? [m.extent()] : []))
156
+ );
147
157
  const rawX = $derived([...rootValues.xs, ...extents.flatMap((e) => e.x ?? [])]);
148
158
  const rawY = $derived([...rootValues.ys, ...extents.flatMap((e) => e.y ?? [])]);
149
159
 
@@ -287,10 +297,11 @@
287
297
  const yText = labelled(yLabel, yValueFormat(toY(point.y)));
288
298
  const horizontal = mark?.horizontal?.() ?? false;
289
299
  const parts = [horizontal ? yText : xText, horizontal ? xText : yText];
290
- if (point.series) parts.splice(mark?.list ? 0 : 1, 0, point.series);
291
- const share = mark?.share?.(point);
292
- if (share) parts.push(share);
293
- return parts.join(', ');
300
+ parts.splice(mark?.list ? 0 : 1, 0, point.series);
301
+ // The share of a slice and the texts of the rules come last. An empty text adds nothing.
302
+ return [...parts, mark?.share?.(point), ...[...rules.keys()].map((rule) => rule.note(point))]
303
+ .filter(Boolean)
304
+ .join(', ');
294
305
  }
295
306
 
296
307
  // The point that has the tab stop. Without a valid one, the first point has it.
@@ -456,7 +467,9 @@
456
467
  const labels = $derived({
457
468
  'aria-label': ariaLabel,
458
469
  'aria-labelledby': ariaLabel ? undefined : (ariaLabelledBy ?? titleId ?? undefined),
459
- 'aria-describedby': ariaDescribedBy
470
+ // The description of each rule comes after the description from the consumer.
471
+ 'aria-describedby':
472
+ [ariaDescribedBy, ...ruleList().map((rule) => rule.id)].join(' ').trim() || undefined
460
473
  });
461
474
 
462
475
  const ctx: ChartContext = {
@@ -539,6 +552,13 @@
539
552
  get marks() {
540
553
  return [...marks.values()];
541
554
  },
555
+ rule(rule) {
556
+ rules.set(rule, 0);
557
+ return () => rules.delete(rule);
558
+ },
559
+ get rules() {
560
+ return ruleList();
561
+ },
542
562
  get entries() {
543
563
  return entries;
544
564
  },
@@ -90,6 +90,22 @@ export type ChartMark = {
90
90
  */
91
91
  share?: (point: ChartPoint) => string;
92
92
  };
93
+ /**
94
+ * A rule, as it registers with the root. A rule is not a mark: it has no points, and the keyboard
95
+ * does not stop on it.
96
+ */
97
+ export type ChartRule = {
98
+ /** The id of the element with the description of the rule. */
99
+ readonly id: string;
100
+ /** The text of the rule, for example "Target". */
101
+ readonly label: string;
102
+ /** The value as text, in the format of its axis. It is empty when the rule makes no line. */
103
+ readonly text: string;
104
+ /** The value, when it goes into the domain of its axis: a number or a date. */
105
+ extent: () => ChartExtent;
106
+ /** The text that the rule adds to the name of a point. It can be empty. */
107
+ note: (point: ChartPoint) => string;
108
+ };
93
109
  /** One series of a mark with points, in the order of the keyboard. */
94
110
  export type ChartEntry = {
95
111
  mark: string;
@@ -173,6 +189,10 @@ export type ChartContext = {
173
189
  id: string;
174
190
  unregister(): void;
175
191
  };
192
+ /** Adds a rule. It returns the function that removes it. */
193
+ rule(rule: ChartRule): () => void;
194
+ /** The rules that make a line, in mount order. The plot lists their ids in `aria-describedby`. */
195
+ readonly rules: ReadonlyArray<ChartRule>;
176
196
  /** The marks, in mount order. A part reads their series, for example a legend. */
177
197
  readonly marks: ReadonlyArray<ChartMark>;
178
198
  /** The series of all of the marks with points, in the order of the keyboard. */
@@ -0,0 +1,77 @@
1
+ <script lang="ts">
2
+ import { dev } from '../../internal/environment.js';
3
+ import { round } from '../internal/path.js';
4
+ import { isValue } from '../internal/series.js';
5
+ import { useChartContext } from '../root/context.js';
6
+ import type { ChartRuleProps } from '../types.js';
7
+
8
+ let { value, axis = 'y', label, compare, class: className = '' }: ChartRuleProps = $props();
9
+
10
+ const uid = $props.id();
11
+ const ctx = useChartContext('Chart.Rule');
12
+ const id = `chart-${ctx.instanceId}-rule-${uid}`;
13
+ const vertical = $derived(axis === 'x');
14
+ const n = $derived(vertical ? ctx.toX(value) : ctx.toY(value));
15
+ // The position of the line. A category needs a band scale, and a band scale needs a category. A
16
+ // value out of the range has no position: a sighted user does not see it, thus the description
17
+ // does not give it. The product of the distances to the two ends is negative between them.
18
+ const position = $derived.by(() => {
19
+ const scale = vertical ? ctx.xScale : ctx.yScale;
20
+ const [a, b] = scale.range;
21
+ const p = scale(n);
22
+ return isValue(value) &&
23
+ (typeof value === 'string') === (scale.kind === 'band') &&
24
+ (p - a) * (p - b) < 1
25
+ ? round(p)
26
+ : NaN;
27
+ });
28
+ const text = $derived(isNaN(position) ? '' : vertical ? ctx.formatX(n) : ctx.formatY(n));
29
+ const channel = $derived(vertical ? ctx.xLabel : ctx.yLabel);
30
+ // The line goes across the plot: the range of the other axis. The label of a vertical line goes
31
+ // at the top.
32
+ const across = $derived(vertical ? ctx.yScale.range : ctx.xScale.range);
33
+ const top = $derived(Math.min(...across));
34
+
35
+ const unregister = ctx.rule({
36
+ id,
37
+ get label() {
38
+ return label;
39
+ },
40
+ get text() {
41
+ return text;
42
+ },
43
+ // A category does not go into the domain: a rule does not add a category.
44
+ extent: () => (isValue(value) && typeof value !== 'string' ? { [axis]: [value] } : {}),
45
+ note: (point) => (text && compare?.(point, value)) || ''
46
+ });
47
+ $effect(() => unregister);
48
+
49
+ $effect(() => {
50
+ if (dev && !text) console.warn(`Chart.Rule: "${label}" has no place on the ${axis} scale.`);
51
+ });
52
+ </script>
53
+
54
+ <!--
55
+ A rule is a reference for the values, not a value: it takes no focus, and it is hidden from
56
+ assistive technology. The plot gives its description, and the data table gives its row.
57
+ -->
58
+ <g class={className} data-rule="" data-axis={axis} aria-hidden="true">
59
+ {#if text}
60
+ <line
61
+ x1={vertical ? position : across[0]}
62
+ x2={vertical ? position : across[1]}
63
+ y1={vertical ? across[0] : position}
64
+ y2={vertical ? across[1] : position}
65
+ stroke="currentColor"
66
+ />
67
+ <text
68
+ x={vertical ? position : across[1]}
69
+ y={vertical ? top : position}
70
+ dx={vertical ? '0.32em' : undefined}
71
+ dy={vertical ? '0.71em' : '-0.32em'}
72
+ text-anchor={vertical ? undefined : 'end'}
73
+ fill="currentColor">{label}</text
74
+ >
75
+ <desc {id}>{label}, {channel ? `${channel} ${text}` : text}</desc>
76
+ {/if}
77
+ </g>
@@ -0,0 +1,4 @@
1
+ import type { ChartRuleProps } from '../types.js';
2
+ declare const ChartRule: import("svelte").Component<ChartRuleProps, {}, "">;
3
+ type ChartRule = ReturnType<typeof ChartRule>;
4
+ export default ChartRule;
@@ -211,6 +211,29 @@ export type ChartGridProps = {
211
211
  /** The CSS class names of the group of the grid. */
212
212
  class?: string;
213
213
  };
214
+ export type ChartRuleProps = {
215
+ /**
216
+ * The value of the rule: a number, a date, or a category of a band scale. A number or a date
217
+ * goes into the domain of its axis. A value with no place on the scale makes no line.
218
+ */
219
+ value: ChartValue;
220
+ /**
221
+ * The axis of the value. The default is `y`: a horizontal line. `x` makes a vertical line.
222
+ */
223
+ axis?: 'x' | 'y';
224
+ /**
225
+ * The text of the rule, for example "Target". The plot shows it at the end of the line. The
226
+ * description of the plot and the data table give it with the value.
227
+ */
228
+ label: string;
229
+ /**
230
+ * A function of a point and the value of the rule. Its text goes at the end of the name of the
231
+ * point, for example "above target". An empty text adds nothing.
232
+ */
233
+ compare?: (point: ChartPoint, value: ChartValue) => string;
234
+ /** The CSS class names of the group of the rule. */
235
+ class?: string;
236
+ };
214
237
  export type ChartBarProps<T> = ChartMarkData<T> & {
215
238
  /**
216
239
  * The name of the series of this mark, when it has no series channel. The legend, the names of
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",