(function () {
const shopPath = "/shop-online";
const categories = [
["Wine", "/shop-online/wine"],
["Cheese", "/shop-online/cheese"],
["Gifts", "/shop-online/gifts"],
["Build Your Own Hamper", "/shop-online/build-your-own-hamper-ingredients"],
["Luxury Groceries", "/shop-online/groceries"],
["Charcuterie and Smokehouse", "/shop-online/charcuterie-smokehouse"],
["Crackers for Cheese", "/shop-online/crackers"],
["Coffee, Tea and Hot Chocolate", "/shop-online/coffee-tea-hot-chocolate"],
["Jams & Preserves", "/shop-online/jams-preserves"],
["Spirits", "/shop-online/spirits"],
["Cocktails", "/shop-online/cocktails"],
["Savoury Snacks", "/shop-online/savoury-snacks"],
["Beer & Cider", "/shop-online/beer"],
["Sweet Treats", "/shop-online/sweet-treats"],
["Bread", "/shop-online/bread"],
["Shop All", "/shop-online"]
];
function addCategoryNavigation() {
const path = window.location.pathname.replace(/\/$/, "");
// Only show on individual shop category pages
if (!path.startsWith(shopPath + "/")) return;
// Avoid adding the menu more than once
if (document.querySelector(".stonewell-main-categories")) return;
const main = document.querySelector("main") || document.body;
// Find the category page heading
const headings = Array.from(
main.querySelectorAll("h1, h2")
);
const heading = headings.find(el => {
return el.textContent.trim().length > 0;
});
if (!heading) return;
const nav = document.createElement("nav");
nav.className = "stonewell-main-categories";
nav.setAttribute("aria-label", "Shop categories");
const links = document.createElement("div");
links.className = "stonewell-main-categories__links";
categories.forEach(function (category) {
const link = document.createElement("a");
link.textContent = category[0];
link.href = category[1];
if (path === category[1]) {
link.classList.add("is-current");
link.setAttribute("aria-current", "page");
}
links.appendChild(link);
});
nav.appendChild(links);
heading.parentNode.insertBefore(nav, heading);
}
// Run on initial page load
if (document.readyState === "loading") {
document.addEventListener(
"DOMContentLoaded",
addCategoryNavigation
);
} else {
addCategoryNavigation();
}
// Support Squarespace page navigation
const observer = new MutationObserver(function () {
addCategoryNavigation();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();