Debian Codegraph @

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

raw
1 import { useEffect, useRef, useState } from 'react';
2 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
3 import { css } from '@patternfly/react-styles';
4 import styles from '@patternfly/react-styles/css/components/NotificationDrawer/notification-drawer';
5 import maxLines from '@patternfly/react-tokens/dist/esm/c_notification_drawer__group_toggle_title_max_lines';
6
7 import { Badge } from '../Badge';
8 import { Tooltip, TooltipPosition } from '../Tooltip';
9
10 export interface NotificationDrawerGroupProps extends Omit<React.HTMLProps<HTMLDivElement>, 'title'> {
11 /** Content rendered inside the group */
12 children?: React.ReactNode;
13 /** Additional classes added to the group */
14 className?: string;
15 /** Notification drawer group count */
16 count: number;
17 /** Adds styling to the group to indicate expanded state */
18 isExpanded: boolean;
19 /** Adds styling to the group to indicate whether it has been read */
20 isRead?: boolean;
21 /** Callback for when group button is clicked to expand */
22 onExpand?: (event: any, value: boolean) => void;
23 /** Notification drawer group title */
24 title: React.ReactNode;
25 /** Truncate title to number of lines */
26 truncateTitle?: number;
27 /** Position of the tooltip which is displayed if text is truncated */
28 tooltipPosition?:
29 | TooltipPosition
30 | 'auto'
31 | 'top'
32 | 'bottom'
33 | 'left'
34 | 'right'
35 | 'top-start'
36 | 'top-end'
37 | 'bottom-start'
38 | 'bottom-end'
39 | 'left-start'
40 | 'left-end'
41 | 'right-start'
42 | 'right-end';
43 /** Sets the heading level to use for the group title. Default is h1. */
44 headingLevel?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
45 }
46
47 export const NotificationDrawerGroup: React.FunctionComponent<NotificationDrawerGroupProps> = ({
48 children,
49 className = '',
50 count,
51 isExpanded,
52 isRead = false,
53 // eslint-disable-next-line @typescript-eslint/no-unused-vars
54 onExpand = (event: any, expanded: boolean) => undefined as any,
55 title,
56 truncateTitle = 0,
57 tooltipPosition,
58 headingLevel: HeadingLevel = 'h1',
59 ...props
60 }: NotificationDrawerGroupProps) => {
61 const titleRef = useRef(null);
62 const [isTooltipVisible, setIsTooltipVisible] = useState(false);
63 useEffect(() => {
64 // Title will always truncate on overflow regardless of truncateTitle prop
65 const showTooltip = titleRef.current && titleRef.current.offsetHeight < titleRef.current.scrollHeight;
66 if (isTooltipVisible !== showTooltip) {
67 setIsTooltipVisible(showTooltip);
68 }
69 if (!titleRef.current || !truncateTitle) {
70 return;
71 }
72 titleRef.current.style.setProperty(maxLines.name, truncateTitle.toString());
73 }, [titleRef, truncateTitle, isTooltipVisible]);
74
75 const Title = (
76 <div
77 {...(isTooltipVisible && { tabIndex: 0 })}
78 ref={titleRef}
79 className={css(styles.notificationDrawerGroupToggleTitle)}
80 >
81 {title}
82 </div>
83 );
84
85 return (
86 <section
87 {...props}
88 className={css(styles.notificationDrawerGroup, isExpanded && styles.modifiers.expanded, className)}
89 >
90 <HeadingLevel>
91 <button
92 className={css(styles.notificationDrawerGroupToggle)}
93 aria-expanded={isExpanded}
94 onClick={(e) => onExpand(e, !isExpanded)}
95 onKeyDown={(e) => {
96 if (e.key === 'Enter' || e.key === ' ') {
97 e.preventDefault();
98 onExpand(e, !isExpanded);
99 }
100 }}
101 >
102 {isTooltipVisible ? (
103 <Tooltip content={title} position={tooltipPosition}>
104 {Title}
105 </Tooltip>
106 ) : (
107 Title
108 )}
109 <div className={css(styles.notificationDrawerGroupToggleCount)}>
110 <Badge isRead={isRead}>{count}</Badge>
111 </div>
112
113 <span className={styles.notificationDrawerGroupToggleIcon}>
114 <RhMicronsCaretDownIcon />
115 </span>
116 </button>
117 </HeadingLevel>
118 {children}
119 </section>
120 );
121 };
122 NotificationDrawerGroup.displayName = 'NotificationDrawerGroup';