wrolpi/app/src/components/ui/Button.tsx

88 lines
3.3 KiB
TypeScript
Raw Normal View History

ui: token-driven component library on Mantine Adds src/components/ui: buttons, messages, labels, progress, status, tables, form controls, panels, cards, statistics, modals, tooltips, tabs, and icons. Everything resolves its colors through theme tokens, so no component branches on the current theme, and Semantic UI keeps rendering the app meanwhile. Mantine is bridged rather than restyled: its own CSS variables are pointed at our tokens. Four details were not obvious, and each caused a visible defect in night mode before being fixed: - Structural colors must go in the resolver's light/dark blocks. Mantine emits its values under [data-mantine-color-scheme], which outranks the :root block that `variables` becomes, so its grays were winning. - The dark and gray palettes are mapped shade by shade to tokens. Mantine reads them directly for table borders, zebra stripes and input chrome; they can be neither flattened nor left alone. - Mantine writes --button-* as inline styles, which no stylesheet can outrank, so the dashed danger treatment sets real properties instead. - Switch colors are written onto inner elements, so they need scoped CSS rather than a variable on the root. Adds a --knob token for control handles, because Mantine's default thumb follows theme.white, which is near-black in the dark-based themes. An audit of every painted pixel in the /theme-sample gallery now finds no non-red channel in night mode. Icons come from @tabler/icons-react with the 102 Semantic names the app uses mapped to components; they stroke with currentColor, so they follow the theme without any rule of their own. Toasts keep the react-semantic-toasts-2 signature so those call sites migrate by changing an import. TypeScript 5 is required: Mantine's declarations use `const` type parameters, which 4.9 cannot parse (Mantine 7 is the same, so downgrading is no escape). That needs two npm overrides to keep clean installs working, both verified against a Docker build: react-scripts declares peerOptional typescript ^3||^4, and fork-ts-checker 6.5.2 assigns typescript.performance.mark, which TS 5 made getter-only - crashing the dev server's type checker on every rebuild. semantic-baseline.test.js ratchets the removal: it fails if a file not already listed imports Semantic UI. An ESLint rule would have printed 75 warnings per rebuild, and CI unsets CI= for its build step, so it would guard nothing there. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-29 19:22:03 -06:00
import React, {forwardRef} from 'react';
import {ActionIcon, ActionIconProps, Button as MButton, ButtonProps as MButtonProps} from '@mantine/core';
import {Icon} from './Icon';
/*
* Buttons.
*
* Roles carry meaning (Semantic UI heritage): blue = primary/download,
* green = save/confirm, red = destructive, amber = retry/warning, neutral
* outline = cancel. Prefer `role` over naming a color, so a theme that remaps
* a color keeps the meaning.
*/
export type ButtonRole = 'primary' | 'save' | 'retry' | 'danger' | 'cancel';
const roleProps: Record<ButtonRole, {color?: string; variant: string; className?: string}> = {
primary: {color: 'blue', variant: 'filled'},
save: {color: 'green', variant: 'filled'},
retry: {color: 'yellow', variant: 'filled'},
// Danger resolves differently per theme (filled in light/dark, dashed outline in
// night). The branching lives in ui.css, never in a call site.
danger: {color: 'red', variant: 'filled', className: 'wrolpi-button-danger'},
cancel: {variant: 'default'},
};
export interface ButtonProps extends Omit<MButtonProps, 'leftSection' | 'rightSection'> {
role?: ButtonRole;
/** Semantic icon name or a Tabler component, rendered before the label. */
icon?: string | React.ComponentType<any>;
iconAfter?: string | React.ComponentType<any>;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
type?: 'button' | 'submit' | 'reset';
href?: string;
target?: string;
}
const renderIcon = (icon?: string | React.ComponentType<any>) => {
if (!icon) return undefined;
return typeof icon === 'string' ? <Icon name={icon}/> : <Icon component={icon}/>;
}
export const Button = forwardRef<HTMLButtonElement, ButtonProps>((
{role, icon, iconAfter, className, ...props}, ref
) => {
const fromRole = role ? roleProps[role] : undefined;
return <MButton
ref={ref}
// An explicit color/variant still wins, so a call site can deviate when it must.
color={props.color ?? fromRole?.color}
variant={props.variant ?? fromRole?.variant}
className={[fromRole?.className, className].filter(Boolean).join(' ') || undefined}
leftSection={renderIcon(icon)}
rightSection={renderIcon(iconAfter)}
{...props}
/>
});
Button.displayName = 'Button';
export interface IconButtonProps extends Omit<ActionIconProps, 'children'> {
role?: ButtonRole;
icon: string | React.ComponentType<any>;
/** Required: an icon-only button has no visible name. */
label: string;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
type?: 'button' | 'submit' | 'reset';
}
/** A square, icon-only button. */
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>((
{role, icon, label, className, ...props}, ref
) => {
const fromRole = role ? roleProps[role] : undefined;
return <ActionIcon
ref={ref}
aria-label={label}
title={label}
color={props.color ?? fromRole?.color}
variant={props.variant ?? fromRole?.variant ?? 'default'}
className={[fromRole?.className, className].filter(Boolean).join(' ') || undefined}
{...props}
>
{renderIcon(icon)}
</ActionIcon>
});
IconButton.displayName = 'IconButton';
export {ButtonGroup} from '@mantine/core';