Clickable Menu Folders

The SQSP Library of Code is a compilation of different bits of html, CSS and Javascript that I’ve used in my own web design. Rather than keeping the links from creators all over the internet stored locally I thought I’d share them instead! Keep these at hand by subscribing to the foubography rss feed or head to foubography’s instagram and save the posts.

 

This is probably one of the handiest bits of CSS code I put in every Squarespace website I design (that has folders). It’s a bit more complicated than usual, but worth it. Will Meyers explains it brilliantly in his YouTube video and you can copy the code from his website here.

Add the below code into your Settings » Advanced » Code Injection » Footer area.

<script>
(function () {
  let folders;
  function rebuildAnchor(folder) {
    let parent = folder.closest('.header-nav-item--folder'),
        href = folder.href.includes('.com') ? folder.href.split('.com')[1].replace("-folder/", "") : folder.href.replace("-folder/", ""),
        anchorClone = folder.cloneNode(true);
    anchorClone.classList.add('clickable-folder');
    anchorClone.setAttribute('href', href);
    anchorClone.style.cssText = `
          opacity: 1;
          transform: unset;
    `;
    parent.insertAdjacentElement('afterbegin', anchorClone);
    if (href == window.location.pathname) {
      anchorClone.closest('.header-nav-item--folder').classList.add('header-nav-item--active')
    }
  }
  function addToMobile(folder) {
    let href = folder.getAttribute("href"),
        hrefAdjusted = href.includes('.com') ? href.split('.com')[1].replace("-folder/", "") : href.replace("-folder/", ""),
        text = folder.innerText,
        newText = `All ${text}`,
        mobileFolder = document.querySelector(`[data-folder="${href}"]`),
        backButton = mobileFolder.querySelector(".header-menu-nav-folder-content > *:first-of-type"),
        allButton = `<div class="container header-menu-nav-item header-menu-nav-item--external">
  				<a href="${hrefAdjusted}">${newText}</a>
			<div>`;
    backButton.insertAdjacentHTML('afterend', allButton)
  }
  /* Select All Folder Links &  */
  function setFolderLinks() {
    folders = document.querySelectorAll('.header-display-desktop .header-nav-folder-title[href*="-folder/"]');
    for (let folder of folders) {
      window.addEventListener('load', function() {
        addToMobile(folder);
        rebuildAnchor(folder);
        folder.remove();
      });
    }
  }
  setFolderLinks();
})();
</script>
 

Related posts

Sarah Foubert

I’m a photographer and Squarespace web designer based in Altrincham, South Manchester. I specialise in portrait, landscape and opera photography and also offer a “complete website package” where I can offer headshots, photos of your business and design your new website so you can manage it yourself afterwards.

Over the years I’ve built a portfolio career as a professional photographer, opera singer, wardrobe assistant and singing teacher. I have a MMus from the RNCM, a Bachelor in Theatre Sciences and Musicology from Ghent University and I’m a qualified teacher. When my son was born in 2021 I quickly realised that an opera career wasn’t as compatible with being a mum as much as I would like, so I decided to change my focus completely to photography and web design.

I prefer working with daylight in casual surroundings while having a chat making sure you’re at ease and relaxed. It’s the little moments in between that make the best shots!

Feel free to get in touch if you’d like to work with me!

http://www.foubography.com
Previous
Previous

Mobile Menu Font Size