@n1kname/components 0.4.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Legend.astro +100 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -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>