diff --git a/packages/main/src/components/MessageView/MessageItem.tsx b/packages/main/src/components/MessageView/MessageItem.tsx index b9890eed49b..6aed177f976 100644 --- a/packages/main/src/components/MessageView/MessageItem.tsx +++ b/packages/main/src/components/MessageView/MessageItem.tsx @@ -6,7 +6,7 @@ import iconArrowRight from '@ui5/webcomponents-icons/dist/slim-arrow-right.js'; import { useStylesheet } from '@ui5/webcomponents-react-base'; import { clsx } from 'clsx'; import type { ReactNode } from 'react'; -import { forwardRef, useContext } from 'react'; +import { Children, forwardRef, useContext, useEffect, useRef, useState } from 'react'; import { FlexBoxAlignItems, FlexBoxDirection } from '../../enums/index.js'; import { MessageViewContext } from '../../internal/MessageViewContext.js'; import type { CommonProps } from '../../types/index.js'; @@ -59,6 +59,9 @@ export interface MessageItemPropTypes extends CommonProps { */ const MessageItem = forwardRef((props, ref) => { const { titleText, subtitleText, counter, type = ValueState.Negative, children, className, ...rest } = props; + const [isTitleTextOverflowing, setIsTitleTextIsOverflowing] = useState(false); + const titleTextRef = useRef(null); + const hasDetails = !!(children || isTitleTextOverflowing); useStylesheet(styleData, MessageItem.displayName); @@ -71,10 +74,10 @@ const MessageItem = forwardRef((prop subtitleText && classNames.withSubtitle ); - const messageClasses = clsx(classNames.message, children && classNames.withChildren); + const messageClasses = clsx(classNames.message, hasDetails && classNames.withChildren); const handleListItemClick = (e) => { - if (children) { + if (hasDetails) { selectMessage(props); if (typeof rest.onClick === 'function') { rest.onClick(e); @@ -90,13 +93,31 @@ const MessageItem = forwardRef((prop handleListItemClick(e); } }; + + const hasChildren = Children.count(children); + useEffect(() => { + const titleTextObserver = new ResizeObserver(([titleTextSpan]) => { + if (titleTextSpan.target.scrollWidth > titleTextSpan.target.clientWidth) { + setIsTitleTextIsOverflowing(true); + } else { + setIsTitleTextIsOverflowing(false); + } + }); + if (!hasChildren && titleTextRef.current) { + titleTextObserver.observe(titleTextRef.current); + } + return () => { + titleTextObserver.disconnect(); + }; + }, [hasChildren]); + return ( ((prop direction={FlexBoxDirection.Column} style={{ flex: 'auto', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }} > - {titleText && {titleText}} - {subtitleText && } + {titleText && ( + + {titleText} + + )} + {titleText && subtitleText && } {counter != null && {counter}} - {children && } + {hasDetails && } ); diff --git a/packages/main/src/components/MessageView/MessageView.mdx b/packages/main/src/components/MessageView/MessageView.mdx index bb6fd1133b2..84168c6c169 100644 --- a/packages/main/src/components/MessageView/MessageView.mdx +++ b/packages/main/src/components/MessageView/MessageView.mdx @@ -53,6 +53,7 @@ function MyComponent() { const [isOnDetailsPage, setIsOnDetailsPage] = useState(false); return ( , - , + ,