2026-01-02 17:17:35 -08:00
|
|
|
/* Nav construction */
|
|
|
|
|
const cards = [
|
|
|
|
|
{
|
|
|
|
|
title: "card one",
|
|
|
|
|
href: "/card-one/"
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: "card two",
|
|
|
|
|
href: "/card-two/"
|
2026-01-07 14:58:47 -08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: "card three",
|
|
|
|
|
href: "/card-three/"
|
|
|
|
|
}
|
2026-01-02 17:17:35 -08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const constructMenuLink = function(title, href) {
|
|
|
|
|
const path = window.location.pathname;
|
|
|
|
|
|
|
|
|
|
const a = document.createElement("a");
|
|
|
|
|
a.href = href;
|
|
|
|
|
a.title = title;
|
|
|
|
|
a.innerHTML = title;
|
|
|
|
|
if (path === href) a.id = "current-page";
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-02 17:52:59 -08:00
|
|
|
const constructSkipLink = function() {
|
2026-01-02 18:12:36 -08:00
|
|
|
const a = constructMenuLink("skip to main", "#main");
|
2026-01-02 17:52:59 -08:00
|
|
|
a.id = "skip";
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-02 17:17:35 -08:00
|
|
|
const constructDropdown = function() {
|
|
|
|
|
const div = document.createElement("div");
|
|
|
|
|
div.id = "dropdown";
|
|
|
|
|
|
|
|
|
|
const button = document.createElement("button");
|
|
|
|
|
button.innerHTML = "cards ⮷";
|
|
|
|
|
button.id="drop-button";
|
|
|
|
|
button.ariaLabel = "card submenu";
|
|
|
|
|
button.ariaExpanded = "false";
|
2026-01-02 17:52:59 -08:00
|
|
|
button.type = "button";
|
2026-01-02 17:17:35 -08:00
|
|
|
button.setAttribute("aria-controls", "drop-content");
|
|
|
|
|
div.append(button);
|
|
|
|
|
|
|
|
|
|
const ul = document.createElement("ul");
|
|
|
|
|
ul.id = "drop-content";
|
|
|
|
|
ul.ariaHidden = "true";
|
|
|
|
|
|
|
|
|
|
for (const card of cards) {
|
|
|
|
|
const li = document.createElement("li");
|
|
|
|
|
li.append(constructMenuLink(card.title, card.href));
|
|
|
|
|
ul.append(li);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div.append(ul);
|
|
|
|
|
return div;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const constructNav = function() {
|
|
|
|
|
const nav = document.getElementById("top-nav");
|
2026-01-02 17:52:59 -08:00
|
|
|
nav.append(constructSkipLink());
|
2026-01-02 17:17:35 -08:00
|
|
|
nav.append(constructMenuLink("home", "/"));
|
|
|
|
|
nav.append(constructDropdown());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructNav();
|
|
|
|
|
|
|
|
|
|
/* Dropdown handling */
|
|
|
|
|
|
|
|
|
|
const dropdown = document.getElementById("dropdown");
|
|
|
|
|
const dropdownButton = document.getElementById("drop-button");
|
|
|
|
|
const dropdownContent = document.getElementById("drop-content");
|
|
|
|
|
|
|
|
|
|
const dropdownItems = dropdownContent.querySelectorAll("a");
|
|
|
|
|
const firstDropdownItem = dropdownItems[0];
|
|
|
|
|
const lastDropdownItem = dropdownItems[dropdownItems.length - 1];
|
|
|
|
|
|
|
|
|
|
const openDropdown = function() {
|
2026-01-07 14:58:47 -08:00
|
|
|
dropdownContent.classList.add("show");
|
2026-01-02 17:17:35 -08:00
|
|
|
dropdownContent.setAttribute("aria-hidden", "false");
|
|
|
|
|
dropdownButton.setAttribute("aria-expanded", "true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const closeDropdown = function() {
|
|
|
|
|
if (dropdownButton.ariaExpanded === "true") {
|
|
|
|
|
dropdownContent.classList.remove("show");
|
|
|
|
|
dropdownContent.setAttribute("aria-hidden", "true");
|
|
|
|
|
dropdownButton.setAttribute("aria-expanded", "false");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dropdownButton.addEventListener("click", (event) => {
|
|
|
|
|
if (dropdownButton.ariaExpanded === "false") openDropdown();
|
|
|
|
|
else closeDropdown();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dropdownButton.addEventListener("keydown", (event) => {
|
|
|
|
|
if (event.key === "Enter" || event.key === " ") { // Space or Enter key
|
|
|
|
|
event.preventDefault(); // Prevent the default action to stop scrolling when pressing Space
|
|
|
|
|
if (dropdownButton.ariaExpanded === "false") {
|
|
|
|
|
openDropdown();
|
|
|
|
|
} else {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
firstDropdownItem.addEventListener("keydown", (event) => {
|
2026-01-02 18:03:23 -08:00
|
|
|
if (event.key === "Tab" && event.shiftKey) closeDropdown();
|
2026-01-02 17:17:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
lastDropdownItem.addEventListener("keydown", (event) => {
|
2026-01-02 18:03:23 -08:00
|
|
|
if (event.key === "Tab" && !event.shiftKey) closeDropdown();
|
2026-01-02 17:17:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.addEventListener("keydown", (event) => {
|
2026-01-02 18:03:23 -08:00
|
|
|
if (event.key === "Escape") {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
dropdownButton.focus();
|
|
|
|
|
}
|
2026-01-02 17:17:35 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.addEventListener("click", (event) => {
|
|
|
|
|
if (!event.target.matches("#drop-button")) closeDropdown();
|
|
|
|
|
});
|
2026-01-02 18:21:23 -08:00
|
|
|
|
|
|
|
|
/* footer */
|
|
|
|
|
|
|
|
|
|
let footerHTML = "<p>brought to you in 2026</p>";
|
|
|
|
|
document.getElementById("footer").innerHTML = footerHTML;
|