@n1kname/components 0.3.0 → 0.5.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.
- package/package.json +1 -1
- package/src/DataTable.astro +12 -2
- package/src/Legend.astro +100 -0
- package/src/datatable.js +2 -2
package/package.json
CHANGED
package/src/DataTable.astro
CHANGED
|
@@ -9,6 +9,8 @@ interface Column {
|
|
|
9
9
|
unit?: string;
|
|
10
10
|
/** Words are left-aligned, numbers right-aligned. */
|
|
11
11
|
align?: 'left' | 'right';
|
|
12
|
+
/** Words that can run long, such as a name or a description: they wrap, so the figures stay in view. */
|
|
13
|
+
wrap?: boolean;
|
|
12
14
|
sortable?: boolean;
|
|
13
15
|
}
|
|
14
16
|
type Cell = string | number | null | { html: string; sort?: string | number };
|
|
@@ -35,7 +37,7 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
35
37
|
<tr>
|
|
36
38
|
{
|
|
37
39
|
columns.map((c) => (
|
|
38
|
-
<th scope="col" class:list={{ left: c.align === 'left' }}>
|
|
40
|
+
<th scope="col" class:list={{ left: c.align === 'left', wrap: c.wrap }}>
|
|
39
41
|
{c.sortable === false ? (
|
|
40
42
|
<span class="plain">{c.label}{c.unit && <span class="unit"> {c.unit}</span>}</span>
|
|
41
43
|
) : (
|
|
@@ -54,7 +56,7 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
54
56
|
const obj = c !== null && typeof c === 'object' ? c : null;
|
|
55
57
|
const sort = obj ? obj.sort : c;
|
|
56
58
|
return (
|
|
57
|
-
<td class:list={{ left: columns[i]?.align === 'left' }} data-sort={sort ?? undefined}>
|
|
59
|
+
<td class:list={{ left: columns[i]?.align === 'left', wrap: columns[i]?.wrap }} data-sort={sort ?? undefined}>
|
|
58
60
|
{obj ? <Fragment set:html={obj.html} /> : show(c)}
|
|
59
61
|
</td>
|
|
60
62
|
);
|
|
@@ -174,6 +176,14 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
174
176
|
font-size: var(--text-body-sm);
|
|
175
177
|
}
|
|
176
178
|
|
|
179
|
+
/* Long words wrap, and keep enough room to read. Figures never wrap. */
|
|
180
|
+
.data-table :global(td.wrap) {
|
|
181
|
+
min-width: 16ch;
|
|
182
|
+
padding-block: var(--sp-2);
|
|
183
|
+
line-height: var(--leading-caption);
|
|
184
|
+
white-space: normal;
|
|
185
|
+
}
|
|
186
|
+
|
|
177
187
|
.data-table :global(tbody tr) {
|
|
178
188
|
transition: background-color var(--motion-hover) var(--ease-hover);
|
|
179
189
|
}
|
package/src/Legend.astro
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* What each colour or mark in a chart stands for. Put it under the chart it explains.
|
|
4
|
+
*
|
|
5
|
+
* A key's `mark` says how it is drawn:
|
|
6
|
+
* fill a solid swatch, for bars, areas and cells (the default)
|
|
7
|
+
* line a short stroke, for a line chart
|
|
8
|
+
* dot a filled dot, for a dot plot
|
|
9
|
+
* hollow a ring, for a second kind of dot
|
|
10
|
+
* stripes diagonal stripes, for something known and set aside
|
|
11
|
+
* empty an outline only, for "nothing here"
|
|
12
|
+
*/
|
|
13
|
+
interface Key {
|
|
14
|
+
label: string;
|
|
15
|
+
/** Any CSS colour, best a chart token such as var(--c-chart-1). Not needed for `empty`. */
|
|
16
|
+
color?: string;
|
|
17
|
+
mark?: 'fill' | 'line' | 'dot' | 'hollow' | 'stripes' | 'empty';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
keys: Key[];
|
|
22
|
+
/** Read out before the keys. Hide the legend with aria-hidden when the chart's own text already says it all. */
|
|
23
|
+
label?: string;
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { keys, label = 'Legend', class: className, ...rest } = Astro.props;
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
<ul class:list={['legend', className]} aria-label={label} {...rest}>
|
|
31
|
+
{
|
|
32
|
+
keys.map((k) => (
|
|
33
|
+
<li>
|
|
34
|
+
<i class:list={['mark', k.mark ?? 'fill']} style={k.color ? `--mark: ${k.color}` : undefined} />
|
|
35
|
+
{k.label}
|
|
36
|
+
</li>
|
|
37
|
+
))
|
|
38
|
+
}
|
|
39
|
+
</ul>
|
|
40
|
+
|
|
41
|
+
<style>
|
|
42
|
+
.legend {
|
|
43
|
+
display: flex;
|
|
44
|
+
flex-wrap: wrap;
|
|
45
|
+
gap: var(--sp-2) var(--sp-4);
|
|
46
|
+
margin: var(--sp-3) 0 0;
|
|
47
|
+
padding: 0;
|
|
48
|
+
list-style: none;
|
|
49
|
+
font-size: var(--text-caption);
|
|
50
|
+
line-height: var(--leading-caption);
|
|
51
|
+
color: var(--c-text-secondary);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
li {
|
|
55
|
+
display: inline-flex;
|
|
56
|
+
align-items: center;
|
|
57
|
+
gap: var(--sp-2);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.mark {
|
|
61
|
+
flex: none;
|
|
62
|
+
width: 12px;
|
|
63
|
+
height: 12px;
|
|
64
|
+
border-radius: 3px;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.fill {
|
|
68
|
+
background: var(--mark);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.line {
|
|
72
|
+
height: 2px;
|
|
73
|
+
border-radius: 1px;
|
|
74
|
+
background: var(--mark);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.dot,
|
|
78
|
+
.hollow {
|
|
79
|
+
width: 10px;
|
|
80
|
+
height: 10px;
|
|
81
|
+
border-radius: 50%;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.dot {
|
|
85
|
+
background: var(--mark);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.hollow {
|
|
89
|
+
border: 2px solid var(--mark);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.stripes {
|
|
93
|
+
background: repeating-linear-gradient(135deg, var(--mark) 0 2px, transparent 2px 5px);
|
|
94
|
+
box-shadow: inset 0 0 0 1px var(--mark);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.empty {
|
|
98
|
+
box-shadow: inset 0 0 0 1px var(--c-border-strong);
|
|
99
|
+
}
|
|
100
|
+
</style>
|
package/src/datatable.js
CHANGED
|
@@ -10,9 +10,9 @@ export const setRows = (table, rows, rowClass = () => '') => {
|
|
|
10
10
|
.map((cells, r) => {
|
|
11
11
|
const tds = cells.map((c, i) => {
|
|
12
12
|
const v = c !== null && typeof c === 'object' ? c : { html: esc(c ?? '–'), sort: c };
|
|
13
|
-
const
|
|
13
|
+
const from = ['left', 'wrap'].filter((c) => head[i]?.classList.contains(c));
|
|
14
14
|
const sort = v.sort === null || v.sort === undefined ? '' : ` data-sort="${esc(v.sort)}"`;
|
|
15
|
-
return `<td class="${[
|
|
15
|
+
return `<td class="${[...from, v.class].filter(Boolean).join(' ')}"${sort}>${v.html}</td>`;
|
|
16
16
|
});
|
|
17
17
|
return `<tr class="${rowClass(rows[r], r)}">${tds.join('')}</tr>`;
|
|
18
18
|
})
|