Skip to content

Commit 8079b78

Browse files
committed
mobile menu
1 parent ae5a54a commit 8079b78

4 files changed

Lines changed: 188 additions & 14 deletions

File tree

themes/usips/sass/_base.scss

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,7 @@ blockquote {
645645
display: none !important;
646646
}
647647

648-
.main-nav {
649-
ul {
650-
gap: 1rem; // Reduce gap between items on mobile
651-
}
652-
}
648+
// Mobile navigation styles are now handled in _header.scss
653649

654650
// Ensure "always-show" items are visible and properly spaced
655651
.always-show {

themes/usips/sass/_header.scss

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
display: flex;
99
justify-content: space-between;
1010
align-items: stretch;
11+
position: relative;
1112
}
1213
}
1314

@@ -38,6 +39,40 @@
3839
}
3940
}
4041

42+
.mobile-menu-toggle {
43+
display: none;
44+
flex-direction: column;
45+
justify-content: center;
46+
align-items: center;
47+
background: none;
48+
border: none;
49+
cursor: pointer;
50+
padding: 10px;
51+
z-index: 1001;
52+
53+
span {
54+
display: block;
55+
width: 25px;
56+
height: 3px;
57+
background-color: $text-black;
58+
margin: 3px 0;
59+
transition: 0.3s;
60+
transform-origin: center;
61+
}
62+
63+
&.active span:nth-child(1) {
64+
transform: rotate(45deg) translate(6px, 6px);
65+
}
66+
67+
&.active span:nth-child(2) {
68+
opacity: 0;
69+
}
70+
71+
&.active span:nth-child(3) {
72+
transform: rotate(-45deg) translate(6px, -6px);
73+
}
74+
}
75+
4176
#main-nav {
4277

4378
ul {
@@ -58,8 +93,11 @@
5893
color: $text-hover-color;
5994
background: $tertiary-color;
6095

61-
.submenu {
62-
display: block;
96+
97+
@media (min-width: 769px) {
98+
.submenu {
99+
display: block;
100+
}
63101
}
64102
}
65103
}
@@ -73,8 +111,6 @@
73111
text-align: right;
74112
}
75113

76-
span {}
77-
78114
.submenu {
79115
display: none;
80116
position: absolute;
@@ -98,4 +134,97 @@
98134
padding: 0.5em 1em;
99135
}
100136
}
137+
}
138+
139+
// Mobile styles
140+
@media (max-width: 768px) {
141+
.mobile-menu-toggle {
142+
display: flex;
143+
}
144+
145+
#main-nav {
146+
position: absolute;
147+
top: 100%;
148+
left: 0;
149+
right: 0;
150+
background-color: $background-color;
151+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
152+
transform: translateY(-100%);
153+
opacity: 0;
154+
visibility: hidden;
155+
transition: all 0.3s ease;
156+
z-index: 1000;
157+
158+
&.mobile-open {
159+
transform: translateY(0);
160+
opacity: 1;
161+
visibility: visible;
162+
}
163+
164+
ul {
165+
flex-direction: column;
166+
align-items: stretch;
167+
height: auto;
168+
padding: 0;
169+
}
170+
171+
li {
172+
height: auto;
173+
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
174+
175+
&:hover {
176+
background: none;
177+
}
178+
179+
&.has-submenu>a::after {
180+
content: '';
181+
margin-left: auto;
182+
transition: transform 0.3s ease;
183+
}
184+
185+
&.submenu-open>a::after {
186+
transform: rotate(180deg);
187+
}
188+
}
189+
190+
a {
191+
padding: 1rem;
192+
height: auto;
193+
justify-content: space-between;
194+
border: none;
195+
}
196+
197+
.submenu {
198+
position: static;
199+
display: none;
200+
background: rgba(0, 0, 0, 0.05);
201+
border: none;
202+
box-shadow: none;
203+
204+
ul {
205+
padding-left: 1rem;
206+
}
207+
208+
li {
209+
border-bottom: none;
210+
211+
&:hover {
212+
background: rgba(0, 0, 0, 0.05);
213+
}
214+
}
215+
216+
a {
217+
padding: 0.75rem 1rem;
218+
align-items: flex-start;
219+
}
220+
}
221+
222+
.submenu-open .submenu {
223+
display: block;
224+
}
225+
}
226+
227+
.hide-mobile {
228+
display: none !important;
229+
}
101230
}

themes/usips/templates/base.html

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,50 @@
2323

2424
{% include "macros/footer.html" %}
2525

26-
<!-- TODO: Add JS? -->
27-
<!-- <script src="{{ get_url(path='js/main.js') }}?v={{ config.extra.build_timestamp }}"></script> -->
26+
27+
<script>
28+
document.addEventListener('DOMContentLoaded', function () {
29+
const mobileToggle = document.getElementById('mobile-menu-toggle');
30+
const mainNav = document.getElementById('main-nav');
31+
const submenuToggles = document.querySelectorAll('.submenu-toggle');
32+
33+
// Toggle main mobile menu
34+
mobileToggle.addEventListener('click', function () {
35+
mainNav.classList.toggle('mobile-open');
36+
this.classList.toggle('active');
37+
});
38+
39+
// Toggle submenus on mobile
40+
submenuToggles.forEach(function (toggle) {
41+
toggle.addEventListener('click', function (e) {
42+
if (window.innerWidth <= 768) {
43+
e.preventDefault();
44+
const parentLi = this.closest('li');
45+
parentLi.classList.toggle('submenu-open');
46+
}
47+
});
48+
});
49+
50+
// Close mobile menu when clicking outside
51+
document.addEventListener('click', function (e) {
52+
if (!mainNav.contains(e.target) && !mobileToggle.contains(e.target)) {
53+
mainNav.classList.remove('mobile-open');
54+
mobileToggle.classList.remove('active');
55+
}
56+
});
57+
58+
// Handle window resize
59+
window.addEventListener('resize', function () {
60+
if (window.innerWidth > 768) {
61+
mainNav.classList.remove('mobile-open');
62+
mobileToggle.classList.remove('active');
63+
document.querySelectorAll('.has-submenu').forEach(function (item) {
64+
item.classList.remove('submenu-open');
65+
});
66+
}
67+
});
68+
});
69+
</script>
2870

2971
</body>
3072

themes/usips/templates/macros/header.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
<img src="{{ get_url(path='image/logo.small.png') }}" alt="{{ config.title }}" class="logo-img" />
55
<span class="logo-text">{{ config.title }}</span>
66
</a>
7+
<button id="mobile-menu-toggle" class="mobile-menu-toggle" aria-label="Toggle mobile menu">
8+
<span></span>
9+
<span></span>
10+
<span></span>
11+
</button>
712
<nav id="main-nav">
813
<ul>
914
{% for item in config.extra.menu %}
1015
{% if 'desktop' in item.show_in %}
11-
<li class="{% if 'mobile' not in item.show_in %}hide-mobile{% endif %}">
12-
<a href="{{ item.url | safe }}"><span>{{ item.name }}</span></a>
16+
<li class="{% if item.submenu %} has-submenu{% endif %}">
17+
<a href="{{ item.url | safe }}" {% if item.submenu %} class="submenu-toggle" {% endif %}><span>{{ item.name
18+
}}</span></a>
1319
{% if item.submenu %}
1420
<nav class="submenu">
1521
<ul>
@@ -19,7 +25,8 @@
1925
</li>
2026
{% endfor %}
2127
</ul>
22-
{% endif %}
28+
</nav>
29+
{% endif %}
2330
</li>
2431
{% endif %}
2532
{% endfor %}

0 commit comments

Comments
 (0)