{"version":3,"file":"UserBubble.mjs","names":[],"sources":["../../../../src/ai/user-bubble/UserBubble.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 */\n\nimport { Avatar, CleanIconButton, Typography } from '@neo4j-ndl/react';\nimport classNames from 'classnames';\nimport { useLayoutEffect, useRef, useState } from 'react';\n\nimport type { CommonProps } from '../../_common/types';\nimport { ChevronDownIconOutline } from '../../icons/generated/heroIcons/ChevronDownIconOutline';\nimport { ChevronUpIconOutline } from '../../icons/generated/heroIcons/ChevronUpIconOutline';\n\ntype UserBubbleProps = {\n  /** The content of the user bubble */\n  children?: React.ReactNode;\n  /** The props for the avatar */\n  avatarProps?: React.ComponentProps<typeof Avatar>;\n  /** The element to display after the content. Could be a CleanIconButton to edit the user bubble. */\n  trailingElement?: React.ReactNode;\n  /**\n   * Maximum number of lines visible before the message is collapsed.\n   * Set to `null` to always show the full message.\n   * @default 4\n   */\n  maxLines?: number | null;\n};\n\n/**\n * The component is used to display the user's message in a chat.\n * It has an avatar to represent the user and a trailing element to display potential additional actions.\n *\n * @alpha - Changes to this component may be breaking.\n */\nconst UserBubbleComponent = ({\n  avatarProps,\n  trailingElement,\n  children,\n  maxLines = 4,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'div', UserBubbleProps>) => {\n  const classes = classNames('ndl-ai-user-bubble', className);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const [isExpanded, setIsExpanded] = useState(false);\n  const [hasOverflow, setHasOverflow] = useState(false);\n  const isCollapseEnabled = maxLines !== null;\n\n  useLayoutEffect(() => {\n    const element = contentRef.current;\n    if (element === null || maxLines === null) {\n      setHasOverflow(false);\n      return;\n    }\n\n    const checkOverflow = () => {\n      const lineHeight = Number.parseFloat(\n        window.getComputedStyle(element).lineHeight,\n      );\n      if (!Number.isFinite(lineHeight)) {\n        return;\n      }\n\n      setHasOverflow(element.scrollHeight > lineHeight * maxLines + 1);\n    };\n\n    checkOverflow();\n\n    const resizeObserver = new ResizeObserver(checkOverflow);\n    resizeObserver.observe(element);\n\n    return () => {\n      resizeObserver.disconnect();\n    };\n  }, [children, isExpanded, maxLines]);\n\n  const expandButton = hasOverflow ? (\n    <div className=\"ndl-ai-user-bubble-expand-wrap\">\n      <CleanIconButton\n        className=\"ndl-ai-user-bubble-expand\"\n        size=\"small\"\n        onClick={() => setIsExpanded((expanded) => !expanded)}\n        description={isExpanded ? 'Show less' : 'Show more'}\n        htmlAttributes={{\n          'aria-expanded': isExpanded,\n          type: 'button',\n        }}\n      >\n        {isExpanded ? <ChevronUpIconOutline /> : <ChevronDownIconOutline />}\n      </CleanIconButton>\n      <>\n        <div className=\"ndl-ai-user-bubble-expand-blur\" aria-hidden=\"true\" />\n        <div className=\"ndl-ai-user-bubble-expand-fill\" aria-hidden=\"true\" />\n      </>\n    </div>\n  ) : null;\n\n  return (\n    <div\n      ref={ref}\n      className={classes}\n      style={style}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      {avatarProps && (\n        <Avatar\n          type=\"image\"\n          {...avatarProps}\n          size=\"small\"\n          shape=\"circle\"\n          className={classNames(\n            'ndl-ai-user-bubble-avatar',\n            avatarProps?.className,\n          )}\n        />\n      )}\n      <div className=\"ndl-ai-user-bubble-main\">\n        <Typography\n          as=\"div\"\n          variant=\"body-medium\"\n          ref={contentRef}\n          className={classNames('ndl-ai-user-bubble-content', {\n            'ndl-ai-user-bubble-content-clamped':\n              isCollapseEnabled && !isExpanded,\n          })}\n          style={\n            isCollapseEnabled\n              ? ({\n                  '--ndl-ai-user-bubble-collapsed-lines': maxLines,\n                } as React.CSSProperties)\n              : undefined\n          }\n        >\n          {children}\n          {hasOverflow && isExpanded && (\n            <span\n              className=\"ndl-ai-user-bubble-expand-spacer\"\n              aria-hidden=\"true\"\n            />\n          )}\n        </Typography>\n        {hasOverflow && (\n          <div className=\"ndl-ai-user-bubble-toggle\">{expandButton}</div>\n        )}\n      </div>\n      <div className=\"ndl-ai-user-bubble-trailing\">{trailingElement}</div>\n    </div>\n  );\n};\n\nexport { UserBubbleComponent as UserBubble };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmDE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;;;AARF,MAAM,uBAAuB,SAUc;CAVd,IAAA,EAC3B,aACA,iBACA,UACA,WAAW,GACX,WACA,OACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,UAAU,WAAW,sBAAsB,SAAS;CAC1D,MAAM,aAAa,OAAuB,IAAI;CAC9C,MAAM,CAAC,YAAY,iBAAiB,SAAS,KAAK;CAClD,MAAM,CAAC,aAAa,kBAAkB,SAAS,KAAK;CACpD,MAAM,oBAAoB,aAAa;CAEvC,sBAAsB;EACpB,MAAM,UAAU,WAAW;EAC3B,IAAI,YAAY,QAAQ,aAAa,MAAM;GACzC,eAAe,KAAK;GACpB;EACF;EAEA,MAAM,sBAAsB;GAC1B,MAAM,aAAa,OAAO,WACxB,OAAO,iBAAiB,OAAO,CAAC,CAAC,UACnC;GACA,IAAI,CAAC,OAAO,SAAS,UAAU,GAC7B;GAGF,eAAe,QAAQ,eAAe,aAAa,WAAW,CAAC;EACjE;EAEA,cAAc;EAEd,MAAM,iBAAiB,IAAI,eAAe,aAAa;EACvD,eAAe,QAAQ,OAAO;EAE9B,aAAa;GACX,eAAe,WAAW;EAC5B;CACF,GAAG;EAAC;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