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 = { 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 { role?: ButtonRole; /** Semantic icon name or a Tabler component, rendered before the label. */ icon?: string | React.ComponentType; iconAfter?: string | React.ComponentType; onClick?: React.MouseEventHandler; type?: 'button' | 'submit' | 'reset'; href?: string; target?: string; component?: any; } const renderIcon = (icon?: string | React.ComponentType) => { if (!icon) return undefined; return typeof icon === 'string' ? : ; } /* * 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 = { 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 = (size: T): T => typeof size === 'string' ? ((semanticSizes[size] ?? size) as T) : size; export const Button = forwardRef(( {role, icon, iconAfter, className, ...props}, ref ) => { const fromRole = role ? roleProps[role] : undefined; return }); Button.displayName = 'Button'; export interface IconButtonProps extends Omit { role?: ButtonRole; icon: string | React.ComponentType; /** Required: an icon-only button has no visible name. */ label: string; onClick?: React.MouseEventHandler; type?: 'button' | 'submit' | 'reset'; href?: string; target?: string; component?: any; } /** A square, icon-only button. */ export const IconButton = forwardRef(( {role, icon, label, className, ...props}, ref ) => { const fromRole = role ? roleProps[role] : undefined; return {renderIcon(icon)} }); IconButton.displayName = 'IconButton'; export {ButtonGroup} from '@mantine/core';