Skip to content

Commit f81ef7d

Browse files
authored
Merge pull request #1290 from makeabilitylab/1288-navbar-collapse-vanilla
Vanilla navbar collapse; remove dead back-to-top + jQuery Easing (#1288)
2 parents 46220ac + 24968cf commit f81ef7d

6 files changed

Lines changed: 73 additions & 193 deletions

File tree

website/static/website/css/backtop.css

Lines changed: 0 additions & 83 deletions
This file was deleted.
-548 Bytes
Binary file not shown.

website/static/website/js/backtop.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

website/static/website/js/jquery.easing.min.js

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,76 @@
11
/*!
2-
* Based on Grayscale:
3-
*
4-
* Start Bootstrap - Grayscale Bootstrap Theme (http://startbootstrap.com)
5-
* Code licensed under the Apache License v2.0.
6-
* For details, see http://www.apache.org/licenses/LICENSE-2.0.
2+
* Top navbar behavior — vanilla JS, no jQuery.
3+
*
4+
* Drives the responsive (mobile) navbar collapse, replacing Bootstrap 3's
5+
* collapse data-api as part of the jQuery / Bootstrap-JS removal
6+
* (Track A, see issues #1288 / #1253). The desktop layout is handled purely
7+
* by Bootstrap's `.navbar-collapse` CSS (shown at >= 768px regardless of the
8+
* `in` class); this script only toggles the `in` class at mobile widths.
9+
*
10+
* Behavior:
11+
* - Toggle button opens/closes the menu and keeps `aria-expanded` in sync.
12+
* - Clicking a nav link closes the open menu.
13+
* - Clicking outside the navbar closes the open menu.
14+
* - Escape closes the open menu and returns focus to the toggle (a11y).
15+
*
16+
* Note: the previous version also added a `top-nav-scrolled` class on scroll
17+
* (no CSS rule existed for it — a no-op) and a jQuery-Easing "page-scroll"
18+
* smooth scroll that only targeted the logo's page URL (it errored and fell
19+
* through to normal navigation). Both were dead and have been dropped.
720
*/
21+
(function () {
22+
'use strict';
823

9-
// $(document).ready(function() {
10-
// // Your code here
11-
// console.log("LOADING TOP NAVBAR JS!!!");
12-
// console.log("navbar-white", "{{navbar_white}}");
13-
// });
14-
15-
// jQuery to collapse the navbar on scroll
16-
$(window).scroll(function() {
17-
if ($(".navbar").offset().top > 50) {
18-
$(".navbar-fixed-top").addClass("top-nav-scrolled");
19-
} else {
20-
$(".navbar-fixed-top").removeClass("top-nav-scrolled");
24+
document.addEventListener('DOMContentLoaded', function () {
25+
var toggle = document.querySelector('.navbar-toggle');
26+
var collapse = document.getElementById('navbar-main-collapse');
27+
if (!toggle || !collapse) {
28+
return;
2129
}
22-
});
23-
24-
// jQuery for page scrolling feature - requires jQuery Easing plugin
25-
$(function() {
26-
$('a.page-scroll').bind('click', function(event) {
27-
var $anchor = $(this);
28-
$('html, body').stop().animate({
29-
scrollTop: $($anchor.attr('href')).offset().top
30-
}, 1500, 'easeInOutExpo');
31-
event.preventDefault();
30+
31+
function isOpen() {
32+
return collapse.classList.contains('in');
33+
}
34+
35+
function openMenu() {
36+
collapse.classList.add('in');
37+
toggle.setAttribute('aria-expanded', 'true');
38+
}
39+
40+
function closeMenu() {
41+
collapse.classList.remove('in');
42+
toggle.setAttribute('aria-expanded', 'false');
43+
}
44+
45+
toggle.addEventListener('click', function (event) {
46+
event.preventDefault();
47+
if (isOpen()) {
48+
closeMenu();
49+
} else {
50+
openMenu();
51+
}
52+
});
53+
54+
// Close the open mobile menu when a nav link inside it is clicked.
55+
collapse.addEventListener('click', function (event) {
56+
if (isOpen() && event.target.closest('a')) {
57+
closeMenu();
58+
}
3259
});
33-
});
3460

35-
// Closes the Responsive Menu on Menu Item Click
36-
$('.navbar-collapse ul li a').click(function() {
37-
$('.navbar-toggle:visible').click();
38-
});
61+
// Close the open menu when clicking anywhere outside the navbar.
62+
document.addEventListener('click', function (event) {
63+
if (isOpen() && !event.target.closest('.navbar')) {
64+
closeMenu();
65+
}
66+
});
67+
68+
// Close on Escape and restore focus to the toggle for keyboard users.
69+
document.addEventListener('keydown', function (event) {
70+
if (event.key === 'Escape' && isOpen()) {
71+
closeMenu();
72+
toggle.focus();
73+
}
74+
});
75+
});
76+
})();

website/templates/website/base.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
<link rel="stylesheet" href="{% static 'website/css/carousel_fade.css' %}">
8989
<link rel="stylesheet" href="{% static 'website/css/base.css' %}">
9090
<link rel="stylesheet" href="{% static 'website/css/top-navbar.css' %}">
91-
<link rel="stylesheet" href="{% static 'website/css/backtop.css' %}">
9291
<link rel="stylesheet"
9392
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css"
9493
integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg=="
@@ -103,7 +102,6 @@
103102
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
104103
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
105104
crossorigin="anonymous"></script>
106-
<script src="{% static 'website/js/jquery.easing.min.js' %}"></script>
107105
<script src="{% static 'website/js/top-navbar.js' %}"></script>
108106

109107
{% block external_scripts %}{% endblock %}
@@ -150,18 +148,16 @@
150148
<div class="col-xs-12">
151149

152150
<div class="navbar-header">
153-
<button type="button"
154-
class="navbar-toggle"
155-
data-toggle="collapse"
156-
data-target=".navbar-main-collapse"
151+
<button type="button"
152+
class="navbar-toggle"
157153
aria-expanded="false"
158154
aria-controls="navbar-main-collapse"
159155
aria-label="Toggle navigation menu">
160156
<i class="fa fa-bars" aria-hidden="true"></i>
161157
<span class="sr-only">Menu</span>
162158
</button>
163159

164-
<a class="navbar-brand page-scroll" href="{% url 'website:index' %}"
160+
<a class="navbar-brand" href="{% url 'website:index' %}"
165161
aria-label="Makeability Lab home">
166162
<img src="{% static 'website/img/logos/makelab_logo_white_no_text_100x67.png' %}"
167163
alt=""

0 commit comments

Comments
 (0)