Wishlist heart on product cards
A heart on every product card that saves the product to the shopper's wishlist and shows filled for products already saved (sir.wishlistToggle).
On this page
Goal: product cards with a heart button that saves the product to the wishlist and shows filled for products the shopper already saved, with a "Saved" link above the grid. No JavaScript of your own.
sir.wishlistToggle on each heart does all of it: the click, the save, the rollback on failure, the
sign-in for a guest, and the opening state. The page HTML is the same for every shopper (so it can be
cached), so the server cannot know which hearts to fill; the SIR runtime fills them in the browser.
The block
import { block, f, Image, Link, Money, products, sir } from '@bizzpocket/theme';
import './wish-grid.css';
function Heart() {
return (
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round">
<path d="M12 20s-7-4.35-7-10a4 4 0 0 1 7-2.65A4 4 0 0 1 19 10c0 5.65-7 10-7 10Z" />
</svg>
);
}
export default block({
name: 'Wishlist grid',
category: 'products',
description: 'Products from a collection, each with a heart that saves it to the wishlist.',
fields: {
heading: f.text({ optional: true, default: 'New this week' }),
collection: f.collection(),
count: f.number({ default: 8, min: 1, max: 24 }),
savedLabel: f.text({ default: 'Saved' }),
},
data: ({ collection, count }) => ({ items: products({ collection, first: count }) }),
render: ({ heading, savedLabel, items }) => {
const list = items?.products ?? [];
if (list.length === 0) return null;
return (
<div className="wg">
<div className="wg__top">
{heading ? <h2 className="wg__heading">{heading}</h2> : null}
<a className="wg__saved" href="/account/wishlist">
{savedLabel}
</a>
</div>
<ul className="wg__grid">
{list.map((p) => (
<li key={p.id} className="wg__card">
<Link href={`/products/${encodeURIComponent(p.handle)}`} className="wg__link">
<div className="wg__media">
{p.image ? (
<Image src={p.image.src} alt={p.image.alt} width={400} height={400} sizes="(min-width: 64rem) 25vw, 50vw" />
) : null}
</div>
<span className="wg__title">{p.title}</span>
<Money amount={p.price.amount} currency={p.price.currency} className="wg__price" />
</Link>
{/* A sibling of the link, not inside it: a button inside an <a> is not allowed. */}
<button
type="button"
className="wg__heart"
aria-label={`Save ${p.title}`}
{...sir.wishlistToggle({ 'data-product-id': p.id })}
>
<Heart />
</button>
</li>
))}
</ul>
</div>
);
},
});[data-theme] .wg__top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 1rem;
margin-bottom: 1rem;
}
[data-theme] .wg__heading {
margin: 0;
}
[data-theme] .wg__saved {
color: inherit;
white-space: nowrap;
}
[data-theme] .wg__grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: var(--spacing-s, 1rem);
margin: 0;
padding: 0;
list-style: none;
}
@media (min-width: 64rem) {
[data-theme] .wg__grid {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
}
[data-theme] .wg__card {
position: relative;
}
[data-theme] .wg__link {
display: flex;
flex-direction: column;
gap: 0.25rem;
color: inherit;
text-decoration: none;
}
[data-theme] .wg__media {
aspect-ratio: 1;
overflow: hidden;
border-radius: var(--radius-card, 0.75rem);
background: var(--color-surface, rgba(0, 0, 0, 0.04));
}
[data-theme] .wg__media img {
width: 100%;
height: 100%;
object-fit: cover;
}
[data-theme] .wg__heart {
position: absolute;
top: 0.5rem;
right: 0.5rem;
display: grid;
place-items: center;
width: 2.5rem;
height: 2.5rem;
border: 0;
border-radius: 999px;
background: rgba(255, 255, 255, 0.9);
color: var(--color-text, #111);
cursor: pointer;
}
/* The SIR runtime sets aria-pressed: on a click, and for products already saved. */
[data-theme] .wg__heart[aria-pressed='true'] {
color: var(--color-sale, #d0213a);
}
[data-theme] .wg__heart[aria-pressed='true'] svg {
fill: currentColor;
}How it works
- The heart.
sir.wishlistToggle({ 'data-product-id': p.id })addsdata-bp-island="wishlist-toggle". On a click the SIR runtime flipsaria-pressedanddata-wishedat once (optimistic), sendsbizzpocket:wishlist:toggle, and saves through the storefront's account endpoint. If the save fails it flips back. A guest is asked to sign in (/account/login?next=<this page>), because a wishlist lives on the shopper's account. - Its opening state. Each heart listens for
bizzpocket:wishlist:updatedand asks for it once (bizzpocket:wishlist:request, one request for all the hearts on the page). The storefront answers with the shopper's saved product ids, and the runtime setsaria-pressedanddata-wishedon every heart whose product is in the list. It follows later updates too, so a product shown in two blocks stays in step. For a guest the list is empty and every heart stays empty. - Data stays static. The products come from
data()withproducts(), so the size table saysstatic: one data call for the page, cached like any other block. - No layout shift. The hearts change colour only.
Note. Want a count next to "Saved"? useWishlist() in a small 'use client' island gives count and
ready (interactivity.md). The hearts do not need it.
JS cost
0 bytes: the hearts are the shared SIR runtime, and the block has no island.
See also
- Interactivity:
sirattributes:sir.wishlistToggleand the other behaviours. - Interactivity: client hooks:
useWishlist(),useCart(),useCustomer(). - Data:
products()and why this block isstatic.