|
1 | 1 | /*! |
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. |
7 | 20 | */ |
| 21 | +(function () { |
| 22 | + 'use strict'; |
8 | 23 |
|
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; |
21 | 29 | } |
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 | + } |
32 | 59 | }); |
33 | | -}); |
34 | 60 |
|
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 | +})(); |
0 commit comments