Shop: pick photos, and colors from a drop-down #33

Merged
jcoffey-dev merged 2 commits from shop/photo-gallery into main 2026-10-17 01:12:39 +00:00
10 changed files with 319 additions and 40 deletions

No files matched your search

+1 -1
View File
@@ -646,7 +646,7 @@ func importFourthwall(args []string) error {
taxonomy := fl.String("taxonomy", "", "a taxonomy in site.yaml (e.g. tags) to file products under the shop's other collections")
checkout := fl.String("checkout", "", "the shop's checkout address, e.g. https://shop.example.com (default: the shop's own, from Fourthwall)")
currency := fl.String("currency", "", "prices in this currency (default: the shop's)")
photos := fl.Int("photos", 6, "at most this many photos a product; 0 for all")
photos := fl.Int("photos", 6, "at most this many photos a product, or a color when it has several; 0 for all")
prune := fl.Bool("prune", false, "delete the pages of products that are no longer in the shop")
_ = fl.Parse(args)
token := os.Getenv("HOTDOG_FOURTHWALL_TOKEN")
+6
View File
@@ -260,6 +260,11 @@ func prepareWith(siteDir string, s *site.Site, out string, formOverrides map[str
return nil, err
}
}
if hasVariantGroups(s) {
if err := assets.add(out, "hotdog-variants.js", variantScript); err != nil {
return nil, err
}
}
if s.Config.Analytics != nil {
if err := assets.add(out, "hotdog-consent.js", consentScript); err != nil {
return nil, err
@@ -297,6 +302,7 @@ func prepareWith(siteDir string, s *site.Site, out string, formOverrides map[str
fm["cart"] = func() template.HTML { return cartTags(s.Config, assets) }
fm["cartLink"] = func() template.HTML { return cartLink(s.Config) }
fm["cartOn"] = func() bool { return s.Config.Cart.On() }
fm["variantPicker"] = func() template.HTML { return variantPicker(assets) }
fm["verificationTags"] = func() template.HTML { return verificationTags(s.Config) }
fm["consentActive"] = func() bool { return s.Config.Analytics != nil }
fm["privacyFacts"] = func() []site.PrivacyFact { return privacyFacts(s.Config, siteForms) }
+26
View File
@@ -16,6 +16,8 @@ var (
cartScript []byte
//go:embed static/hotdog-cart.css
cartStyle []byte
//go:embed static/hotdog-variants.js
variantScript []byte
)
// cartTags is the cart's stylesheet and script, for a theme's head: {{ cart }}.
@@ -37,3 +39,27 @@ func cartLink(cfg *site.Config) template.HTML {
}
return template.HTML(`<a class="cart-link" href="` + template.HTMLEscapeString(cfg.Cart.PagePath()) + `" data-cart-link hidden>Cart <span data-cart-count>0</span></a>`)
}
// variantPicker is the script that turns a product's sizes grouped by color
// into a color drop-down: {{ variantPicker }} in the product layout. The
// build only carries it when some page has grouped variants.
func variantPicker(a *Assets) template.HTML {
js, err := a.URL("hotdog-variants.js")
if err != nil {
return ""
}
return template.HTML(`<script src="` + template.HTMLEscapeString(js) + `" defer></script>`)
}
// hasVariantGroups says whether any page lists variants in groups (group:).
func hasVariantGroups(s *site.Site) bool {
for _, p := range s.Pages {
vs, _ := p.Params["variants"].([]any)
for _, v := range vs {
if m, ok := v.(map[string]any); ok && m["group"] != nil && m["group"] != "" {
return true
}
}
}
return false
}
+57
View File
@@ -0,0 +1,57 @@
// A product with its sizes grouped by color ({{ variantPicker }}): a
// drop-down picks the color, and only that color's sizes and photos show.
// Without this script the page lists every color's sizes and photos, and
// works the same.
(() => {
const select = document.querySelector("[data-group-select]");
const form = select && select.closest("form");
if (!form) return;
const sets = [...form.querySelectorAll("fieldset.variants[data-group]")];
const slides = [...document.querySelectorAll(".photo-slide")];
const picks = [...document.querySelectorAll(".photo-pick")];
const show = (group) => {
// The size chosen in the color being left, to choose again in the new one.
const was = form.querySelector("input[data-option]:checked");
const size = was ? was.dataset.option : "";
for (const set of sets) {
const on = set.dataset.group === group;
// A hidden color's sizes are disabled, so they're never sent or checked.
set.disabled = !on;
if (!on) {
set.hidden = true;
continue;
}
const sizes = [...set.querySelectorAll("input[data-option]")];
const again = sizes.find((i) => i.dataset.option === size && !i.disabled);
if (again) again.checked = true;
else if (was) was.checked = false;
// A color with one choice needs no row of its own: the drop-down is it.
if (sizes.length === 1 && !sizes[0].disabled) sizes[0].checked = true;
set.hidden = sizes.length === 1;
}
// Photos of this color, and those of no one color.
let first = null;
picks.forEach((pick, i) => {
const g = pick.dataset.group;
const on = !g || g === group;
pick.hidden = !on;
if (slides[i]) slides[i].hidden = !on;
if (on && !first && g) first = pick;
});
first = first || picks.find((p) => !p.hidden);
if (first) first.querySelector("input").checked = true;
};
for (const set of sets) {
const legend = set.querySelector("legend");
if (legend) legend.textContent = select.dataset.choose;
}
select.closest("[data-group-pick]").hidden = false;
// Coming back to the page, the browser may have kept a size: keep its color.
const kept = form.querySelector("input[data-option]:checked");
const keptSet = kept && kept.closest("fieldset[data-group]");
if (keptSet) select.value = keptSet.dataset.group;
select.addEventListener("change", () => show(select.value));
show(select.value);
})();
+91 -18
View File
@@ -41,7 +41,7 @@ type FourthwallOptions struct {
Taxonomy string // front matter key for the shop's other collections, e.g. "tags"; "" leaves them out
Checkout string // the checkout's address, e.g. https://shop.example.com; "" asks the shop
Currency string // prices in this currency; "" is the shop's own
Photos int // at most this many photos a product; 0 is all of them
Photos int // at most this many photos a product (a color, when it has several); 0 is all of them
Prune bool // delete pages of products no longer in the shop
API string // the API's address; "" is FourthwallAPI
Client *http.Client
@@ -112,6 +112,63 @@ type fwVariant struct {
Type string `json:"type"`
InStock *int `json:"inStock"`
} `json:"stock"`
Images []fwImage `json:"images"`
}
// fwPhoto is a product photo and the color it shows, "" when it isn't one
// color's alone.
type fwPhoto struct {
fwImage
group string
}
// fwPhotos lists a product's photos. With more than one color, and photos
// that say which color they show, each color's come together (at most max
// of them, 0 for all) and carry its name; otherwise they're the product's
// own list, cut to max.
func fwPhotos(p fwProduct, colors []string, max int) []fwPhoto {
var out []fwPhoto
if len(colors) > 1 {
owner := map[string]string{}
for _, v := range p.Variants {
c := v.Attributes.Color
if c == nil || c.Name == "" {
continue
}
for _, im := range v.Images {
if o, ok := owner[im.ID]; ok && o != c.Name {
owner[im.ID] = "" // two colors share it
} else if !ok {
owner[im.ID] = c.Name
}
}
}
if len(owner) > 0 {
count := map[string]int{}
for _, c := range colors {
for _, im := range p.Images {
if owner[im.ID] == c && (max <= 0 || count[c] < max) {
out = append(out, fwPhoto{im, c})
count[c]++
}
}
}
for _, im := range p.Images {
if g, ok := owner[im.ID]; (!ok || g == "") && (max <= 0 || count[""] < max) {
out = append(out, fwPhoto{im, ""})
count[""]++
}
}
return out
}
}
for _, im := range p.Images {
if max > 0 && len(out) == max {
break
}
out = append(out, fwPhoto{im, ""})
}
return out
}
func (v fwVariant) soldOut() bool {
@@ -317,21 +374,45 @@ func SyncFourthwall(opt FourthwallOptions) (*FourthwallReport, error) {
}
}
// Photos.
// The colors, in the shop's order, and whether there are sizes.
var colors []string
sized := false
for _, v := range p.Variants {
if c := v.Attributes.Color; c != nil && c.Name != "" && !slices.Contains(colors, c.Name) {
colors = append(colors, c.Name)
}
if v.Attributes.Size != nil && v.Attributes.Size.Name != "" {
sized = true
}
}
// Photos: with several colors, each one's own (at most -photos of
// each), marked with the color so the page can show the chosen one's.
mediaDir := path.Join("shop", slug)
images := p.Images
if opt.Photos > 0 && len(images) > opt.Photos {
images = images[:opt.Photos]
images := fwPhotos(p, colors, opt.Photos)
of := map[string]int{}
for _, im := range images {
of[im.group]++
}
var photos []map[string]any
using := map[string]bool{}
seen := map[string]int{}
for n, im := range images {
seen[im.group]++
alt := p.Name
if len(images) > 1 {
alt = fmt.Sprintf("%s, photo %d of %d", p.Name, n+1, len(images))
if im.group != "" {
alt += ", " + im.group
}
if of[im.group] > 1 {
alt = fmt.Sprintf("%s, photo %d of %d", alt, seen[im.group], of[im.group])
}
photo := map[string]any{"alt": alt, "source": im.ID}
if im.group != "" {
photo["group"] = im.group
}
if img, ok := havePhoto[im.ID]; ok {
photos = append(photos, map[string]any{"image": img, "alt": alt, "source": im.ID})
photo["image"] = img
photos = append(photos, photo)
using[img] = true
continue
}
@@ -351,7 +432,8 @@ func SyncFourthwall(opt FourthwallOptions) (*FourthwallReport, error) {
}
}
rep.Pictures++
photos = append(photos, map[string]any{"image": img.URL, "alt": alt, "source": im.ID})
photo["image"] = img.URL
photos = append(photos, photo)
using[img.URL] = true
}
// Photos the product no longer has go, with their smaller sizes.
@@ -377,15 +459,6 @@ func SyncFourthwall(opt FourthwallOptions) (*FourthwallReport, error) {
// Variants: one per color and size, each with its own checkout.
// Colors head groups of sizes only when there's more than one.
colors, sized := map[string]bool{}, false
for _, v := range p.Variants {
if c := v.Attributes.Color; c != nil && c.Name != "" {
colors[c.Name] = true
}
if v.Attributes.Size != nil && v.Attributes.Size.Name != "" {
sized = true
}
}
cur := ""
lowest, highest := p.Variants[0].UnitPrice.Value, p.Variants[0].UnitPrice.Value
var variants []map[string]any
+38
View File
@@ -161,3 +161,41 @@ func TestFourthwallSync(t *testing.T) {
t.Fatalf("bad token: %v", err)
}
}
func TestFourthwallPhotosByColor(t *testing.T) {
im := func(ids ...string) []fwImage {
var out []fwImage
for _, id := range ids {
out = append(out, fwImage{ID: id})
}
return out
}
variant := func(color string, ids ...string) fwVariant {
var v fwVariant
v.Attributes.Color = &struct {
Name string `json:"name"`
Swatch string `json:"swatch"`
}{Name: color}
v.Images = im(ids...)
return v
}
p := fwProduct{Images: im("a", "b", "c", "d", "e"), Variants: []fwVariant{
variant("Red", "a", "b", "e"), variant("Red", "a", "b", "e"), variant("Blue", "c", "d", "e"),
}}
got := func(ps []fwPhoto) string {
var s []string
for _, p := range ps {
s = append(s, p.ID+"="+p.group)
}
return strings.Join(s, " ")
}
if g := got(fwPhotos(p, []string{"Red", "Blue"}, 0)); g != "a=Red b=Red c=Blue d=Blue e=" {
t.Errorf("every photo, by color, a shared one last: %s", g)
}
if g := got(fwPhotos(p, []string{"Red", "Blue"}, 1)); g != "a=Red c=Blue e=" {
t.Errorf("one of each color: %s", g)
}
if g := got(fwPhotos(p, []string{"Red"}, 2)); g != "a= b=" {
t.Errorf("one color, the product's own list: %s", g)
}
}
+14 -12
View File
@@ -159,6 +159,7 @@ func TestBundledPacksBuildAndPass(t *testing.T) {
{"content/products/_index.md", "---\ntitle: Shop\nsummary: Things to buy.\n---\n"},
{"content/products/kit.md", "---\ntitle: Starter kit\nsummary: A jar of starter.\nprice: 24\ncurrency: USD\nweight: 1\nphotos:\n - { image: /media/a.png, alt: A jar }\n - { image: /media/b.png, alt: The card }\nbuy: \"https://buy.stripe.com/test_1\"\noptions:\n - { name: With a basket, price: 39.5, buy: \"https://buy.stripe.com/test_2\" }\n---\nWhat's in the box.\n"},
{"content/products/mug.md", "---\ntitle: Mug\nsummary: A mug.\nprice: 12\ncurrency: EUR\nweight: 2\nsold_out: true\n---\n"},
{"content/products/hoodie.md", "---\ntitle: Hoodie\nprice: 40\ncurrency: USD\nweight: 4\nphotos:\n - { image: /media/a.png, alt: Hoodie in red, group: Red }\n - { image: /media/b.png, alt: Hoodie in blue, group: Blue }\nchoose: Size\nvariants:\n - { group: Red, name: S, price: 40, id: r1 }\n - { group: Red, name: M, price: 40, id: r2 }\n - { group: Blue, name: S, price: 40, id: b1 }\ncheckout: { action: \"https://shop.example/cart/checkout\", field: products, item: \"{id}:{qty}\" }\n---\n"},
{"content/products/tee.md", "---\ntitle: Tee\nsummary: A tee.\nprice: 24\nprice_from: true\ncurrency: USD\nweight: 3\ntags: [Linux, Shirts]\nphotos:\n - { image: /media/a.png, alt: The tee }\nchoose: Size\nvariants:\n - { name: S, price: 24, id: v1 }\n - { name: 2XL, price: 26, id: v2 }\n - { name: 3XL, price: 28, id: v3, sold_out: true }\ncheckout: { action: \"https://shop.example/cart/checkout\", field: products, item: \"{id}:{qty}\", join: \",\", fields: { currency: USD } }\n---\n"},
{"content/products/cap.md", "---\ntitle: Cap\nsummary: A cap.\nprice: 20\ncurrency: USD\nweight: 4\ntags: [linux]\nvariants:\n - { group: Black, name: One size, price: 20, value: \"c1:1\" }\ncheckout: { action: \"https://shop.example/cart/checkout\" }\n---\n"},
{"content/cart.md", "---\ntitle: Your cart\nlayout: cart\nsuggest: 3\n---\n"},
@@ -168,18 +169,19 @@ func TestBundledPacksBuildAndPass(t *testing.T) {
{"content/picks.md", "---\ntitle: Picks\nsummary: Picks.\nsections:\n - products: { heading: Penguin picks, tag: Linux, more: All Linux things }\n---\n"},
{"content/products/print.md", "---\ntitle: Print\nsummary: A print.\nprice: 30\ncurrency: USD\nweight: 5\nvariants:\n - { group: Matte, name: A4, price: 30, buy: \"https://buy.stripe.com/a4\" }\n - { group: Matte, name: A3, price: 40, buy: \"https://buy.stripe.com/a3\", sold_out: true }\n---\n"},
}, map[string][]string{
"products/tee/index.html": {`<form class="product-form" method="get" action="https://shop.example/cart/checkout" data-cart data-field="products" data-item="{id}:{qty}" data-join="," data-title="Tee" data-href="/products/tee/" data-currency="USD" data-image="/media/a.png">`, `<input type="hidden" name="currency" value="USD">`, "<legend>Size</legend>", `name="products" value="v1:1" required data-cart-variant data-id="v1" data-name="S" data-price="24">`, "2XL <small>$26.00</small>", `value="v3:1" required disabled data-cart-variant`, "<span>3XL <small>sold out</small>", `data-cart-add hidden>Add to cart</button>`, `product-buy-button" type="submit">Buy now</button>`, `<script src="/hotdog-cart.`, `data-page="/cart/"`, `<a class="cart-link" href="/cart/" data-cart-link hidden>`, `<span class="product-from">From</span> $24.00`, `"@type":"AggregateOffer"`, `"lowPrice":"24.00"`, `"highPrice":"28.00"`, `"offerCount":3`},
"products/cap/index.html": {`<input type="hidden" name="products" value="c1:1" data-cart-variant data-id="c1:1" data-name="Black · One size" data-price="20">`, ">Buy now</button>"},
"cart/index.html": {`<div class="cart-view" data-cart-view data-shop="/products/">`, "The cart needs JavaScript.", `data-cart-suggest data-count="3" hidden>`, `<li class="shop-card" data-product="/products/tee/" data-tags="Linux,Shirts">`},
"privacy/index.html": {"What you put in your cart"},
"tags/linux/index.html": {"<h1>Linux things</h1>", "For the penguin people.", `class="wrap page shop shelf"`, `data-product="/products/tee/"`, `data-product="/products/cap/"`},
"tags/kitchen/index.html": {"Kitchen", "Coming soon."},
"tags/shirts/index.html": {`<p class="shelf-summary">Cotton, mostly.</p>`, `data-product="/products/tee/"`},
"picks/index.html": {"Penguin picks", `data-product="/products/tee/"`, `data-product="/products/cap/"`, `href="/tags/linux/">All Linux things`},
"products/print/index.html": {`href="https://buy.stripe.com/a4" rel="noopener">Matte · A4 · $30.00`, `<span class="btn variant-sold">Matte · A3 · sold out</span>`},
"products/index.html": {`href="/products/kit/"`, "$24.00", "€12.00 · <em>Sold out</em>"},
"products/kit/index.html": {`href="https://buy.stripe.com/test_1" rel="noopener">Buy`, "With a basket · $39.50", `alt="The card"`, `"@type":"Product"`, `"priceCurrency":"USD"`, `"price":"24.00"`, "InStock"},
"products/mug/index.html": {"Sold out", "OutOfStock"},
"products/tee/index.html": {`<form class="product-form" method="get" action="https://shop.example/cart/checkout" data-cart data-field="products" data-item="{id}:{qty}" data-join="," data-title="Tee" data-href="/products/tee/" data-currency="USD" data-image="/media/a.png">`, `<input type="hidden" name="currency" value="USD">`, "<legend>Size</legend>", `name="products" value="v1:1" required data-option="S" data-cart-variant data-id="v1" data-name="S" data-price="24">`, "2XL <small>$26.00</small>", `value="v3:1" required disabled data-option="3XL" data-cart-variant`, "<span>3XL <small>sold out</small>", `data-cart-add hidden>Add to cart</button>`, `product-buy-button" type="submit">Buy now</button>`, `<script src="/hotdog-cart.`, `data-page="/cart/"`, `<a class="cart-link" href="/cart/" data-cart-link hidden>`, `<span class="product-from">From</span> $24.00`, `"@type":"AggregateOffer"`, `"lowPrice":"24.00"`, `"highPrice":"28.00"`, `"offerCount":3`},
"products/cap/index.html": {`<input type="hidden" name="products" value="c1:1" data-cart-variant data-id="c1:1" data-name="Black · One size" data-price="20">`, ">Buy now</button>"},
"cart/index.html": {`<div class="cart-view" data-cart-view data-shop="/products/">`, "The cart needs JavaScript.", `data-cart-suggest data-count="3" hidden>`, `<li class="shop-card" data-product="/products/tee/" data-tags="Linux,Shirts">`},
"privacy/index.html": {"What you put in your cart"},
"tags/linux/index.html": {"<h1>Linux things</h1>", "For the penguin people.", `class="wrap page shop shelf"`, `data-product="/products/tee/"`, `data-product="/products/cap/"`},
"tags/kitchen/index.html": {"Kitchen", "Coming soon."},
"tags/shirts/index.html": {`<p class="shelf-summary">Cotton, mostly.</p>`, `data-product="/products/tee/"`},
"picks/index.html": {"Penguin picks", `data-product="/products/tee/"`, `data-product="/products/cap/"`, `href="/tags/linux/">All Linux things`},
"products/print/index.html": {`href="https://buy.stripe.com/a4" rel="noopener">Matte · A4 · $30.00`, `<span class="btn variant-sold">Matte · A3 · sold out</span>`},
"products/index.html": {`href="/products/kit/"`, "$24.00", "€12.00 · <em>Sold out</em>"},
"products/hoodie/index.html": {`<label class="variant-group" data-group-pick hidden><span>Color</span>`, `<select data-group-select data-choose="Size"><option>Red</option><option>Blue</option></select>`, `<script src="/hotdog-variants.`, `<fieldset class="variants" data-group="Blue"><legend>Blue</legend>`, `<div class="photo-slide" data-group="Blue">`, `<label class="photo-pick" data-group="Red">`},
"products/kit/index.html": {`href="https://buy.stripe.com/test_1" rel="noopener">Buy`, `<div class="photo-thumbs" role="radiogroup" aria-label="Photos">`, `<label class="photo-pick"><input type="radio" name="product-photo" checked>`, "With a basket · $39.50", `alt="The card"`, `"@type":"Product"`, `"priceCurrency":"USD"`, `"price":"24.00"`, "InStock"},
"products/mug/index.html": {"Sold out", "OutOfStock"},
}},
{"contact-forms", []page{
{"content/quote.md", "---\ntitle: Get a quote\nsummary: Tell us what you need.\nsections:\n - contact-form:\n heading: Ask for a quote\n form: quote\n email: [email protected]\n phone: (217) 555-0142\n address: |\n 12 Main Street\n Springfield\n note: We answer within **one** working day.\n---\n"},
+57 -3
View File
@@ -6,9 +6,58 @@
.shop-price { color: var(--muted); }
.product-main { display: grid; gap: 32px; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; margin-bottom: 28px; }
@media (max-width: 760px) { .product-main { grid-template-columns: minmax(0, 1fr); } }
.product-photos { display: grid; gap: 10px; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.product-photo-main { grid-column: 1 / -1; width: 100%; border-radius: var(--radius); }
.product-photo { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: calc(var(--radius) - 4px); }
.product-photos { display: grid; gap: 10px; }
.product-photo-main { width: 100%; border-radius: var(--radius); display: block; }
.product-photo { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: calc(var(--radius) - 4px); display: block; }
/* The photo picker: the first photo shows until another small one is picked.
Browsers without :has() keep the first. Up to 40 photos. */
.photo-thumbs { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); }
.photo-pick { position: relative; cursor: pointer; border-radius: calc(var(--radius) - 2px); }
.photo-pick input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; pointer-events: none; }
.photo-pick:hover .product-photo { opacity: .85; }
.photo-pick:has(input:checked) { outline: 2px solid var(--accent); outline-offset: 2px; }
.photo-pick:has(input:focus-visible) { outline: 2px dashed var(--accent); outline-offset: 4px; }
.photo-slide:not(:first-child) { display: none; }
.product-photos:has(.photo-pick:not(:first-child) input:checked) .photo-slide:first-child { display: none; }
.product-photos:has(.photo-pick:nth-child(2) input:checked) .photo-slide:nth-child(2),
.product-photos:has(.photo-pick:nth-child(3) input:checked) .photo-slide:nth-child(3),
.product-photos:has(.photo-pick:nth-child(4) input:checked) .photo-slide:nth-child(4),
.product-photos:has(.photo-pick:nth-child(5) input:checked) .photo-slide:nth-child(5),
.product-photos:has(.photo-pick:nth-child(6) input:checked) .photo-slide:nth-child(6),
.product-photos:has(.photo-pick:nth-child(7) input:checked) .photo-slide:nth-child(7),
.product-photos:has(.photo-pick:nth-child(8) input:checked) .photo-slide:nth-child(8),
.product-photos:has(.photo-pick:nth-child(9) input:checked) .photo-slide:nth-child(9),
.product-photos:has(.photo-pick:nth-child(10) input:checked) .photo-slide:nth-child(10),
.product-photos:has(.photo-pick:nth-child(11) input:checked) .photo-slide:nth-child(11),
.product-photos:has(.photo-pick:nth-child(12) input:checked) .photo-slide:nth-child(12),
.product-photos:has(.photo-pick:nth-child(13) input:checked) .photo-slide:nth-child(13),
.product-photos:has(.photo-pick:nth-child(14) input:checked) .photo-slide:nth-child(14),
.product-photos:has(.photo-pick:nth-child(15) input:checked) .photo-slide:nth-child(15),
.product-photos:has(.photo-pick:nth-child(16) input:checked) .photo-slide:nth-child(16),
.product-photos:has(.photo-pick:nth-child(17) input:checked) .photo-slide:nth-child(17),
.product-photos:has(.photo-pick:nth-child(18) input:checked) .photo-slide:nth-child(18),
.product-photos:has(.photo-pick:nth-child(19) input:checked) .photo-slide:nth-child(19),
.product-photos:has(.photo-pick:nth-child(20) input:checked) .photo-slide:nth-child(20),
.product-photos:has(.photo-pick:nth-child(21) input:checked) .photo-slide:nth-child(21),
.product-photos:has(.photo-pick:nth-child(22) input:checked) .photo-slide:nth-child(22),
.product-photos:has(.photo-pick:nth-child(23) input:checked) .photo-slide:nth-child(23),
.product-photos:has(.photo-pick:nth-child(24) input:checked) .photo-slide:nth-child(24),
.product-photos:has(.photo-pick:nth-child(25) input:checked) .photo-slide:nth-child(25),
.product-photos:has(.photo-pick:nth-child(26) input:checked) .photo-slide:nth-child(26),
.product-photos:has(.photo-pick:nth-child(27) input:checked) .photo-slide:nth-child(27),
.product-photos:has(.photo-pick:nth-child(28) input:checked) .photo-slide:nth-child(28),
.product-photos:has(.photo-pick:nth-child(29) input:checked) .photo-slide:nth-child(29),
.product-photos:has(.photo-pick:nth-child(30) input:checked) .photo-slide:nth-child(30),
.product-photos:has(.photo-pick:nth-child(31) input:checked) .photo-slide:nth-child(31),
.product-photos:has(.photo-pick:nth-child(32) input:checked) .photo-slide:nth-child(32),
.product-photos:has(.photo-pick:nth-child(33) input:checked) .photo-slide:nth-child(33),
.product-photos:has(.photo-pick:nth-child(34) input:checked) .photo-slide:nth-child(34),
.product-photos:has(.photo-pick:nth-child(35) input:checked) .photo-slide:nth-child(35),
.product-photos:has(.photo-pick:nth-child(36) input:checked) .photo-slide:nth-child(36),
.product-photos:has(.photo-pick:nth-child(37) input:checked) .photo-slide:nth-child(37),
.product-photos:has(.photo-pick:nth-child(38) input:checked) .photo-slide:nth-child(38),
.product-photos:has(.photo-pick:nth-child(39) input:checked) .photo-slide:nth-child(39),
.product-photos:has(.photo-pick:nth-child(40) input:checked) .photo-slide:nth-child(40) { display: block; }
.product-buy h1 { margin: 0 0 6px; }
.product-price { font-size: 1.6rem; font-weight: 750; margin: 0 0 12px; }
.product-summary { color: var(--muted); margin: 0 0 18px; }
@@ -28,6 +77,11 @@
.variant input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.variant-sold span, .btn.variant-sold { opacity: .5; text-decoration: line-through; }
.variant-only { margin: 0; color: var(--muted); }
.variant-group { display: grid; gap: 8px; justify-items: start; }
.variant-group span { font-weight: 650; }
.variant-group select { font: inherit; color: var(--text); background: var(--bg, transparent); border: 1px solid var(--line, currentColor); border-radius: var(--radius); padding: 9px 36px 9px 14px; min-width: 12rem; }
.variant-group select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.product [hidden] { display: none !important; }
.product-buy-button { justify-self: start; }
.product-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
.product-buttons [hidden] { display: none; }
+19 -5
View File
@@ -4,9 +4,18 @@
<article class="wrap page product">
{{ with index .Site.Lists .Collection }}<p class="eyebrow"><a href="{{ .Path }}">{{ .Title }}</a></p>{{ end }}
<div class="product-main">
<div class="product-photos">
{{ range $i, $p := .Params.photos }}{{ if eq $i 0 }}{{ picture $p.image $p.alt "(min-width: 760px) 55vw, 100vw" "class=product-photo-main fetchpriority=high" }}{{ else }}{{ picture $p.image $p.alt "(min-width: 760px) 14vw, 25vw" "class=product-photo loading=lazy" }}{{ end }}{{ end }}
</div>
{{/* The big photo, and a small one for each photo under it. Picking a
small one (a click, or the arrow keys) shows it big; no script. */}}
<div class="product-photos">{{ with .Params.photos }}
<div class="photo-stage">
{{ range $i, $p := . }}<div class="photo-slide"{{ with $p.group }} data-group="{{ . }}"{{ end }}>{{ if eq $i 0 }}{{ picture $p.image $p.alt "(min-width: 760px) 55vw, 100vw" "class=product-photo-main fetchpriority=high" }}{{ else }}{{ picture $p.image $p.alt "(min-width: 760px) 55vw, 100vw" "class=product-photo-main loading=lazy" }}{{ end }}</div>{{ end }}
</div>
{{ if gt (len .) 1 }}
<div class="photo-thumbs" role="radiogroup" aria-label="Photos">
{{ range $i, $p := . }}<label class="photo-pick"{{ with $p.group }} data-group="{{ . }}"{{ end }}><input type="radio" name="product-photo"{{ if eq $i 0 }} checked{{ end }}>{{ picture $p.image $p.alt "(min-width: 760px) 9vw, 22vw" "class=product-photo loading=lazy" }}</label>{{ end }}
</div>
{{ end }}
{{ end }}</div>
<div class="product-buy">
<h1>{{ .Title }}</h1>
{{ with .Params.price }}<p class="product-price">{{ if eq (printf "%v" $.Params.price_from) "true" }}<span class="product-from">From</span> {{ end }}{{ money . $cur }}</p>{{ end }}
@@ -24,11 +33,16 @@
<input type="hidden" name="{{ $field }}" value="{{ if .value }}{{ .value }}{{ else }}{{ $item | replace "{id}" (printf "%v" .id) | replace "{qty}" "1" }}{{ end }}" data-cart-variant data-id="{{ default .value .id }}" data-name="{{ with .group }}{{ . }} · {{ end }}{{ .name }}" data-price="{{ .price }}">
{{ with .name }}{{ if ne . "One size" }}<p class="variant-only">{{ with (index $vs 0).group }}{{ . }} · {{ end }}{{ . }}</p>{{ end }}{{ end }}
{{ end }}{{ else }}
{{ if (index $vs 0).group }}{{/* Colors: the picker script turns this on; then only the chosen one's sizes and photos show. */}}{{ variantPicker }}
<label class="variant-group" data-group-pick hidden><span>{{ default "Color" $.Params.choose_group }}</span>
<select data-group-select data-choose="{{ default "Choose one" $.Params.choose }}">{{ $og := "-" }}{{ range $vs }}{{ if ne (default "" .group) $og }}{{ $og = default "" .group }}<option>{{ $og }}</option>{{ end }}{{ end }}</select>
</label>
{{ end }}
{{ $g := "-" }}
{{ range $i, $v := $vs }}{{ $vg := default "" $v.group }}{{ if ne $vg $g }}{{ if ne $i 0 }}</div></fieldset>{{ end }}{{ $g = $vg }}
<fieldset class="variants"><legend>{{ if $vg }}{{ $vg }}{{ else }}{{ default "Choose one" $.Params.choose }}{{ end }}</legend><div class="variant-list">{{ end }}
<fieldset class="variants"{{ if $vg }} data-group="{{ $vg }}"{{ end }}><legend>{{ if $vg }}{{ $vg }}{{ else }}{{ default "Choose one" $.Params.choose }}{{ end }}</legend><div class="variant-list">{{ end }}
{{ $sold := eq (printf "%v" $v.sold_out) "true" }}
<label class="variant{{ if $sold }} variant-sold{{ end }}"><input type="radio" name="{{ $field }}" value="{{ if $v.value }}{{ $v.value }}{{ else }}{{ $item | replace "{id}" (printf "%v" $v.id) | replace "{qty}" "1" }}{{ end }}" required{{ if $sold }} disabled{{ end }} data-cart-variant data-id="{{ default $v.value $v.id }}" data-name="{{ with $v.group }}{{ . }} · {{ end }}{{ $v.name }}" data-price="{{ $v.price }}"><span>{{ $v.name }}{{ if $sold }} <small>sold out</small>{{ else if and $v.price (ne (printf "%v" $v.price) (printf "%v" $base)) }} <small>{{ money $v.price $cur }}</small>{{ end }}</span></label>
<label class="variant{{ if $sold }} variant-sold{{ end }}"><input type="radio" name="{{ $field }}" value="{{ if $v.value }}{{ $v.value }}{{ else }}{{ $item | replace "{id}" (printf "%v" $v.id) | replace "{qty}" "1" }}{{ end }}" required{{ if $sold }} disabled{{ end }} data-option="{{ $v.name }}" data-cart-variant data-id="{{ default $v.value $v.id }}" data-name="{{ with $v.group }}{{ . }} · {{ end }}{{ $v.name }}" data-price="{{ $v.price }}"><span>{{ $v.name }}{{ if $sold }} <small>sold out</small>{{ else if and $v.price (ne (printf "%v" $v.price) (printf "%v" $base)) }} <small>{{ money $v.price $cur }}</small>{{ end }}</span></label>
{{ end }}</div></fieldset>
{{ end }}
<div class="product-buttons">
+10 -1
View File
@@ -1,5 +1,5 @@
name: shop
version: 1.3.0
version: 1.4.0
title: Shop
description: A product catalog with a page per product (photos, price, options), where Buy opens your payment provider's checkout, and an optional cart. No card details on your site.
license: MIT
@@ -28,6 +28,10 @@ notes: |
---
What's in the box, how it ships, and anything else, in Markdown.
The first photo shows large. With more than one, a row of small ones sits
under it, and picking one (a click, or the arrow keys) shows it large, with
no script. Up to 24 photos; the alt text names each one to a screen reader.
Sizes and colors: list them as variants, each with its own buy link and
price; a group (a color) gathers them under a heading, and sold_out: true
crosses one out. price_from: true shows "From" before the price.
@@ -36,6 +40,11 @@ notes: |
- { group: Black, name: S, price: 24, buy: "https://buy.stripe.com/s" }
- { group: Black, name: XL, price: 26, buy: "https://buy.stripe.com/xl" }
With groups and a checkout form (below), a drop-down picks the group
(choose_group: names it; Color by default) and only its sizes show. Give
a photo the same group: and it shows only with that color. Without
JavaScript every group's sizes and photos are listed instead.
A provider whose checkout takes the choice in its address (Fourthwall's
does) gets a proper picker instead: give the product a checkout form and
each variant the value it sends, and Buy goes to the checkout with the