|
1 | 1 | /* eslint-disable no-param-reassign, no-undef */ |
| 2 | + |
2 | 3 | import MoveTo from 'moveto'; |
3 | 4 |
|
4 | 5 | const initAnchors = () => { |
5 | 6 | const easeFunctions = { |
6 | 7 | easeInQuad(t, b, c, d) { t /= d; return c * t * t + b; }, |
7 | | - easeOutQuad(t, b, c, d) { |
8 | | - t /= d; return -c * t * (t - 2) + b; |
9 | | - }, |
| 8 | + easeOutQuad(t, b, c, d) { t /= d; return -c * t * (t - 2) + b; }, |
10 | 9 | }; |
11 | 10 |
|
12 | 11 | const moveTo = new MoveTo( |
13 | 12 | { ease: 'easeInQuad' }, |
14 | 13 | easeFunctions, |
15 | 14 | ); |
16 | 15 |
|
17 | | - // Get all links that have only the hash as href and is not back to top link |
18 | | - const triggers = document.querySelectorAll('a[href*="#"]:not([href="#"]):not(#top)'); |
| 16 | + let triggers = document.querySelectorAll('a[href*="#"]:not([href="#"]):not(#top)'); |
| 17 | + |
| 18 | + triggers = Array.from(triggers); |
19 | 19 |
|
20 | | - // eslint-disable-next-line no-plusplus |
21 | | - for (let i = 0; i < triggers.length; i++) { |
22 | | - // Move to target smoothly |
23 | | - moveTo.registerTrigger(triggers[i]); |
24 | | - const target = document.getElementById(triggers[i].hash.substring(1)); |
| 20 | + triggers.forEach((trigger) => { |
| 21 | + moveTo.registerTrigger(trigger); |
| 22 | + const targetId = trigger.hash.substring(1); |
| 23 | + const target = document.getElementById(targetId); |
25 | 24 |
|
26 | | - // If the trigger is nav-link, close nav |
27 | | - if (triggers[i].classList.contains('nav-link')) { |
28 | | - document.body.classList.remove('js-nav-active'); |
29 | | - } |
| 25 | + trigger.addEventListener('click', (event) => { |
| 26 | + event.preventDefault(); // Prevent default behavior of anchor links |
30 | 27 |
|
31 | | - triggers[i].addEventListener('click', () => { |
32 | 28 | // If the trigger is nav-link, close nav |
33 | | - if (triggers[i].classList.contains('nav-link')) { |
| 29 | + if (trigger.classList.contains('nav-link') || trigger.classList.contains('dropdown-item')) { |
34 | 30 | document.body.classList.remove('js-nav-active'); |
| 31 | + |
| 32 | + // Additional navigation cleanup |
| 33 | + const html = document.documentElement; |
| 34 | + const container = document.getElementById('main-navigation-wrapper'); |
| 35 | + const menu = container?.querySelector('ul'); |
| 36 | + const button = document.getElementById('nav-toggle'); |
| 37 | + |
| 38 | + if (html) html.classList.remove('disable-scroll'); |
| 39 | + if (container) container.classList.remove('is-active'); |
| 40 | + if (button) { |
| 41 | + button.classList.remove('is-active'); |
| 42 | + button.setAttribute('aria-expanded', 'false'); |
| 43 | + } |
| 44 | + if (menu) menu.setAttribute('aria-expanded', 'false'); |
35 | 45 | } |
36 | 46 |
|
37 | | - // Focus to target |
| 47 | + // Check if the target element exists on the current page |
38 | 48 | if (target) { |
39 | | - // Needs delay for smooth moveTo scroll |
| 49 | + // Scroll to the target element |
| 50 | + moveTo.move(target); |
| 51 | + |
| 52 | + // Update URL history |
| 53 | + window.history.pushState('', '', trigger.hash); |
| 54 | + |
| 55 | + // Focus on the target element after a delay |
40 | 56 | setTimeout(() => { |
41 | 57 | target.setAttribute('tabindex', '-1'); |
42 | 58 | target.focus(); |
43 | 59 | }, 500); |
| 60 | + } else { |
| 61 | + // Navigate to the target page |
| 62 | + window.location.href = trigger.href; |
44 | 63 | } |
45 | 64 | }); |
46 | | - } |
| 65 | + }); |
47 | 66 | }; |
48 | 67 |
|
49 | 68 | export default initAnchors; |
0 commit comments