sonda/web/src/components/StatBars.tsx
tgiachi 9bf6fd0039 style: square the corners of the web app
The base radius goes from 8 px to 2 px, which squares buttons, fields, dialogs,
covers and cards at once. The things that were made round on purpose are square
now too: the play button, the switch and its knob, the chip of a friend, the bars
of the statistics, the tab marker, the dot of a request, and the volume slider
(a thin track and a bar for a knob). The rings of a missing cover stay round:
they are the drawing of Sonda.
2026-11-16 11:27:18 +01:00

95 lines
3.9 KiB
TypeScript

import type { ReactNode } from 'react'
import { cn } from '@/lib/utils'
export interface Bar {
key: string
/** What is shown at the side of the bar. */
label: ReactNode
/** From 0 to 1: how much of the track the bar fills. */
value: number
/** What is shown after the bar. */
text: string
}
/**
* Horizontal bars, one a line: a name, a bar as long as its share, and the figure. They are a list, so a screen reader
* reads each line (name and figure) and skips the drawing.
*/
export function ShareBars({ label, bars, wide = false }: { label: string; bars: Bar[]; wide?: boolean }) {
return (
<ul aria-label={label} className="flex flex-col gap-2">
{bars.map((b) => (
<li key={b.key} className={cn('grid items-center gap-3', wide ? 'grid-cols-[minmax(9rem,20rem)_1fr_3.5rem]' : 'grid-cols-[minmax(6rem,10rem)_1fr_3.5rem]')}>
<span className="min-w-0 truncate font-medium first-letter:uppercase">{b.label}</span>
<span aria-hidden="true" className="h-2 overflow-hidden rounded-sm bg-card">
<span className="block h-full rounded-sm bg-primary" style={{ width: `${Math.min(1, Math.max(0, b.value)) * 100}%` }} />
</span>
<span className="text-right text-sm tabular-nums text-muted-foreground">{b.text}</span>
</li>
))}
</ul>
)
}
export interface Column {
key: string
/** What is shown under the column; the others can be left out when there are many. */
label: string
value: number
/** What a screen reader reads for the column, which has no text of its own. */
name: string
}
const HEIGHT = 96
/** Vertical columns as high as their value, the highest filling the track: a year by year, or hour by hour chart. */
export function Columns({ label, columns, className }: { label: string; columns: Column[]; className?: string }) {
const biggest = Math.max(1, ...columns.map((c) => c.value))
return (
<ul aria-label={label} className={cn('flex items-end gap-1', className)}>
{columns.map((c) => (
<li key={c.key} aria-label={c.name} title={c.name} className="flex min-w-0 flex-1 flex-col items-center justify-end gap-1">
<span aria-hidden="true" className="w-full rounded-sm bg-primary" style={{ height: `${Math.max(c.value > 0 ? 3 : 0, (c.value / biggest) * HEIGHT)}px` }} />
<span aria-hidden="true" className="h-4 text-[13px] leading-4 tabular-nums text-muted-foreground">
{c.label}
</span>
</li>
))}
</ul>
)
}
export interface StackedColumn {
key: string
/** What is shown under the column. */
label: string
/** What a screen reader reads for the column. */
name: string
/** The parts of the whole, from the bottom up, each from 0 to 1. A column of no parts is empty. */
parts: number[]
}
const STACK_HEIGHT = 144
/**
* Columns that are each the whole, cut in parts: how the share of each genre changed from one year to the next. The
* colours are given by index, the same as in the legend; a part that is 0 is not drawn.
*/
export function StackedColumns({ label, columns, colors }: { label: string; columns: StackedColumn[]; colors: string[] }) {
return (
<ul aria-label={label} className="flex items-end gap-1">
{columns.map((c) => (
<li key={c.key} aria-label={c.name} title={c.name} className="flex min-w-0 flex-1 flex-col items-center justify-end gap-1">
<span aria-hidden="true" className="flex w-full flex-col-reverse overflow-hidden rounded-sm bg-card" style={{ height: `${STACK_HEIGHT}px` }}>
{c.parts.map((p, i) =>
p > 0 ? <span key={i} className="block w-full border-t border-background first:border-t-0" style={{ height: `${p * 100}%`, background: colors[i] }} /> : null,
)}
</span>
<span aria-hidden="true" className="h-4 text-[13px] leading-4 tabular-nums text-muted-foreground">
{c.label}
</span>
</li>
))}
</ul>
)
}