248 lines
8.1 KiB
TypeScript
248 lines
8.1 KiB
TypeScript
import React, { ElementType, ReactNode, forwardRef, CSSProperties } from 'react';
|
|
import { ResponsiveValue } from './Box';
|
|
|
|
export type TextSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | 'base';
|
|
|
|
export interface TextProps {
|
|
children: ReactNode;
|
|
variant?: 'high' | 'med' | 'low' | 'primary' | 'success' | 'warning' | 'critical' | 'telemetry' | 'inherit' | any;
|
|
size?: TextSize | { base: TextSize; sm?: TextSize; md?: TextSize; lg?: TextSize; xl?: TextSize } | any;
|
|
weight?: 'light' | 'normal' | 'medium' | 'semibold' | 'bold' | any;
|
|
as?: ElementType;
|
|
align?: 'left' | 'center' | 'right' | any;
|
|
mono?: boolean;
|
|
uppercase?: boolean;
|
|
leading?: 'none' | 'tight' | 'snug' | 'normal' | 'relaxed' | 'loose' | any;
|
|
block?: boolean;
|
|
truncate?: boolean;
|
|
className?: string;
|
|
style?: CSSProperties;
|
|
mt?: number | any;
|
|
mb?: number | any;
|
|
ml?: number | any;
|
|
mr?: number | any;
|
|
marginTop?: number | any;
|
|
marginBottom?: number | any;
|
|
font?: 'sans' | 'mono' | string;
|
|
color?: string;
|
|
letterSpacing?: string;
|
|
lineHeight?: string | number;
|
|
flexGrow?: number;
|
|
flexShrink?: number;
|
|
lineClamp?: number;
|
|
display?: string | ResponsiveValue<string | any>;
|
|
opacity?: number;
|
|
maxWidth?: string | number;
|
|
mx?: string | number;
|
|
pl?: number;
|
|
px?: number;
|
|
py?: number;
|
|
paddingX?: number;
|
|
paddingY?: number;
|
|
textAlign?: string;
|
|
groupHoverTextColor?: string;
|
|
transition?: boolean;
|
|
borderLeft?: boolean;
|
|
borderStyle?: string;
|
|
borderColor?: string;
|
|
ariaLabel?: string;
|
|
hoverVariant?: string;
|
|
fontSize?: string | any;
|
|
italic?: boolean;
|
|
animate?: string;
|
|
capitalize?: boolean;
|
|
alignItems?: string;
|
|
gap?: number;
|
|
cursor?: string;
|
|
width?: string | number;
|
|
htmlFor?: string;
|
|
transform?: string;
|
|
}
|
|
|
|
/**
|
|
* Text - Redesigned for "Modern Precision" theme.
|
|
* Includes extensive compatibility props to prevent app-wide breakage.
|
|
*/
|
|
export const Text = forwardRef<HTMLElement, TextProps>(({
|
|
children,
|
|
variant = 'med',
|
|
size = 'md',
|
|
weight = 'normal',
|
|
as = 'p',
|
|
align = 'left',
|
|
mono = false,
|
|
uppercase = false,
|
|
leading = 'normal',
|
|
block = false,
|
|
truncate = false,
|
|
className,
|
|
style,
|
|
mt,
|
|
mb,
|
|
ml,
|
|
mr,
|
|
marginTop,
|
|
marginBottom,
|
|
font,
|
|
color,
|
|
letterSpacing,
|
|
lineHeight,
|
|
flexGrow,
|
|
flexShrink,
|
|
lineClamp,
|
|
display,
|
|
opacity,
|
|
maxWidth,
|
|
mx,
|
|
pl,
|
|
px,
|
|
py,
|
|
paddingX,
|
|
paddingY,
|
|
textAlign,
|
|
groupHoverTextColor,
|
|
transition,
|
|
borderLeft,
|
|
borderStyle,
|
|
borderColor,
|
|
ariaLabel,
|
|
hoverVariant,
|
|
fontSize,
|
|
italic,
|
|
animate,
|
|
capitalize,
|
|
alignItems,
|
|
gap,
|
|
cursor,
|
|
width,
|
|
htmlFor,
|
|
transform,
|
|
}, ref) => {
|
|
const variantClasses = {
|
|
high: 'text-[var(--ui-color-text-high)]',
|
|
med: 'text-[var(--ui-color-text-med)]',
|
|
low: 'text-[var(--ui-color-text-low)]',
|
|
primary: 'text-[var(--ui-color-intent-primary)]',
|
|
success: 'text-[var(--ui-color-intent-success)]',
|
|
warning: 'text-[var(--ui-color-intent-warning)]',
|
|
critical: 'text-[var(--ui-color-intent-critical)]',
|
|
telemetry: 'text-[var(--ui-color-intent-telemetry)]',
|
|
inherit: 'text-inherit',
|
|
};
|
|
|
|
const sizeMap: Record<string, string> = {
|
|
xs: 'text-[10px]',
|
|
sm: 'text-xs',
|
|
base: 'text-sm',
|
|
md: 'text-sm',
|
|
lg: 'text-base',
|
|
xl: 'text-lg',
|
|
'2xl': 'text-xl',
|
|
'3xl': 'text-2xl',
|
|
'4xl': 'text-3xl',
|
|
};
|
|
|
|
const getResponsiveSize = (s: any) => {
|
|
if (!s) return sizeMap['md'];
|
|
if (typeof s === 'string') return sizeMap[s] || sizeMap['md'];
|
|
const classes = [];
|
|
if (s.base) classes.push(sizeMap[s.base]);
|
|
if (s.sm) classes.push(`sm:${sizeMap[s.sm]}`);
|
|
if (s.md) classes.push(`md:${sizeMap[s.md]}`);
|
|
if (s.lg) classes.push(`lg:${sizeMap[s.lg]}`);
|
|
if (s.xl) classes.push(`xl:${sizeMap[s.xl]}`);
|
|
return classes.join(' ');
|
|
};
|
|
|
|
const weightClasses = {
|
|
light: 'font-light',
|
|
normal: 'font-normal',
|
|
medium: 'font-medium',
|
|
semibold: 'font-semibold',
|
|
bold: 'font-bold',
|
|
};
|
|
|
|
const leadingClasses = {
|
|
none: 'leading-none',
|
|
tight: 'leading-tight',
|
|
snug: 'leading-snug',
|
|
normal: 'leading-normal',
|
|
relaxed: 'leading-relaxed',
|
|
loose: 'leading-loose',
|
|
};
|
|
|
|
const getResponsiveClasses = (prefix: string, value: any | ResponsiveValue<any> | undefined) => {
|
|
if (value === undefined) return '';
|
|
if (typeof value === 'object') {
|
|
const classes = [];
|
|
if (value.base !== undefined) classes.push(prefix ? `${prefix}-${value.base}` : String(value.base));
|
|
if (value.sm !== undefined) classes.push(prefix ? `sm:${prefix}-${value.sm}` : `sm:${value.sm}`);
|
|
if (value.md !== undefined) classes.push(prefix ? `md:${prefix}-${value.md}` : `md:${value.md}`);
|
|
if (value.lg !== undefined) classes.push(prefix ? `lg:${prefix}-${value.lg}` : `lg:${value.lg}`);
|
|
if (value.xl !== undefined) classes.push(prefix ? `xl:${prefix}-${value.xl}` : `xl:${value.xl}`);
|
|
return classes.join(' ');
|
|
}
|
|
return prefix ? `${prefix}-${value}` : String(value);
|
|
};
|
|
|
|
const classes = [
|
|
variantClasses[variant as keyof typeof variantClasses] || '',
|
|
getResponsiveSize(size),
|
|
weightClasses[weight as keyof typeof weightClasses] || '',
|
|
align === 'center' || textAlign === 'center' ? 'text-center' : (align === 'right' || textAlign === 'right' ? 'text-right' : 'text-left'),
|
|
(mono || font === 'mono') ? 'font-mono' : 'font-sans',
|
|
uppercase ? 'uppercase tracking-widest' : '',
|
|
leadingClasses[leading as keyof typeof leadingClasses] || '',
|
|
block ? 'block' : 'inline',
|
|
truncate ? 'truncate' : '',
|
|
getResponsiveClasses('display', display),
|
|
transition ? 'transition-all duration-200' : '',
|
|
italic ? 'italic' : '',
|
|
animate === 'pulse' ? 'animate-pulse' : '',
|
|
capitalize ? 'capitalize' : '',
|
|
className,
|
|
].filter(Boolean).join(' ');
|
|
|
|
const combinedStyle: React.CSSProperties = {
|
|
...style,
|
|
...(typeof display === 'string' ? { display } : {}),
|
|
...(alignItems ? { alignItems } : {}),
|
|
...(gap !== undefined ? { gap: `${gap * 0.25}rem` } : {}),
|
|
...(cursor ? { cursor } : {}),
|
|
...(width !== undefined ? { width } : {}),
|
|
...(opacity !== undefined ? { opacity } : {}),
|
|
...(maxWidth !== undefined ? { maxWidth } : {}),
|
|
...(mx === 'auto' ? { marginLeft: 'auto', marginRight: 'auto' } : {}),
|
|
...(pl !== undefined ? { paddingLeft: `${pl * 0.25}rem` } : {}),
|
|
...(px !== undefined ? { paddingLeft: `${px * 0.25}rem`, paddingRight: `${px * 0.25}rem` } : {}),
|
|
...(py !== undefined ? { paddingTop: `${py * 0.25}rem`, paddingBottom: `${py * 0.25}rem` } : {}),
|
|
...(paddingX !== undefined ? { paddingLeft: `${paddingX * 0.25}rem`, paddingRight: `${paddingX * 0.25}rem` } : {}),
|
|
...(paddingY !== undefined ? { paddingTop: `${paddingY * 0.25}rem`, paddingBottom: `${paddingY * 0.25}rem` } : {}),
|
|
...(mt !== undefined ? { marginTop: typeof mt === 'number' ? `${mt * 0.25}rem` : mt } : {}),
|
|
...(mb !== undefined ? { marginBottom: typeof mb === 'number' ? `${mb * 0.25}rem` : mb } : {}),
|
|
...(ml !== undefined ? { marginLeft: typeof ml === 'number' ? `${ml * 0.25}rem` : ml } : {}),
|
|
...(mr !== undefined ? { marginRight: typeof mr === 'number' ? `${mr * 0.25}rem` : mr } : {}),
|
|
...(marginTop !== undefined ? { marginTop: typeof marginTop === 'number' ? `${marginTop * 0.25}rem` : marginTop } : {}),
|
|
...(marginBottom !== undefined ? { marginBottom: typeof marginBottom === 'number' ? `${marginBottom * 0.25}rem` : marginBottom } : {}),
|
|
...(color ? { color } : {}),
|
|
...(letterSpacing ? { letterSpacing } : {}),
|
|
...(lineHeight ? { lineHeight } : {}),
|
|
...(flexGrow !== undefined ? { flexGrow } : {}),
|
|
...(flexShrink !== undefined ? { flexShrink } : {}),
|
|
...(lineClamp !== undefined ? { display: '-webkit-box', WebkitLineClamp: lineClamp, WebkitBoxOrient: 'vertical', overflow: 'hidden' } : {}),
|
|
...(borderLeft ? { borderLeft: `1px solid ${borderColor || 'var(--ui-color-border-default)'}` } : {}),
|
|
...(fontSize ? { fontSize } : {}),
|
|
...(transform ? { textTransform: transform as any } : {}),
|
|
};
|
|
|
|
const Tag = as || 'p';
|
|
|
|
return (
|
|
<Tag ref={ref} className={classes} style={combinedStyle} aria-label={ariaLabel} htmlFor={htmlFor}>
|
|
{children}
|
|
</Tag>
|
|
);
|
|
});
|
|
|
|
Text.displayName = 'Text';
|