import { Text, XStack } from '@hanzo/gui'; import * as React from 'react'; import { CloseButton } from './close-button'; import { Skeleton } from './skeleton'; import { Tooltip } from './tooltip'; // Inline constants const nbsp = String.fromCharCode(160); // Inline truncation tooltip function TruncatedTextTooltip({ label, children }: { readonly label: React.ReactNode; readonly children: React.ReactNode }) { return ( { children } ); } type TagVariant = 'subtle' | 'clickable' | 'filter' | 'select'; type TagSize = 'sm' | 'md' | 'lg'; /** The chip — background, cursor, and the hover opacity `clickable`/`select` get. */ const TAG_VARIANT: Record }> = { subtle: { backgroundColor: 'var(--color-tag-subtle-bg)' }, clickable: { backgroundColor: 'var(--color-tag-clickable-bg)', cursor: 'pointer', hoverStyle: { opacity: 0.76 } }, filter: { backgroundColor: 'var(--color-tag-filter-bg)' }, select: { backgroundColor: 'var(--color-tag-select-bg)', cursor: 'pointer', hoverStyle: { opacity: 0.76 } }, }; /** The label inside it — same four variants, the colour half (`filter` has none). */ const TAG_VARIANT_FG: Record = { subtle: 'var(--color-tag-subtle-fg)', clickable: 'var(--color-tag-clickable-fg)', filter: undefined, select: 'var(--color-tag-select-fg)', }; const TAG_SIZE: Record> = { sm: { paddingHorizontal: 4, paddingVertical: 2, minHeight: 20, gap: 4 }, md: { paddingHorizontal: 4, paddingVertical: 2, minHeight: 24, gap: 4 }, lg: { paddingHorizontal: 6, paddingVertical: 6, minHeight: 32, minWidth: 32, gap: 4 }, }; const TAG_SIZE_FONT: Record = { sm: 12, md: 14, lg: 14 }; export interface TagProps extends Omit, 'size'> { readonly variant?: TagVariant; readonly size?: TagSize; readonly startElement?: React.ReactNode; readonly endElement?: React.ReactNode; readonly endElementProps?: React.HTMLAttributes; readonly label?: string; readonly onClose?: VoidFunction; readonly closable?: boolean; readonly truncated?: boolean; readonly loading?: boolean; readonly selected?: boolean; readonly disabled?: boolean; } export const Tag = React.forwardRef( function Tag(props, ref) { const { variant, size, startElement, endElement, endElementProps, label, onClose, closable = Boolean(onClose), children, truncated = false, loading, selected, disabled, className, ...rest } = props; const resolvedVariant = variant ?? 'subtle'; const resolvedSize = size ?? 'md'; const isSelected = Boolean(selected) && !loading; // `selected` overrides the variant's own colours; `disabled` only dims — // neither is known until render, so resolve the final tokens here rather // than lean on a class-merge order that no longer exists. const backgroundColor = isSelected ? 'var(--color-tag-select-selected-bg)' : TAG_VARIANT[resolvedVariant].backgroundColor; const color = isSelected ? 'var(--color-tag-select-selected-fg)' : TAG_VARIANT_FG[resolvedVariant]; const hoverStyle = isSelected ? { opacity: 0.76 } : TAG_VARIANT[resolvedVariant].hoverStyle; // Unselected `select` rows preview the selected look on hover; once // selected the hover colour equals the resting colour, i.e. no change. const textHoverColor = !isSelected && resolvedVariant === 'select' ? 'var(--color-hover)' : undefined; const labelElement = label ? ( { label }:{ nbsp } ) : null; const labelSpan = ( // `ellipsis` is Text's own built-in single-line-truncation variant — // the same rule Tailwind's `line-clamp-1`/`text-ellipsis` drew, through // the component's variants rather than three separate style props. { labelElement }{ children } ); const contentElement = truncated ? ( { labelSpan } ) : labelSpan; return ( } display="inline-flex" alignItems="center" verticalAlign="top" maxWidth="100%" userSelect="text" { ...TAG_SIZE[resolvedSize] } backgroundColor={ backgroundColor as never } hoverStyle={ hoverStyle } focusVisibleStyle={{ outlineWidth: 2, outlineOffset: 2, outlineColor: 'var(--color-gray-500)' as never, outlineStyle: 'solid', }} // One cursor, decided once: disabled outranks the variant's. cursor={ disabled ? 'not-allowed' : TAG_VARIANT[resolvedVariant].cursor } { ...(disabled ? { opacity: 0.4, pointerEvents: 'none' } : {}) } { ...(isSelected && { 'data-selected': true }) } { ...(disabled && { 'data-disabled': true }) } className={ className } { ...(rest as object) } > { startElement && ( { startElement } ) } { contentElement } { endElement && (() => { const { className: endClassName, ...endElementRest } = endElementProps ?? {}; return ( { endElement } ); })() } { closable && ( ) } ); }, );