mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 15:06:19 -05:00
Each of these was reported by an agent working around it rather than patched in one file, which is why all three are fixed once, for every call site. Button accepted an `href` and dropped it. Mantine renders an anchor only when it also gets `component='a'`; Semantic spelled that `as='a'`. So a migrated call site that kept just the href produced something that looks like a download link and navigates nowhere -- silently, with no warning. Button and IconButton now infer the anchor from the href. The media filter ran twice on nested media. A `.media` wrapper filters its subtree as one unit -- the audio player needs that, because its control glyphs are SVG and the element-type rule does not reach them -- but the poster image inside also matched that rule. Two passes of a luminance-to-red matrix is not one: the second takes 0.2126 of an already-red pixel, so the poster came out markedly darker than everything around it. Leaf media inside a filtered wrapper is now left alone. Progress had no indeterminate state. Semantic's `indicating` animated a stripe when the size was unknown; without it an upload that has not reported yet sits at 0% and reads as stalled -- and `aria-valuenow="0"` tells a screen reader the same wrong thing. It now animates, reports no value, and falls back to a static fill under prefers-reduced-motion. Also: night and amber now override an inline `--label-color`. A call site may legitimately need many distinguishable colours -- the Flasher's chip badges use a twelve-colour palette no four tokens can express -- but on a night-adapted screen a green or blue chip badge defeats the entire mode. The labels lose their distinctness there, which is the right trade.
126 lines
4.8 KiB
TypeScript
126 lines
4.8 KiB
TypeScript
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;
|
|
component?: any;
|
|
}
|
|
|
|
const renderIcon = (icon?: string | React.ComponentType<any>) => {
|
|
if (!icon) return undefined;
|
|
return typeof icon === 'string' ? <Icon name={icon}/> : <Icon component={icon}/>;
|
|
}
|
|
|
|
/*
|
|
* Semantic's size names, mapped onto Mantine's scale.
|
|
*
|
|
* Unmigrated call sites still pass `size='tiny'`, and Mantine silently ignores a
|
|
* size it does not know — the button renders at the default size and nobody sees
|
|
* a warning. Translating here keeps those call sites looking right until they
|
|
* migrate, and costs nothing once they have.
|
|
*/
|
|
const semanticSizes: Record<string, string> = {
|
|
mini: 'xs',
|
|
tiny: 'xs',
|
|
small: 'sm',
|
|
medium: 'md',
|
|
large: 'lg',
|
|
big: 'lg',
|
|
huge: 'xl',
|
|
massive: 'xl',
|
|
};
|
|
|
|
// Generic so it passes each component's own size type straight through; only a
|
|
// string that Semantic knew about is rewritten.
|
|
export const resolveSize = <T, >(size: T): T =>
|
|
typeof size === 'string' ? ((semanticSizes[size] ?? size) as T) : size;
|
|
|
|
export const Button = forwardRef<HTMLButtonElement, ButtonProps>((
|
|
{role, icon, iconAfter, className, ...props}, ref
|
|
) => {
|
|
const fromRole = role ? roleProps[role] : undefined;
|
|
return <MButton
|
|
ref={ref}
|
|
/*
|
|
* An `href` makes this an anchor. Mantine needs `component='a'` to render one, and
|
|
* without it the href is accepted and silently dropped — the control looks like a
|
|
* link and navigates nowhere. Semantic used `as='a'` for this, so every migrated
|
|
* call site that carried `href` alone was quietly broken.
|
|
*/
|
|
component={props.component ?? (props.href ? 'a' : undefined)}
|
|
// 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}
|
|
size={resolveSize(props.size)}
|
|
/>
|
|
});
|
|
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';
|
|
href?: string;
|
|
target?: string;
|
|
component?: any;
|
|
}
|
|
|
|
/** 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}
|
|
// See Button: an href without `component='a'` renders a non-navigating button.
|
|
component={props.component ?? (props.href ? 'a' : undefined)}
|
|
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}
|
|
size={resolveSize(props.size)}
|
|
>
|
|
{renderIcon(icon)}
|
|
</ActionIcon>
|
|
});
|
|
IconButton.displayName = 'IconButton';
|
|
|
|
export {ButtonGroup} from '@mantine/core';
|