From de77aa5f0c084c4a4cff0bc436aea2f5a501c070 Mon Sep 17 00:00:00 2001 From: SundasNoreen Date: Tue, 23 May 2023 12:16:22 +0500 Subject: [PATCH] feat: notification tray closes when clicked outside --- src/Notifications/Notifications.jsx | 52 ++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/src/Notifications/Notifications.jsx b/src/Notifications/Notifications.jsx index f246265..0a70add 100644 --- a/src/Notifications/Notifications.jsx +++ b/src/Notifications/Notifications.jsx @@ -1,4 +1,6 @@ -import React, { useState, useCallback, useEffect } from 'react'; +import React, { + useState, useCallback, useEffect, useRef, +} from 'react'; import { NotificationsNone, Settings } from '@edx/paragon/icons'; import { Badge, Form, Icon, IconButton, OverlayTrigger, Popover, @@ -12,11 +14,12 @@ import { messages } from './messages'; const Notifications = () => { const [showNotificationTray, setShowNotificationTray] = useState(false); - const notificationCounts = useSelector(getNotificationTotalUnseenCounts()); const intl = useIntl(); - + const popoverRef = useRef(null); + const buttonRef = useRef(null); const dispatch = useDispatch(); const notificationStatus = useSelector(getNotificationStatus()); + const notificationCounts = useSelector(getNotificationTotalUnseenCounts()); useEffect(() => { if (notificationStatus === 'idle') { @@ -28,6 +31,23 @@ const Notifications = () => { setShowNotificationTray(value); }, []); + useEffect(() => { + const handleClickOutside = (event) => { + if ( + popoverRef.current + && buttonRef.current + && !popoverRef.current.contains(event.target) + && !buttonRef.current.contains(event.target) + ) { + setShowNotificationTray(false); + } + }; + document.addEventListener('mousedown', handleClickOutside); + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, []); + return (
{ id="popover-positioned-bottom" className="notification-tray-container" > - -

- {intl.formatMessage(messages.notificationTitle)} -

-
- -
-
- - - +
+ +

+ {intl.formatMessage(messages.notificationTitle)} +

+
+ +
+
+ + + +
)} > @@ -60,7 +82,7 @@ const Notifications = () => { {notificationCounts?.Total} )} -
+
{ handleNotificationTray(!showNotificationTray); }} src={NotificationsNone}