Last active
December 27, 2018 12:51
-
-
Save vincentorback/4f8149b676b74ba81447f0847b1c7865 to your computer and use it in GitHub Desktop.
Hide empty Squarespace cart, until a product is added.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.Cart { | |
transition: 300ms; | |
} | |
.Cart[hidden] { | |
display: inline-block; | |
opacity: 0; | |
visibility: hidden; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Hide Cart until items in the cart | |
*/ | |
(function () { | |
var carts = document.querySelectorAll('.Cart') | |
var mobileBarCart = document.querySelector('.Mobile-bar--bottom') | |
Array.from(carts, function (cart) { | |
hideCart(cart) | |
}) | |
hideCart(mobileBarCart) | |
/** | |
* Hide Cart | |
*/ | |
function hideCart (cart) { | |
var cartQty = cart.querySelector('.sqs-cart-quantity') | |
// Handler | |
function handler(target) { | |
if (target.innerHTML === '0') { | |
cart.setAttribute('hidden', '') | |
} else { | |
cart.removeAttribute('hidden') | |
} | |
} | |
// Observer handler | |
var observer = new MutationObserver(function (mutations) { | |
handler(mutations[0].target) | |
}) | |
// Hide/show the cart when the page is loaded | |
handler(cartQty) | |
// Hide/show the cart when an item was added/removed | |
observer.observe(cartQty, { | |
childList: true | |
}) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment