Debian Codegraph @

node/@patternfly/react-core/src/components/NotificationBadge/NotificationBadge.tsx

raw
1 import { useEffect, useState } from 'react';
2 import { Button, ButtonVariant, ButtonProps } from '../Button';
3 import RhUiAttentionBellFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-attention-bell-fill-icon';
4 import RhUiNotificationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-notification-fill-icon';
5 import styles from '@patternfly/react-styles/css/components/Button/button';
6 import { css } from '@patternfly/react-styles';
7
8 export enum NotificationBadgeVariant {
9 read = 'read',
10 unread = 'unread',
11 attention = 'attention',
12 plain = 'plain'
13 }
14
15 export interface NotificationBadgeProps extends Omit<ButtonProps, 'variant'> {
16 /** Adds an accessible label to the notification badge. */
17 'aria-label'?: string;
18 /** Icon to display for attention variant. */
19 attentionIcon?: React.ReactNode;
20 /** Content rendered inside the notification badge. */
21 children?: React.ReactNode;
22 /** Additional classes added to the notification badge. */
23 className?: string;
24 /** A number displayed in the badge alongside the icon. */
25 count?: number;
26 /** Icon to display in the notification badge. */
27 icon?: React.ReactNode;
28 /** Flag for applying expanded styling and setting the aria-expanded attribute on the
29 * notification badge.
30 */
31 isExpanded?: boolean;
32 /** Determines the variant of the notification badge. */
33 variant?: NotificationBadgeVariant | 'read' | 'unread' | 'attention' | 'plain';
34 /** Flag indicating whether the notification badge animation should be triggered. Each
35 * time this prop is true, the animation will be triggered a single time.
36 */
37 shouldNotify?: boolean;
38 /** Callback for when the animation of the notification badge icon ends.
39 */
40 onAnimationEnd?: (event: React.AnimationEvent<HTMLButtonElement>) => void;
41 }
42
43 export const NotificationBadge: React.FunctionComponent<NotificationBadgeProps> = ({
44 children,
45 variant = NotificationBadgeVariant.read,
46 count = 0,
47 attentionIcon = <RhUiAttentionBellFillIcon />,
48 icon = <RhUiNotificationFillIcon />,
49 className,
50 isExpanded = false,
51 shouldNotify = false,
52 onAnimationEnd,
53 ...props
54 }: NotificationBadgeProps) => {
55 const [isAnimating, setIsAnimating] = useState(shouldNotify);
56 const hasCount = count > 0;
57 const hasChildren = children !== undefined;
58 const isAttention = variant === NotificationBadgeVariant.attention;
59 const isPlain = variant === NotificationBadgeVariant.plain;
60 const _buttonState = isPlain ? undefined : (variant as 'read' | 'unread' | 'attention');
61 const notificationIcon = isAttention ? attentionIcon : icon;
62 let notificationContent: React.ReactNode = null;
63
64 if (hasCount) {
65 notificationContent = count;
66 } else if (hasChildren) {
67 notificationContent = children;
68 }
69
70 const buttonClassName = isAnimating ? css(className, styles.modifiers.notify) : className;
71
72 useEffect(() => {
73 setIsAnimating(shouldNotify);
74 }, [shouldNotify]);
75
76 const handleAnimationEnd = (event: React.AnimationEvent<HTMLButtonElement>) => {
77 onAnimationEnd?.(event);
78 setIsAnimating(false);
79 };
80
81 return (
82 <Button
83 variant={isPlain ? ButtonVariant.plain : ButtonVariant.stateful}
84 className={buttonClassName}
85 aria-expanded={isExpanded}
86 state={_buttonState}
87 isClicked={isExpanded}
88 icon={notificationIcon}
89 onAnimationEnd={handleAnimationEnd}
90 {...props}
91 >
92 {notificationContent}
93 </Button>
94 );
95 };
96 NotificationBadge.displayName = 'NotificationBadge';