/* eslint-disable react/require-default-props */ import React, { ReactNode } from 'react'; import styled from 'styled-components'; import { border, color, color as styledColor, space, TextColorProps, } from 'styled-system'; import Box from '../Box'; import Image from '../Image'; import Placeholder from '../Placeholder'; import Text from '../Text'; export interface AvatarImageProps { optimizeSrc?: boolean; src?: string; size?: number; className?: string; alt?: string; loading?: boolean; children?: ReactNode; } export interface AvatarProps extends Omit { label?: string; textColor?: TextColorProps['color']; } const AvatarPlaceholder = styled((props) => ( ))` ${({ theme }) => border({ theme, borderWidth: 2, borderColor: 'shades.white', borderStyle: 'solid', borderRadius: '50%', })}; `; const DefaultAvatar = styled((props) => )` ${({ theme }) => border({ theme, borderWidth: 2, borderColor: 'shades.white', borderStyle: 'solid', borderRadius: '50%', })}; ${({ theme }) => color({ theme, bg: 'brand.red', color: 'text.inverse', })}; `; const StyledText = styled((props) => )` user-select: none; text-transform: uppercase; `; const AvatarImage = ({ src, optimizeSrc = true, size = 40, className, alt, loading, }: AvatarImageProps) => { const parts = alt?.split(' ').filter(Boolean); const initials = parts && parts.length ? `${parts[0][0]}${parts.length > 1 ? parts[parts.length - 1][0] : ''}` : '?'; if (loading) return ; if (!src) return ( {alt ? ( {initials} ) : ( ? )} ); if (optimizeSrc) return ( {alt ); return ( {alt ); }; const StyledAvatarImage = styled(AvatarImage)<{ src?: string }>` border-radius: 50%; border: 2px solid white; box-sizing: border-box; `; const StyledAvatar = styled.span` display: flex; align-items: center; `; const AvatarLabel = styled.span>` ${({ theme }) => space({ pl: 2, theme })} ${({ textColor, theme }) => styledColor({ color: textColor, theme })} `; const Avatar: React.FC = ({ label = '', textColor, children, ...rest }) => ( {label && label.length > 0 && ( {label} )} {children} ); export default Avatar;