Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Everything else here is convention, and the import-alias rule in particular has
- Prefer short-circuit (`&&`) over a ternary when the else branch is `null` or `undefined`, especially in React rendering. Do: `{isActive && <Badge />}`. Don't: `{isActive ? <Badge /> : null}`. Guard the condition so it is a real boolean (`!!count && ...`), never a bare number that could render `0`.
- Prefer optional chaining (`?.`). When optional chaining is used, ALWAYS pair it with nullish coalescing (`??`) to supply a fallback.
- Prefer a single configurable object parameter over multiple positional parameters so argument order doesn't matter. Don't: `doSomething(foo, bar, hello)`. Do: `doSomething({ foo, bar, hello })`.
- A comment that fits on one line uses `//`. A comment that spans more than one line uses one block, `/* ... */`, or `/** ... */` when it documents an export, a type or one of its fields. Never stack `//` lines into a paragraph, and never wrap a one-line comment in `/* */`.
- Exceptions: comments inside JSX stay `{/* ... */}`, since JSX has no `//`; a trailing comment after code on the same line is always `//`; and a `/** ... */` doc comment that fits on one line stays as it is, because editors only show `/** */` comments on hover.

## Accessibility

Expand Down
80 changes: 70 additions & 10 deletions apps/admin-portal/src/components/Chart/ChartLegend.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { classNames } from '@/components/Chart/classNames'
import { swatchClass, type ChartMark } from '@/components/Chart/ChartTooltip'
import styles from '@/components/Chart/chart.module.scss'

Expand All @@ -12,20 +13,79 @@ export type ChartLegendItem = {
readonly note?: string
}

/** Which series the reader left off the plot, and how to flip one. A chart
takes this whole or not at all, so a hidden series always comes with the
checkbox that brings it back. */
export type SeriesToggle = {
/** Keys of the series currently left off the plot. */
readonly hidden: readonly string[]
readonly onToggle: (key: string) => void
}

/** Lets the reader choose which series are drawn. Without it the legend is a
plain key. */
export type ChartLegendToggle = SeriesToggle & {
/** Names the group of checkboxes for a screen reader, such as "Hosts shown
on the CPU chart". */
readonly label: string
}

type ChartLegendProps = {
readonly items: readonly ChartLegendItem[]
readonly mark?: ChartMark
readonly toggle?: ChartLegendToggle
}

/** What each mark on the plot stands for, named in words beside its colour. */
export const ChartLegend = ({ items, mark = 'square' }: ChartLegendProps) => (
<ul className={styles.legend}>
{items.map((item) => (
<li key={item.key} className={styles.legendItem}>
<span className={swatchClass({ mark, tint: item.swatchClass })} aria-hidden="true" />
<span>{item.name}</span>
{item.note !== undefined && <span className={styles.legendNote}>{item.note}</span>}
</li>
))}
/** The swatch, name and note one entry shows, shared by both forms. */
const EntryBody = ({
item,
mark,
}: {
readonly item: ChartLegendItem
readonly mark: ChartMark
}) => (
<>
<span className={swatchClass({ mark, tint: item.swatchClass })} aria-hidden="true" />
<span>{item.name}</span>
{item.note !== undefined && <span className={styles.legendNote}>{item.note}</span>}
</>
)

/** What each mark on the plot stands for, named in words beside its colour.
*
* Given a toggle, every entry is also a checkbox that shows or hides its
* series. A native checkbox inside its label, so the whole entry is the click
* target, the space bar works, and the checked state is announced without any
* ARIA of our own. The box is tinted the series colour, and an unchecked entry
* also dims and strikes its name, so hidden is never told by colour alone.
*/
export const ChartLegend = ({ items, mark = 'square', toggle }: ChartLegendProps) => (
<ul
className={classNames([styles.legend, !!toggle && styles.legendToggles])}
aria-label={toggle?.label}
>
{items.map((item) => {
const hidden = toggle?.hidden.includes(item.key) ?? false
return (
<li key={item.key} className={styles.legendItem}>
{toggle ? (
<label
className={classNames([styles.legendToggle, item.swatchClass])}
data-hidden={hidden}
>
<input
type="checkbox"
className={styles.legendCheck}
checked={!hidden}
onChange={() => toggle.onToggle(item.key)}
/>
<EntryBody item={item} mark={mark} />
</label>
) : (
<EntryBody item={item} mark={mark} />
)}
</li>
)
})}
</ul>
)
9 changes: 4 additions & 5 deletions apps/admin-portal/src/components/Chart/ChartTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { classNames } from '@/components/Chart/classNames'
import styles from '@/components/Chart/chart.module.scss'

/** Which mark a series is drawn with, so its key in the tooltip and the legend
Expand Down Expand Up @@ -28,9 +29,7 @@ type ChartTooltipProps = {

/** One series' key: the colour it is drawn in, in the shape it is drawn as. */
export const swatchClass = ({ mark, tint }: { mark: ChartMark; tint?: string }): string =>
[styles.swatch, mark === 'line' ? styles.swatchLine : '', tint ?? '']
.filter((part) => part.length > 0)
.join(' ')
classNames([styles.swatch, mark === 'line' && styles.swatchLine, tint])

/** Every series' reading at the inspected point on a chart.
*
Expand All @@ -39,8 +38,8 @@ export const swatchClass = ({ mark, tint }: { mark: ChartMark; tint?: string }):
* chart's own formatter instead of raw.
*/
export const ChartTooltip = ({ title, rows, total, mark = 'square' }: ChartTooltipProps) => {
// A chart whose series carry no mark of their own gets no swatch column at
// all, rather than one blank indenting every value it holds.
/* A chart whose series carry no mark of their own gets no swatch column at
all, rather than one blank indenting every value it holds. */
const keyed = rows.some((row) => row.swatchClass !== undefined)
const rowClass = keyed ? styles.tooltipRow : `${styles.tooltipRow} ${styles.tooltipRowPlain}`

Expand Down
123 changes: 86 additions & 37 deletions apps/admin-portal/src/components/Chart/chart.module.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The chrome every chart in the admin portal wears: a recessive grid, one
// tooltip box, one legend row. Written once here because three copies of it
// is how two charts a page apart end up a token different from each other.
/* The chrome every chart in the admin portal wears: a recessive grid, one
tooltip box, one legend row. Written once here because three copies of it
is how two charts a page apart end up a token different from each other. */
.chart {
display: grid;
grid-template-columns: minmax(0, 1fr);
Expand All @@ -22,43 +22,43 @@
display: grid;
grid-template-columns: minmax(0, 1fr);

// The SVG is drawn at a measured pixel width, so between mount and the
// first measurement it may be wider than this box. Scrolling inside is the
// house rule for wide content: the mismatch must never widen the page.
/* The SVG is drawn at a measured pixel width, so between mount and the
first measurement it may be wider than this box. Scrolling inside is the
house rule for wide content: the mismatch must never widen the page. */
overflow-x: auto;
padding: var(--space-1) 0;
border: 1px solid var(--color-border);
border-radius: var(--radius-md);
background: var(--color-surface);
}

// Bars run to the edge of their own category, so a bar chart's box keeps its
// horizontal padding at zero. A line chart has a stroke that can sit right on
// the axis ceiling, and wants the same air all round.
/* Bars run to the edge of their own category, so a bar chart's box keeps its
horizontal padding at zero. A line chart has a stroke that can sit right on
the axis ceiling, and wants the same air all round. */
.plotWrapPadded {
padding: var(--space-2);
}

// Horizontal guides only, kept recessive: border-coloured hairlines sit
// behind the data ink instead of competing with it. Recharts stamps the
// class on every gridline element itself, so the stroke sits on the class
// directly, not on a descendant.
/* Horizontal guides only, kept recessive: border-coloured hairlines sit
behind the data ink instead of competing with it. Recharts stamps the
class on every gridline element itself, so the stroke sits on the class
directly, not on a descendant. */
.grid {
stroke: var(--color-border);
}

// Only the baseline lives inside the classed axis group; the tick text is
// hoisted into its own layer and classed per element via the tick prop.
/* Only the baseline lives inside the classed axis group; the tick text is
hoisted into its own layer and classed per element via the tick prop. */
.axis {
line {
stroke: var(--color-border);
}
}

// Each tick's text element, reached through the axes' tick prop. Sized to
// what the scale's axisWidth budgets for: at the page's body size a
// "200.0 MB/s" label overruns the axis margin and clips. Tabular figures
// keep times and rates the same width from tick to tick.
/* Each tick's text element, reached through the axes' tick prop. Sized to
what the scale's axisWidth budgets for: at the page's body size a
"200.0 MB/s" label overruns the axis margin and clips. Tabular figures
keep times and rates the same width from tick to tick. */
.tickLabel {
fill: var(--color-muted);
font-size: var(--font-size-xs);
Expand All @@ -71,17 +71,17 @@
stroke-width: 1;
}

// A bar chart's cursor: the hovered column lifts against a wash, so the
// reader sees which bar the tooltip belongs to.
/* A bar chart's cursor: the hovered column lifts against a wash, so the
reader sees which bar the tooltip belongs to. */
.hoverBand {
fill: var(--color-text);
fill-opacity: 0.06;
}

// Recharts measures this box and pins it beside the cursor, clamped to the
// plot, so it must stay in normal flow: positioned absolutely it collapses
// the measuring wrapper to zero size, the background never paints, and every
// word wraps onto its own line down the y axis.
/* Recharts measures this box and pins it beside the cursor, clamped to the
plot, so it must stay in normal flow: positioned absolutely it collapses
the measuring wrapper to zero size, the background never paints, and every
word wraps onto its own line down the y axis. */
.tooltip {
display: grid;
grid-template-columns: minmax(0, 1fr);
Expand All @@ -107,17 +107,17 @@
list-style: none;
}

// The value leads and the name follows: at the tooltip the reader has already
// chosen the moment and wants the number.
/* The value leads and the name follows: at the tooltip the reader has already
chosen the moment and wants the number. */
.tooltipRow {
display: grid;
grid-template-columns: auto auto minmax(0, 1fr);
align-items: center;
gap: var(--space-1);
}

// A row on a chart whose series carry no mark of their own, where the swatch
// column would only indent every value by a blank.
/* A row on a chart whose series carry no mark of their own, where the swatch
column would only indent every value by a blank. */
.tooltipRowPlain {
grid-template-columns: auto minmax(0, 1fr);
}
Expand Down Expand Up @@ -147,31 +147,80 @@
gap: var(--space-1);
}

/* A legend whose entries are checkboxes: a little more room between them, so
each entry is a comfortable target rather than a run of words. */
.legendToggles {
gap: var(--space-1) var(--space-2);
}

/* The whole entry is the click target. Padding instead of a bare word gives it
a hit area worth aiming at, and the focus ring wraps the entry rather than
the 1rem box, so a keyboard reader sees which series they are on. */
.legendToggle {
display: grid;
align-items: center;
grid-auto-flow: column;
gap: var(--space-1);
padding: var(--space-0) var(--space-1);
border-radius: var(--radius-sm);
cursor: pointer;

&:hover {
background: color-mix(in oklab, var(--color-text) 6%, transparent);
}

&:has(:focus-visible) {
outline: 2px solid var(--color-accent);
outline-offset: 2px;
}

/* Hidden is told three ways: the box is empty, the name dims, and the name
is struck through, so it never rests on colour alone. */
&[data-hidden='true'] {
color: var(--color-muted);
text-decoration: line-through;
}
}

/* Native, so it keeps its own semantics and keyboard handling; tinted the
series colour so the box doubles as the key. */
.legendCheck {
width: var(--space-2);
height: var(--space-2);
accent-color: var(--series-color, var(--color-accent));
cursor: pointer;

// the entry draws the ring, so the box does not draw a second one
&:focus-visible {
outline: none;
}
}

.legendNote {
color: var(--color-muted);
font-size: var(--font-size-xs);
}

// A filled square for a bar series, mirroring the mark it stands for. The
// series colour comes from whichever palette class rides alongside this one;
// identity is always also stated in words beside the swatch, never by colour
// alone.
/* A filled square for a bar series, mirroring the mark it stands for. The
series colour comes from whichever palette class rides alongside this one;
identity is always also stated in words beside the swatch, never by colour
alone. */
.swatch {
width: var(--space-2);
height: var(--space-2);
border-radius: 2px;
background: var(--series-color, var(--mark-color, var(--color-muted)));
}

// The same key for a line series: a short stroke rather than a block, so the
// mark in the legend is the mark on the plot.
/* The same key for a line series: a short stroke rather than a block, so the
mark in the legend is the mark on the plot. */
.swatchLine {
height: 2px;
border-radius: 1px;
}

// Holds the swatch column open on a row that stands for no series, such as a
// total.
/* Holds the swatch column open on a row that stands for no series, such as a
total. */
.swatchGap {
width: var(--space-2);
}
4 changes: 4 additions & 0 deletions apps/admin-portal/src/components/Chart/classNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** The class attribute for a set of parts, dropping the absent ones so no
stray space or "undefined" reaches the DOM. */
export const classNames = (parts: readonly (string | false | undefined)[]): string =>
parts.filter((part): part is string => !!part).join(' ')
10 changes: 7 additions & 3 deletions apps/admin-portal/src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type IconName =
| 'cpu'
| 'disk'
| 'expand'
| 'external'
| 'gauge'
| 'gpu'
| 'help'
Expand All @@ -23,9 +24,9 @@ export type IconName =
| 'users'
| 'warn'

// One 16-unit grid, strokes only, round caps and joins: the geometry every
// glyph shares, so the set reads as one hand whatever size it is shown at. The
// single filled mark (the disk's indicator dot) says so inline.
/* One 16-unit grid, strokes only, round caps and joins: the geometry every
glyph shares, so the set reads as one hand whatever size it is shown at. The
single filled mark (the disk's indicator dot) says so inline. */
const GLYPHS: Record<IconName, ReactElement> = {
box: (
<>
Expand Down Expand Up @@ -57,6 +58,9 @@ const GLYPHS: Record<IconName, ReactElement> = {
</>
),
expand: <path d="M2.5 6V2.5H6M13.5 10v3.5H10M2.5 2.5l4 4M13.5 13.5l-4-4" />,
external: (
<path d="M9.5 2.5h4v4M13.5 2.5l-6 6M11.5 9.5v3a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1h3" />
),
gauge: (
<>
<path d="M2 12.5a6 6 0 1 1 12 0" />
Expand Down
Loading
Loading