@columenlabs/lumen-ui 0.1.55 → 0.1.57
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/README.md +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/pace.d.ts +63 -0
- package/dist/pace.js +132 -0
- package/dist/time-to-cap.d.ts +25 -0
- package/dist/time-to-cap.js +45 -0
- package/dist/trend-value.d.ts +43 -0
- package/dist/trend-value.js +40 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -81,6 +81,8 @@ Every export, its props and an example are on the [docs](https://lumen.columenla
|
|
|
81
81
|
- A floating bulk-action bar over a table's selection: Apply clears the selection and the bar goes away; the toast's Undo restores the change, not the selection. Cancel keeps the selection.
|
|
82
82
|
- Bars in a column line up: use `ShareMeter` (fixed track, fixed right-aligned number), never a bar sized by its label.
|
|
83
83
|
- Meter levels are the kit's, not the app's: a share of a cap reads `ok` (grey) under 75%, `watch` (amber) from 75%, `alert` (red) from 90%. `ShareMeter` takes its colour from the share unless you pass `color`; `UnitValue share={0.8}` tints its number and dot the same way; usage progress cards follow the same levels. For any other mark (a dot, a chip, a "nearest cap" cell) call `meterLevel(share)` and colour it with `METER_LEVEL_COLOR` or `meterValueTone(share)`. Override per app with `configureLumen({ meterLevels: { watch: 0.8 } })` (each level can move alone) or per meter with `levels={{ watch: 0.5 }}`. Don't hand-roll `share >= 0.75` checks.
|
|
84
|
+
- Pace (time to a cap at the recent rate) is the kit's: `burnRate(samples, { per, gauge })` over the window's complete samples (hourly, or daily for `7d`), then `timeToCap(rate, used, cap, resetInHours)`. A lone sample above 3× the window's median counts as 3× the median (`spikes` says how many); a run of hot samples counts in full. Show it with `TimeToCap` (`in 2h 05m`, `At cap`, `Resets first`; `short` on a phone), coloured by the meter level of where the meter lands at reset, and pick the window with `PaceWindowPicker` (6h, 12h, 24h, 7d) in the page header; `usePaceWindow()` reads the same choice everywhere, kept in the viewer's browser (`setDefaultPaceWindow` sets the app's default). Where most hours are empty (median 0) a lone nonzero hour counts as 0, and a window with too little use (`timeToCap(..., { minTotal })`, default 0.1% of the cap) is `thin`: no time to the cap. Don't hand-roll ETAs or a window toggle.
|
|
85
|
+
- A change between periods (this week against last) is `trendChange(now, prev, { minBase })` drawn by `TrendValue`: `▲ 12%` over a big enough earlier period, the plain difference `▲ +3` when it is at or under `minBase`, `▲ >10×` past ten times. A % over a near-empty baseline misleads; never show one.
|
|
84
86
|
- Table rows are one line. A cell shows one value (with a small bar or tag at most); anything more (other metrics, the allowance, a breakdown) goes in a hover tooltip, never in stacked text or sub-rows.
|
|
85
87
|
- Chart colours are theme tokens (`var(--chart-1)`, `useChartTokens()`), never hex picked for one theme; `LumenEChart` resolves `var(--…)` in its option so sparklines read in light and dark.
|
|
86
88
|
- "No label" filters come built in: `LumenTokenizedFilter` pins a "No <field>" row on any text field where some rows have no value (an `empty` token); don't add your own toggle for it.
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,9 @@ export { RankedBars, StackedBars, StackedColumns } from './lumen-widget-charts.j
|
|
|
24
24
|
export { ShareMeter } from './share-meter.js';
|
|
25
25
|
export { METER_LEVEL_COLOR, meterLevel, meterValueTone } from './meter-level.js';
|
|
26
26
|
export { UnitValue } from './unit-value.js';
|
|
27
|
+
export { type BurnRate, burnRate, durationText, PACE_WINDOWS, type Pace, type PaceWindow, paceHours, setDefaultPaceWindow, timeToCap, usePaceWindow, } from './pace.js';
|
|
28
|
+
export { PaceWindowPicker, paceText, TimeToCap } from './time-to-cap.js';
|
|
29
|
+
export { type TrendChange, TrendValue, trendChange } from './trend-value.js';
|
|
27
30
|
export { COVERAGE_LEVELS, CoverageCell, type CoverageJobState, type CoverageLevel, CoverageLegend, coverageClass, coverageLabel, coverageLevel, } from './coverage-cells.js';
|
|
28
31
|
export { type CoverageBlock, type CoverageData, type CoverageFact, type CoveragePick, type CoverageRow, type CoverageSummary, LumenCoverageMap, type LumenCoverageMapProps, blockName, blockOf, } from './coverage-map.js';
|
|
29
32
|
export { type CalendarFact, LumenCoverageCalendarMap, type LumenCoverageCalendarMapProps, } from './coverage-calendar-map.js';
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,9 @@ export { RankedBars, StackedBars, StackedColumns } from './lumen-widget-charts.j
|
|
|
31
31
|
export { ShareMeter } from './share-meter.js';
|
|
32
32
|
export { METER_LEVEL_COLOR, meterLevel, meterValueTone } from './meter-level.js';
|
|
33
33
|
export { UnitValue } from './unit-value.js';
|
|
34
|
+
export { burnRate, durationText, PACE_WINDOWS, paceHours, setDefaultPaceWindow, timeToCap, usePaceWindow, } from './pace.js';
|
|
35
|
+
export { PaceWindowPicker, paceText, TimeToCap } from './time-to-cap.js';
|
|
36
|
+
export { TrendValue, trendChange } from './trend-value.js';
|
|
34
37
|
export { COVERAGE_LEVELS, CoverageCell, CoverageLegend, coverageClass, coverageLabel, coverageLevel, } from './coverage-cells.js';
|
|
35
38
|
export { LumenCoverageMap, blockName, blockOf, } from './coverage-map.js';
|
|
36
39
|
export { LumenCoverageCalendarMap, } from './coverage-calendar-map.js';
|
package/dist/pace.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** The windows a pace is read over. */
|
|
2
|
+
export declare const PACE_WINDOWS: readonly [{
|
|
3
|
+
readonly id: '6h';
|
|
4
|
+
readonly hours: 6;
|
|
5
|
+
}, {
|
|
6
|
+
readonly id: '12h';
|
|
7
|
+
readonly hours: 12;
|
|
8
|
+
}, {
|
|
9
|
+
readonly id: '24h';
|
|
10
|
+
readonly hours: 24;
|
|
11
|
+
}, {
|
|
12
|
+
readonly id: '7d';
|
|
13
|
+
readonly hours: 168;
|
|
14
|
+
}];
|
|
15
|
+
export type PaceWindow = (typeof PACE_WINDOWS)[number]['id'];
|
|
16
|
+
export declare const paceHours: (w: PaceWindow) => 6 | 12 | 24 | 168;
|
|
17
|
+
export interface BurnRate {
|
|
18
|
+
/** Units per hour; a stored amount's growth per hour (can be negative). */
|
|
19
|
+
rate: number;
|
|
20
|
+
/** The plain average, before a lone spike was capped. */
|
|
21
|
+
raw: number;
|
|
22
|
+
/** Lone hot samples that were capped. */
|
|
23
|
+
spikes: number;
|
|
24
|
+
/** The window's own total, before any spike was capped. */
|
|
25
|
+
total: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The rate over `samples` (oldest first, complete periods only), each `per` hours long. `gauge`:
|
|
29
|
+
* the samples are sizes, so the rate is their growth from first to last. `damp: false` keeps spikes.
|
|
30
|
+
*/
|
|
31
|
+
export declare function burnRate(samples: readonly number[], { per, gauge, damp }?: {
|
|
32
|
+
per?: number;
|
|
33
|
+
gauge?: boolean;
|
|
34
|
+
damp?: boolean;
|
|
35
|
+
}): BurnRate;
|
|
36
|
+
export interface Pace extends BurnRate {
|
|
37
|
+
/** Hours until the cap at this rate: 0 when already there, Infinity when not growing. */
|
|
38
|
+
eta: number;
|
|
39
|
+
/** Where the meter lands at reset, as a share of the cap (1.2 = 120%). */
|
|
40
|
+
atReset: number;
|
|
41
|
+
/** Reaches the cap before it resets. */
|
|
42
|
+
hits: boolean;
|
|
43
|
+
/** Hours until the cap resets (or the horizon for a stored amount). */
|
|
44
|
+
resetIn: number;
|
|
45
|
+
/** The rate that lands exactly on the cap at reset (0 once there). */
|
|
46
|
+
safe: number;
|
|
47
|
+
/** The window had too little use to project (below `minTotal`): no time to the cap. */
|
|
48
|
+
thin: boolean;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* When `used` reaches `cap` at `rate`, and where it stands when the cap resets in `resetIn` hours.
|
|
52
|
+
* A window whose use is under `minTotal` (default 0.1% of the cap) is `thin`: no projection, so a
|
|
53
|
+
* handful of operations never reads as a time to the cap. Already at the cap reads as at the cap.
|
|
54
|
+
*/
|
|
55
|
+
export declare function timeToCap(rate: BurnRate, used: number, cap: number, resetIn: number, { minTotal }?: {
|
|
56
|
+
minTotal?: number;
|
|
57
|
+
}): Pace;
|
|
58
|
+
/** `40m`, `2h 05m`, `29h`, `3 days`; `short` gives `2h`, `29h`, `3d` (a phone cell). */
|
|
59
|
+
export declare function durationText(hours: number, short?: boolean): string;
|
|
60
|
+
/** The app's window before anyone picks one (`12h` unless set). */
|
|
61
|
+
export declare function setDefaultPaceWindow(w: PaceWindow): void;
|
|
62
|
+
/** The pace window in use and its setter; every `PaceWindowPicker` and reader shares it. */
|
|
63
|
+
export declare function usePaceWindow(): [PaceWindow, (w: PaceWindow) => void];
|
package/dist/pace.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Pace: how fast a meter is being used over a recent window, carried forward to when it reaches its
|
|
2
|
+
// cap. The same for every product: the app passes the window's samples (hourly or daily amounts, or
|
|
3
|
+
// sizes for a stored amount), what is used so far, the cap and the hours until the cap resets.
|
|
4
|
+
// A lone hot sample (above 3× the window's median, with neither neighbour above it) counts as 3×
|
|
5
|
+
// the median, so one burst can't fake a crisis; a run of hot samples is a real change and counts.
|
|
6
|
+
// Where most samples are empty (median 0), a lone nonzero sample is that burst and counts as 0.
|
|
7
|
+
// Too little use in the window (`minTotal`, by default 0.1% of the cap) gives no time to the cap:
|
|
8
|
+
// three operations in a day say nothing about a million-a-month allowance.
|
|
9
|
+
// `TimeToCap` draws the answer; its colour is where the meter lands at reset, on the meter levels.
|
|
10
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
11
|
+
/** The windows a pace is read over. */
|
|
12
|
+
export const PACE_WINDOWS = [
|
|
13
|
+
{ id: '6h', hours: 6 },
|
|
14
|
+
{ id: '12h', hours: 12 },
|
|
15
|
+
{ id: '24h', hours: 24 },
|
|
16
|
+
{ id: '7d', hours: 168 },
|
|
17
|
+
];
|
|
18
|
+
export const paceHours = (w) => PACE_WINDOWS.find((x) => x.id === w)?.hours ?? 12;
|
|
19
|
+
const median = (xs) => {
|
|
20
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
21
|
+
return s[Math.floor(s.length / 2)] ?? 0;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The rate over `samples` (oldest first, complete periods only), each `per` hours long. `gauge`:
|
|
25
|
+
* the samples are sizes, so the rate is their growth from first to last. `damp: false` keeps spikes.
|
|
26
|
+
*/
|
|
27
|
+
export function burnRate(samples, { per = 1, gauge = false, damp = true } = {}) {
|
|
28
|
+
const n = samples.length;
|
|
29
|
+
if (!n)
|
|
30
|
+
return { rate: 0, raw: 0, spikes: 0, total: 0 };
|
|
31
|
+
if (gauge) {
|
|
32
|
+
const d = n > 1 ? (samples[n - 1] ?? 0) - (samples[0] ?? 0) : 0;
|
|
33
|
+
const r = n > 1 ? d / ((n - 1) * per) : 0;
|
|
34
|
+
return { rate: r, raw: r, spikes: 0, total: Math.abs(d) };
|
|
35
|
+
}
|
|
36
|
+
const total = samples.reduce((s, v) => s + v, 0);
|
|
37
|
+
// With a median of 0, any use is above it: a lone sample among empty ones is damped to 0.
|
|
38
|
+
const cap = median(samples) * 3;
|
|
39
|
+
const hot = (i) => i >= 0 && i < n && (samples[i] ?? 0) > cap;
|
|
40
|
+
let kept = 0;
|
|
41
|
+
let spikes = 0;
|
|
42
|
+
for (let i = 0; i < n; i++) {
|
|
43
|
+
if (damp && hot(i) && !hot(i - 1) && !hot(i + 1)) {
|
|
44
|
+
kept += cap;
|
|
45
|
+
spikes++;
|
|
46
|
+
}
|
|
47
|
+
else
|
|
48
|
+
kept += samples[i] ?? 0;
|
|
49
|
+
}
|
|
50
|
+
const hours = n * per;
|
|
51
|
+
return { rate: kept / hours, raw: total / hours, spikes, total };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* When `used` reaches `cap` at `rate`, and where it stands when the cap resets in `resetIn` hours.
|
|
55
|
+
* A window whose use is under `minTotal` (default 0.1% of the cap) is `thin`: no projection, so a
|
|
56
|
+
* handful of operations never reads as a time to the cap. Already at the cap reads as at the cap.
|
|
57
|
+
*/
|
|
58
|
+
export function timeToCap(rate, used, cap, resetIn, { minTotal = cap * 0.001 } = {}) {
|
|
59
|
+
const left = cap - used;
|
|
60
|
+
const thin = left > 0 && rate.total < minTotal;
|
|
61
|
+
const r = thin ? 0 : Math.max(rate.rate, 0);
|
|
62
|
+
const eta = left <= 0 ? 0 : r > 0 ? left / r : Number.POSITIVE_INFINITY;
|
|
63
|
+
return {
|
|
64
|
+
...rate,
|
|
65
|
+
eta,
|
|
66
|
+
atReset: cap > 0 ? (used + r * resetIn) / cap : 0,
|
|
67
|
+
hits: eta <= resetIn,
|
|
68
|
+
resetIn,
|
|
69
|
+
safe: left > 0 && resetIn > 0 ? left / resetIn : 0,
|
|
70
|
+
thin,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** `40m`, `2h 05m`, `29h`, `3 days`; `short` gives `2h`, `29h`, `3d` (a phone cell). */
|
|
74
|
+
export function durationText(hours, short = false) {
|
|
75
|
+
if (!Number.isFinite(hours))
|
|
76
|
+
return '—';
|
|
77
|
+
if (hours < 1)
|
|
78
|
+
return `${Math.max(1, Math.round(hours * 60))}m`;
|
|
79
|
+
if (short)
|
|
80
|
+
return hours < 48 ? `${Math.round(hours)}h` : `${Math.round(hours / 24)}d`;
|
|
81
|
+
if (hours < 10) {
|
|
82
|
+
const h = Math.floor(hours);
|
|
83
|
+
const m = Math.round((hours - h) * 60);
|
|
84
|
+
return m === 60 ? `${h + 1}h 00m` : `${h}h ${String(m).padStart(2, '0')}m`;
|
|
85
|
+
}
|
|
86
|
+
return hours < 48 ? `${Math.round(hours)}h` : `${Math.round(hours / 24)} days`;
|
|
87
|
+
}
|
|
88
|
+
// The window someone picked, kept in their browser (one choice per app host, every page shares it).
|
|
89
|
+
const KEY = 'lumen:pace-window';
|
|
90
|
+
const listeners = new Set();
|
|
91
|
+
let picked = null;
|
|
92
|
+
let fallback = '12h';
|
|
93
|
+
const isWindow = (v) => PACE_WINDOWS.some((x) => x.id === v);
|
|
94
|
+
function read() {
|
|
95
|
+
if (picked)
|
|
96
|
+
return picked;
|
|
97
|
+
try {
|
|
98
|
+
const v = localStorage.getItem(KEY);
|
|
99
|
+
if (isWindow(v))
|
|
100
|
+
picked = v;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
// Storage refused: the app's default holds.
|
|
104
|
+
}
|
|
105
|
+
return picked ?? fallback;
|
|
106
|
+
}
|
|
107
|
+
function write(w) {
|
|
108
|
+
picked = w;
|
|
109
|
+
try {
|
|
110
|
+
localStorage.setItem(KEY, w);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// Storage refused: the pick holds until reload.
|
|
114
|
+
}
|
|
115
|
+
for (const l of listeners)
|
|
116
|
+
l();
|
|
117
|
+
}
|
|
118
|
+
const subscribe = (l) => {
|
|
119
|
+
listeners.add(l);
|
|
120
|
+
return () => listeners.delete(l);
|
|
121
|
+
};
|
|
122
|
+
/** The app's window before anyone picks one (`12h` unless set). */
|
|
123
|
+
export function setDefaultPaceWindow(w) {
|
|
124
|
+
fallback = w;
|
|
125
|
+
for (const l of listeners)
|
|
126
|
+
l();
|
|
127
|
+
}
|
|
128
|
+
/** The pace window in use and its setter; every `PaceWindowPicker` and reader shares it. */
|
|
129
|
+
export function usePaceWindow() {
|
|
130
|
+
const w = useSyncExternalStore(subscribe, read, () => fallback);
|
|
131
|
+
return [w, useCallback((x) => write(x), [])];
|
|
132
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { MeterLevels } from '@columenlabs/lumen-ui-types/views';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import { meterLevel } from './meter-level.js';
|
|
4
|
+
import { type Pace } from './pace.js';
|
|
5
|
+
/** Picks the pace window (6h, 12h, 24h, 7d) for every page at once; a header action. */
|
|
6
|
+
export declare function PaceWindowPicker({ className }: {
|
|
7
|
+
className?: string;
|
|
8
|
+
}): import("react").JSX.Element;
|
|
9
|
+
/** What a pace reads as: the words and the level they're coloured by. */
|
|
10
|
+
export declare function paceText(p: Pace, short?: boolean): {
|
|
11
|
+
text: string;
|
|
12
|
+
level: ReturnType<typeof meterLevel>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* One meter's time to its cap at the pace window's rate. `label` names the meter when it isn't the
|
|
16
|
+
* one the row already shows; `short` is the phone cell (`2h`, `3d`, `—`).
|
|
17
|
+
*/
|
|
18
|
+
export declare function TimeToCap({ pace, label, short, levels, className, }: {
|
|
19
|
+
pace: Pace | null;
|
|
20
|
+
label?: ReactNode;
|
|
21
|
+
short?: boolean;
|
|
22
|
+
/** This cell's own levels; left out, the app's `meterLevels`. */
|
|
23
|
+
levels?: Partial<MeterLevels>;
|
|
24
|
+
className?: string;
|
|
25
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ZapIcon } from 'lucide-react';
|
|
3
|
+
import { useIsMobile } from './hooks/use-mobile.js';
|
|
4
|
+
import { cn } from './lib/utils.js';
|
|
5
|
+
import { LumenSelector } from './lumen-selector.js';
|
|
6
|
+
import { METER_LEVEL_COLOR, meterLevel } from './meter-level.js';
|
|
7
|
+
import { PACE_WINDOWS, durationText, usePaceWindow } from './pace.js';
|
|
8
|
+
import { ToggleGroup, ToggleGroupItem } from './ui/toggle-group.js';
|
|
9
|
+
/** Picks the pace window (6h, 12h, 24h, 7d) for every page at once; a header action. */
|
|
10
|
+
export function PaceWindowPicker({ className }) {
|
|
11
|
+
const [w, setW] = usePaceWindow();
|
|
12
|
+
const mobile = useIsMobile();
|
|
13
|
+
if (mobile)
|
|
14
|
+
return (_jsx(LumenSelector, { trigger: "chip", icon: ZapIcon, label: "Pace window", size: "sm", align: "end", value: w, onChange: (v) => setW(v), options: PACE_WINDOWS.map((x) => ({ value: x.id, label: x.id, detail: `last ${x.id}` })), className: className }));
|
|
15
|
+
return (_jsxs(ToggleGroup, { type: "single", variant: "outline", size: "sm", spacing: 0, value: w, onValueChange: (v) => v && setW(v), "aria-label": "Pace window", title: "Pace: the rate over the last\u2026", className: className, children: [_jsxs("span", { className: "flex h-8 items-center gap-1 rounded-l-md border border-r-0 px-2 text-muted-foreground text-xs", children: [_jsx(ZapIcon, { className: "size-3.5" }), "Pace"] }), PACE_WINDOWS.map((x) => (_jsx(ToggleGroupItem, { value: x.id, className: "num px-2.5 text-xs", children: x.id }, x.id)))] }));
|
|
16
|
+
}
|
|
17
|
+
/** What a pace reads as: the words and the level they're coloured by. */
|
|
18
|
+
export function paceText(p, short = false) {
|
|
19
|
+
if (p.eta === 0)
|
|
20
|
+
return { text: short ? 'cap' : 'At cap', level: 'alert' };
|
|
21
|
+
const level = meterLevel(p.atReset);
|
|
22
|
+
if (p.hits)
|
|
23
|
+
return { text: short ? durationText(p.eta, true) : `in ${durationText(p.eta)}`, level };
|
|
24
|
+
return { text: short ? '—' : 'Resets first', level: short ? 'ok' : level };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One meter's time to its cap at the pace window's rate. `label` names the meter when it isn't the
|
|
28
|
+
* one the row already shows; `short` is the phone cell (`2h`, `3d`, `—`).
|
|
29
|
+
*/
|
|
30
|
+
export function TimeToCap({ pace, label, short, levels, className, }) {
|
|
31
|
+
if (!pace)
|
|
32
|
+
return _jsx("span", { className: "text-muted-foreground", children: "\u2014" });
|
|
33
|
+
const t = paceText(pace, short);
|
|
34
|
+
const level = levels && pace.eta > 0 ? meterLevel(pace.atReset, levels) : t.level;
|
|
35
|
+
const c = METER_LEVEL_COLOR[level];
|
|
36
|
+
const quiet = level === 'ok' && !pace.hits;
|
|
37
|
+
return (_jsxs("span", { className: cn('inline-flex h-5 min-w-0 items-center gap-1.5 whitespace-nowrap rounded-sm text-xs', quiet ? 'text-muted-foreground' : 'px-1.5', className), style: quiet
|
|
38
|
+
? undefined
|
|
39
|
+
: level === 'ok'
|
|
40
|
+
? { background: 'var(--muted)' }
|
|
41
|
+
: {
|
|
42
|
+
background: `color-mix(in oklab, ${c} 18%, transparent)`,
|
|
43
|
+
color: `color-mix(in oklab, ${c} 80%, var(--foreground))`,
|
|
44
|
+
}, children: [label && _jsx("span", { className: "truncate max-md:max-w-16", children: label }), _jsx("span", { className: "num", children: t.text })] }));
|
|
45
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type TrendChange =
|
|
2
|
+
/** Both periods empty, or nothing to compare. */
|
|
3
|
+
{
|
|
4
|
+
kind: 'none';
|
|
5
|
+
}
|
|
6
|
+
/** No visible change. */
|
|
7
|
+
| {
|
|
8
|
+
kind: 'flat';
|
|
9
|
+
}
|
|
10
|
+
/** A share of the earlier period (0.12 = +12%). */
|
|
11
|
+
| {
|
|
12
|
+
kind: 'pct';
|
|
13
|
+
ratio: number;
|
|
14
|
+
}
|
|
15
|
+
/** Past `max` times the earlier period. */
|
|
16
|
+
| {
|
|
17
|
+
kind: 'many';
|
|
18
|
+
up: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** The earlier period is at or under `minBase`: the plain difference. */
|
|
21
|
+
| {
|
|
22
|
+
kind: 'abs';
|
|
23
|
+
delta: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* How `now` compares with `prev`. `minBase`: the smallest earlier period a share is worked out
|
|
27
|
+
* from (in the values' own unit); `max`: the multiple from which it reads `>10×`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function trendChange(now: number, prev: number, { minBase, max }?: {
|
|
30
|
+
minBase?: number;
|
|
31
|
+
max?: number;
|
|
32
|
+
}): TrendChange;
|
|
33
|
+
/**
|
|
34
|
+
* Draws a `trendChange`: `▲ 12%`, `▼ 8%`, `▲ >10×`, `▲ +3` (`format` writes the difference), `0%`
|
|
35
|
+
* muted when flat, a dash when there is nothing to compare.
|
|
36
|
+
*/
|
|
37
|
+
export declare function TrendValue({ change, format, title, className, }: {
|
|
38
|
+
change: TrendChange | null;
|
|
39
|
+
/** Writes a plain difference (without its sign). */
|
|
40
|
+
format?: (v: number) => string;
|
|
41
|
+
title?: string;
|
|
42
|
+
className?: string;
|
|
43
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A change between two periods (this week against the last) that never misleads: a share only when
|
|
3
|
+
// the earlier period is big enough to divide by, the plain difference (`▲ +3`) when it is tiny or
|
|
4
|
+
// empty, and `▲ >10×` once the change is past ten times, never a five-digit percent.
|
|
5
|
+
import { cn } from './lib/utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* How `now` compares with `prev`. `minBase`: the smallest earlier period a share is worked out
|
|
8
|
+
* from (in the values' own unit); `max`: the multiple from which it reads `>10×`.
|
|
9
|
+
*/
|
|
10
|
+
export function trendChange(now, prev, { minBase = 0, max = 10 } = {}) {
|
|
11
|
+
const delta = now - prev;
|
|
12
|
+
if (!now && !prev)
|
|
13
|
+
return { kind: 'none' };
|
|
14
|
+
if (prev <= minBase)
|
|
15
|
+
return delta ? { kind: 'abs', delta } : { kind: 'flat' };
|
|
16
|
+
const ratio = now / prev - 1;
|
|
17
|
+
if (now / prev >= max)
|
|
18
|
+
return { kind: 'many', up: true };
|
|
19
|
+
if (Math.round(ratio * 100) === 0)
|
|
20
|
+
return { kind: 'flat' };
|
|
21
|
+
return { kind: 'pct', ratio };
|
|
22
|
+
}
|
|
23
|
+
const minus = (s) => s.replace(/^-/, '−');
|
|
24
|
+
/**
|
|
25
|
+
* Draws a `trendChange`: `▲ 12%`, `▼ 8%`, `▲ >10×`, `▲ +3` (`format` writes the difference), `0%`
|
|
26
|
+
* muted when flat, a dash when there is nothing to compare.
|
|
27
|
+
*/
|
|
28
|
+
export function TrendValue({ change, format = (v) => Math.round(v).toLocaleString('en-US'), title, className, }) {
|
|
29
|
+
if (!change || change.kind === 'none')
|
|
30
|
+
return _jsx("span", { className: cn('text-muted-foreground', className), children: "\u2014" });
|
|
31
|
+
if (change.kind === 'flat')
|
|
32
|
+
return (_jsx("span", { className: cn('num text-muted-foreground', className), title: title, children: "0%" }));
|
|
33
|
+
const up = change.kind === 'pct' ? change.ratio > 0 : change.kind === 'abs' ? change.delta > 0 : change.up;
|
|
34
|
+
const text = change.kind === 'pct'
|
|
35
|
+
? `${Math.abs(Math.round(change.ratio * 100))}%`
|
|
36
|
+
: change.kind === 'many'
|
|
37
|
+
? '>10×'
|
|
38
|
+
: `${up ? '+' : '−'}${minus(format(Math.abs(change.delta)))}`;
|
|
39
|
+
return (_jsxs("span", { className: cn('num whitespace-nowrap', className), title: title, children: [up ? '▲' : '▼', " ", text] }));
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@columenlabs/lumen-ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.57",
|
|
4
4
|
"description": "Lumen: shared React components (frame, views, widgets, charts, drawers).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@codemirror/lang-javascript": "6.2.5",
|
|
42
42
|
"@codemirror/lang-json": "6.0.2",
|
|
43
43
|
"@codemirror/lang-sql": "6.10.0",
|
|
44
|
-
"@columenlabs/lumen-ui-types": "0.1.
|
|
44
|
+
"@columenlabs/lumen-ui-types": "0.1.57",
|
|
45
45
|
"@dnd-kit/core": "6.3.1",
|
|
46
46
|
"@dnd-kit/sortable": "10.0.0",
|
|
47
47
|
"@dnd-kit/utilities": "3.2.2",
|