File size: 4,704 Bytes
62c3fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, { useState, useEffect, useRef } from "react";
import { Link, useLocation } from "react-router-dom";
import { Logo } from "../utils";
import styled from "styled-components";

// Styled Components
const HeaderContainer = styled.header`
  z-index: 500;
  background: rgba(0, 0, 0, 0.8);
  font-family: "SF Pro", sans-serif;
  font-weight: 800;
  font-size: 15px;
  position: sticky;
  top: 0;
  width: 100%;
  margin: 0;
  padding: 0;
`;

const NavWrapper = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0.5rem 1rem;
  width: 100%;
  margin: 0;

  @media (max-width: 768px) {
    justify-content: space-between;
  }
`;

const SLogo = styled(Link)`
  display: flex;
  align-items: center;
  font-weight: bold;
  color: white;
  text-decoration: none;

  img {
    height: 50px;
    margin-right: 0.5rem;
  }
`;

const DesktopMenu = styled.nav`
  display: none;

  @media (min-width: 768px) {
    display: flex;
    gap: 1rem;
  }
`;

const MenuLink = styled(Link)`
  color: ${(props) => (props.$active ? "transparent" : "white")};
  padding: 0.5rem 2rem;
  text-decoration: none;
  position: relative;

  @media (min-width: 992px) {
    padding: 0.5rem 4rem;
  }

  &:hover,
  &:active {
    color: #3267B9;
    background-clip: text;
    -webkit-background-clip: text;
  }

  ${(props) =>
    props.$active &&
    `
      color: #3267B9;
      background-clip: text;
      -webkit-background-clip: text;
  `}
`;

const MobileMenuButton = styled.button`
  display: block;
  background: none;
  border: none;
  color: white;
  font-size: 2.5rem;
  cursor: pointer;
  transition: transform 0.2s ease;

  &:hover {
    color: #0ea8b6;
  }

  &:active {
    transform: scale(0.9);
  }

  @media (min-width: 768px) {
    display: none;
  }
`;

const MobileMenu = styled.nav`
  background: rgba(0, 0, 0, 0.9);
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  max-height: ${(props) => (props.$isOpen ? "400px" : "0")};
  overflow: hidden;
  opacity: ${(props) => (props.$isOpen ? 1 : 0)};
  transform: ${(props) => (props.$isOpen ? "translateY(0)" : "translateY(-10px)")};
  transition: max-height 0.3s ease, opacity 0.3s ease, transform 0.3s ease;

  a {
    display: block;
    padding: 0.5rem 1rem;
    color: white;
    text-decoration: none;

    &:hover {
      background: #333;
    }
  }
`;

const Header = () => {
  const [isMenuOpen, setIsMenuOpen] = useState(false);
  const location = useLocation();
  const menuRef = useRef(null); // Ref for the menu
  const buttonRef = useRef(null); // Ref for the button

  const toggleMenu = () => setIsMenuOpen(prev => !prev);

  // Close menu if clicking outside
  useEffect(() => {
    const handleClickOutside = (event) => {
      if (
        menuRef.current &&
        !menuRef.current.contains(event.target) && // Not inside the menu
        buttonRef.current &&
        !buttonRef.current.contains(event.target) // Not the button
      ) {
        setIsMenuOpen(false);
      }
    };

    document.addEventListener("mousedown", handleClickOutside);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, []);

  // Close menu on navigation
  useEffect(() => {
    setIsMenuOpen(false);
  }, [location.pathname]);

  return (
    <HeaderContainer>
      <NavWrapper>
        <SLogo to="/">
          <img src={Logo} alt="Logo" />
        </SLogo>

        <DesktopMenu>
        {["/vardig", "/about", "/blogs", "/jobs", "/contact"].map((path, i) => (
          <MenuLink key={i} to={path} $active={location.pathname === path}>
            {path === "/vardig"
              ? "VarDiG"
              : path.replace("/", "").charAt(0).toUpperCase() +
                path.replace("/", "").slice(1)}
          </MenuLink>
        ))}
        </DesktopMenu>

        <MobileMenuButton
          onClick={toggleMenu}
          aria-expanded={isMenuOpen}
          ref={buttonRef}
        >

        </MobileMenuButton>
      </NavWrapper>

      <MobileMenu $isOpen={isMenuOpen} ref={menuRef} data-testid="mobile-menu">
        {["/", "/vardig", "/about", "/blogs", "/jobs", "/contact"].map((path, i) => (
          <Link
            key={i}
            to={path}
            onClick={() => setIsMenuOpen(false)} // Close on click
            style={{  
              backgroundColor: location.pathname === path ? "#1d9b62" : "transparent",
            }}
          >
            {path === "/"
              ? "Home"
              : path.replace("/", "").charAt(0).toUpperCase() +
                path.replace("/", "").slice(1)}
          </Link>
        ))}
      </MobileMenu>
    </HeaderContainer>
  );
};

export default Header;