import React from 'react'; import {Loader as MLoader, Skeleton} from '@mantine/core'; import {Icon} from './Icon'; import {pxToRem} from './scale'; import {RoleName, 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 /* * `error` is the original spelling and stays: 26 call sites use it, and renaming those * is app-level churn for no behaviour change. `danger` is the same thing under the * name the token uses, and is what new code should write. */ export type MessageKind = 'info' | 'success' | 'warning' | 'error' | 'danger'; /** The role each kind means. Never a hue -- see the note on roles in tokens.css. */ const messageRoles: Record = { info: 'info', success: 'success', warning: 'warning', error: 'danger', danger: 'danger', }; 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; /** 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
{icon &&
{typeof icon === 'string' ? : }
}
{title &&
{title}
} {children &&
{children}
}
{onDismiss && }
} // -------------------------------------------------------------------- Labels export interface LabelProps { color?: SemanticColorName | RoleName | 'black' | 'white'; icon?: string | React.ComponentType; /** * 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; 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 {icon && (typeof icon === 'string' ? : )} {children} } // ------------------------------------------------------------------ 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 | RoleName; /** 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
{(showPercent || label) && {label ?? `${Math.round(clamped)}%`} }
} // -------------------------------------------------------------------- Status export type StatusKind = 'complete' | 'active' | 'pending' | 'failed'; /* * Status was the one component already doing this correctly: it had per-theme overrides * in ui.css because night has no hues to distinguish four states with. Those are gone -- * the roles carry the brightness ramp now, so this table is the whole of it and every * theme is served by the same rule. */ const statusRoles: Record = { complete: 'success', active: 'info', pending: 'neutral', failed: 'danger', }; const statusIcons: Record = { 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 {!plain && } {children} } // ------------------------------------------------------------------- Loaders export interface LoaderProps { size?: number | 'xs' | 'sm' | 'md' | 'lg' | 'xl'; label?: string; } export function Loader({size = 'sm', label}: LoaderProps) { return } 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 ``, which 19 call sites * used with their own wrapper div. */ export function Loading({children, size = 'sm', padding = '2em'}: LoadingProps) { // `padding` is the caller's, so it stays inline; the rest is in ui.css so it scales. return
{children &&
{children}
}
} /** Placeholder for content that has not arrived. Replaces Semantic's Placeholder. */ export function Placeholder({lines = 3, height = 12}: {lines?: number; height?: number}) { return
{Array.from({length: lines}, (_, index) => )}
} export {Skeleton};