wrolpi/app/src/components/ui/Surfaces.tsx
Roland 0cca56a250 ui: Statistic and StatisticGroup, and the four defects they had
These three exports had no test of their own.  Statistic had already been
caught out once -- it relied on Semantic's `Statistic.Group` for its
spacing, so it broke silently when that was uninstalled -- and they are
on the Status, Dashboard, Apps, Docs, Videos, Archive and Channels pages.

Four defects, each with a test that goes red without the fix:

* StatisticGroup ruled between columns only, with a `border-right` on
  every cell but the last.  `auto-fit` wraps, so the seven statistics on
  the Docs page became four rows on a phone with no rule between them,
  and the cell ending each earlier row drew its border straight onto the
  group's outer one, making that edge 2px on the right and 1px on the
  left.  The hairlines are now a 1px grid gap filled by an outline on
  each cell, which rules both axes however the tracks fall.

  Ruling the gaps with a border-coloured background behind the grid --
  which is where this went first -- has the same reach but paints every
  track no cell landed in: an odd count left the last slot a solid grey
  block.  An outline is drawn only where a cell is.  Both of these are
  guarded, because the second was introduced while fixing the first.

* A statistic that renders nothing left a padded cell and a stray
  hairline mid-row.  FanRPMStatistic returns null on a device with no fan
  connector, which is most of them, so this was the Status page's normal
  appearance.  The group cannot render a child to find out, so the cell
  is still emitted and `:empty` takes it out of the grid -- which is why
  the cells carry no inline style for that rule to have to outrank.

* Statistic destructured five props and dropped the rest.  LoadStatistic
  and the four Status wrappers all end in `{...props}`, so anything
  passed through a wrapper vanished with no error anywhere.

* VideosStatistics passed `style={{margin: '2em'}}` to every statistic,
  Semantic-era spacing that the grid now provides.  It beat the group's
  own rule, being inline, and made that one page's statistics sit
  differently from the six others.

CardGroup was the only component in the library with no class of its own,
which left nothing to assert against -- `container.firstChild` in jsdom
is Mantine's injected <style> element.  It has one now, and takes
className/style/rest like its siblings.

The gallery grows a seventh statistic, so the group wraps at a normal
window width where the row hairlines can be seen, and a second group of
coloured readings as Status renders them.

Geometry, hairlines and per-theme token resolution are in ui-layout.cy.js
rather than jest: jsdom returns zeros from getBoundingClientRect and
never resolves a var(), so it cannot judge any of it.
2026-07-30 19:23:04 -06:00

184 lines
7.3 KiB
TypeScript

import React from 'react';
import {Card as MCard, Tabs as MTabs, Accordion as MAccordion, Breadcrumbs} from '@mantine/core';
import {Icon} from './Icon';
/*
* Surfaces and structure: headers, panels, cards, statistics, tabs, accordions,
* breadcrumbs. Elevation is expressed with borders and background steps —
* never a shadow (shadows are zeroed in the Mantine bridge).
*/
// ------------------------------------------------------------------- Headers
export type HeaderLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5';
export interface HeaderProps extends Omit<React.HTMLAttributes<HTMLHeadingElement>, 'color'> {
/** Heading level. Also the visual size — pick it for the outline, not the look. */
as?: HeaderLevel;
/** Semantic icon name or a Tabler component, shown before the text. */
icon?: string | React.ComponentType<any>;
/** Secondary line below, in muted text. */
subheader?: React.ReactNode;
/** Hairline rule underneath, separating the section from what follows. */
dividing?: boolean;
/**
* A token colour name (`green`, `red`, ...) for a heading that carries meaning —
* "3 items to add" against "2 items to remove". Call sites kept reaching for an
* inline style to do this, which is how a hex ends up in the markup.
*/
color?: string;
}
/**
* A section heading. Named and shaped like Semantic's Header (`as`, `icon`,
* `subheader`, `dividing`) so the app's 47 call sites migrate by import.
*
* Sizes come from ui.css, keyed on the level, rather than each call site
* choosing — that is what keeps the type scale a scale.
*/
export function Header({
as = 'h3', icon, subheader, dividing, color, className, style, children, ...props
}: HeaderProps) {
const Tag = as;
return <div className={['wrolpi-header', dividing ? 'wrolpi-header-dividing' : '', className]
.filter(Boolean).join(' ')}>
<Tag
className={`wrolpi-header-text wrolpi-header-${as}`}
style={color ? {color: `var(--${color})`, ...style} : style}
{...props}
>
{icon && (typeof icon === 'string'
? <Icon name={icon} size='medium'/>
: <Icon component={icon} size='medium'/>)}
<span>{children}</span>
</Tag>
{subheader && <div className='wrolpi-header-sub'>{subheader}</div>}
</div>
}
// -------------------------------------------------------------------- Panels
export interface PanelProps extends React.HTMLAttributes<HTMLDivElement> {
/** A danger zone: dashed red border, for destructive groupings. */
danger?: boolean;
}
/** A bordered surface. Replaces Semantic's Segment. */
export function Panel({danger, className, children, ...props}: PanelProps) {
return <div
className={['wrolpi-panel', danger ? 'wrolpi-panel-danger' : '', className]
.filter(Boolean).join(' ')}
{...props}
>
{children}
</div>
}
// ---------------------------------------------------------------- Statistics
export interface StatisticProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'color'> {
/** The number. Rendered as given, so `0` shows as a zero rather than nothing. */
value: React.ReactNode;
label: React.ReactNode;
/** A token colour name, for a reading that carries meaning: load, temperature, IO wait. */
color?: string;
}
/**
* One figure and its label. The rest of the props reach the outer element, because
* `LoadStatistic` and the four Status wrappers all end in `{...props}` and used to have
* everything past the named five silently dropped.
*/
export function Statistic({value, label, color, className, ...props}: StatisticProps) {
return <div className={['wrolpi-statistic', className].filter(Boolean).join(' ')} {...props}>
<div className='wrolpi-statistic-value' style={{color: color ? `var(--${color})` : undefined}}>
{value}
</div>
<div className='wrolpi-statistic-label'>{label}</div>
</div>
}
/**
* A row of statistics separated by hairlines, sharing one outer border. The cells carry
* no inline style of their own so that `.wrolpi-statistic-cell:empty` can hide the ones
* whose statistic rendered nothing — Status omits the fan reading on the devices, most of
* them, with no fan connector.
*/
export function StatisticGroup({children, className, ...props}: React.HTMLAttributes<HTMLDivElement>) {
return <div className={['wrolpi-statistic-group', className].filter(Boolean).join(' ')} {...props}>
{React.Children.toArray(children).map((child, index) =>
<div key={index} className='wrolpi-statistic-cell'>{child}</div>)}
</div>
}
// --------------------------------------------------------------------- Cards
export interface CardProps {
/** Image or poster area above the body. */
media?: React.ReactNode;
title?: React.ReactNode;
meta?: React.ReactNode;
children?: React.ReactNode;
onClick?: React.MouseEventHandler<HTMLDivElement>;
/**
* A token colour name, drawn as an accent along the card's top edge. File cards use
* it to carry the mimetype's colour, so a grid of results is scannable by kind before
* any text is read. It remaps per theme, so night gets a red accent rather than a
* dozen hues.
*/
color?: string;
className?: string;
}
export function Card({media, title, meta, children, onClick, color, className}: CardProps) {
return <MCard
withBorder
padding={0}
className={className}
onClick={onClick}
style={{
background: 'var(--panel)',
borderColor: 'var(--border)',
cursor: onClick ? 'pointer' : undefined,
display: 'flex',
flexDirection: 'column',
// An accent edge rather than a tinted surface: the design rules keep surfaces
// flat, and a 3px edge survives night mode without becoming a bright patch.
// Along the bottom, where it does not compete with the poster's top edge.
borderBottom: color ? `3px solid var(--${color})` : undefined,
}}
>
{media && <div style={{borderBottom: '1px solid var(--border)'}}>{media}</div>}
<div style={{padding: '10px 12px 12px', display: 'flex', flexDirection: 'column', gap: 4, flex: 1}}>
{title && <div style={{fontSize: 13, fontWeight: 600, lineHeight: 1.35}}>{title}</div>}
{children}
{meta && <div style={{fontSize: 12, color: 'var(--muted)', marginTop: 'auto'}}>{meta}</div>}
</div>
</MCard>
}
export interface CardGroupProps extends React.HTMLAttributes<HTMLDivElement> {
/**
* The narrowest a card may be laid out. `auto-fill` rather than `auto-fit`, so a group
* of two cards in a wide column keeps them card-sized instead of stretching each to half
* the page.
*/
minWidth?: number;
}
export function CardGroup({children, minWidth = 200, className, style, ...props}: CardGroupProps) {
return <div
className={['wrolpi-card-group', className].filter(Boolean).join(' ')}
style={{
display: 'grid',
gridTemplateColumns: `repeat(auto-fill, minmax(${minWidth}px, 1fr))`,
gap: 14,
...style,
}}
{...props}
>
{children}
</div>
}
export {MTabs as Tabs, MAccordion as Accordion, Breadcrumbs};