-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
56 lines (47 loc) · 1.3 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { setCurrentPageLink } from "./menu.js";
function getHamburgerContentNextState(content) {
return content.textContent.includes('Open menu') ? 'Close menu' : 'Open menu'
}
function getId(clickedLink) {
const {
hash
} = clickedLink;
return hash;
}
function removeCurrentPageLinkAttributes(currentPageLink) {
if (currentPageLink) {
currentPageLink.removeAttribute('aria-current');
currentPageLink.classList.remove('header__link--current');
}
}
function toggleAttribute(el, attrName, attrValue) {
if (el.getAttribute(attrName)) {
el.removeAttribute(attrName);
} else {
el.setAttribute(attrName, attrValue);
}
}
function toggleCurrentPageLinkAttributes(link) {
toggleAttribute(link, 'aria-current', 'page');
link.classList.toggle('header__link--current');
}
function updateHash(clickedLink, state = {}) {
history.pushState(state, '', clickedLink.href);
}
function isOpen(hamburger) {
return hamburger.getAttribute('aria-expanded') === 'true';
}
function setCurrentPageLinkOnReload() {
if (location.hash) {
const link = document.querySelector(`.header__link[href="${location.hash}"]`);
setCurrentPageLink({ target: link });
}
}
export {
updateHash,
toggleCurrentPageLinkAttributes,
getId,
getHamburgerContentNextState,
isOpen,
setCurrentPageLinkOnReload
}