basalt-ui 2.26.0 → 2.27.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/CHANGELOG.md +24 -0
- package/MIGRATING.md +87 -12
- package/dist/charts/Bars.d.ts.map +1 -1
- package/dist/charts/Bars.js +37 -1
- package/dist/charts/ChartCard.d.ts +8 -2
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +2 -0
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +48 -10
- package/dist/charts/chart-box.module.css +76 -1
- package/dist/charts/chart-box.module.css.d.ts +1 -0
- package/dist/charts/chart-legend.module.css +0 -14
- package/dist/charts/compile.d.ts +20 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +82 -12
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +37 -87
- package/dist/charts/state.d.ts +7 -0
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +14 -3
- package/dist/cli/shot/capture.d.ts +32 -3
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +17 -7
- package/dist/dashboard/meter-model.d.ts +17 -3
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +69 -19
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +28 -12
- package/dist/dashboard/meter.module.css +12 -1
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +89 -21
- package/dist/dashboard/stat-strip.module.css +13 -3
- package/dist/dashboard/stat-strip.module.css.d.ts +1 -0
- package/llms.txt +3 -2
- package/package.json +1 -1
- package/src/charts/Bars.tsx +50 -2
- package/src/charts/ChartCard.tsx +9 -1
- package/src/charts/Sparkline.tsx +67 -9
- package/src/charts/chart-box.module.css +76 -1
- package/src/charts/chart-box.module.css.d.ts +1 -0
- package/src/charts/chart-legend.module.css +0 -14
- package/src/charts/compile.ts +129 -18
- package/src/charts/legend.tsx +10 -74
- package/src/charts/state.tsx +30 -5
- package/src/cli/shot/capture.ts +25 -9
- package/src/dashboard/meter-model.ts +122 -31
- package/src/dashboard/meter-parts.tsx +53 -19
- package/src/dashboard/meter.module.css +12 -1
- package/src/dashboard/stat-strip.module.css +13 -3
- package/src/dashboard/stat-strip.module.css.d.ts +1 -0
- package/src/dashboard/stat-strip.tsx +113 -25
package/src/charts/ChartCard.tsx
CHANGED
|
@@ -139,7 +139,10 @@ type ChartCardBaseProps = BasaltProps &
|
|
|
139
139
|
/** Right-aligned controls. A `<Text size="xs">` caption here renders at 11 (the chart-text step,
|
|
140
140
|
* beside the plot's ticks); wrap it in a `span` or give it your own class to opt out. */
|
|
141
141
|
actions?: ReactNode
|
|
142
|
-
/** A resolved state replaces `children` with its placeholder; the header stays.
|
|
142
|
+
/** A resolved state replaces `children` with its placeholder; the header stays. The card learns
|
|
143
|
+
* a state only from here: a body that draws its own loading or empty line (`Meter loading`,
|
|
144
|
+
* `BarList emptyText`) must ALSO be reported here (`{ pending: true }`, `{ empty }`), or the
|
|
145
|
+
* `facts` above it render over a state they contradict. */
|
|
143
146
|
state?: ChartState | undefined
|
|
144
147
|
/** Body height while a placeholder shows. Default: the chart height law, i.e. exactly the box an
|
|
145
148
|
* unsized chart takes — pass the chart's own `height` when it states one, so a `<Suspense
|
|
@@ -149,6 +152,9 @@ type ChartCardBaseProps = BasaltProps &
|
|
|
149
152
|
* its own loading (`BarList loading`, a loading `Meter`); a children-less `<Suspense
|
|
150
153
|
* fallback>` should pass a numeric height instead. */
|
|
151
154
|
placeholderHeight?: number | ResponsiveChartHeight | 'content' | undefined
|
|
155
|
+
/** The pending plot's ghost: `'bars'` (default), `'line'` for a line or area chart, or the bar
|
|
156
|
+
* count when the loaded chart's data length is known. */
|
|
157
|
+
ghost?: 'bars' | 'line' | number | undefined
|
|
152
158
|
/** Placeholder data is showing: the facts row dims with the plot (a strip cell's `stale`). */
|
|
153
159
|
stale?: boolean | undefined
|
|
154
160
|
/** What an errored `state` names: "Could not load {stateNoun}". Default: no noun. */
|
|
@@ -253,6 +259,7 @@ export function ChartCard({
|
|
|
253
259
|
stateAction,
|
|
254
260
|
stale,
|
|
255
261
|
stateNoun,
|
|
262
|
+
ghost,
|
|
256
263
|
onRetry,
|
|
257
264
|
retrying,
|
|
258
265
|
className,
|
|
@@ -310,6 +317,7 @@ export function ChartCard({
|
|
|
310
317
|
state={state}
|
|
311
318
|
{...(placeholderHeight !== undefined && { height: placeholderHeight })}
|
|
312
319
|
{...(stateNoun !== undefined && { noun: stateNoun })}
|
|
320
|
+
{...(ghost !== undefined && { ghost })}
|
|
313
321
|
{...(onRetry !== undefined && { onRetry })}
|
|
314
322
|
{...(retrying !== undefined && { retrying })}
|
|
315
323
|
action={stateAction}
|
package/src/charts/Sparkline.tsx
CHANGED
|
@@ -80,6 +80,9 @@ const READOUT_GAP = 8
|
|
|
80
80
|
/** Air round a line's plot, px (the engine's plot margin): the end marker stays inside the svg box,
|
|
81
81
|
* so it clears the 2px focus outline round it. A bar plot is flush. */
|
|
82
82
|
const LINE_INSET = 3
|
|
83
|
+
/** A bare line sparkline shorter than this has no room for a two-line readout (about 34px with its
|
|
84
|
+
* padding and border): it keeps the one-line form, in the in-box band farthest from the line. */
|
|
85
|
+
const SHORT_BOX = 36
|
|
83
86
|
/** The muted tone's line: the same alpha the consumers hand-built (`alpha(VX.muted, .35)`). */
|
|
84
87
|
const MUTED_LINE = alpha(VX.muted, 0.35)
|
|
85
88
|
/** Air between the plot's start and the reference label, so the focus outline never covers it. */
|
|
@@ -449,6 +452,56 @@ function placeStacked(node: HTMLElement, line: () => readonly LinePoint[], fx: n
|
|
|
449
452
|
node.style.marginLeft = `${fit.left - slid}px`
|
|
450
453
|
}
|
|
451
454
|
|
|
455
|
+
/** The plotted line's y (fraction of the box) at x, interpolated along the polyline; null outside it. */
|
|
456
|
+
function lineYAt(pts: readonly (readonly [number, number])[], x: number): number | null {
|
|
457
|
+
for (let i = 0; i + 1 < pts.length; i += 1) {
|
|
458
|
+
const [ax, ay] = pts[i] as readonly [number, number]
|
|
459
|
+
const [bx, by] = pts[i + 1] as readonly [number, number]
|
|
460
|
+
if (x >= Math.min(ax, bx) && x <= Math.max(ax, bx))
|
|
461
|
+
return bx === ax ? ay : ay + ((by - ay) * (x - ax)) / (bx - ax)
|
|
462
|
+
}
|
|
463
|
+
return null
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* A short bare line's one-line chip stays inside its box (it can never reach a sibling above or
|
|
468
|
+
* below). It slides with the point and takes the band, top or bottom, farthest from the plotted line
|
|
469
|
+
* under it; a box so short that the chip fills it moves beside the point instead, so the chip never
|
|
470
|
+
* covers the dot. Offsets go on `margin-left` and `top`, which React's `left` / `translate` do not own.
|
|
471
|
+
*/
|
|
472
|
+
function placeInBand(node: HTMLElement, line: () => readonly LinePoint[], fx: number): void {
|
|
473
|
+
const layer = node.parentElement
|
|
474
|
+
if (!layer) return
|
|
475
|
+
const [W, H, w, h] = [layer.clientWidth, layer.clientHeight, node.offsetWidth, node.offsetHeight]
|
|
476
|
+
if (W <= 0 || H <= 0) return
|
|
477
|
+
const pts = line().map(([x, y]): [number, number] => [x * W, y * H])
|
|
478
|
+
const slid = fx * (W - w)
|
|
479
|
+
const room = (left: number): number => Math.min(Math.max(left, 0), Math.max(W - w, 0))
|
|
480
|
+
const lefts = [slid, room(fx * W + CLEAR * 2), room(fx * W - CLEAR * 2 - w)]
|
|
481
|
+
const bands = [0, Math.max(H - h, 0)]
|
|
482
|
+
const dotY = lineYAt(pts, fx * W)
|
|
483
|
+
const score = (left: number, top: number): number => {
|
|
484
|
+
const covers =
|
|
485
|
+
dotY !== null &&
|
|
486
|
+
fx * W + CLEAR > left &&
|
|
487
|
+
fx * W - CLEAR < left + w &&
|
|
488
|
+
dotY + CLEAR > top &&
|
|
489
|
+
dotY - CLEAR < top + h
|
|
490
|
+
let nearest = Number.POSITIVE_INFINITY
|
|
491
|
+
for (let x = left; x <= left + w; x += 2) {
|
|
492
|
+
const y = lineYAt(pts, x)
|
|
493
|
+
if (y === null) continue
|
|
494
|
+
nearest = Math.min(nearest, y < top ? top - y : y > top + h ? y - (top + h) : 0)
|
|
495
|
+
}
|
|
496
|
+
return (covers ? -1000 : 0) + Math.min(nearest, 999)
|
|
497
|
+
}
|
|
498
|
+
const fit = lefts
|
|
499
|
+
.flatMap((left) => bands.map((top) => ({ left, top, s: score(left, top) })))
|
|
500
|
+
.reduce((a, b) => (b.s > a.s ? b : a))
|
|
501
|
+
node.style.top = `${fit.top}px`
|
|
502
|
+
node.style.marginLeft = `${fit.left - slid}px`
|
|
503
|
+
}
|
|
504
|
+
|
|
452
505
|
/**
|
|
453
506
|
* A readout never loses its separator or its date to an ellipsis: when the one-line chip overflows
|
|
454
507
|
* its box it first tightens (`data-tight`: less padding, the separator's air stays), then stacks
|
|
@@ -459,6 +512,7 @@ function fitReadout(
|
|
|
459
512
|
node: HTMLElement,
|
|
460
513
|
line: (() => readonly LinePoint[]) | null,
|
|
461
514
|
fx: number,
|
|
515
|
+
band: boolean,
|
|
462
516
|
): void {
|
|
463
517
|
const overflows = (): boolean => node.scrollWidth > node.clientWidth
|
|
464
518
|
node.removeAttribute('data-tight')
|
|
@@ -468,11 +522,14 @@ function fitReadout(
|
|
|
468
522
|
node.style.removeProperty('top')
|
|
469
523
|
node.style.removeProperty('margin-left')
|
|
470
524
|
}
|
|
471
|
-
if (
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
525
|
+
if (overflows()) {
|
|
526
|
+
node.setAttribute('data-tight', '')
|
|
527
|
+
// A short bare box has no room for two lines: it stays one line, tight.
|
|
528
|
+
if (overflows() && !band) node.setAttribute('data-stacked', '')
|
|
529
|
+
}
|
|
530
|
+
if (line === null) return
|
|
531
|
+
if (band) placeInBand(node, line, fx)
|
|
532
|
+
else if (node.hasAttribute('data-stacked')) placeStacked(node, line, fx)
|
|
476
533
|
}
|
|
477
534
|
|
|
478
535
|
export function Sparkline(props: SparklineProps): ReactElement {
|
|
@@ -583,6 +640,7 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
583
640
|
// `translate` take one fraction), inside the box at either edge whatever its width, and the
|
|
584
641
|
// stylesheet lifts it above the plot. Bare: beside the point, on the roomier side and on the half of
|
|
585
642
|
// the box away from it, so the chip covers neither the dot nor anything the host draws round it.
|
|
643
|
+
const short = !hosted && mark === 'line' && height < SHORT_BOX
|
|
586
644
|
const beside = at === null ? null : at.fx > 0.5 ? 'left' : 'right'
|
|
587
645
|
// The chip's fit depends on its text and (bare) its side, not on the pointer's every tick.
|
|
588
646
|
const shown = at !== null
|
|
@@ -596,7 +654,7 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
596
654
|
// A hosted line's stacked chip clears the plotted line, built only when the chip stacks; a bar
|
|
597
655
|
// or a bare chip keeps its place.
|
|
598
656
|
const line =
|
|
599
|
-
hosted && mark === 'line' && model !== null
|
|
657
|
+
(hosted || short) && mark === 'line' && model !== null
|
|
600
658
|
? (): LinePoint[] =>
|
|
601
659
|
model.rows.flatMap((r, i): LinePoint[] =>
|
|
602
660
|
r.v === null
|
|
@@ -610,10 +668,10 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
610
668
|
)
|
|
611
669
|
: null
|
|
612
670
|
fitRef.current = () => {
|
|
613
|
-
if (readoutRef.current !== null) fitReadout(readoutRef.current, line, fxRef.current)
|
|
671
|
+
if (readoutRef.current !== null) fitReadout(readoutRef.current, line, fxRef.current, short)
|
|
614
672
|
}
|
|
615
673
|
fitRef.current()
|
|
616
|
-
}, [active, at?.label, at?.value, beside, hosted, mark, model])
|
|
674
|
+
}, [active, at?.label, at?.value, beside, hosted, short, mark, model])
|
|
617
675
|
// ...and on the box, which a container resize changes without a hover.
|
|
618
676
|
useEffect(() => {
|
|
619
677
|
const layer = readoutRef.current?.parentElement
|
|
@@ -625,7 +683,7 @@ export function Sparkline(props: SparklineProps): ReactElement {
|
|
|
625
683
|
const readoutStyle: CSSProperties | undefined =
|
|
626
684
|
at === null
|
|
627
685
|
? undefined
|
|
628
|
-
: hosted
|
|
686
|
+
: hosted || short
|
|
629
687
|
? { left: `${at.fx * 100}%`, translate: `${-at.fx * 100}% 0`, maxWidth: '100%' }
|
|
630
688
|
: {
|
|
631
689
|
...(beside === 'right'
|
|
@@ -115,6 +115,16 @@
|
|
|
115
115
|
bottom / 100% 1px no-repeat;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
.ghostLine {
|
|
119
|
+
width: 100%;
|
|
120
|
+
height: 100%;
|
|
121
|
+
fill: none;
|
|
122
|
+
stroke: color-mix(in srgb, var(--vx-ink) 24%, var(--vx-surface-panel));
|
|
123
|
+
stroke-width: 2px;
|
|
124
|
+
stroke-linecap: round;
|
|
125
|
+
stroke-linejoin: round;
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
.ghostBar {
|
|
119
129
|
flex: 1 1 0;
|
|
120
130
|
min-width: 0;
|
|
@@ -135,7 +145,9 @@
|
|
|
135
145
|
}
|
|
136
146
|
|
|
137
147
|
:global(.ts-chart-tooltip__row > span:nth-child(2)) {
|
|
138
|
-
|
|
148
|
+
/* Words wrap, tokens do not: the engine's `anywhere` on the card would split `Load (7)` as
|
|
149
|
+
* `Load` / `7)` and `TE 2.1/0.4` mid-figure. A single token wider than the card overflows it. */
|
|
150
|
+
overflow-wrap: normal;
|
|
139
151
|
white-space: normal;
|
|
140
152
|
}
|
|
141
153
|
|
|
@@ -154,3 +166,66 @@
|
|
|
154
166
|
)
|
|
155
167
|
var(--vx-surface-elevated) !important;
|
|
156
168
|
}
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* A slotted `Bars` (`data-basalt-slot`, set by `compile.ts`) is a micro-visual: its tooltip is the
|
|
172
|
+
* Sparkline's one-line chip, `<value> · <x label>` in the line the host reserves above the plot. The
|
|
173
|
+
* first row's figure leads, the header follows it, further rows are hidden (a chart that needs rows
|
|
174
|
+
* lives in a `ChartCard`); an overlong header ends in an ellipsis. Height, type and edge are the
|
|
175
|
+
* chip's (`sparkline.module.css`); the tooltip is portalled out of the host, so its fallbacks stand.
|
|
176
|
+
*/
|
|
177
|
+
:global(.ts-chart-tooltip[data-basalt-slot]) {
|
|
178
|
+
display: flex !important;
|
|
179
|
+
align-items: center;
|
|
180
|
+
box-sizing: border-box;
|
|
181
|
+
width: max-content;
|
|
182
|
+
block-size: 1.125rem;
|
|
183
|
+
padding: 0 4px !important;
|
|
184
|
+
overflow: hidden;
|
|
185
|
+
font-family: var(--basalt-font-numeral, var(--basalt-font-sans));
|
|
186
|
+
font-variant-numeric: lining-nums;
|
|
187
|
+
font-size: var(--vx-text-xs);
|
|
188
|
+
line-height: calc(1.125rem - 2px);
|
|
189
|
+
white-space: nowrap !important;
|
|
190
|
+
border-radius: var(--vx-radius-ctrl) !important;
|
|
191
|
+
/* theme-allow css-raw-surface — the chip is flat, like the Sparkline's, not a floating panel */
|
|
192
|
+
box-shadow: none !important;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
:global(.ts-chart-tooltip[data-basalt-slot]:is([hidden], [popover]:not(:popover-open))) {
|
|
196
|
+
/* The flex above must not beat the engine's own hide. */
|
|
197
|
+
display: none !important;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__rows) {
|
|
201
|
+
flex: none;
|
|
202
|
+
order: 0;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__row) {
|
|
206
|
+
display: block !important;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__row:not(:first-child)),
|
|
210
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__row > span:not(:last-child)) {
|
|
211
|
+
display: none;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__title) {
|
|
215
|
+
display: block !important;
|
|
216
|
+
flex: 0 1 auto;
|
|
217
|
+
min-width: 0;
|
|
218
|
+
order: 1;
|
|
219
|
+
margin: 0 !important;
|
|
220
|
+
overflow: hidden;
|
|
221
|
+
font-weight: inherit !important;
|
|
222
|
+
text-overflow: ellipsis;
|
|
223
|
+
white-space: nowrap;
|
|
224
|
+
overflow-wrap: normal;
|
|
225
|
+
opacity: 0.7;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
:global(.ts-chart-tooltip[data-basalt-slot] .ts-chart-tooltip__title::before) {
|
|
229
|
+
content: ' · ';
|
|
230
|
+
white-space: pre;
|
|
231
|
+
}
|
|
@@ -15,9 +15,7 @@
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
@container (min-width: 240px) {
|
|
18
|
-
/* `foldLegend` (legend.tsx) folds the zones / references that wrap the row into `+N`; series never fold. */
|
|
19
18
|
.auto {
|
|
20
|
-
--bc-fold: 1;
|
|
21
19
|
flex-wrap: wrap;
|
|
22
20
|
display: flex;
|
|
23
21
|
column-gap: var(--bc-dots-gap);
|
|
@@ -27,7 +25,6 @@
|
|
|
27
25
|
|
|
28
26
|
@container (min-width: 480px) {
|
|
29
27
|
.auto {
|
|
30
|
-
--bc-fold: 0;
|
|
31
28
|
column-gap: var(--bc-legend-gap);
|
|
32
29
|
--vx-hit-gap: var(--bc-legend-gap);
|
|
33
30
|
}
|
|
@@ -47,14 +44,3 @@
|
|
|
47
44
|
display: none;
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
|
-
|
|
51
|
-
/* The compact fold (`foldLegend`, which only folds while `--bc-fold` is 1); wide keeps every entry.
|
|
52
|
-
* `!important`: an entry's inline `all: unset` (legend.tsx) would otherwise reset `display`. */
|
|
53
|
-
.auto [data-legend-folded] {
|
|
54
|
-
display: none !important;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
.auto [data-legend-more] {
|
|
58
|
-
order: 1;
|
|
59
|
-
flex: none;
|
|
60
|
-
}
|
package/src/charts/compile.ts
CHANGED
|
@@ -15,7 +15,10 @@ import { ruleX, ruleY } from '@tanstack/charts/rule'
|
|
|
15
15
|
import { scaleLinear } from '@tanstack/charts/scales/linear'
|
|
16
16
|
import { scaleUtc } from 'd3-scale'
|
|
17
17
|
import { portal } from '@tanstack/charts/tooltip/portal'
|
|
18
|
-
import type {
|
|
18
|
+
import type {
|
|
19
|
+
ChartTooltipPortalExtension,
|
|
20
|
+
ChartTooltipPortalPositionContext,
|
|
21
|
+
} from '@tanstack/charts/tooltip/portal'
|
|
19
22
|
import type {
|
|
20
23
|
ChartBuildContext,
|
|
21
24
|
ChartTooltipAnchorContext,
|
|
@@ -1024,6 +1027,20 @@ const stripAnchor = (
|
|
|
1024
1027
|
}
|
|
1025
1028
|
const STRIP_TOOLTIP_PLACEMENT = ['bottom', 'top'] as const
|
|
1026
1029
|
const STRIP_TOOLTIP_OFFSET = 8
|
|
1030
|
+
/**
|
|
1031
|
+
* A slotted `Bars` (`tooltipBelow` and `bare`): a micro-visual whose tooltip is the one-line chip
|
|
1032
|
+
* (`chart-box.module.css`, `data-basalt-slot`) in the line the cell reserves above the plot. It
|
|
1033
|
+
* anchors to the plot's top edge, flush, and opens upward, never below the plot and never over the
|
|
1034
|
+
* bars; the card clamp's vertical flip does not apply to it.
|
|
1035
|
+
*/
|
|
1036
|
+
const slotAnchor = (
|
|
1037
|
+
points: readonly ChartPoint[],
|
|
1038
|
+
{ plot }: ChartTooltipAnchorContext,
|
|
1039
|
+
): { x: number; y: number } => {
|
|
1040
|
+
const xs = points.map((p) => p.x)
|
|
1041
|
+
return { x: (Math.min(...xs) + Math.max(...xs)) / 2, y: plot.y }
|
|
1042
|
+
}
|
|
1043
|
+
const SLOT_TOOLTIP_PLACEMENT = ['top'] as const
|
|
1027
1044
|
|
|
1028
1045
|
/** A tooltip's chrome (title row, padding) and each series row, px: the guess the bar anchor reserves. */
|
|
1029
1046
|
const TIP_CHROME = 30
|
|
@@ -1062,38 +1079,126 @@ const barAnchor =
|
|
|
1062
1079
|
const TIP_CARD = '[data-basalt-chart-card], [data-strip-cell], .mantine-Card-root'
|
|
1063
1080
|
/** Air between a card-clamped tooltip and the card's edge, px. */
|
|
1064
1081
|
const TIP_CARD_AIR = 4
|
|
1082
|
+
/** A card narrower than this keeps the engine's width and horizontal placement (a ~120px strip
|
|
1083
|
+
* cell would squeeze a tooltip to its 6rem label floor); the vertical rule applies to every card. */
|
|
1084
|
+
const TIP_CARD_ROOMY = 192
|
|
1065
1085
|
|
|
1066
1086
|
/**
|
|
1067
|
-
* The engine places a portalled tooltip against the viewport, so
|
|
1068
|
-
*
|
|
1069
|
-
*
|
|
1070
|
-
*
|
|
1071
|
-
* stands when the card is narrower than the tooltip).
|
|
1087
|
+
* The engine places a portalled tooltip against the viewport, so it opens past the card's edge, over
|
|
1088
|
+
* the neighbouring card or the section heading above. Its placements (a hover, and its own resize /
|
|
1089
|
+
* scroll repositions, which never pass through `position`) all write the element's inline `left` and
|
|
1090
|
+
* `top`, so each write is followed by a settle inside the card (`settleInCard`).
|
|
1072
1091
|
*/
|
|
1073
|
-
|
|
1092
|
+
/** What a card clamp does: `card` a chart tooltip (both axes, the card's width), `slot` a slotted
|
|
1093
|
+
* `Bars` chip (flush above its plot), `strip` a `BandStrip`'s (sideways only, as before Wave 39). */
|
|
1094
|
+
type TipMode = 'card' | 'slot' | 'strip'
|
|
1095
|
+
|
|
1096
|
+
const cardPortal = (mode: TipMode): ChartTooltipPortalExtension => ({
|
|
1074
1097
|
id: portal.id,
|
|
1075
1098
|
create(context, options) {
|
|
1076
1099
|
const inner = portal.create(context, options)
|
|
1077
1100
|
const { element, container } = context
|
|
1078
|
-
|
|
1101
|
+
if (mode === 'slot') element.dataset['basaltSlot'] = ''
|
|
1102
|
+
let last: ChartTooltipPortalPositionContext | undefined
|
|
1103
|
+
const watch = new MutationObserver(() => {
|
|
1104
|
+
if (last) settleInCard(element, container, last, mode)
|
|
1105
|
+
})
|
|
1079
1106
|
watch.observe(element, { attributes: true, attributeFilter: ['style'] })
|
|
1080
1107
|
return {
|
|
1081
1108
|
...inner,
|
|
1109
|
+
position(next) {
|
|
1110
|
+
last = next
|
|
1111
|
+
return inner.position(next)
|
|
1112
|
+
},
|
|
1082
1113
|
destroy() {
|
|
1083
1114
|
watch.disconnect()
|
|
1084
1115
|
inner.destroy()
|
|
1085
1116
|
},
|
|
1086
1117
|
}
|
|
1087
1118
|
},
|
|
1088
|
-
}
|
|
1119
|
+
})
|
|
1089
1120
|
|
|
1090
|
-
|
|
1121
|
+
/**
|
|
1122
|
+
* Keeps a tooltip inside its card on both axes. A card with room lets the tooltip grow up to its
|
|
1123
|
+
* width before a label wraps, and re-centres it on the anchor at that width. A tooltip that would
|
|
1124
|
+
* cross the card's top or bottom flips to the other side of its anchor when that side fits; neither
|
|
1125
|
+
* fitting, it slides in; taller than the card, it aligns to the card's top (the only overflow).
|
|
1126
|
+
* A `slot` tooltip (a slotted `Bars`, one line) keeps its place above the plot, centred on the
|
|
1127
|
+
* anchor, capped at the card's width and held inside the card sideways and at its top. A hidden or unmeasured tooltip is left alone, and an unchanged position
|
|
1128
|
+
* is not written back (a write re-runs this).
|
|
1129
|
+
*/
|
|
1130
|
+
export function settleInCard(
|
|
1131
|
+
tip: HTMLElement,
|
|
1132
|
+
within: Element,
|
|
1133
|
+
ctx: ChartTooltipPortalPositionContext,
|
|
1134
|
+
mode: TipMode = 'card',
|
|
1135
|
+
): void {
|
|
1136
|
+
const slot = mode === 'slot'
|
|
1137
|
+
if (tip.hidden || tip.offsetHeight === 0) return
|
|
1091
1138
|
const card = within.closest(TIP_CARD)?.getBoundingClientRect()
|
|
1092
|
-
const
|
|
1093
|
-
const width =
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
if (
|
|
1139
|
+
const surface = ctx.surface.element.getBoundingClientRect()
|
|
1140
|
+
const { width: sceneW, height: sceneH } = ctx.scene
|
|
1141
|
+
let left = Number.parseFloat(tip.style.left)
|
|
1142
|
+
let top = Number.parseFloat(tip.style.top)
|
|
1143
|
+
if (!card || Number.isNaN(left) || Number.isNaN(top) || !surface.width || !sceneW || !sceneH)
|
|
1144
|
+
return
|
|
1145
|
+
const w0 = tip.offsetWidth
|
|
1146
|
+
const toClient = (p: { x: number; y: number }): { x: number; y: number } => ({
|
|
1147
|
+
x: surface.left + (p.x / sceneW) * surface.width,
|
|
1148
|
+
y: surface.top + (p.y / sceneH) * surface.height,
|
|
1149
|
+
})
|
|
1150
|
+
if (mode === 'strip') {
|
|
1151
|
+
// A `BandStrip`: sideways only, as before; the engine's width and vertical placement stand.
|
|
1152
|
+
if (w0 + 2 * TIP_CARD_AIR <= card.width)
|
|
1153
|
+
left = Math.min(Math.max(left, card.left + TIP_CARD_AIR), card.right - w0 - TIP_CARD_AIR)
|
|
1154
|
+
if (Math.abs(left - Number.parseFloat(tip.style.left)) > 0.5) tip.style.left = `${left}px`
|
|
1155
|
+
return
|
|
1156
|
+
}
|
|
1157
|
+
const roomy = card.width >= TIP_CARD_ROOMY
|
|
1158
|
+
// A wider tooltip is centred again on its anchor at that width, and held inside the card.
|
|
1159
|
+
const grown = roomy || slot
|
|
1160
|
+
if (!grown) {
|
|
1161
|
+
// A card that shrank under a wider tooltip: the engine's own width and cap again.
|
|
1162
|
+
if (tip.dataset['basaltMax'] !== undefined) {
|
|
1163
|
+
tip.style.maxWidth = tip.dataset['basaltMax']
|
|
1164
|
+
tip.style.removeProperty('width')
|
|
1165
|
+
delete tip.dataset['basaltMax']
|
|
1166
|
+
}
|
|
1167
|
+
} else {
|
|
1168
|
+
tip.dataset['basaltMax'] ??= tip.style.maxWidth
|
|
1169
|
+
// Grows to its content, capped at the card's width; the engine's shrink-to-fit would narrow it
|
|
1170
|
+
// to the room right of its `left`.
|
|
1171
|
+
const cap = `${card.width - 2 * TIP_CARD_AIR}px`
|
|
1172
|
+
if (tip.style.maxWidth !== cap) tip.style.maxWidth = cap
|
|
1173
|
+
if (tip.style.width !== 'max-content') tip.style.width = 'max-content'
|
|
1174
|
+
}
|
|
1175
|
+
const [w, h] = [tip.offsetWidth, tip.offsetHeight]
|
|
1176
|
+
const anchor = toClient(ctx.anchor)
|
|
1177
|
+
const gap = Math.max(0, Number.isFinite(ctx.offset) ? (ctx.offset ?? 10) : 10)
|
|
1178
|
+
const minTop = card.top + TIP_CARD_AIR
|
|
1179
|
+
const maxTop = card.bottom - TIP_CARD_AIR - h
|
|
1180
|
+
const side = tip.dataset['placement']
|
|
1181
|
+
if (slot) {
|
|
1182
|
+
// Flush above the plot, in the reserved line: no flip, never pushed down over the bars.
|
|
1183
|
+
top = Math.max(anchor.y - gap - h, card.top)
|
|
1184
|
+
left = anchor.x - w / 2
|
|
1185
|
+
} else if (side === 'top' || side === 'bottom') {
|
|
1186
|
+
const above = anchor.y - gap - h
|
|
1187
|
+
const below = anchor.y + gap
|
|
1188
|
+
const order = side === 'top' ? [above, below] : [below, above]
|
|
1189
|
+
const fits = order.findIndex((t) => t >= minTop && t <= maxTop)
|
|
1190
|
+
top = order[Math.max(fits, 0)] ?? top
|
|
1191
|
+
if (fits > 0) tip.dataset['placement'] = side === 'top' ? 'bottom' : 'top'
|
|
1192
|
+
if (roomy) left = anchor.x - w / 2
|
|
1193
|
+
}
|
|
1194
|
+
if (!slot) top = maxTop < minTop ? minTop : Math.min(Math.max(top, minTop), maxTop)
|
|
1195
|
+
if (grown || w + 2 * TIP_CARD_AIR <= card.width)
|
|
1196
|
+
left = Math.min(
|
|
1197
|
+
Math.max(left, card.left + TIP_CARD_AIR),
|
|
1198
|
+
Math.max(card.right - w - TIP_CARD_AIR, card.left + TIP_CARD_AIR),
|
|
1199
|
+
)
|
|
1200
|
+
if (Math.abs(left - Number.parseFloat(tip.style.left)) > 0.5) tip.style.left = `${left}px`
|
|
1201
|
+
if (Math.abs(top - Number.parseFloat(tip.style.top)) > 0.5) tip.style.top = `${top}px`
|
|
1097
1202
|
}
|
|
1098
1203
|
|
|
1099
1204
|
function tooltipSpec<T, X extends ChartX>(opts: {
|
|
@@ -1108,19 +1213,25 @@ function tooltipSpec<T, X extends ChartX>(opts: {
|
|
|
1108
1213
|
}): NonNullable<CartesianDefinition['tooltip']> {
|
|
1109
1214
|
const { input, axes, visible, keys, visibleKeys, formatX, dayOnly } = opts
|
|
1110
1215
|
const hidden = input.series.filter((s) => !visibleKeys.has(s.key))
|
|
1216
|
+
const slot = input.tooltipBelow === true && input.bare === true
|
|
1111
1217
|
const rowSeries = (input.reverseRows ? visible.toReversed() : visible).filter(
|
|
1112
1218
|
(s) => s.tooltip !== false,
|
|
1113
1219
|
)
|
|
1114
1220
|
return {
|
|
1115
1221
|
use: quietWhileFollowing,
|
|
1116
1222
|
...followerOptions(input.tooltip, input.cursorGroup, input.chartId),
|
|
1117
|
-
portal: cardPortal,
|
|
1223
|
+
portal: cardPortal(slot ? 'slot' : input.tooltipBelow === true ? 'strip' : 'card'),
|
|
1118
1224
|
anchor: input.tooltipBelow
|
|
1119
|
-
?
|
|
1225
|
+
? slot
|
|
1226
|
+
? slotAnchor
|
|
1227
|
+
: stripAnchor
|
|
1120
1228
|
: visible.some((s) => s.mark === 'bar')
|
|
1121
1229
|
? barAnchor(rowSeries.length)
|
|
1122
1230
|
: 'group-center',
|
|
1123
|
-
...(input.tooltipBelow &&
|
|
1231
|
+
...(input.tooltipBelow &&
|
|
1232
|
+
(slot
|
|
1233
|
+
? { placement: SLOT_TOOLTIP_PLACEMENT, offset: 0 }
|
|
1234
|
+
: { placement: STRIP_TOOLTIP_PLACEMENT, offset: STRIP_TOOLTIP_OFFSET })),
|
|
1124
1235
|
content: (points: readonly ChartPoint[]) => {
|
|
1125
1236
|
const primary = points[0]
|
|
1126
1237
|
if (!primary) return { rows: [] }
|
package/src/charts/legend.tsx
CHANGED
|
@@ -38,7 +38,7 @@ export function legendEntries<T>(
|
|
|
38
38
|
groups: boolean,
|
|
39
39
|
): ChartSeries<T>[][] {
|
|
40
40
|
const entries = series.filter((s) => s.legend !== false)
|
|
41
|
-
// Series lead, zones and reference lines follow: the compact
|
|
41
|
+
// Series lead, zones and reference lines follow: the compact row wraps between entries and names them all.
|
|
42
42
|
if (!groups) return entries.length ? [entries.toSorted(byReferenceLast)] : []
|
|
43
43
|
return ROLE_ORDER.map((role) => entries.filter((s) => (s.role ?? 'series') === role)).filter(
|
|
44
44
|
(g) => g.length > 0,
|
|
@@ -200,99 +200,36 @@ export type ChartLegendProps<T> = {
|
|
|
200
200
|
hideAtWide?: boolean
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
const VISUALLY_HIDDEN = {
|
|
204
|
-
position: 'absolute',
|
|
205
|
-
width: '1px',
|
|
206
|
-
height: '1px',
|
|
207
|
-
overflow: 'hidden',
|
|
208
|
-
clipPath: 'inset(50%)',
|
|
209
|
-
whiteSpace: 'nowrap',
|
|
210
|
-
} as const
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* The number of lines the visible entries (and the chip) sit on. Bucketed by vertical centre, so the
|
|
214
|
-
* chip's own height (shorter than an entry's) does not read as another line.
|
|
215
|
-
*/
|
|
216
|
-
function lineCount(row: HTMLElement, more: HTMLElement): number {
|
|
217
|
-
const shown = [...row.querySelectorAll<HTMLElement>('[data-legend-key]')].filter(
|
|
218
|
-
(it) => !it.hasAttribute('data-legend-folded'),
|
|
219
|
-
)
|
|
220
|
-
if (!more.hidden) shown.push(more)
|
|
221
|
-
const centres = shown
|
|
222
|
-
.map((it) => {
|
|
223
|
-
const r = it.getBoundingClientRect()
|
|
224
|
-
return r.top + r.height / 2
|
|
225
|
-
})
|
|
226
|
-
.toSorted((x, y) => x - y)
|
|
227
|
-
// A new line starts where the centre jumps by more than half an entry's height.
|
|
228
|
-
return centres.filter((c, i) => i === 0 || c - (centres[i - 1] ?? c) > 6).length
|
|
229
|
-
}
|
|
230
|
-
|
|
231
203
|
/**
|
|
232
|
-
* The compact legend names every
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* The entries are in display order: series, then zones / references.
|
|
236
|
-
* Measured on the node, never in React state, so a hover or a resize does not re-render.
|
|
204
|
+
* The compact legend names every entry at every width: nothing folds, the row wraps between entries
|
|
205
|
+
* (series, then overlays, then references). A label that ellipsizes carries its full name in
|
|
206
|
+
* `title`; one that fits carries none. Measured on the node, never in React state.
|
|
237
207
|
*/
|
|
238
|
-
function
|
|
239
|
-
const items = [...row.querySelectorAll<HTMLElement>('[data-legend-key]')]
|
|
240
|
-
const more = row.querySelector<HTMLElement>('[data-legend-more]')
|
|
241
|
-
for (const it of items) it.removeAttribute('data-legend-folded')
|
|
242
|
-
more?.setAttribute('hidden', '')
|
|
243
|
-
// A label that ellipsizes carries its full name in `title`; one that fits carries none.
|
|
208
|
+
function titleClipped(row: HTMLElement): void {
|
|
244
209
|
for (const label of row.querySelectorAll<HTMLElement>('[data-legend-label]')) {
|
|
245
210
|
if (label.scrollWidth > label.clientWidth + 0.5) label.title = label.textContent ?? ''
|
|
246
211
|
else label.removeAttribute('title')
|
|
247
212
|
}
|
|
248
|
-
// `--bc-fold` is the stylesheet's compact flag (chart-legend.module.css); wide keeps every entry.
|
|
249
|
-
if (more === null || getComputedStyle(row).getPropertyValue('--bc-fold').trim() !== '1') return
|
|
250
|
-
const references = items.filter((it) => it.dataset['legendRole'] === 'reference')
|
|
251
|
-
// The series-only line count is the floor: folding a reference that frees no line only hides a name.
|
|
252
|
-
for (const r of references) r.setAttribute('data-legend-folded', '')
|
|
253
|
-
const floor = lineCount(row, more)
|
|
254
|
-
for (const r of references) r.removeAttribute('data-legend-folded')
|
|
255
|
-
const folded: HTMLElement[] = []
|
|
256
|
-
while (folded.length < references.length && lineCount(row, more) > floor) {
|
|
257
|
-
const last = references[references.length - 1 - folded.length]
|
|
258
|
-
if (last === undefined) break
|
|
259
|
-
last.setAttribute('data-legend-folded', '')
|
|
260
|
-
folded.push(last)
|
|
261
|
-
more.removeAttribute('hidden')
|
|
262
|
-
const names = folded.map((f) => f.textContent).join(', ')
|
|
263
|
-
more.textContent = `+${folded.length}`
|
|
264
|
-
more.title = names
|
|
265
|
-
// The folded labels, visually hidden: assistive tech reads `+2: Caution, Danger`.
|
|
266
|
-
const hiddenNames = document.createElement('span')
|
|
267
|
-
hiddenNames.textContent = `: ${names}`
|
|
268
|
-
Object.assign(hiddenNames.style, VISUALLY_HIDDEN)
|
|
269
|
-
more.append(hiddenNames)
|
|
270
|
-
}
|
|
271
|
-
// Folding freed no line (the chip's own width cost it): show every name instead.
|
|
272
|
-
if (folded.length > 0 && lineCount(row, more) > floor) {
|
|
273
|
-
for (const f of folded) f.removeAttribute('data-legend-folded')
|
|
274
|
-
more.setAttribute('hidden', '')
|
|
275
|
-
}
|
|
276
213
|
}
|
|
277
214
|
|
|
278
215
|
export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null {
|
|
279
216
|
const rowRef = useRef<HTMLFieldSetElement>(null)
|
|
280
217
|
const auto = props.mode === 'auto'
|
|
281
|
-
// Re-
|
|
218
|
+
// Re-title when the entries change; a resize re-titles through the observer.
|
|
282
219
|
const signature = props.series.map((s) => s.key).join('|')
|
|
283
|
-
// `signature` is intentionally an extra dep: the
|
|
220
|
+
// `signature` is intentionally an extra dep: the titles must re-run when the entries change.
|
|
284
221
|
useLayoutEffect(() => {
|
|
285
|
-
if (auto && rowRef.current !== null)
|
|
222
|
+
if (auto && rowRef.current !== null) titleClipped(rowRef.current)
|
|
286
223
|
}, [auto, signature])
|
|
287
224
|
// Same: `signature` re-subscribes the observer to the re-rendered row.
|
|
288
225
|
useEffect(() => {
|
|
289
226
|
const row = rowRef.current
|
|
290
227
|
if (!auto || row === null || typeof ResizeObserver === 'undefined') return
|
|
291
|
-
const observer = new ResizeObserver(() =>
|
|
228
|
+
const observer = new ResizeObserver(() => titleClipped(row))
|
|
292
229
|
observer.observe(row)
|
|
293
230
|
// A web font landing changes the entry widths without resizing the row.
|
|
294
231
|
let live = true
|
|
295
|
-
void document.fonts?.ready?.then(() => live &&
|
|
232
|
+
void document.fonts?.ready?.then(() => live && titleClipped(row))
|
|
296
233
|
return () => {
|
|
297
234
|
live = false
|
|
298
235
|
observer.disconnect()
|
|
@@ -359,7 +296,6 @@ export function ChartLegend<T>(props: ChartLegendProps<T>): ReactElement | null
|
|
|
359
296
|
)
|
|
360
297
|
}),
|
|
361
298
|
])}
|
|
362
|
-
{auto && <span data-legend-more="" hidden style={noteStyle} />}
|
|
363
299
|
</fieldset>
|
|
364
300
|
)
|
|
365
301
|
}
|