@tonycodes-ui/mortar 0.28.1 → 0.30.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.
@@ -0,0 +1,124 @@
1
+ import type { CSSProperties } from "react";
2
+ import { componentRoot, cx } from "./internal.js";
3
+ import {
4
+ areaPath,
5
+ extent,
6
+ linearScale,
7
+ linePath,
8
+ seriesColour,
9
+ type ChartSlot,
10
+ } from "./chart-core.js";
11
+
12
+ export interface SparklineProps {
13
+ /** The values in order, oldest first. Missing values (NaN) break the line. */
14
+ values: readonly number[];
15
+ /** What the values measure, e.g. "Revenue, last 12 months". Read as the start of its summary. */
16
+ label: string;
17
+ /** Formats values in the spoken summary. Defaults to the locale's number format. */
18
+ format?: (value: number) => string;
19
+ /** Series colour slot, 1–8. Default 1. */
20
+ slot?: ChartSlot;
21
+ /** Height in pixels; the width follows the container. Default 32. */
22
+ height?: number;
23
+ /** Mark the latest value with a dot. Default true. */
24
+ showLast?: boolean;
25
+ /** Fill under the line. */
26
+ area?: boolean;
27
+ /** Shade a reference range, such as a target or normal band. */
28
+ band?: { min: number; max: number };
29
+ id?: string;
30
+ className?: string;
31
+ }
32
+
33
+ const number = new Intl.NumberFormat();
34
+
35
+ /** The spoken summary: label, count, range and latest value. */
36
+ export function sparklineSummary(
37
+ values: readonly number[],
38
+ label: string,
39
+ format: (value: number) => string = (v) => number.format(v),
40
+ ) {
41
+ const finite = values.filter(Number.isFinite);
42
+ if (!finite.length) return `${label}: no data`;
43
+ const [min, max] = extent(finite);
44
+ const last = finite[finite.length - 1];
45
+ return finite.length === 1
46
+ ? `${label}: ${format(last)}`
47
+ : `${label}: ${finite.length} values from ${format(min)} to ${format(max)}, latest ${format(last)}`;
48
+ }
49
+
50
+ /** A small, axis-free trend line for a stat tile, table cell or list row. */
51
+ export function Sparkline({
52
+ values,
53
+ label,
54
+ format,
55
+ slot = 1,
56
+ height = 32,
57
+ showLast = true,
58
+ area = false,
59
+ band,
60
+ id,
61
+ className,
62
+ }: SparklineProps) {
63
+ const [min, max] = extent(
64
+ band ? [...values, band.min, band.max] : [...values],
65
+ );
66
+ // A single value maps through a flat domain to the middle.
67
+ const x = linearScale([0, values.length - 1], [0, 100]);
68
+ const y = linearScale([min, max], [100, 0]);
69
+ const points = values.map((v, i) => ({ x: x(i), y: y(v) }));
70
+ let lastIndex = values.length - 1;
71
+ while (lastIndex >= 0 && !Number.isFinite(values[lastIndex])) lastIndex--;
72
+ const last = lastIndex >= 0 ? points[lastIndex] : undefined;
73
+ return (
74
+ <span
75
+ id={id}
76
+ {...componentRoot("Sparkline")}
77
+ className={cx("m-sparkline", className)}
78
+ role="img"
79
+ aria-label={sparklineSummary(values, label, format)}
80
+ style={
81
+ {
82
+ height,
83
+ "--m-sparkline-colour": seriesColour(slot),
84
+ } as CSSProperties
85
+ }
86
+ >
87
+ {/* The plot sits inside padding sized for the dot and its ring, so
88
+ nothing reaches past the sparkline's own box. */}
89
+ <span className="m-sparkline-plot">
90
+ <svg
91
+ viewBox="0 0 100 100"
92
+ preserveAspectRatio="none"
93
+ aria-hidden="true"
94
+ focusable="false"
95
+ >
96
+ {band && (
97
+ <rect
98
+ className="m-sparkline-band"
99
+ x="0"
100
+ width="100"
101
+ y={Math.min(y(band.min), y(band.max))}
102
+ height={Math.abs(y(band.min) - y(band.max))}
103
+ />
104
+ )}
105
+ {area && (
106
+ <path className="m-sparkline-area" d={areaPath(points, 100)} />
107
+ )}
108
+ <path
109
+ className="m-sparkline-line"
110
+ d={linePath(points)}
111
+ vectorEffect="non-scaling-stroke"
112
+ />
113
+ </svg>
114
+ {showLast && last && (
115
+ <span
116
+ className="m-sparkline-last"
117
+ style={{ left: `${last.x}%`, top: `${last.y}%` }}
118
+ aria-hidden="true"
119
+ />
120
+ )}
121
+ </span>
122
+ </span>
123
+ );
124
+ }
@@ -0,0 +1,128 @@
1
+ // Table building blocks shared by DataTable and DataGrid. Internal: the public
2
+ // types are re-exported from application.tsx and the package entry.
3
+ import { useEffect, useRef, type ReactNode } from "react";
4
+ export type SortDirection = "ascending" | "descending";
5
+ export interface TableSort {
6
+ key: string;
7
+ direction: SortDirection;
8
+ }
9
+ export interface DataTableColumn<T> {
10
+ key: string;
11
+ label: string;
12
+ render: (row: T) => ReactNode;
13
+ /** `end` aligns numbers and amounts to the trailing edge. */
14
+ align?: "start" | "end";
15
+ /** Enable header sorting. Supply sortValue unless row[key] is a string, number or Date. */
16
+ sortable?: boolean;
17
+ sortValue?: (row: T) => string | number | Date | null | undefined;
18
+ /** CSS width, e.g. "12rem" or "30%". */
19
+ width?: string;
20
+ /** Clip long values to one line with an ellipsis. */
21
+ truncate?: boolean;
22
+ /**
23
+ * How long the column stays as the table narrows (its own width, not the
24
+ * screen's): `medium` hides below 480px, `low` below 768px. The first
25
+ * column always shows. Hidden columns leave the accessibility tree too, so
26
+ * keep what people need in visible columns or on the detail page.
27
+ */
28
+ priority?: "high" | "medium" | "low";
29
+ /**
30
+ * `end` pins the column to the trailing edge while the table scrolls
31
+ * sideways, so a row-actions or status column stays reachable.
32
+ */
33
+ pin?: "end";
34
+ }
35
+ /** Responsive attributes shared by a column's `col`, header and cells. */
36
+ export function columnLayout<T>(col: DataTableColumn<T>, index: number) {
37
+ return {
38
+ "data-priority":
39
+ index > 0 && col.priority && col.priority !== "high"
40
+ ? col.priority
41
+ : undefined,
42
+ "data-pin": col.pin,
43
+ };
44
+ }
45
+ const collator = new Intl.Collator(undefined, {
46
+ numeric: true,
47
+ sensitivity: "base",
48
+ });
49
+ export const isEmpty = (v: unknown) =>
50
+ v === null || v === undefined || v === "";
51
+ export function compareValues(a: unknown, b: unknown) {
52
+ if (isEmpty(a) || isEmpty(b))
53
+ return isEmpty(a) === isEmpty(b) ? 0 : isEmpty(a) ? 1 : -1;
54
+ if (a instanceof Date && b instanceof Date) return a.getTime() - b.getTime();
55
+ if (typeof a === "number" && typeof b === "number") return a - b;
56
+ return collator.compare(String(a), String(b));
57
+ }
58
+ /** Stable sort; empty values always sort last regardless of direction. */
59
+ export function sortRows<T>(
60
+ rows: T[],
61
+ column: Pick<DataTableColumn<T>, "key" | "sortValue">,
62
+ direction: SortDirection,
63
+ ) {
64
+ const value =
65
+ column.sortValue ||
66
+ ((row: T) => (row as Record<string, unknown>)[column.key] as string);
67
+ const sign = direction === "ascending" ? 1 : -1;
68
+ return rows
69
+ .map((row, index) => ({ row, index, v: value(row) }))
70
+ .sort(
71
+ (a, b) =>
72
+ // Empty values sort last in both directions, so only real values flip.
73
+ (isEmpty(a.v) || isEmpty(b.v) ? 1 : sign) * compareValues(a.v, b.v) ||
74
+ a.index - b.index,
75
+ )
76
+ .map((entry) => entry.row);
77
+ }
78
+ export function SelectAll({
79
+ checked,
80
+ partial,
81
+ onChange,
82
+ }: {
83
+ checked: boolean;
84
+ partial: boolean;
85
+ onChange: () => void;
86
+ }) {
87
+ const ref = useRef<HTMLInputElement>(null);
88
+ useEffect(() => {
89
+ if (ref.current) ref.current.indeterminate = partial;
90
+ }, [partial]);
91
+ return (
92
+ <input
93
+ ref={ref}
94
+ type="checkbox"
95
+ aria-label="Select all rows"
96
+ checked={checked}
97
+ onChange={onChange}
98
+ />
99
+ );
100
+ }
101
+ /** Buckets ordered rows by group; groups keep their first-appearance order in the unsorted rows. */
102
+ export function groupRows<T>(
103
+ rows: T[],
104
+ ordered: T[],
105
+ groupBy: (row: T) => string,
106
+ ) {
107
+ const groups = new Map<string, T[]>(rows.map((row) => [groupBy(row), []]));
108
+ ordered.forEach((row) => groups.get(groupBy(row))!.push(row));
109
+ return [...groups];
110
+ }
111
+ /** Full-width header row that labels the rows of its tbody. */
112
+ export function TableGroupRow({
113
+ label,
114
+ span,
115
+ }: {
116
+ label: string;
117
+ span: number;
118
+ }) {
119
+ return (
120
+ <tr className="m-table-group">
121
+ <th colSpan={span} scope="rowgroup">
122
+ {label}
123
+ </th>
124
+ </tr>
125
+ );
126
+ }
127
+ export const columnAlign = (align?: "start" | "end") =>
128
+ align === "end" ? "end" : undefined;
package/src/tokens.css CHANGED
@@ -90,6 +90,16 @@
90
90
  --m-warning: #945700;
91
91
  --m-danger: #c70014;
92
92
  --m-info: #0060c7;
93
+ /* Chart series, in fixed order; validated for colour-blind separation on
94
+ the light and dark surfaces (docs/charts.md). Not for status. */
95
+ --m-chart-1: #2a78d6;
96
+ --m-chart-2: #eb6834;
97
+ --m-chart-3: #1baf7a;
98
+ --m-chart-4: #eda100;
99
+ --m-chart-5: #e87ba4;
100
+ --m-chart-6: #008300;
101
+ --m-chart-7: #6250d6;
102
+ --m-chart-8: #e34948;
93
103
  --m-on-feedback: #ffffff;
94
104
  --m-code-bg: #000000;
95
105
  --m-code-line: rgb(255 255 255 / 0.12);
@@ -176,6 +186,14 @@
176
186
  --m-warning: #ffd60a;
177
187
  --m-danger: #ff6961;
178
188
  --m-info: #5aa9ff;
189
+ --m-chart-1: #3987e5;
190
+ --m-chart-2: #d95926;
191
+ --m-chart-3: #199e70;
192
+ --m-chart-4: #c98500;
193
+ --m-chart-5: #d55181;
194
+ --m-chart-6: #0ca30c;
195
+ --m-chart-7: #9085e9;
196
+ --m-chart-8: #e66767;
179
197
  --m-on-feedback: #000000;
180
198
  --m-code-bg: #0e0e10;
181
199
  --m-code-line: rgb(255 255 255 / 0.1);
@@ -261,6 +279,14 @@
261
279
  --m-warning: #ffd60a;
262
280
  --m-danger: #ff6961;
263
281
  --m-info: #5aa9ff;
282
+ --m-chart-1: #3987e5;
283
+ --m-chart-2: #d95926;
284
+ --m-chart-3: #199e70;
285
+ --m-chart-4: #c98500;
286
+ --m-chart-5: #d55181;
287
+ --m-chart-6: #0ca30c;
288
+ --m-chart-7: #9085e9;
289
+ --m-chart-8: #e66767;
264
290
  --m-on-feedback: #000000;
265
291
  --m-code-bg: #0e0e10;
266
292
  --m-code-line: rgb(255 255 255 / 0.1);
package/styles.css CHANGED
@@ -5,6 +5,7 @@
5
5
  @import "./src/overlays.css";
6
6
  @import "./src/application.css";
7
7
  @import "./src/data.css";
8
+ @import "./src/charts.css";
8
9
  @import "./src/calendar.css";
9
10
  @import "./src/marketing.css";
10
11
  @import "./src/docs.css";