import React from 'react' import useTheme from '../use-theme' import useScale, { withScale } from '../use-scale' import useClasses from '../use-classes' interface Props { src?: string stacked?: boolean text?: string isSquare?: boolean className?: string } const defaultProps = { stacked: false, text: '', isSquare: false, className: '' } type NativeAttrs = Omit< Partial & React.HTMLAttributes>, keyof Props > export type AvatarProps = Props & NativeAttrs const safeText = (text: string): string => { if (text.length <= 4) return text return text.slice(0, 3) } const AvatarComponent: React.FC = ({ src, stacked, text, isSquare, className, ...props }: AvatarProps & typeof defaultProps) => { const theme = useTheme() const { SCALES } = useScale() const showText = !src const radius = isSquare ? theme.layout.radius : '50%' const marginLeft = stacked ? SCALES.ml(-0.625) : SCALES.ml(0) const classes = useClasses('avatar', className) return ( {!showText && ( avatar )} {showText && ( {safeText(text)} )} ) } AvatarComponent.defaultProps = defaultProps AvatarComponent.displayName = 'HuiAvatar' const Avatar = withScale(AvatarComponent) export default Avatar