Repository navigation
Expand file tree
/
Copy pathtext.js
More file actions
123 lines (109 loc) · 4.56 KB
/
Copy pathtext.js
File metadata and controls
123 lines (109 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ============================================================
// XERJ.ai — Text primitives
// Every component is a pure function returning an HTML string.
// No state, no events, no DOM access. See UX_BOOK.md §3.
// ============================================================
/** Escape user text for safe interpolation into template strings. */
export const esc = (s) =>
String(s ?? '').replace(/[&<>"']/g, (c) => ({
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
}[c]));
/** Compact number formatter (4.82M, 12.3K, 0.83). */
const nf = new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 2 });
export const fmt = (v, { decimals } = {}) => {
if (v == null || Number.isNaN(v)) return '—';
if (decimals != null) return Number(v).toFixed(decimals);
// Exact integers stay integers — a count of 4 must never read "4.0".
if (Number.isInteger(v) && Math.abs(v) < 10_000) return new Intl.NumberFormat('en').format(v);
if (Math.abs(v) < 1) return Number(v).toFixed(2);
if (Math.abs(v) < 100) return Number(v).toFixed(1);
return nf.format(v);
};
/** Pad an integer string to a fixed width with thin spaces (looks tabular). */
export const num = (v) => new Intl.NumberFormat('en').format(v);
// ----- Eyebrow / headings ------------------------------------
/** Small-caps eyebrow label. The ONLY way to title a panel. */
export const Key = (text) => `<div class="key">${esc(text)}</div>`;
/** `role` ∈ 'scene' | 'section'. Scene is once per view. */
export const H = (role, text) => {
const cls = role === 'scene' ? 'h-scene' : 'h-section';
const tag = role === 'scene' ? 'h1' : 'h2';
return `<${tag} class="${cls}">${esc(text)}</${tag}>`;
};
// ----- Delta indicator ---------------------------------------
/**
* `▲ 12.3%` / `▼ 0.12%` — standalone delta.
* `good: 'up' | 'down' | null` — which direction lights the accent.
* If `good` is null, delta is always muted (neutral).
*/
export const Change = (pct, { good = 'up', unit = '%', fractionDigits = 1 } = {}) => {
if (pct == null || Number.isNaN(pct)) return '';
const up = pct >= 0;
const arrow = up ? '▲' : '▼';
const abs = Math.abs(pct).toFixed(fractionDigits);
let klass = 'meh';
if (good === 'up') klass = up ? 'good' : 'bad';
else if (good === 'down') klass = up ? 'bad' : 'good';
return `<span class="delta ${klass}">${arrow} ${abs}${esc(unit)}</span>`;
};
// ----- Headline metric ---------------------------------------
/**
* Num — the point of every panel.
* value: string or number (preformatted is fine)
* unit: short unit like 'events', 'e/s', '%', 'ms'
* spark: optional Spark(...) string rendered to the right
* delta: signed percent, rendered with Change
* deltaGood: 'up' | 'down' | null — which direction is "good"
* hint: small mono hint rendered below ('at 14:23', etc.)
* emphasis: true → 96/56px display + accent; false → 32px ink
*/
export const Num = ({
value,
unit = '',
spark = '',
delta = null,
deltaGood = 'up',
hint = '',
emphasis = true,
} = {}) => {
const valCls = emphasis ? 'num-big accent' : 'num-md';
const unitSpan = unit ? `<span class="num-unit">${esc(unit)}</span>` : '';
const deltaPart = delta != null ? Change(delta, { good: deltaGood }) : '';
const hintPart = hint
? `<span class="hint">${esc(hint)}</span>`
: '';
const foot = (deltaPart || hintPart)
? `<div class="row-flex" style="margin-top:10px;">${deltaPart}${hintPart}</div>`
: '';
return `
<div class="stack">
<div class="row-flex">
<div class="${valCls}">${esc(value)}${unitSpan}</div>
${spark}
</div>
${foot}
</div>`;
};
// ----- Horizontal-bar row (one line of a TopN) ---------------
/**
* A single ranked row. Use `TopN` to render lists; this is exported so you
* can hand-build a row in custom panels.
*/
export const HBar = ({ label, value, pct, frac, barWidth = 200 }) => {
const f = Math.max(0, Math.min(1, frac ?? 0));
const x = (f * barWidth).toFixed(1);
return `
<div class="row">
<div class="row__label">${esc(label)}</div>
<div class="row__val">${esc(value)}</div>
<div class="row__bar">
<svg class="chart" height="6" viewBox="0 0 ${barWidth} 6" preserveAspectRatio="none">
<line x1="0" y1="5" x2="${x}" y2="5" stroke="currentColor" stroke-width="1"/>
</svg>
</div>
<div class="row__pct">${pct != null ? esc(pct) : ''}</div>
</div>`;
};
// ----- Inline key/value used in scene kicker ----------------
export const Kv = (k, v) =>
`<span class="mono"><span class="faint">${esc(k)}</span> ${esc(v)}</span>`;