diff --git a/app/src/components/ThemeSamplePage.js b/app/src/components/ThemeSamplePage.js index bde6a425..02006c91 100644 --- a/app/src/components/ThemeSamplePage.js +++ b/app/src/components/ThemeSamplePage.js @@ -151,12 +151,28 @@ export function ThemeSamplePage() {
+ {/* + * Seven, deliberately: enough to wrap on a narrow window, which is where the row + * hairlines are worth looking at. The group rules both axes from --border, so the + * grid holds together however the tracks fall. + */} + + + + + {/* Status colours a reading that carries meaning. Check these in night, where + there is no hue to spend and the value has to read by brightness. */} + + + + +
diff --git a/app/src/components/Videos.js b/app/src/components/Videos.js index d9fa7592..df2484a9 100644 --- a/app/src/components/Videos.js +++ b/app/src/components/Videos.js @@ -795,7 +795,7 @@ export function VideosStatistics() { {names.map( ({key, label}) => - + )} diff --git a/app/src/components/ui/Surfaces.tsx b/app/src/components/ui/Surfaces.tsx index 1951aa99..9321b2a0 100644 --- a/app/src/components/ui/Surfaces.tsx +++ b/app/src/components/ui/Surfaces.tsx @@ -76,16 +76,21 @@ export function Panel({danger, className, children, ...props}: PanelProps) { // ---------------------------------------------------------------- Statistics -export interface StatisticProps { +export interface StatisticProps extends Omit, '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; - className?: string; - style?: React.CSSProperties; } -export function Statistic({value, label, color, className, style}: StatisticProps) { - return
+/** + * 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
{value}
@@ -93,25 +98,16 @@ export function Statistic({value, label, color, className, style}: StatisticProp
} -/** A row of statistics separated by hairlines, sharing one outer border. */ -export function StatisticGroup({children}: {children: React.ReactNode}) { - const items = React.Children.toArray(children); - return
- {items.map((child, index) =>
- {child} -
)} +/** + * 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) { + return
+ {React.Children.toArray(children).map((child, index) => +
{child}
)}
} @@ -161,12 +157,26 @@ export function Card({media, title, meta, children, onClick, color, className}: } -export function CardGroup({children, minWidth = 200}: {children: React.ReactNode; minWidth?: number}) { - return
+export interface CardGroupProps extends React.HTMLAttributes { + /** + * 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
{children}
} diff --git a/app/src/components/ui/ui-layout.cy.js b/app/src/components/ui/ui-layout.cy.js index 9d3f2836..39c26bc0 100644 --- a/app/src/components/ui/ui-layout.cy.js +++ b/app/src/components/ui/ui-layout.cy.js @@ -1,5 +1,5 @@ import React from 'react'; -import {ActionInput, Button, PathInput, TextInput} from './index'; +import {ActionInput, Button, PathInput, Statistic, StatisticGroup, TextInput} from './index'; import {themeNames} from '../../themes/names'; /* @@ -133,6 +133,171 @@ describe('ActionInput', () => { }); }); +describe('StatisticGroup rules every row it ends up with', () => { + /* + * The group wraps: `auto-fit` with a 150px minimum gives seven statistics one row on a + * desktop and four on a phone, and how many rows there are is decided by the viewport at + * paint time. Only a browser can say -- jsdom has no tracks, no rows and no widths -- so + * the row-separator claims live here. + */ + + // Narrow enough that six statistics cannot fit on one row at a 150px minimum. + const inNarrowColumn = (children) =>
{children}
; + + const sixStatistics = + + + + + + + ; + + const rowTops = (cells) => [...new Set( + [...cells].map((cell) => Math.round(cell.getBoundingClientRect().top)) + )].sort((a, b) => a - b); + + it('wraps into rows at this width, so the rest of these tests mean something', () => { + cy.mountUI(inNarrowColumn(sixStatistics)); + + cy.get('.wrolpi-statistic-cell').should(($cells) => + expect(rowTops($cells).length, 'rows the group wrapped into').to.be.greaterThan(1)); + }); + + it('draws a hairline between wrapped rows', () => { + /* + * This is the defect: the old group put a `border-right` on every cell but the last, + * which rules between columns and says nothing about rows. Wrapped rows sat flush + * against each other, so the Docs page's seven statistics ran together into one block + * on a phone. A row boundary must be a visible 1px of border colour. + */ + cy.mountUI(inNarrowColumn(sixStatistics)); + + cy.get('.wrolpi-statistic-cell').should(($cells) => { + const cells = [...$cells].map((cell) => cell.getBoundingClientRect()); + const tops = rowTops($cells); + + for (let row = 1; row < tops.length; row++) { + const previousRowBottom = Math.max(...cells + .filter((rect) => Math.round(rect.top) === tops[row - 1]) + .map((rect) => rect.bottom)); + const separation = tops[row] - previousRowBottom; + expect(separation, `hairline above row ${row + 1}`).to.be.closeTo(1, 0.6); + } + }); + }); + + it('draws a hairline between columns too', () => { + cy.mountUI(inNarrowColumn(sixStatistics)); + + cy.get('.wrolpi-statistic-cell').should(($cells) => { + const cells = [...$cells].map((cell) => cell.getBoundingClientRect()); + const firstRowTop = rowTops($cells)[0]; + const firstRow = cells + .filter((rect) => Math.round(rect.top) === firstRowTop) + .sort((a, b) => a.left - b.left); + + expect(firstRow.length, 'columns in the first row').to.be.greaterThan(1); + for (let column = 1; column < firstRow.length; column++) { + expect(firstRow[column].left - firstRow[column - 1].right, 'hairline between columns') + .to.be.closeTo(1, 0.6); + } + }); + }); + + it('does not double the outer border where a row ends', () => { + /* + * The old code suppressed `border-right` on the last child only, so the cell ending + * every earlier row -- there is one per wrap -- drew its own hairline directly onto the + * group's outer border, giving a 2px edge on the right and a 1px edge on the left. + * Now no cell has a border at all and the single outer one is the edge. + */ + cy.mountUI(inNarrowColumn(sixStatistics)); + + cy.get('.wrolpi-statistic-cell').each(($cell) => + expect(getComputedStyle($cell[0]).borderRightWidth, 'cell draws no border of its own') + .to.equal('0px')); + cy.get('.wrolpi-statistic-group').should(($group) => + expect(getComputedStyle($group[0]).borderRightWidth, 'one outer hairline').to.equal('1px')); + }); + + it('leaves no painted block in a track no statistic landed in', () => { + /* + * An odd count leaves the last row short: seven statistics in two columns fill the + * eighth slot with nothing. Ruling the gaps with a border-coloured background behind + * the grid -- the obvious way to do it -- painted that empty slot a solid block of + * border grey, which read as a broken cell. The hairlines are outlines on the cells + * instead, so a track with no cell in it is just surface. + */ + cy.mountUI(inNarrowColumn( + + + + + + + + )); + + cy.get('.wrolpi-statistic-cell').should(($cells) => { + // An odd number of cells over an even number of columns, or this proves nothing. + expect($cells.length % 2, 'a short last row').to.equal(1); + }); + cy.get('.wrolpi-statistic-group').should(($group) => { + const group = getComputedStyle($group[0]); + expect(group.backgroundColor, 'the empty track shows surface, not hairline') + .to.not.equal(group.borderRightColor); + }); + }); + + it('hides the cell of a statistic that rendered nothing', () => { + /* + * Status omits the fan reading on a device with no fan connector -- most of them -- + * by returning null from FanRPMStatistic. The group still emits its cell, because it + * cannot render a child to find out, so `:empty` has to take the cell out of the grid. + * Left in, it was a blank padded panel and a stray hairline in the middle of the row. + */ + const NoFanFitted = () => null; + cy.mountUI( + + + + ); + + cy.get('.wrolpi-statistic-cell').should('have.length', 3); + cy.get('.wrolpi-statistic-cell').filter(':empty').should('not.be.visible'); + cy.get('.wrolpi-statistic-cell').filter(':empty').should(($cell) => + expect($cell[0].getBoundingClientRect().width, 'the empty cell takes no track').to.equal(0)); + }); + + themeNames.forEach((theme) => { + it(`takes its hairline and surface from ${theme}'s tokens`, () => { + // Drawn from --border and --panel rather than a fixed grey, which is what lets the + // group survive night mode without putting a non-red pixel on the screen. + cy.mountUI(inNarrowColumn(sixStatistics), {theme}); + + cy.get('.wrolpi-statistic-group').then(($group) => { + const group = getComputedStyle($group[0]); + + cy.get('.wrolpi-statistic-cell').first().should(($cell) => { + const cell = getComputedStyle($cell[0]); + + // The inner hairlines are the cells' outlines and the outer one is the + // group's border; they meet at every edge, so a theme that resolved them + // to different values would show a two-tone frame. + expect(cell.outlineColor, 'inner hairline matches the outer') + .to.equal(group.borderRightColor); + // And the hairline has to be visible against the surface it sits on -- + // both resolved from tokens, so a theme that forgot one shows up here. + expect(cell.outlineColor, 'hairline is not the surface colour') + .to.not.equal(cell.backgroundColor); + expect(cell.backgroundColor, 'cell has a surface').to.not.equal('rgba(0, 0, 0, 0)'); + }); + }); + }); + }); +}); + describe('theme tokens actually resolve', () => { themeNames.forEach((theme) => { it(`paints text and borders from ${theme}'s table`, () => { diff --git a/app/src/components/ui/ui.css b/app/src/components/ui/ui.css index 3db1a4c0..4983221c 100644 --- a/app/src/components/ui/ui.css +++ b/app/src/components/ui/ui.css @@ -852,9 +852,10 @@ html[data-theme="amber"] .wrolpi-card-tag { /* ------------------------------------------------------------- Statistics */ /* * A statistic carries its own spacing rather than relying on a parent to supply - * it: unmigrated pages still drop these inside Semantic's `Statistic.Group`, - * whose CSS only spaces elements carrying Semantic's own classes, and without - * this the labels run together into one unreadable line. + * it, so that one dropped on a page by itself still sits clear of its + * neighbours. Semantic's `Statistic.Group` only spaced elements carrying + * Semantic's own classes, so without this the labels ran together into one + * unreadable line. */ .wrolpi-statistic { @@ -863,6 +864,44 @@ html[data-theme="amber"] .wrolpi-card-tag { margin: 0 26px 10px 0; } +/* + * The group's hairlines are an outline on each cell, drawn into a 1px grid gap. + * + * Not a border per cell: a border can only separate the siblings the author can + * count -- the old code suppressed `border-right` on the last child -- and + * `auto-fit` wraps, so the seven statistics on the Docs page became four rows on + * a phone with no rule between them, while the cell ending each earlier row drew + * its border straight onto the outer one. + * + * Nor a border-coloured background behind the gaps, which rules both axes but + * also paints every track no cell landed in: seven statistics in two columns + * left the eighth slot a solid grey block. An outline is drawn outside the + * cell, so it appears only where a cell actually is. Adjacent outlines meet in + * the same 1px of gap, and the outer ones land on the group's own border, all in + * the same colour, so every rule reads as a single hairline. + */ +.wrolpi-statistic-group { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); + gap: 1px; + background-color: var(--panel); + border: 1px solid var(--border); +} + +.wrolpi-statistic-cell { + background-color: var(--panel); + outline: 1px solid var(--border); + padding: 14px 16px; +} + +/* + * A statistic that renders nothing -- Status omits the fan reading on a device + * with no fan connector -- must not leave a padded cell and a hairline behind. + */ +.wrolpi-statistic-cell:empty { + display: none; +} + /* Inside our own group the grid does the spacing, so the intrinsic margin goes. */ .wrolpi-statistic-cell .wrolpi-statistic { display: block; diff --git a/app/src/components/ui/ui.test.js b/app/src/components/ui/ui.test.js index 034c5842..bf58ae9f 100644 --- a/app/src/components/ui/ui.test.js +++ b/app/src/components/ui/ui.test.js @@ -12,6 +12,7 @@ import { ActionInput, Button, Card, + CardGroup, Confirm, Header, Icon, @@ -28,6 +29,8 @@ import { PathInput, Progress, resolveIconName, + Statistic, + StatisticGroup, Status, Table, ThemePicker, @@ -645,6 +648,114 @@ describe('Card', () => { }); }); +describe('CardGroup', () => { + it('renders every card it is given', () => { + renderUI( + + + + ); + + expect(screen.getByText('One')).toBeInTheDocument(); + expect(screen.getByText('Two')).toBeInTheDocument(); + expect(screen.getByText('Three')).toBeInTheDocument(); + }); + + it('lays cards out on a track no narrower than minWidth', () => { + // The default suits file results; a group of wide cards raises it. If the value were + // dropped the grid would fall back to one column, which is how a card wall becomes a + // list on a desktop. + const {container} = renderUI(); + + expect(container.querySelector('.wrolpi-card-group')) + .toHaveStyle({gridTemplateColumns: 'repeat(auto-fill, minmax(320px, 1fr))'}); + }); +}); + +describe('Statistic', () => { + it('shows the value and its label', () => { + renderUI(); + + expect(screen.getByText('87.4 GiB')).toBeInTheDocument(); + expect(screen.getByText('Free space')).toBeInTheDocument(); + }); + + it('colours the value from a token, not a hex', () => { + // Status turns load, temperature and IO wait red or orange. A hex here would not + // remap in night mode, putting a green or orange pixel on a red-only screen. + const {container} = renderUI(); + + expect(container.querySelector('.wrolpi-statistic-value')).toHaveStyle({color: 'var(--red)'}); + }); + + it('leaves the value in body text when no colour is given', () => { + const {container} = renderUI(); + + expect(container.querySelector('.wrolpi-statistic-value').style.color).toBe(''); + }); + + it('renders a zero rather than nothing', () => { + // `pending_downloads` is 0 on an idle WROLPi, and a falsy check would blank the cell. + renderUI(); + + expect(screen.getByText('0')).toBeInTheDocument(); + }); + + it('forwards the props its wrappers spread onto it', () => { + // `LoadStatistic` and the four Status wrappers (`CPUTemperatureStatistic`, + // `FanRPMStatistic`, `IOWaitStatistic`, `UptimeStatistic`) all end in `{...props}`. + // Statistic destructured a fixed five and dropped the rest on the floor, so anything + // a caller passed through a wrapper vanished with no error anywhere. + renderUI(); + + expect(screen.getByTitle('Reported by the fan connector')).toBeInTheDocument(); + }); +}); + +describe('StatisticGroup', () => { + it('gives each statistic its own cell', () => { + const {container} = renderUI( + + + ); + + expect(container.querySelectorAll('.wrolpi-statistic-cell')).toHaveLength(2); + }); + + it('leaves a cell empty when its statistic renders nothing', () => { + // `FanRPMStatistic` returns null on a device with no fan connector, which is most of + // them. The group cannot know that in advance, so the cell is still emitted and + // `.wrolpi-statistic-cell:empty` hides it -- which only works while the cell has no + // padding or background of its own inline for that rule to have to outrank. + const Nothing = () => null; + const {container} = renderUI( + + + ); + + const cells = container.querySelectorAll('.wrolpi-statistic-cell'); + expect(cells[1].childNodes).toHaveLength(0); + expect(cells[1].getAttribute('style')).toBeNull(); + }); + + it('draws its hairlines with the grid, not a border per cell', () => { + // A per-cell border can only separate siblings the author can count -- the old code + // suppressed `borderRight` on the last child -- and `auto-fit` wraps. Seven + // statistics on a phone became four rows with no rule between them, while the cell + // ending each earlier row drew its border straight onto the outer one. A 1px gap + // over a border-coloured background rules both axes however the tracks fall. + const {container} = renderUI( + + + ); + + for (const cell of container.querySelectorAll('.wrolpi-statistic-cell')) { + expect(cell.style.borderRight).toBe(''); + } + expect(container.querySelector('.wrolpi-statistic-group')).toBeInTheDocument(); + }); +}); + describe('Panel', () => { it('marks danger zones', () => { const {container} = renderUI(Destructive actions);