Skip to content

Instantly share code, notes, and snippets.

@vincentorback
Last active December 27, 2018 12:51
Show Gist options
  • Save vincentorback/4f8149b676b74ba81447f0847b1c7865 to your computer and use it in GitHub Desktop.
Save vincentorback/4f8149b676b74ba81447f0847b1c7865 to your computer and use it in GitHub Desktop.
Hide empty Squarespace cart, until a product is added.
.Cart {
transition: 300ms;
}
.Cart[hidden] {
display: inline-block;
opacity: 0;
visibility: hidden;
}
/**
* 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