import { Text, XStack } from '@hanzo/gui'; import React from 'react'; import { Skeleton } from './skeleton'; import { Tooltip } from './tooltip'; // Inline truncation tooltip (replaces explorer's TruncatedTextTooltip) function TruncatedTextTooltip({ label, children }: { readonly label: React.ReactNode; readonly children: React.ReactNode }) { return ( { children } ); } type ColorPalette = 'gray' | 'green' | 'red' | 'purple' | 'orange' | 'blue' | 'yellow' | 'teal' | 'cyan' | 'pink' | 'purple_alt' | 'blue_alt' | 'bright_gray' | 'bright_green' | 'bright_red' | 'bright_blue' | 'bright_yellow' | 'bright_teal' | 'bright_cyan' | 'bright_orange' | 'bright_purple' | 'bright_pink'; type BadgeSize = 'sm' | 'md' | 'lg'; /** The chip itself — layout only. `color`/`fontSize` are Text props, not View props. */ const SIZE: Record> = { sm: { padding: 4, height: 18, minHeight: 18 }, md: { paddingHorizontal: 4, paddingVertical: 2, minHeight: 24 }, lg: { paddingHorizontal: 8, paddingVertical: 4, minHeight: 28 }, }; /** The label inside it — same three sizes, the typography half. */ const SIZE_TEXT: Record = { sm: { fontSize: 12, fontWeight: '500' }, md: { fontSize: 14, fontWeight: '500' }, lg: { fontSize: 14, fontWeight: '600' }, }; const COLOR_PALETTE: Record = { gray: { bg: 'var(--color-badge-gray-bg)', fg: 'var(--color-badge-gray-fg)' }, green: { bg: 'var(--color-badge-green-bg)', fg: 'var(--color-badge-green-fg)' }, red: { bg: 'var(--color-badge-red-bg)', fg: 'var(--color-badge-red-fg)' }, purple: { bg: 'var(--color-badge-purple-bg)', fg: 'var(--color-badge-purple-fg)' }, orange: { bg: 'var(--color-badge-orange-bg)', fg: 'var(--color-badge-orange-fg)' }, blue: { bg: 'var(--color-badge-blue-bg)', fg: 'var(--color-badge-blue-fg)' }, yellow: { bg: 'var(--color-badge-yellow-bg)', fg: 'var(--color-badge-yellow-fg)' }, teal: { bg: 'var(--color-badge-teal-bg)', fg: 'var(--color-badge-teal-fg)' }, cyan: { bg: 'var(--color-badge-cyan-bg)', fg: 'var(--color-badge-cyan-fg)' }, pink: { bg: 'var(--color-badge-pink-bg)', fg: 'var(--color-badge-pink-fg)' }, purple_alt: { bg: 'var(--color-badge-purple-alt-bg)', fg: 'var(--color-badge-purple-alt-fg)' }, blue_alt: { bg: 'var(--color-badge-blue-alt-bg)', fg: 'var(--color-badge-blue-alt-fg)' }, bright_gray: { bg: 'var(--color-badge-bright-gray-bg)', fg: 'var(--color-badge-bright-gray-fg)' }, bright_green: { bg: 'var(--color-badge-bright-green-bg)', fg: 'var(--color-badge-bright-green-fg)' }, bright_red: { bg: 'var(--color-badge-bright-red-bg)', fg: 'var(--color-badge-bright-red-fg)' }, bright_blue: { bg: 'var(--color-badge-bright-blue-bg)', fg: 'var(--color-badge-bright-blue-fg)' }, bright_yellow: { bg: 'var(--color-badge-bright-yellow-bg)', fg: 'var(--color-badge-bright-yellow-fg)' }, bright_teal: { bg: 'var(--color-badge-bright-teal-bg)', fg: 'var(--color-badge-bright-teal-fg)' }, bright_cyan: { bg: 'var(--color-badge-bright-cyan-bg)', fg: 'var(--color-badge-bright-cyan-fg)' }, bright_orange: { bg: 'var(--color-badge-bright-orange-bg)', fg: 'var(--color-badge-bright-orange-fg)' }, bright_purple: { bg: 'var(--color-badge-bright-purple-bg)', fg: 'var(--color-badge-bright-purple-fg)' }, bright_pink: { bg: 'var(--color-badge-bright-pink-bg)', fg: 'var(--color-badge-bright-pink-fg)' }, }; export type BadgeVariant = 'subtle' | 'solid' | 'outline'; /** * How much of the palette the chip shows. * * `subtle` tints the ground and keeps the palette's own foreground; `solid` * inverts that, so the chip is the colour and the label reads on it; `outline` * spends the colour on a border and leaves the ground alone, for a chip that has * to sit on a surface it must not compete with. */ const TONE: Record Record> = { subtle: (p) => ({ backgroundColor: p.bg, color: p.fg, borderWidth: 0 }), solid: (p) => ({ backgroundColor: p.fg, color: p.bg, borderWidth: 0 }), outline: (p) => ({ backgroundColor: 'transparent', color: p.fg, borderWidth: 1, borderColor: p.fg, }), }; export interface BadgeProps extends Omit, 'color'> { readonly loading?: boolean; readonly startElement?: React.ReactNode; readonly endElement?: React.ReactNode; readonly truncated?: boolean; readonly colorPalette?: ColorPalette; readonly variant?: BadgeVariant; readonly size?: 'sm' | 'md' | 'lg'; } export const Badge = React.forwardRef( function Badge(props, ref) { const { loading, startElement, children, truncated = false, endElement, variant = 'subtle', size = 'md', colorPalette = 'gray', className, style: badgeStyle, ...rest } = props; const palette = COLOR_PALETTE[colorPalette]; const tone = TONE[variant](palette); const child = children ? ( // `ellipsis` is gui Text's own built-in variant for single-line // truncation — it expands to the same overflow/text-overflow/ // white-space/max-width rule Tailwind's `text-ellipsis` did, but // through the component's own variants (guaranteed to compile), // rather than naming each longhand as a separate style prop. { children } ) : null; const childrenElement = truncated ? ( { child } ) : child; const badgeElement = ( } display="inline-flex" alignItems="center" alignSelf="flex-start" borderRadius={ 2 } gap={ 4 } maxWidth="100%" userSelect="text" { ...SIZE[size] } { ...tone } className={ className } style={ badgeStyle } { ...(rest as object) } > { startElement } { childrenElement } { endElement } ); if (loading) { return ( { badgeElement } ); } return badgeElement; }, );