@n1kname/components 0.7.0 → 0.9.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,17 +1,20 @@
1
1
  {
2
2
  "name": "@n1kname/components",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./*.astro": "./src/*.astro",
8
8
  "./type.css": "./src/type.css",
9
+ "./marks.css": "./src/marks.css",
9
10
  "./icons": "./src/icons.ts",
10
11
  "./datatable.js": "./src/datatable.js",
11
- "./dotrows.js": "./src/dotrows.js"
12
+ "./dotrows.js": "./src/dotrows.js",
13
+ "./rangebars.js": "./src/rangebars.js",
14
+ "./daystrips.js": "./src/daystrips.js"
12
15
  },
13
16
  "dependencies": {
14
- "@n1kname/charts": "0.3.0"
17
+ "@n1kname/charts": "0.4.0"
15
18
  },
16
19
  "peerDependencies": {
17
20
  "astro": "^7.4.4"
@@ -0,0 +1,176 @@
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
+ import './marks.css';
11
+
12
+ interface Props {
13
+ id: string;
14
+ rows?: number;
15
+ [key: string]: unknown;
16
+ }
17
+
18
+ const { id, rows = 0, class: className, style, ...rest } = Astro.props;
19
+ ---
20
+
21
+ <div id={id} class:list={['day-strips', className]} style={[rows ? `--rows: ${rows}` : '', style ?? ''].filter(Boolean).join('; ') || undefined} {...rest}></div>
22
+
23
+ <style>
24
+ .day-strips {
25
+ display: grid;
26
+ grid-template-columns: 9rem minmax(0, 1fr);
27
+ gap: 0 var(--sp-3);
28
+ /* A strip is 20px with 6px under it; the ticks 20px. */
29
+ min-height: calc(var(--rows, 0) * 26px + 20px);
30
+ font-size: var(--text-caption);
31
+ }
32
+
33
+ @media (max-width: 640px) {
34
+ .day-strips {
35
+ grid-template-columns: 7rem minmax(0, 1fr);
36
+ }
37
+
38
+ .day-strips :global(.wide-only) {
39
+ display: none;
40
+ }
41
+ }
42
+
43
+ .day-strips :global(.names div) {
44
+ display: flex;
45
+ align-items: center;
46
+ height: 20px;
47
+ margin-bottom: 6px;
48
+ overflow: hidden;
49
+ white-space: nowrap;
50
+ text-overflow: ellipsis;
51
+ color: var(--c-text-secondary);
52
+ }
53
+
54
+ .day-strips :global(.area) {
55
+ position: relative;
56
+ }
57
+
58
+ .day-strips :global(.strip) {
59
+ position: relative;
60
+ height: 20px;
61
+ margin-bottom: 6px;
62
+ overflow: hidden;
63
+ border-radius: 3px;
64
+ /* Between a gap's stripes, the card, never the days under it. */
65
+ --mark-ground: var(--c-surface);
66
+ }
67
+
68
+ .day-strips :global(.strip i) {
69
+ position: absolute;
70
+ inset-block: 0;
71
+ }
72
+
73
+ /* A stream's first and last day are rounded, wherever in the strip it starts. */
74
+ .day-strips :global(.strip .start) {
75
+ border-start-start-radius: 3px;
76
+ border-end-start-radius: 3px;
77
+ }
78
+
79
+ .day-strips :global(.strip .end) {
80
+ border-start-end-radius: 3px;
81
+ border-end-end-radius: 3px;
82
+ }
83
+
84
+ /* A gap is cut into the strip: a hairline of card on each side keeps it apart from the days
85
+ around it. Too narrow for its stripes to show, it is drawn solid in its colour instead. */
86
+ .day-strips :global(.strip .gap) {
87
+ box-shadow:
88
+ -1px 0 0 var(--c-surface),
89
+ 1px 0 0 var(--c-surface);
90
+ }
91
+
92
+ .day-strips :global(.strip .gap[data-mark='stripes']) {
93
+ box-shadow:
94
+ -1px 0 0 var(--c-surface),
95
+ 1px 0 0 var(--c-surface),
96
+ inset 0 0 0 1px var(--mark);
97
+ }
98
+
99
+ .day-strips :global(.strip .gap.narrow) {
100
+ background: var(--mark);
101
+ }
102
+
103
+ .day-strips :global(.ticks) {
104
+ position: relative;
105
+ height: 16px;
106
+ overflow: hidden;
107
+ font: 11px/16px var(--font-mono);
108
+ color: var(--c-text-muted);
109
+ }
110
+
111
+ .day-strips :global(.ticks span) {
112
+ position: absolute;
113
+ top: 0;
114
+ padding-left: 4px;
115
+ border-left: 1px solid var(--c-border-strong);
116
+ }
117
+
118
+ .day-strips :global(.ticks .year) {
119
+ font-weight: 600;
120
+ color: var(--c-text-secondary);
121
+ }
122
+
123
+ .day-strips :global(.cursor) {
124
+ position: absolute;
125
+ top: 0;
126
+ width: 1px;
127
+ height: calc(100% - 22px);
128
+ background: var(--c-text);
129
+ opacity: 0.5;
130
+ pointer-events: none;
131
+ }
132
+
133
+ .day-strips :global(.tip) {
134
+ position: absolute;
135
+ top: 100%;
136
+ z-index: var(--z-sticky);
137
+ width: max-content;
138
+ max-width: 220px;
139
+ padding: var(--sp-2) var(--sp-3);
140
+ border: 1px solid var(--c-border);
141
+ border-radius: var(--radius-md);
142
+ background: var(--c-surface);
143
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
144
+ pointer-events: none;
145
+ }
146
+
147
+ .day-strips :global(.tip strong) {
148
+ display: block;
149
+ font-family: var(--font-mono);
150
+ }
151
+
152
+ .day-strips :global(.tip .line) {
153
+ display: flex;
154
+ align-items: center;
155
+ gap: var(--sp-2);
156
+ margin-top: 2px;
157
+ }
158
+
159
+ .day-strips :global(.tip .line i) {
160
+ flex: none;
161
+ width: 8px;
162
+ height: 8px;
163
+ border-radius: 2px;
164
+ }
165
+
166
+ .day-strips :global(.tip em) {
167
+ margin-left: auto;
168
+ padding-left: var(--sp-3);
169
+ font-style: normal;
170
+ color: var(--c-text-muted);
171
+ }
172
+
173
+ .day-strips :global(.tip .quiet) {
174
+ color: var(--c-text-muted);
175
+ }
176
+ </style>
package/src/Legend.astro CHANGED
@@ -12,6 +12,8 @@
12
12
  * faint a paler swatch, for an estimate: no invoice or reading yet
13
13
  * dashed paler still with a dashed edge, for a projection of something still running
14
14
  */
15
+ import './marks.css';
16
+
15
17
  interface Key {
16
18
  label: string;
17
19
  /** Any CSS colour, best a chart token such as var(--c-chart-1). Not needed for `empty`. */
@@ -33,7 +35,7 @@ const { keys, label = 'Legend', class: className, ...rest } = Astro.props;
33
35
  {
34
36
  keys.map((k) => (
35
37
  <li>
36
- <i class:list={['mark', k.mark ?? 'fill']} style={k.color ? `--mark: ${k.color}` : undefined} />
38
+ <i class="mark" data-mark={k.mark ?? 'fill'} style={k.color ? `--mark: ${k.color}` : undefined} />
37
39
  {k.label}
38
40
  </li>
39
41
  ))
@@ -66,48 +68,25 @@ const { keys, label = 'Legend', class: className, ...rest } = Astro.props;
66
68
  border-radius: 3px;
67
69
  }
68
70
 
69
- .fill {
70
- background: var(--mark);
71
- }
72
-
73
- .line {
71
+ /* Shapes on .mark too, so a page's own .legend or i rule can't reach in and square them. */
72
+ .mark[data-mark='line'] {
74
73
  height: 2px;
75
74
  border-radius: 1px;
76
75
  background: var(--mark);
77
76
  }
78
77
 
79
- .dot,
80
- .hollow {
78
+ .mark[data-mark='dot'],
79
+ .mark[data-mark='hollow'] {
81
80
  width: 10px;
82
81
  height: 10px;
83
82
  border-radius: 50%;
84
83
  }
85
84
 
86
- .dot {
85
+ .mark[data-mark='dot'] {
87
86
  background: var(--mark);
88
87
  }
89
88
 
90
- .hollow {
89
+ .mark[data-mark='hollow'] {
91
90
  border: 2px solid var(--mark);
92
91
  }
93
-
94
- .stripes {
95
- background: repeating-linear-gradient(135deg, var(--mark) 0 2px, transparent 2px 5px);
96
- box-shadow: inset 0 0 0 1px var(--mark);
97
- }
98
-
99
- .empty {
100
- box-shadow: inset 0 0 0 1px var(--c-border-strong);
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
- }
113
92
  </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,108 @@
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, and solid when too narrow for its stripes. The colours are best the record
10
+ // state tokens, --c-state-complete, -waiting, -set-aside and -missing. Each stream is { label, title?, days, summary }: `days` its state per day,
11
+ // `summary` what a screen reader hears for its strip. Hovering shows the date and every
12
+ // stream whose state that day is not `quiet`.
13
+ import { esc } from './datatable.js';
14
+
15
+ // A one-day gap is about a pixel wide at a few years' scale; never draw one thinner than this.
16
+ const MIN_GAP_PX = 4;
17
+ // Below this, stripes are noise: the gap is drawn solid in its colour instead.
18
+ const MIN_STRIPES_PX = 8;
19
+ const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
20
+
21
+ const addDays = (iso, n) => {
22
+ const d = new Date(`${iso}T12:00:00Z`);
23
+ d.setUTCDate(d.getUTCDate() + n);
24
+ return d.toISOString().slice(0, 10);
25
+ };
26
+
27
+ const runs = (days) => {
28
+ const out = [];
29
+ days.forEach((state, i) => {
30
+ const last = out[out.length - 1];
31
+ if (last && last.state === state) last.len++;
32
+ else out.push({ state, from: i, len: 1 });
33
+ });
34
+ return out;
35
+ };
36
+
37
+ export const setDayStrips = (box, { from, streams, states, quiet, quietText, date = (iso) => iso, month = (m) => MONTHS[m - 1] }) => {
38
+ const days = Math.max(...streams.map((s) => s.days.length));
39
+ const pct = (i) => `${(i / days) * 100}%`;
40
+ const swatch = (state) => (states[state].color ? ` style="--mark: ${states[state].color}"` : '');
41
+
42
+ const rows = streams
43
+ .map((s) => {
44
+ const list = runs(s.days).filter((x) => states[x.state].mark !== 'empty');
45
+ // Quiet states first, gaps on top.
46
+ const order = [...list.filter((x) => !states[x.state].gap), ...list.filter((x) => states[x.state].gap)];
47
+ const first = list[0];
48
+ const last = list[list.length - 1];
49
+ const bar = (x) => {
50
+ const { gap, mark, color } = states[x.state];
51
+ const cls = [gap && 'gap', x === first && 'start', x === last && 'end'].filter(Boolean).join(' ');
52
+ const style = `left: ${pct(x.from)}; width: ${pct(x.len)}${gap ? `; min-width: ${MIN_GAP_PX}px` : ''}${color ? `; --mark: ${color}` : ''}`;
53
+ return `<i${cls ? ` class="${cls}"` : ''} data-mark="${mark}" style="${style}"></i>`;
54
+ };
55
+ return `<div class="strip" role="img" aria-label="${esc(s.summary)}">${order.map(bar).join('')}</div>`;
56
+ })
57
+ .join('');
58
+
59
+ // A tick at the first of each quarter, the year at each January. Quarters on a wide strip,
60
+ // half years on a phone, and none in the last few percent, where its label has no room.
61
+ const ticks = [];
62
+ for (let i = 0; i < days; i++) {
63
+ const [y, m, d] = addDays(from, i).split('-').map(Number);
64
+ if (d === 1 && (m - 1) % 3 === 0 && i / days < 0.96) {
65
+ const cls = [m === 1 ? 'year' : '', (m - 1) % 6 === 0 ? '' : 'wide-only'].filter(Boolean).join(' ');
66
+ ticks.push(`<span class="${cls}" style="left: ${pct(i)}">${m === 1 ? y : month(m)}</span>`);
67
+ }
68
+ }
69
+
70
+ box.innerHTML =
71
+ `<div class="names">${streams.map((s) => `<div${s.title ? ` title="${esc(s.title)}"` : ''}>${esc(s.label)}</div>`).join('')}</div>` +
72
+ `<div class="area"><div class="rows">${rows}</div><div class="ticks" aria-hidden="true">${ticks.join('')}</div>` +
73
+ '<div class="cursor" hidden></div><div class="tip" hidden></div></div>';
74
+
75
+ // A gap too narrow for its stripes is drawn solid; how narrow depends on the width the strip has.
76
+ const gaps = [...box.querySelectorAll('.strip .gap')];
77
+ const fit = () => gaps.forEach((g) => g.classList.toggle('narrow', g.getBoundingClientRect().width < MIN_STRIPES_PX));
78
+ fit();
79
+ box.stripsObserver?.disconnect();
80
+ box.stripsObserver = new ResizeObserver(fit);
81
+ box.stripsObserver.observe(box);
82
+
83
+ // Hover for the date, and what was not quiet that day.
84
+ const area = box.querySelector('.area');
85
+ const cursor = box.querySelector('.cursor');
86
+ const tip = box.querySelector('.tip');
87
+ area.addEventListener('pointermove', (e) => {
88
+ const r = area.getBoundingClientRect();
89
+ const i = Math.floor(((e.clientX - r.left) / r.width) * days);
90
+ if (i < 0 || i >= days) return;
91
+ const notable = streams.filter((s) => s.days[i] !== quiet);
92
+ cursor.style.left = pct(i + 0.5);
93
+ tip.innerHTML =
94
+ `<strong>${esc(date(addDays(from, i)))}</strong>` +
95
+ (notable.length
96
+ ? notable
97
+ .map((s) => `<span class="line"><i data-mark="${states[s.days[i]].mark}"${swatch(s.days[i])}></i>${esc(s.label)}<em>${esc(states[s.days[i]].label)}</em></span>`)
98
+ .join('')
99
+ : `<span class="quiet">${esc(quietText)}</span>`);
100
+ const right = i / days > 0.6;
101
+ tip.style.left = right ? 'auto' : pct(i);
102
+ tip.style.right = right ? pct(days - i) : 'auto';
103
+ cursor.hidden = tip.hidden = false;
104
+ });
105
+ area.addEventListener('pointerleave', () => {
106
+ cursor.hidden = tip.hidden = true;
107
+ });
108
+ };
package/src/marks.css ADDED
@@ -0,0 +1,34 @@
1
+ /*
2
+ * How each kind of mark is coloured, in one place, so a Legend's key and the chart it explains
3
+ * look alike. Legend and DayStrips import it; a page that draws its own marks can too.
4
+ *
5
+ * <i data-mark="stripes" style="--mark: var(--c-state-set-aside)"></i>
6
+ *
7
+ * --mark is the colour. --mark-ground is what shows between the stripes: transparent unless
8
+ * set, and set to the card where a stripe sits on other marks, so they never show through.
9
+ * Size and shape stay with the component.
10
+ */
11
+
12
+ [data-mark='fill'] {
13
+ background: var(--mark);
14
+ }
15
+
16
+ [data-mark='stripes'] {
17
+ background: repeating-linear-gradient(135deg, var(--mark) 0 2px, var(--mark-ground, transparent) 2px 5px);
18
+ box-shadow: inset 0 0 0 1px var(--mark);
19
+ }
20
+
21
+ [data-mark='empty'] {
22
+ box-shadow: inset 0 0 0 1px var(--c-border-strong);
23
+ }
24
+
25
+ /* The same fades as a bar chart's unsettled rows, a little stronger so a small swatch still shows. */
26
+ [data-mark='faint'] {
27
+ background: color-mix(in oklab, var(--mark) 65%, transparent);
28
+ }
29
+
30
+ [data-mark='dashed'] {
31
+ background: color-mix(in oklab, var(--mark) 37%, transparent);
32
+ outline: 1px dashed var(--mark);
33
+ outline-offset: -1px;
34
+ }
@@ -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
+ };