1
- import React , { useEffect } from "react" ;
1
+ import React , { useEffect , useState , useRef } from "react" ;
2
2
import { BiSearchAlt } from "react-icons/bi" ;
3
3
import { FaBell , FaChevronDown } from "react-icons/fa" ;
4
4
import women from "../img/women.jpg" ;
5
5
6
6
function TopContainer ( ) {
7
+ const [ isDropdownOpen , setIsDropdownOpen ] = useState ( false ) ;
8
+ const dropdownRef = useRef ( null ) ;
9
+
7
10
useEffect ( ( ) => {
8
- const mouseTarget = document . getElementById ( "menuChevron" ) ;
9
- const menuContainer = document . getElementById ( "menuContainer" ) ;
10
- mouseTarget . addEventListener ( "mouseenter" , ( ) => {
11
- mouseTarget . style . transform = "rotate(180deg)" ;
12
- menuContainer . style . transform = "translateX(0px)" ;
13
- } ) ;
14
-
15
- menuContainer . addEventListener ( "mouseleave" , ( ) => {
16
- mouseTarget . style . transform = "rotate(0deg)" ;
17
- menuContainer . style . transform = "translateX(300px)" ;
18
- } ) ;
11
+ // Add event listener to detect clicks anywhere on the page
12
+ window . addEventListener ( "click" , handleOutsideClick ) ;
13
+
14
+ // Cleanup function to remove the event listener
15
+ return ( ) => {
16
+ window . removeEventListener ( "click" , handleOutsideClick ) ;
17
+ } ;
19
18
} , [ ] ) ;
20
19
20
+ const toggleDropdown = ( ) => {
21
+ setIsDropdownOpen ( ! isDropdownOpen ) ;
22
+ } ;
23
+
24
+ const handleOutsideClick = ( event ) => {
25
+ // Check if the clicked element is outside the dropdown content
26
+ if ( dropdownRef . current && ! dropdownRef . current . contains ( event . target ) ) {
27
+ setIsDropdownOpen ( false ) ;
28
+ }
29
+ } ;
30
+
21
31
return (
22
32
< div className = "topContainer" >
23
33
< div className = "inputBox" >
@@ -28,12 +38,44 @@ function TopContainer() {
28
38
</ div >
29
39
30
40
< div className = "profileContainer" >
31
- < i className = "profileIcon" >
32
- < FaBell />
33
- </ i >
41
+ < div className = "notification-container" ref = { dropdownRef } >
42
+ < div className = "profileIcon" onClick = { toggleDropdown } >
43
+ < FaBell />
44
+ </ div >
45
+ { isDropdownOpen && (
46
+ < div className = "dropdown-content" >
47
+ { /* Notification items */ }
48
+ < div className = "notification-item" >
49
+ User "JohnDoe" has uploaded a new project titled "Introduction
50
+ to Machine Learning." Check it out now!
51
+ </ div >
52
+ < div className = "notification-item" >
53
+ User "JaneSmith" has commented on your project "Data
54
+ Visualization with D3.js." View the comment now.
55
+ </ div >
56
+ < div className = "notification-item" >
57
+ User "TechMaster" has updated the project "Web Development with
58
+ React.js" with bug fixes. Explore the updated version.
59
+ </ div >
60
+ < div className = "notification-item" >
61
+ Congratulations! Your project "Artificial Intelligence in
62
+ Robotics" has been approved by the moderators. It's now live on
63
+ the platform.
64
+ </ div >
65
+ < div className = "notification-item" >
66
+ User "CodeNinja" has sent you a collaboration request for the
67
+ project "Cybersecurity Best Practices." Accept or decline the
68
+ request.
69
+ </ div >
70
+ < p className = "seeAll" > See all notifications</ p >
71
+ </ div >
72
+ ) }
73
+ </ div >
74
+
34
75
< div className = "profileImage" >
35
76
< img src = { women } alt = "" />
36
77
</ div >
78
+
37
79
< p className = "profileName" > Sugam Arora</ p >
38
80
< i className = "menuChevron" id = "menuChevron" >
39
81
< FaChevronDown />
0 commit comments