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

175 lines
5.8 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 from 'react';
import * as Tabler from '@tabler/icons-react';
/*
* Icons.
*
* Semantic UI shipped an icon font; we use Tabler's bundled SVG components, which
* are tree-shaken at build time and need nothing from the network.
*
* Icons never name a color. They stroke with `currentColor`, inheriting from the
* button or text that wraps them, so status color and every theme come for free.
*/
/** 16px inline (default), 20px emphasis, 24px page-level. */
export type IconSize = 'small' | 'medium' | 'large';
const sizes: Record<IconSize, number> = {small: 16, medium: 20, large: 24};
/**
* Semantic icon name -> Tabler component name.
*
* Covers every name the app renders today so call sites can migrate without also
* choosing new icons. New code should import the Tabler component directly and
* pass it as `component`; this map exists for the migration, not forever.
*/
const semanticNames: Record<string, keyof typeof Tabler> = {
'add': 'IconPlus',
'archive': 'IconArchive',
'arrow alternate circle up': 'IconCircleArrowUp',
'arrow down': 'IconArrowDown',
'arrow right': 'IconArrowRight',
'arrow up': 'IconArrowUp',
'balance scale': 'IconScale',
'book': 'IconBook',
'bug': 'IconBug',
'calculator': 'IconCalculator',
'calendar': 'IconCalendar',
'charset': 'IconLanguage',
'check': 'IconCheck',
'check circle': 'IconCircleCheck',
'checkmark': 'IconCheck',
'chevron left': 'IconChevronLeft',
'chevron right': 'IconChevronRight',
'chrome': 'IconBrandChrome',
'circle': 'IconCircle',
'circle notch': 'IconLoader2',
'circle notched': 'IconLoader2',
'close': 'IconX',
'columns': 'IconColumns',
'copy': 'IconCopy',
'cube': 'IconCube',
'directory': 'IconFolder',
'disk': 'IconDatabase',
'download': 'IconDownload',
'dropdown': 'IconChevronDown',
'edit': 'IconEdit',
'exclamation': 'IconExclamationMark',
'exclamation triangle': 'IconAlertTriangle',
'expand arrows alternate': 'IconArrowsMaximize',
'external': 'IconExternalLink',
'eye': 'IconEye',
'eye slash': 'IconEyeOff',
'file': 'IconFile',
'file alternate': 'IconFileDescription',
'file alternate outline': 'IconFileDescription',
'file code': 'IconFileCode',
'file pdf outline': 'IconFileTypePdf',
'file text': 'IconFileText',
'file video': 'IconFileSmile',
'film': 'IconMovie',
'filter': 'IconFilter',
'firefox': 'IconBrandFirefox',
'folder': 'IconFolder',
'folder open': 'IconFolderOpen',
'folder open outline': 'IconFolderOpen',
'folder outline': 'IconFolder',
'globe': 'IconWorld',
'hand point right': 'IconHandFinger',
'hdd': 'IconDeviceSdCard',
'heart': 'IconHeart',
'heartbeat': 'IconHeartRateMonitor',
'history': 'IconHistory',
'image': 'IconPhoto',
'lightning': 'IconBolt',
'linkify': 'IconLink',
'list': 'IconList',
'lock': 'IconLock',
'map outline': 'IconMap',
'microchip': 'IconCpu',
'minus': 'IconMinus',
'play': 'IconPlayerPlay',
'plug': 'IconPlug',
'plus': 'IconPlus',
'print': 'IconPrinter',
'puzzle piece': 'IconPuzzle',
'qrcode': 'IconQrcode',
'question': 'IconHelp',
'redo': 'IconArrowForwardUp',
'refresh': 'IconRefresh',
'rss': 'IconRss',
'save': 'IconDeviceFloppy',
'search': 'IconSearch',
'server': 'IconServer',
'settings': 'IconSettings',
'share': 'IconShare',
'shield': 'IconShield',
'spinner': 'IconLoader2',
'star': 'IconStarFilled',
'star outline': 'IconStar',
'stop': 'IconPlayerStop',
'sync': 'IconRefresh',
'tachometer alternate': 'IconGauge',
'tag': 'IconTag',
'terminal': 'IconTerminal2',
'th': 'IconLayoutGrid',
'thumbs up': 'IconThumbUp',
'trash': 'IconTrash',
'undo': 'IconArrowBackUp',
'unlock': 'IconLockOpen',
'upload': 'IconUpload',
'usb': 'IconUsb',
'volume up': 'IconVolume',
'warning': 'IconAlertTriangle',
'warning circle': 'IconAlertCircle',
'warning sign': 'IconAlertTriangle',
'wifi': 'IconWifi',
'wrench': 'IconTool',
'x': 'IconX',
};
export interface IconProps extends Omit<React.SVGProps<SVGSVGElement>, 'name' | 'ref'> {
/** A Semantic UI icon name, for migrated call sites. */
name?: string;
/** A Tabler component, for new code. */
component?: React.ComponentType<any>;
size?: IconSize | number;
/** Rotate continuously; for spinners and in-progress states. */
loading?: boolean;
/** Accessible name. Omit for decorative icons, which are hidden instead. */
label?: string;
}
/** Resolve a Semantic icon name to its Tabler component, or undefined if unmapped. */
export const resolveIconName = (name: string): React.ComponentType<any> | undefined => {
const taberName = semanticNames[name.trim().toLowerCase()];
return taberName ? (Tabler[taberName] as React.ComponentType<any>) : undefined;
}
export function Icon({name, component, size = 'small', loading, label, ...props}: IconProps) {
let Component = component;
if (!Component && name) {
Component = resolveIconName(name);
if (!Component) {
// Loud on purpose: a silently missing icon leaves a hole in the interface.
console.error(`No icon for Semantic name "${name}"; pass a Tabler component instead.`);
Component = Tabler.IconHelpCircle;
}
}
if (!Component) {
console.error('Icon requires `name` or `component`.');
return null;
}
const pixels = typeof size === 'number' ? size : sizes[size];
return <Component
size={pixels}
stroke={2}
className={loading ? 'wrolpi-icon-spin' : undefined}
aria-hidden={label ? undefined : 'true'}
aria-label={label}
role={label ? 'img' : undefined}
{...props}
/>
}