mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 01:16:18 -05:00
Four things, three cosmetic and one a real legibility bug. * Tags were square chips. They now have a pointed left edge and an eyelet, like a tag tied to a garment -- the shape Semantic gave them, and the reason a tag reads as a tag rather than as a small button. The point is a square rotated behind the left edge, sized so its diagonal equals the body height and its two edges meet the body at the corners. Only where something is actually a tag. `Label` carries the count badges in the Tags table too -- file, zim, channel and domain counts -- and a point on a number reads as an arrow, so the shape is behind a `tag` prop and `NameToTagLabel` opts in. * They were smaller than what they replaced: 11.5px text with 3px of padding, against Semantic's 12px and ~7px. Now 12.5px and 5px, which also keeps them a reasonable thing to tap. * Wrapped rows of tags sat on the row above, so they carry a bottom margin, plus a left margin for the point to occupy. * The text colour was unreadable in night and wrong in amber. Tags.js calculates black-or-light against the colour the user chose for each tag, and wrote the answer to `color` inline -- a declaration no stylesheet rule can outrank. Night turns a tag into an outline over a near-black page, so a near-white tag kept the black text calculated for its fill and was left legible only by its border: 1.1:1, measured. Amber replaces every fill with its own bright orange and was painting `#dddddd` on `#ff9500`, about 1.9:1, and a neutral grey in a theme whose whole point is that no other hue reaches the eye. The fix is to stop hard-coding `color` so the theme can decide: the calculated colour goes into `--label-text`, and night and amber override it alongside the fill they already override. There is no second contrast function to write, which is what this looked like it needed -- once a theme has discarded the user's colour there is nothing left to calculate against, and the answer is just the theme's own token. `.wrolpi-label-white` and `-black` moved to the same variable. Setting `color` there outranked the base rule, so amber was painting `--white` on `--text`: light orange on orange. `contrastingColor` was also picking the worse of its two options. It compared a brightness figure against a threshold, and the figure weighted the gamma-encoded bytes without linearising them, so Semantic's blue `#2185d0` got light text at 2.9:1 where dark text gives 5.3:1. Every mid-tone blue, teal and purple a user might pick sat in that band. It now measures the WCAG contrast ratio of both candidates and returns the better one, which needs no threshold at all. Tests ----- `Tags.test.js` is new, and covers the chip nothing covered: every other spec takes `NameToTagLabel` from `tagsContextFixture`, which stubs it as a function returning the name. That is how a tag could be unreadable without a single test noticing. Two of these were verified red against the inline `color`. `cy.contrastRatio()` measures text against the surface *actually* behind it, walking up to the first ancestor that paints something -- a transparent tag has no background of its own, so a naive check reads rgba(0,0,0,0) and concludes anything is legible. Twelve cases, three tag colours across four themes, at WCAG AA. Plus a test asserting the old inline-`color` version still measures below 1.5:1, so the check is provably able to fail. The gallery had label swatches in theme colours and no user-coloured tag, which is why none of this was visible in it. It has eleven now, spanning the range where the black-or-light decision flips.
213 lines
7.7 KiB
TypeScript
213 lines
7.7 KiB
TypeScript
import React from 'react';
|
|
import {Loader as MLoader, Skeleton} from '@mantine/core';
|
|
import {Icon} from './Icon';
|
|
import {SemanticColorName} from '../../themes/mantine';
|
|
|
|
/*
|
|
* Feedback: messages, labels, progress, status text, loaders.
|
|
*
|
|
* These carry the design rules that change form (not just color) between
|
|
* themes, so their markup is ours and their values come from tokens.
|
|
*/
|
|
|
|
// ------------------------------------------------------------------ Messages
|
|
|
|
export type MessageKind = 'info' | 'success' | 'warning' | 'error';
|
|
|
|
const messageColors: Record<MessageKind, string> = {
|
|
info: 'var(--blue)',
|
|
success: 'var(--green)',
|
|
warning: 'var(--amber)',
|
|
error: 'var(--red)',
|
|
};
|
|
|
|
export interface MessageProps {
|
|
kind?: MessageKind;
|
|
title?: React.ReactNode;
|
|
children?: React.ReactNode;
|
|
/** Semantic icon name or a Tabler component, shown to the left of the text. */
|
|
icon?: string | React.ComponentType<any>;
|
|
/** Renders a dismiss button. Omit for messages the user cannot clear. */
|
|
onDismiss?: () => void;
|
|
className?: string;
|
|
}
|
|
|
|
export function Message({kind = 'info', title, children, icon, onDismiss, className}: MessageProps) {
|
|
return <div
|
|
// Errors and warnings interrupt; info and success are announced politely.
|
|
role={kind === 'error' ? 'alert' : 'status'}
|
|
className={['wrolpi-message', kind === 'error' ? 'wrolpi-message-error' : '', className]
|
|
.filter(Boolean).join(' ')}
|
|
style={{['--message-color' as string]: messageColors[kind]}}
|
|
>
|
|
{icon && <div className='wrolpi-message-icon'>
|
|
{typeof icon === 'string' ? <Icon name={icon} size={20}/> : <Icon component={icon} size={20}/>}
|
|
</div>}
|
|
<div className='wrolpi-message-content'>
|
|
{title && <div className='wrolpi-message-title'>{title}</div>}
|
|
{children && <div className='wrolpi-message-body'>{children}</div>}
|
|
</div>
|
|
{onDismiss && <button
|
|
type='button'
|
|
className='wrolpi-message-dismiss'
|
|
aria-label='Dismiss'
|
|
onClick={onDismiss}
|
|
>
|
|
<Icon name='close' size={16}/>
|
|
</button>}
|
|
</div>
|
|
}
|
|
|
|
// -------------------------------------------------------------------- Labels
|
|
|
|
export interface LabelProps {
|
|
color?: SemanticColorName | 'black' | 'white';
|
|
icon?: string | React.ComponentType<any>;
|
|
/**
|
|
* Draw it as a physical tag — pointed left edge and an eyelet — rather than a plain
|
|
* chip. Reserved for things that actually are tags: the same component also carries
|
|
* the count badges in the Tags table, and a point on a number reads as an arrow.
|
|
*/
|
|
tag?: boolean;
|
|
children?: React.ReactNode;
|
|
onClick?: React.MouseEventHandler<HTMLSpanElement>;
|
|
className?: string;
|
|
}
|
|
|
|
/** A chip. Filled in light, dark and amber; an outline in night, where a filled
|
|
* patch would be a bright surface. */
|
|
export function Label({color = 'grey', icon, tag, children, onClick, className}: LabelProps) {
|
|
return <span
|
|
className={['wrolpi-label', `wrolpi-label-${color}`, tag ? 'wrolpi-tag' : '', className]
|
|
.filter(Boolean).join(' ')}
|
|
style={{['--label-color' as string]: `var(--${color})`, cursor: onClick ? 'pointer' : undefined}}
|
|
onClick={onClick}
|
|
>
|
|
{icon && (typeof icon === 'string' ? <Icon name={icon} size={14}/> : <Icon component={icon} size={14}/>)}
|
|
{children}
|
|
</span>
|
|
}
|
|
|
|
// ------------------------------------------------------------------ Progress
|
|
|
|
export interface ProgressProps {
|
|
/** 0-100. Values outside the range are clamped. */
|
|
percent?: number;
|
|
/**
|
|
* Work is happening but its size is unknown — an upload that has not reported yet.
|
|
* Animates instead of sitting at 0%, which otherwise reads as stalled.
|
|
*/
|
|
indeterminate?: boolean;
|
|
/** Show the percentage inside the bar. */
|
|
showPercent?: boolean;
|
|
color?: SemanticColorName;
|
|
/** Replaces the percentage with arbitrary text (e.g. "2.1 GB / 5.3 GB"). */
|
|
label?: React.ReactNode;
|
|
className?: string;
|
|
}
|
|
|
|
export function Progress({
|
|
percent = 0, showPercent = true, color = 'blue', label, indeterminate, className,
|
|
}: ProgressProps) {
|
|
const clamped = Math.min(100, Math.max(0, Number.isFinite(percent) ? percent : 0));
|
|
return <div
|
|
className={['wrolpi-progress', indeterminate ? 'wrolpi-progress-indeterminate' : '', className]
|
|
.filter(Boolean).join(' ')}
|
|
role='progressbar'
|
|
// An indeterminate bar reports no value, which is what tells assistive tech the
|
|
// amount is unknown rather than zero.
|
|
aria-valuenow={indeterminate ? undefined : Math.round(clamped)}
|
|
aria-valuemin={0}
|
|
aria-valuemax={100}
|
|
>
|
|
<div
|
|
className='wrolpi-progress-fill'
|
|
style={{
|
|
width: indeterminate ? undefined : `${clamped}%`,
|
|
['--progress-color' as string]: `var(--${color})`,
|
|
}}
|
|
/>
|
|
{(showPercent || label) && <span className='wrolpi-progress-text'>
|
|
{label ?? `${Math.round(clamped)}%`}
|
|
</span>}
|
|
</div>
|
|
}
|
|
|
|
// -------------------------------------------------------------------- Status
|
|
|
|
export type StatusKind = 'complete' | 'active' | 'pending' | 'failed';
|
|
|
|
const statusIcons: Record<StatusKind, string> = {
|
|
complete: 'check',
|
|
active: 'circle notch',
|
|
pending: 'circle',
|
|
failed: 'x',
|
|
};
|
|
|
|
export interface StatusProps {
|
|
kind: StatusKind;
|
|
children?: React.ReactNode;
|
|
/** Hide the leading icon. */
|
|
plain?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Status text. Encoded by hue in light and dark; night mode has no hue to
|
|
* spend, so brightness carries it (see ui.css). Callers name the state, never
|
|
* the color.
|
|
*/
|
|
export function Status({kind, children, plain}: StatusProps) {
|
|
return <span className={`wrolpi-status wrolpi-status-${kind}`}>
|
|
{!plain && <Icon name={statusIcons[kind]} size={14} loading={kind === 'active'}/>}
|
|
{children}
|
|
</span>
|
|
}
|
|
|
|
// ------------------------------------------------------------------- Loaders
|
|
|
|
export interface LoaderProps {
|
|
size?: number | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
label?: string;
|
|
}
|
|
|
|
export function Loader({size = 'sm', label}: LoaderProps) {
|
|
return <MLoader size={size} color='var(--blue)' aria-label={label ?? 'Loading'}/>
|
|
}
|
|
|
|
export interface LoadingProps {
|
|
/** What is being waited on: "Loading backups…". Also the accessible name. */
|
|
children?: React.ReactNode;
|
|
size?: LoaderProps['size'];
|
|
/** Vertical padding around the loader. */
|
|
padding?: number | string;
|
|
}
|
|
|
|
/**
|
|
* A centered loader with a caption, for a region that has nothing to show yet.
|
|
* Replaces Semantic's `<Loader active inline='centered'>`, which 19 call sites
|
|
* used with their own wrapper div.
|
|
*/
|
|
export function Loading({children, size = 'sm', padding = '2em'}: LoadingProps) {
|
|
return <div style={{
|
|
display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10, padding,
|
|
}}>
|
|
<Loader size={size} label={typeof children === 'string' ? children : undefined}/>
|
|
{children && <div style={{fontSize: 13, color: 'var(--muted)'}}>{children}</div>}
|
|
</div>
|
|
}
|
|
|
|
/** Placeholder for content that has not arrived. Replaces Semantic's Placeholder. */
|
|
export function Placeholder({lines = 3, height = 12}: {lines?: number; height?: number}) {
|
|
return <div style={{display: 'flex', flexDirection: 'column', gap: 8}}>
|
|
{Array.from({length: lines}, (_, index) => <Skeleton
|
|
key={index}
|
|
height={height}
|
|
// A ragged last line reads as text rather than a block.
|
|
width={index === lines - 1 ? '60%' : '100%'}
|
|
radius={0}
|
|
animate
|
|
/>)}
|
|
</div>
|
|
}
|
|
|
|
export {Skeleton};
|