Skip to content

Commit 6093bb2

Browse files
authored
Merge pull request #250 from digitoimistodude/DEV-262
working anchor links for both outside and inside links, link DEV-262
2 parents ae1a503 + ac1e2b6 commit 6093bb2

File tree

8 files changed

+45
-26
lines changed

8 files changed

+45
-26
lines changed

.github/workflows/html.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: push
55
jobs:
66
build:
77
name: Test HTML and accessibility with NU HTML Checker
8-
runs-on: ubuntu-20.04
8+
runs-on: ubuntu-22.04
99

1010
# Test on macOS:
1111
# brew install openjdk

.github/workflows/js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [push, pull_request]
55
jobs:
66
build:
77
name: Test JS
8-
runs-on: ubuntu-20.04
8+
runs-on: ubuntu-22.04
99

1010
steps:
1111
- name: Checkout the repository

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111
jobs:
1212
build:
1313
name: Test php
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-22.04
1515

1616
steps:
1717
- name: Checkout the repository

.github/workflows/php8.3.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111
jobs:
1212
build:
1313
name: Test for PHP 8.3 support
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-22.04
1515

1616
steps:
1717
- name: Checkout the repository

.github/workflows/styles.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [push, pull_request]
55
jobs:
66
build:
77
name: Test styles
8-
runs-on: ubuntu-20.04
8+
runs-on: ubuntu-22.04
99

1010
steps:
1111
- name: Checkout the repository

js/dev/front-end.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/prod/front-end.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/modules/anchors.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,68 @@
11
/* eslint-disable no-param-reassign, no-undef */
2+
23
import MoveTo from 'moveto';
34

45
const initAnchors = () => {
56
const easeFunctions = {
67
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; },
109
};
1110

1211
const moveTo = new MoveTo(
1312
{ ease: 'easeInQuad' },
1413
easeFunctions,
1514
);
1615

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);
1919

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);
2524

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
3027

31-
triggers[i].addEventListener('click', () => {
3228
// 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')) {
3430
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');
3545
}
3646

37-
// Focus to target
47+
// Check if the target element exists on the current page
3848
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
4056
setTimeout(() => {
4157
target.setAttribute('tabindex', '-1');
4258
target.focus();
4359
}, 500);
60+
} else {
61+
// Navigate to the target page
62+
window.location.href = trigger.href;
4463
}
4564
});
46-
}
65+
});
4766
};
4867

4968
export default initAnchors;

0 commit comments

Comments
 (0)