import React, { CSSProperties, ReactElement, cloneElement, isValidElement } from 'react'; import classNames from 'classnames'; import { config } from 'folds'; import { Presence } from '../../hooks/useUserPresence'; type PresenceAvatarProps = { presence?: Presence; children: ReactElement; className?: string; }; const PRESENCE_COLOR: Record = { [Presence.Online]: '#38842b', [Presence.Unavailable]: '#959e30', [Presence.Offline]: '#454545', }; const rowStyle: CSSProperties = { display: 'inline-flex', flexDirection: 'row', alignItems: 'stretch', flexShrink: 0, lineHeight: 0, }; const faceStyle: CSSProperties = { borderTopLeftRadius: 0, borderBottomLeftRadius: 0, // Keep the right side rounded to match size-200 nav avatars. borderTopRightRadius: config.radii.R400, borderBottomRightRadius: config.radii.R400, }; /** * Presence strip as a real sibling (not ::before). * Folds Avatar uses overflow:hidden, which clips outside ::before bars. */ export function PresenceAvatar({ presence = Presence.Offline, children, className, }: PresenceAvatarProps) { const stripColor = PRESENCE_COLOR[presence] ?? PRESENCE_COLOR[Presence.Offline]; const face = isValidElement(children) ? cloneElement(children, { className: classNames((children.props as { className?: string }).className), style: { ...((children.props as { style?: CSSProperties }).style ?? {}), ...faceStyle, }, // Avoid the folds radii shorthand fighting the square-left corners. radii: '0', } as Partial) : children; return (
{face}
); }