@n1kname/components 0.6.0 → 0.8.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -8,10 +8,12 @@
8
8
  "./type.css": "./src/type.css",
9
9
  "./icons": "./src/icons.ts",
10
10
  "./datatable.js": "./src/datatable.js",
11
- "./dotrows.js": "./src/dotrows.js"
11
+ "./dotrows.js": "./src/dotrows.js",
12
+ "./rangebars.js": "./src/rangebars.js",
13
+ "./daystrips.js": "./src/daystrips.js"
12
14
  },
13
15
  "dependencies": {
14
- "@n1kname/charts": "0.2.0"
16
+ "@n1kname/charts": "0.4.0"
15
17
  },
16
18
  "peerDependencies": {
17
19
  "astro": "^7.4.4"
@@ -0,0 +1,156 @@
1
+ ---
2
+ /**
3
+ * Every day of a long record, one strip per stream: runs of colour for the state of each day,
4
+ * with the gaps on top and never too thin to see. For finding where something is missing,
5
+ * across years at a glance.
6
+ *
7
+ * The strips arrive at runtime: fill them with setDayStrips from `@n1kname/components/daystrips.js`.
8
+ * `rows` is how many streams to hold room for until then, so nothing below moves when they come.
9
+ */
10
+ interface Props {
11
+ id: string;
12
+ rows?: number;
13
+ [key: string]: unknown;
14
+ }
15
+
16
+ const { id, rows = 0, class: className, style, ...rest } = Astro.props;
17
+ ---
18
+
19
+ <div id={id} class:list={['day-strips', className]} style={[rows ? `--rows: ${rows}` : '', style ?? ''].filter(Boolean).join('; ') || undefined} {...rest}></div>
20
+
21
+ <style>
22
+ .day-strips {
23
+ display: grid;
24
+ grid-template-columns: 9rem minmax(0, 1fr);
25
+ gap: 0 var(--sp-3);
26
+ /* A strip is 20px with 6px under it; the ticks 20px. */
27
+ min-height: calc(var(--rows, 0) * 26px + 20px);
28
+ font-size: var(--text-caption);
29
+ }
30
+
31
+ @media (max-width: 640px) {
32
+ .day-strips {
33
+ grid-template-columns: 7rem minmax(0, 1fr);
34
+ }
35
+
36
+ .day-strips :global(.wide-only) {
37
+ display: none;
38
+ }
39
+ }
40
+
41
+ .day-strips :global(.names div) {
42
+ display: flex;
43
+ align-items: center;
44
+ height: 20px;
45
+ margin-bottom: 6px;
46
+ overflow: hidden;
47
+ white-space: nowrap;
48
+ text-overflow: ellipsis;
49
+ color: var(--c-text-secondary);
50
+ }
51
+
52
+ .day-strips :global(.area) {
53
+ position: relative;
54
+ }
55
+
56
+ .day-strips :global(.strip) {
57
+ position: relative;
58
+ height: 20px;
59
+ margin-bottom: 6px;
60
+ overflow: hidden;
61
+ border-radius: 3px;
62
+ }
63
+
64
+ .day-strips :global(.strip i) {
65
+ position: absolute;
66
+ inset-block: 0;
67
+ }
68
+
69
+ .day-strips :global(.ticks) {
70
+ position: relative;
71
+ height: 16px;
72
+ overflow: hidden;
73
+ font: 11px/16px var(--font-mono);
74
+ color: var(--c-text-muted);
75
+ }
76
+
77
+ .day-strips :global(.ticks span) {
78
+ position: absolute;
79
+ top: 0;
80
+ padding-left: 4px;
81
+ border-left: 1px solid var(--c-border-strong);
82
+ }
83
+
84
+ .day-strips :global(.ticks .year) {
85
+ font-weight: 600;
86
+ color: var(--c-text-secondary);
87
+ }
88
+
89
+ .day-strips :global(.cursor) {
90
+ position: absolute;
91
+ top: 0;
92
+ width: 1px;
93
+ height: calc(100% - 22px);
94
+ background: var(--c-text);
95
+ opacity: 0.5;
96
+ pointer-events: none;
97
+ }
98
+
99
+ .day-strips :global(.tip) {
100
+ position: absolute;
101
+ top: 100%;
102
+ z-index: var(--z-sticky);
103
+ width: max-content;
104
+ max-width: 220px;
105
+ padding: var(--sp-2) var(--sp-3);
106
+ border: 1px solid var(--c-border);
107
+ border-radius: var(--radius-md);
108
+ background: var(--c-surface);
109
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
110
+ pointer-events: none;
111
+ }
112
+
113
+ .day-strips :global(.tip strong) {
114
+ display: block;
115
+ font-family: var(--font-mono);
116
+ }
117
+
118
+ .day-strips :global(.tip .line) {
119
+ display: flex;
120
+ align-items: center;
121
+ gap: var(--sp-2);
122
+ margin-top: 2px;
123
+ }
124
+
125
+ .day-strips :global(.tip .line i) {
126
+ flex: none;
127
+ width: 8px;
128
+ height: 8px;
129
+ border-radius: 2px;
130
+ }
131
+
132
+ .day-strips :global(.tip em) {
133
+ margin-left: auto;
134
+ padding-left: var(--sp-3);
135
+ font-style: normal;
136
+ color: var(--c-text-muted);
137
+ }
138
+
139
+ .day-strips :global(.tip .quiet) {
140
+ color: var(--c-text-muted);
141
+ }
142
+
143
+ /* The marks, the same as a Legend's, so a strip and its key look alike. */
144
+ .day-strips :global(.mark.fill) {
145
+ background: var(--mark);
146
+ }
147
+
148
+ .day-strips :global(.mark.stripes) {
149
+ background: repeating-linear-gradient(135deg, var(--mark) 0 2px, transparent 2px 5px);
150
+ box-shadow: inset 0 0 0 1px var(--mark);
151
+ }
152
+
153
+ .day-strips :global(.mark.empty) {
154
+ box-shadow: inset 0 0 0 1px var(--c-border-strong);
155
+ }
156
+ </style>
package/src/Legend.astro CHANGED
@@ -9,12 +9,14 @@
9
9
  * hollow a ring, for a second kind of dot
10
10
  * stripes diagonal stripes, for something known and set aside
11
11
  * empty an outline only, for "nothing here"
12
+ * faint a paler swatch, for an estimate: no invoice or reading yet
13
+ * dashed paler still with a dashed edge, for a projection of something still running
12
14
  */
13
15
  interface Key {
14
16
  label: string;
15
17
  /** Any CSS colour, best a chart token such as var(--c-chart-1). Not needed for `empty`. */
16
18
  color?: string;
17
- mark?: 'fill' | 'line' | 'dot' | 'hollow' | 'stripes' | 'empty';
19
+ mark?: 'fill' | 'line' | 'dot' | 'hollow' | 'stripes' | 'empty' | 'faint' | 'dashed';
18
20
  }
19
21
 
20
22
  interface Props {
@@ -97,4 +99,15 @@ const { keys, label = 'Legend', class: className, ...rest } = Astro.props;
97
99
  .empty {
98
100
  box-shadow: inset 0 0 0 1px var(--c-border-strong);
99
101
  }
102
+
103
+ /* The same fades as a bar chart's unsettled rows, a little stronger so a small swatch still shows. */
104
+ .faint {
105
+ background: color-mix(in oklab, var(--mark) 65%, transparent);
106
+ }
107
+
108
+ .dashed {
109
+ background: color-mix(in oklab, var(--mark) 37%, transparent);
110
+ outline: 1px dashed var(--mark);
111
+ outline-offset: -1px;
112
+ }
100
113
  </style>
@@ -0,0 +1,101 @@
1
+ ---
2
+ /**
3
+ * One bar per thing, with the range its figure could fall in drawn over it: a line with a
4
+ * cap at each end, ringed in the surface so it reads over the bar. For estimates, where
5
+ * how sure the figure is matters as much as the figure.
6
+ *
7
+ * The rows arrive at runtime: fill them with setRangeBars from `@n1kname/components/rangebars.js`.
8
+ * `rows` is how many to hold room for until then, so nothing below moves when they come.
9
+ */
10
+ interface Props {
11
+ id: string;
12
+ rows?: number;
13
+ [key: string]: unknown;
14
+ }
15
+
16
+ const { id, rows = 0, class: className, style, ...rest } = Astro.props;
17
+ ---
18
+
19
+ <div id={id} class:list={['range-bars', className]} style={[rows ? `--rows: ${rows}` : '', style ?? ''].filter(Boolean).join('; ') || undefined} {...rest}></div>
20
+
21
+ <style>
22
+ .range-bars {
23
+ /* A row is 22px with 6px between; a rest row 13px more for its rule. */
24
+ min-height: calc(var(--rows, 0) * 28px + 13px);
25
+ font-size: var(--text-caption);
26
+ }
27
+
28
+ .range-bars :global(.row) {
29
+ display: grid;
30
+ grid-template-columns: 6.5rem minmax(0, 1fr) 6.5rem;
31
+ align-items: center;
32
+ gap: 0 var(--sp-3);
33
+ }
34
+
35
+ @media (min-width: 640px) {
36
+ .range-bars :global(.row) {
37
+ grid-template-columns: 10rem minmax(0, 1fr) 8rem;
38
+ }
39
+ }
40
+
41
+ .range-bars :global(.row + .row) {
42
+ margin-top: 6px;
43
+ }
44
+
45
+ .range-bars :global(.row.rest) {
46
+ margin-top: var(--sp-3);
47
+ padding-top: var(--sp-3);
48
+ border-top: 1px solid var(--c-border);
49
+ }
50
+
51
+ .range-bars :global(.name) {
52
+ line-height: 1.25;
53
+ color: var(--c-text);
54
+ }
55
+
56
+ .range-bars :global(.track) {
57
+ position: relative;
58
+ height: 20px;
59
+ }
60
+
61
+ .range-bars :global(.track i) {
62
+ position: absolute;
63
+ }
64
+
65
+ /* Rounded at the figure's end, square on the baseline. */
66
+ .range-bars :global(.amount) {
67
+ inset-block: 2px;
68
+ left: 0;
69
+ border-radius: 0 4px 4px 0;
70
+ }
71
+
72
+ .range-bars :global(.span),
73
+ .range-bars :global(.cap) {
74
+ top: 50%;
75
+ background: var(--c-text);
76
+ box-shadow: 0 0 0 1px var(--c-surface);
77
+ }
78
+
79
+ .range-bars :global(.span) {
80
+ height: 2px;
81
+ transform: translateY(-50%);
82
+ }
83
+
84
+ .range-bars :global(.cap) {
85
+ width: 2px;
86
+ height: 10px;
87
+ transform: translate(-50%, -50%);
88
+ }
89
+
90
+ .range-bars :global(.right) {
91
+ text-align: right;
92
+ font-family: var(--font-mono);
93
+ font-variant-numeric: tabular-nums;
94
+ white-space: nowrap;
95
+ }
96
+
97
+ .range-bars :global(.right .muted) {
98
+ margin-left: var(--sp-2);
99
+ color: var(--c-text-muted);
100
+ }
101
+ </style>
@@ -0,0 +1,95 @@
1
+ // Fills a DayStrips after the page has loaded, for data that arrives at runtime.
2
+ //
3
+ // One strip per stream, one place per day from `from`, coloured by that day's state. HTML, not
4
+ // a chart library, because a strip is runs of colour and nothing else.
5
+ //
6
+ // `states` says how each state looks: { label, mark, color, gap }. `mark` is a Legend mark:
7
+ // fill, stripes, or empty (not drawn on the strip, only in the tooltip and the legend). A `gap`
8
+ // state is drawn on top of the rest and never thinner than MIN_GAP_PX, so a one-day gap is
9
+ // never missed. Each stream is { label, title?, days, summary }: `days` its state per day,
10
+ // `summary` what a screen reader hears for its strip. Hovering shows the date and every
11
+ // stream whose state that day is not `quiet`.
12
+ import { esc } from './datatable.js';
13
+
14
+ // A one-day gap is about a pixel wide at a few years' scale; never draw one thinner than this.
15
+ const MIN_GAP_PX = 4;
16
+ const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
17
+
18
+ const addDays = (iso, n) => {
19
+ const d = new Date(`${iso}T12:00:00Z`);
20
+ d.setUTCDate(d.getUTCDate() + n);
21
+ return d.toISOString().slice(0, 10);
22
+ };
23
+
24
+ const runs = (days) => {
25
+ const out = [];
26
+ days.forEach((state, i) => {
27
+ const last = out[out.length - 1];
28
+ if (last && last.state === state) last.len++;
29
+ else out.push({ state, from: i, len: 1 });
30
+ });
31
+ return out;
32
+ };
33
+
34
+ export const setDayStrips = (box, { from, streams, states, quiet, quietText, date = (iso) => iso, month = (m) => MONTHS[m - 1] }) => {
35
+ const days = Math.max(...streams.map((s) => s.days.length));
36
+ const pct = (i) => `${(i / days) * 100}%`;
37
+ const mark = (state) => `mark ${states[state].mark}`;
38
+ const swatch = (state) => (states[state].color ? ` style="--mark: ${states[state].color}"` : '');
39
+
40
+ const rows = streams
41
+ .map((s) => {
42
+ const list = runs(s.days).filter((x) => states[x.state].mark !== 'empty');
43
+ // Quiet states first, gaps on top.
44
+ const order = [...list.filter((x) => !states[x.state].gap), ...list.filter((x) => states[x.state].gap)];
45
+ const bar = (x) => {
46
+ const color = states[x.state].color ? `; --mark: ${states[x.state].color}` : '';
47
+ const min = states[x.state].gap ? `; min-width: ${MIN_GAP_PX}px` : '';
48
+ return `<i class="${mark(x.state)}" style="left: ${pct(x.from)}; width: ${pct(x.len)}${min}${color}"></i>`;
49
+ };
50
+ return `<div class="strip" role="img" aria-label="${esc(s.summary)}">${order.map(bar).join('')}</div>`;
51
+ })
52
+ .join('');
53
+
54
+ // A tick at the first of each quarter, the year at each January. Quarters on a wide strip,
55
+ // half years on a phone, and none in the last few percent, where its label has no room.
56
+ const ticks = [];
57
+ for (let i = 0; i < days; i++) {
58
+ const [y, m, d] = addDays(from, i).split('-').map(Number);
59
+ if (d === 1 && (m - 1) % 3 === 0 && i / days < 0.96) {
60
+ const cls = [m === 1 ? 'year' : '', (m - 1) % 6 === 0 ? '' : 'wide-only'].filter(Boolean).join(' ');
61
+ ticks.push(`<span class="${cls}" style="left: ${pct(i)}">${m === 1 ? y : month(m)}</span>`);
62
+ }
63
+ }
64
+
65
+ box.innerHTML =
66
+ `<div class="names">${streams.map((s) => `<div${s.title ? ` title="${esc(s.title)}"` : ''}>${esc(s.label)}</div>`).join('')}</div>` +
67
+ `<div class="area"><div class="rows">${rows}</div><div class="ticks" aria-hidden="true">${ticks.join('')}</div>` +
68
+ '<div class="cursor" hidden></div><div class="tip" hidden></div></div>';
69
+
70
+ // Hover for the date, and what was not quiet that day.
71
+ const area = box.querySelector('.area');
72
+ const cursor = box.querySelector('.cursor');
73
+ const tip = box.querySelector('.tip');
74
+ area.addEventListener('pointermove', (e) => {
75
+ const r = area.getBoundingClientRect();
76
+ const i = Math.floor(((e.clientX - r.left) / r.width) * days);
77
+ if (i < 0 || i >= days) return;
78
+ const notable = streams.filter((s) => s.days[i] !== quiet);
79
+ cursor.style.left = pct(i + 0.5);
80
+ tip.innerHTML =
81
+ `<strong>${esc(date(addDays(from, i)))}</strong>` +
82
+ (notable.length
83
+ ? notable
84
+ .map((s) => `<span class="line"><i class="${mark(s.days[i])}"${swatch(s.days[i])}></i>${esc(s.label)}<em>${esc(states[s.days[i]].label)}</em></span>`)
85
+ .join('')
86
+ : `<span class="quiet">${esc(quietText)}</span>`);
87
+ const right = i / days > 0.6;
88
+ tip.style.left = right ? 'auto' : pct(i);
89
+ tip.style.right = right ? pct(days - i) : 'auto';
90
+ cursor.hidden = tip.hidden = false;
91
+ });
92
+ area.addEventListener('pointerleave', () => {
93
+ cursor.hidden = tip.hidden = true;
94
+ });
95
+ };
@@ -0,0 +1,29 @@
1
+ // Fills a RangeBars after the page has loaded, for data that arrives at runtime.
2
+ //
3
+ // Each row is { name, value, lo, hi, color, title, right?, rest? }: a bar to `value` with the
4
+ // range the figure could fall in, `lo` to `hi`, drawn over it. `title` says every value in
5
+ // words; it is what a screen reader hears for the row's bar. `right` is HTML, written as is,
6
+ // so escape anything that came from outside. A `rest` row, such as everything no row
7
+ // explains, sits under a rule at the bottom.
8
+ //
9
+ // Widths are shares of `max`, which by default is a little past the highest `hi`, so every
10
+ // range ends inside the track.
11
+ import { esc } from './datatable.js';
12
+
13
+ export const setRangeBars = (box, { rows, max = Math.max(...rows.map((r) => r.hi)) * 1.02 }) => {
14
+ const w = (v) => `${Math.max(0, Math.min(100, (v / max) * 100))}%`;
15
+ box.innerHTML = rows
16
+ .map(
17
+ (r) =>
18
+ `<div class="row${r.rest ? ' rest' : ''}">` +
19
+ `<div class="name">${esc(r.name)}</div>` +
20
+ `<div class="track" role="img" aria-label="${esc(r.title)}" title="${esc(r.title)}">` +
21
+ `<i class="amount" style="width: ${w(r.value)}; background: ${r.color}"></i>` +
22
+ `<i class="span" style="left: ${w(r.lo)}; width: calc(${w(r.hi)} - ${w(r.lo)})"></i>` +
23
+ `<i class="cap" style="left: ${w(r.lo)}"></i><i class="cap" style="left: ${w(r.hi)}"></i>` +
24
+ '</div>' +
25
+ `<div class="right">${r.right ?? ''}</div>` +
26
+ '</div>',
27
+ )
28
+ .join('');
29
+ };