{"version":3,"file":"Avatar.mjs","names":[],"sources":["../../../src/avatar/Avatar.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport type React from 'react';\n\nimport { type PolymorphicCommonProps } from '../_common/types';\nimport { UserIconOutline } from '../icons/generated/heroIcons/UserIconOutline';\nimport { Typography } from '../typography';\n\ntype SizeType = AvatarProps['size'];\ntype StatusType = NonNullable<AvatarProps['status']>;\n\ninterface CircleProperties {\n  /** Center coordinates of the circles */\n  cxAndCy: number;\n  /** Diameter of the circles */\n  diameter: number;\n  /** Radius of the inner circle */\n  rInner: number;\n  /** Radius of the outer circle */\n  rOuter: number;\n}\n\nfunction circleSizeFactory(size: SizeType): CircleProperties {\n  switch (size) {\n    case 'x-small':\n    case 'small':\n    case 'medium':\n      return {\n        cxAndCy: 4.5,\n        diameter: 9,\n        rInner: 3,\n        rOuter: 4.5,\n      };\n    case 'large':\n    case 'x-large':\n    default:\n      return {\n        cxAndCy: 6,\n        diameter: 12,\n        rInner: 4,\n        rOuter: 6,\n      };\n  }\n}\n\nfunction StatusIndicator({\n  size,\n  status,\n}: {\n  size: SizeType;\n  status: StatusType;\n}) {\n  const { cxAndCy, diameter, rInner, rOuter } = circleSizeFactory(size);\n  const statusClasses = `ndl-avatar-status ndl-avatar-status-${status}`;\n\n  return (\n    <div\n      className={statusClasses}\n      title={status.charAt(0).toUpperCase() + status.slice(1)}\n      role=\"presentation\"\n    >\n      <svg\n        xmlns=\"http://www.w3.org/2000/svg\"\n        height={diameter}\n        width={diameter}\n        viewBox={`0 0 ${diameter} ${diameter}`}\n        fill=\"none\"\n      >\n        <circle\n          cx={cxAndCy}\n          cy={cxAndCy}\n          r={rOuter}\n          className=\"ndl-avatar-status-circle-outer\"\n        />\n        <circle\n          cx={cxAndCy}\n          cy={cxAndCy}\n          r={status === 'unknown' ? rInner - 1 : rInner}\n          className=\"ndl-avatar-status-circle-inner\"\n        />\n      </svg>\n    </div>\n  );\n}\n\ntype AvatarProps = {\n  /** Whether to display a status indicator */\n  hasStatus?: boolean;\n  /** Whether the avatar is disabled. When disabled, status indicators are hidden and `onClick` handlers are disabled when cast as a button */\n  isDisabled?: boolean;\n  /** The name associated with the avatar. Used for alt text and aria-label. For letters type, this text is displayed */\n  name?: string;\n  /** The shape of the avatar */\n  shape?: 'circle' | 'square';\n  /** The size of the avatar */\n  size?: 'x-small' | 'small' | 'medium' | 'large' | 'x-large';\n  /** Source URI for the image. When provided with `type=\"image`\", displays the image */\n  source?: string;\n  /** The status to display. Only shown when `hasStatus` is `true` and component is not disabled */\n  status?: 'offline' | 'online' | 'unknown';\n  /** The type of avatar content. If `type=\"image\"` and no source is provided, automatically falls back to `icon` */\n  type?: 'icon' | 'image' | 'letters';\n  /** Custom icon to display when `type` is `\"icon\"`. Defaults to `UserIconOutline` */\n  icon?: React.ReactNode;\n} & (\n  | {\n      as: 'button';\n      onClick: () => void;\n    }\n  | {\n      as?: React.ElementType;\n    }\n);\n\nconst getLettersDisplay = (displayName: string) => {\n  const words = displayName.trim().split(/\\s+/).filter(Boolean);\n  if (words.length <= 1) {\n    return displayName.trim();\n  }\n  return words\n    .map((word) => word.charAt(0))\n    .join('')\n    .toUpperCase();\n};\n\nexport const Avatar = <T extends React.ElementType = 'div'>({\n  as,\n  hasStatus = false,\n  icon,\n  isDisabled = false,\n  name,\n  shape = 'circle',\n  size = 'medium',\n  source,\n  status,\n  type: initialType = 'image',\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, AvatarProps>) => {\n  // If source is not provided, fallback to icon\n  const type =\n    initialType === 'image' && (source === undefined || source === '')\n      ? 'icon'\n      : initialType;\n\n  const Component: React.ElementType = as ?? 'div';\n  const isButton = Component === 'button';\n  const classes = classNames(\n    `ndl-avatar ndl-avatar-${shape} ndl-avatar-${size}`,\n    className,\n  );\n  const shapeClasses = classNames(\n    `ndl-avatar-shape ndl-avatar-${shape} ndl-avatar-${shape}-${size}`,\n    {\n      'ndl-avatar-disabled': isDisabled,\n    },\n  );\n\n  const typeClass = `ndl-avatar-${type}`;\n\n  const renderImageType = () => (\n    <>\n      <img data-testid=\"ndl-avatar-image\" src={source} alt={name} />\n      <div\n        data-testid=\"ndl-avatar-image-overlay\"\n        className={`${shapeClasses} ndl-avatar-image-overlay`}\n      />\n    </>\n  );\n\n  const renderIconType = () => <>{icon ?? <UserIconOutline />}</>;\n\n  const renderLettersType = () => {\n    return (\n      <Typography variant=\"subheading-small\" className=\"ndl-avatar-typography\">\n        {getLettersDisplay(name ?? '')}\n      </Typography>\n    );\n  };\n\n  const TypeComponent =\n    type === 'image'\n      ? renderImageType()\n      : type === 'icon'\n        ? renderIconType()\n        : type === 'letters' && Boolean(name)\n          ? renderLettersType()\n          : null;\n\n  const isStatusIndicatorVisible =\n    hasStatus && !isDisabled && status !== undefined;\n\n  return (\n    <Component\n      ref={ref}\n      className={classes}\n      style={style}\n      aria-hidden={!isButton}\n      {...(isButton && {\n        disabled: isDisabled,\n      })}\n      aria-label={name ? `Avatar ${name}` : 'Avatar'}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <div className={classNames(shapeClasses, typeClass)}>{TypeComponent}</div>\n      {isStatusIndicatorVisible && (\n        <StatusIndicator size={size} status={status} />\n      )}\n    </Component>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiJE;CACA;CACA;CACA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