Debian Codegraph @

node/@patternfly/react-core/src/components/NotificationDrawer/NotificationDrawerListItemHeader.tsx

raw
1 import { Fragment, useEffect, useRef, useState } from 'react';
2 import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon';
3 import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon';
4 import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon';
5 import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon';
6 import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon';
7 import { css } from '@patternfly/react-styles';
8 import styles from '@patternfly/react-styles/css/components/NotificationDrawer/notification-drawer';
9
10 import maxLines from '@patternfly/react-tokens/dist/esm/c_notification_drawer__list_item_header_title_max_lines';
11
12 import { Tooltip, TooltipPosition } from '../Tooltip';
13
14 export const variantIcons = {
15 success: RhUiCheckCircleFillIcon,
16 danger: RhUiErrorFillIcon,
17 warning: RhUiWarningFillIcon,
18 info: RhUiInformationFillIcon,
19 custom: RhUiNotificationFillIcon
20 };
21
22 export interface NotificationDrawerListItemHeaderProps extends React.HTMLProps<HTMLDivElement> {
23 /** Actions rendered inside the notification drawer list item header */
24 children?: React.ReactNode;
25 /** Additional classes for notification drawer list item header. */
26 className?: string;
27 /** Add custom icon for notification drawer list item header */
28 icon?: React.ReactNode;
29 /** Notification drawer list item header screen reader title */
30 srTitle?: string;
31 /** Notification drawer list item title */
32 title: string;
33 /** Variant indicates the severity level */
34 variant?: 'success' | 'danger' | 'warning' | 'info' | 'custom';
35 /** Truncate title to number of lines */
36 truncateTitle?: number;
37 /** Position of the tooltip which is displayed if text is truncated */
38 tooltipPosition?:
39 | TooltipPosition
40 | 'auto'
41 | 'top'
42 | 'bottom'
43 | 'left'
44 | 'right'
45 | 'top-start'
46 | 'top-end'
47 | 'bottom-start'
48 | 'bottom-end'
49 | 'left-start'
50 | 'left-end'
51 | 'right-start'
52 | 'right-end';
53 /** Sets the heading level to use for the list item header title. Default is h2. */
54 headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
55 /** Removes the offset of the notification drawer actions. */
56 actionHasNoOffset?: boolean;
57 }
58
59 export const NotificationDrawerListItemHeader: React.FunctionComponent<NotificationDrawerListItemHeaderProps> = ({
60 children,
61 className = '',
62 icon = null,
63 srTitle,
64 title,
65 variant = 'custom',
66 truncateTitle = 0,
67 tooltipPosition,
68 headingLevel: HeadingLevel = 'h2',
69 actionHasNoOffset = false,
70 ...props
71 }: NotificationDrawerListItemHeaderProps) => {
72 const titleRef = useRef(null);
73 const [isTooltipVisible, setIsTooltipVisible] = useState(false);
74 useEffect(() => {
75 if (!titleRef.current || !truncateTitle) {
76 return;
77 }
78 titleRef.current.style.setProperty(maxLines.name, truncateTitle.toString());
79 const showTooltip = titleRef.current && titleRef.current.offsetHeight < titleRef.current.scrollHeight;
80 if (isTooltipVisible !== showTooltip) {
81 setIsTooltipVisible(showTooltip);
82 }
83 }, [titleRef, truncateTitle, isTooltipVisible]);
84 const Icon = variantIcons[variant];
85 const Title = (
86 <HeadingLevel
87 {...(isTooltipVisible && { tabIndex: 0 })}
88 ref={titleRef}
89 className={css(styles.notificationDrawerListItemHeaderTitle, truncateTitle && styles.modifiers.truncate)}
90 >
91 {srTitle && <span className="pf-v6-screen-reader">{srTitle}</span>}
92 {title}
93 </HeadingLevel>
94 );
95
96 return (
97 <Fragment>
98 <div {...props} className={css(styles.notificationDrawerListItemHeader, className)}>
99 <span className={css(styles.notificationDrawerListItemHeaderIcon)}>{icon ? icon : <Icon />}</span>
100 {isTooltipVisible ? (
101 <Tooltip content={title} position={tooltipPosition}>
102 {Title}
103 </Tooltip>
104 ) : (
105 Title
106 )}
107 </div>
108 {children && (
109 <div className={css(styles.notificationDrawerListItemAction, actionHasNoOffset && styles.modifiers.noOffset)}>
110 {children}
111 </div>
112 )}
113 </Fragment>
114 );
115 };
116 NotificationDrawerListItemHeader.displayName = 'NotificationDrawerListItemHeader';