-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavbar.js
More file actions
163 lines (148 loc) · 5.08 KB
/
navbar.js
File metadata and controls
163 lines (148 loc) · 5.08 KB
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
class CustomNavbar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
// Create navbar container
const navbar = document.createElement("nav");
navbar.classList.add("navbar");
// Navbar HTML structure
navbar.innerHTML = `
<div class="logo">MyLogo</div>
<input type="checkbox" id="menu-toggle" class="menu-toggle">
<label for="menu-toggle" class="hamburger">☰</label>
<ul class="nav-links">
<li><a href="#">Home</a></li>
<a href="#">About ⌄</a>
<li><button class="theme-toggle">🌗</button></li>
</ul>
`;
// Add styles
const style = document.createElement("style");
style.textContent = `
:host {
--navbar-bg: #222;
--navbar-text: white;
--button-bg: crimson;
--button-hover: darkred;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background: var(--navbar-bg);
color: var(--navbar-text);
padding: 10px 20px;
font-family: Arial, sans-serif;
position: relative;
}
.logo {
font-size: 20px;
font-weight: bold;
}
.nav-links {
list-style: none;
display: flex;
gap: 20px;
padding: 0;
margin: 0;
align-items: center;
}
.nav-links li {
display: inline-block;
position: relative;
}
.nav-links a {
text-decoration: none;
color: var(--navbar-text);
font-size: 16px;
padding: 5px;
}
.nav-links .search {
padding: 5px;
border-radius: 5px;
border: none;
}
.theme-toggle {
background: var(--button-bg);
color: white;
padding: 8px 12px;
border: none;
cursor: pointer;
font-size: 14px;
transition: 0.3s;
}
.theme-toggle:hover {
background: var(--button-hover);
}
/* Dropdown */
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
background: var(--navbar-bg);
list-style: none;
padding: 5px;
border-radius: 5px;
width: 120px;
}
.dropdown:hover .dropdown-menu {
display: block;
}
/* Hamburger Menu (Mobile) */
.hamburger {
display: none;
font-size: 24px;
cursor: pointer;
}
.menu-toggle {
display: none;
}
@media (max-width: 768px) {
.hamburger {
display: block;
}
.nav-links {
display: none;
flex-direction: column;
position: absolute;
top: 50px;
left: 0;
background: var(--navbar-bg);
width: 100%;
padding: 10px 0;
}
.menu-toggle:checked + .hamburger + .nav-links {
display: flex;
}
}
`;
// Append styles & navbar to Shadow DOM
this.shadowRoot.append(style, navbar);
const hamburger = navbar.querySelector(".hamburger");
const navLinks = navbar.querySelector(".nav-links");
hamburger.addEventListener("click", () => {
navLinks.classList.toggle("open");
});
// Dark Mode Toggle
const themeToggleBtn = navbar.querySelector(".theme-toggle");
themeToggleBtn.addEventListener("click", () => {
const isDark = this.getAttribute("theme") === "dark";
this.setAttribute("theme", isDark ? "light" : "dark");
});
}
static get observedAttributes() {
return ["theme"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "theme") {
const isDark = newValue === "dark";
this.shadowRoot.host.style.setProperty("--navbar-bg", isDark ? "#fff" : "#222");
this.shadowRoot.host.style.setProperty("--navbar-text", isDark ? "#000" : "white");
this.shadowRoot.host.style.setProperty("--button-bg", isDark ? "blue" : "crimson");
this.shadowRoot.host.style.setProperty("--button-hover", isDark ? "darkblue" : "darkred");
}
}
}
// Register the component
customElements.define("custom-navbar", CustomNavbar);