@growth-labs/store 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/api.d.ts +30 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +126 -0
- package/dist/api.js.map +1 -0
- package/dist/availability.d.ts +30 -0
- package/dist/availability.d.ts.map +1 -0
- package/dist/availability.js +47 -0
- package/dist/availability.js.map +1 -0
- package/dist/cache.d.ts +34 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +80 -0
- package/dist/cache.js.map +1 -0
- package/dist/cart-client.d.ts +54 -0
- package/dist/cart-client.d.ts.map +1 -0
- package/dist/cart-client.js +347 -0
- package/dist/cart-client.js.map +1 -0
- package/dist/checkout-client.d.ts +20 -0
- package/dist/checkout-client.d.ts.map +1 -0
- package/dist/checkout-client.js +33 -0
- package/dist/checkout-client.js.map +1 -0
- package/dist/checkout-url.d.ts +25 -0
- package/dist/checkout-url.d.ts.map +1 -0
- package/dist/checkout-url.js +36 -0
- package/dist/checkout-url.js.map +1 -0
- package/dist/client.d.ts +459 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +139 -0
- package/dist/client.js.map +1 -0
- package/dist/collections.d.ts +27 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +24 -0
- package/dist/collections.js.map +1 -0
- package/dist/health.d.ts +17 -0
- package/dist/health.d.ts.map +1 -0
- package/dist/health.js +38 -0
- package/dist/health.js.map +1 -0
- package/dist/image.d.ts +25 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +43 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/options.d.ts +174 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +59 -0
- package/dist/options.js.map +1 -0
- package/dist/product-card-variant.d.ts +13 -0
- package/dist/product-card-variant.d.ts.map +1 -0
- package/dist/product-card-variant.js +33 -0
- package/dist/product-card-variant.js.map +1 -0
- package/dist/routes/cart-bootstrap.d.ts +3 -0
- package/dist/routes/cart-bootstrap.d.ts.map +1 -0
- package/dist/routes/cart-bootstrap.js +5 -0
- package/dist/routes/cart-bootstrap.js.map +1 -0
- package/dist/routes/checkout.d.ts +4 -0
- package/dist/routes/checkout.d.ts.map +1 -0
- package/dist/routes/checkout.js +14 -0
- package/dist/routes/checkout.js.map +1 -0
- package/dist/routes/webhook.d.ts +4 -0
- package/dist/routes/webhook.d.ts.map +1 -0
- package/dist/routes/webhook.js +12 -0
- package/dist/routes/webhook.js.map +1 -0
- package/dist/state.d.ts +5 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +14 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +5111 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +167 -0
- package/dist/types.js.map +1 -0
- package/dist/variant.d.ts +59 -0
- package/dist/variant.d.ts.map +1 -0
- package/dist/variant.js +81 -0
- package/dist/variant.js.map +1 -0
- package/dist/vite-plugin.d.ts +4 -0
- package/dist/vite-plugin.d.ts.map +1 -0
- package/dist/vite-plugin.js +24 -0
- package/dist/vite-plugin.js.map +1 -0
- package/package.json +59 -0
- package/src/analytics-window.d.ts +15 -0
- package/src/api.ts +184 -0
- package/src/availability.ts +65 -0
- package/src/cache.ts +118 -0
- package/src/cart-client.ts +423 -0
- package/src/checkout-client.ts +49 -0
- package/src/checkout-url.ts +52 -0
- package/src/client.ts +216 -0
- package/src/cloudflare-workers.d.ts +3 -0
- package/src/collections.ts +63 -0
- package/src/components/AddToCartButton.astro +146 -0
- package/src/components/CartDrawer.astro +276 -0
- package/src/components/CartIcon.astro +96 -0
- package/src/components/CollectionCard.astro +81 -0
- package/src/components/CurrencySelector.astro +73 -0
- package/src/components/Price.astro +34 -0
- package/src/components/ProductCard.astro +225 -0
- package/src/components/StoreCartPage.astro +219 -0
- package/src/components/StoreCollectionPage.astro +166 -0
- package/src/components/StoreIndexPage.astro +193 -0
- package/src/components/StoreProductPage.astro +296 -0
- package/src/components/VariantPicker.astro +190 -0
- package/src/health.ts +54 -0
- package/src/image.ts +58 -0
- package/src/index.ts +75 -0
- package/src/options.ts +74 -0
- package/src/product-card-variant.ts +47 -0
- package/src/routes/cart-bootstrap.ts +6 -0
- package/src/routes/checkout.ts +16 -0
- package/src/routes/webhook.ts +14 -0
- package/src/state.ts +20 -0
- package/src/types.ts +196 -0
- package/src/variant.ts +119 -0
- package/src/virtual.d.ts +6 -0
- package/src/vite-plugin.ts +27 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { env as cloudflareEnv } from 'cloudflare:workers'
|
|
3
|
+
|
|
4
|
+
interface FwEnv {
|
|
5
|
+
PUBLIC_FW_STOREFRONT_TOKEN?: string
|
|
6
|
+
PUBLIC_FW_DEFAULT_CURRENCY?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
checkoutPath?: string
|
|
11
|
+
imageTransformEnabled?: boolean
|
|
12
|
+
cartPath?: string
|
|
13
|
+
/** Fourthwall-hosted shop URL, rendered as a `noscript` fallback. */
|
|
14
|
+
fourthwallShopUrl?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const { checkoutPath = '/api/store/checkout', imageTransformEnabled = false, cartPath = '/store/cart/', fourthwallShopUrl } = Astro.props
|
|
18
|
+
|
|
19
|
+
const fwEnv = cloudflareEnv as FwEnv
|
|
20
|
+
const token = fwEnv?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
|
|
21
|
+
const SUPPORTED_CURRENCIES = new Set(['USD', 'GBP', 'EUR', 'CAD', 'AUD'])
|
|
22
|
+
const cookieCurrency = Astro.cookies.get('fw_currency')?.value
|
|
23
|
+
const currency = cookieCurrency && SUPPORTED_CURRENCIES.has(cookieCurrency) ? cookieCurrency : (fwEnv?.PUBLIC_FW_DEFAULT_CURRENCY ?? 'USD')
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
<aside
|
|
27
|
+
id="fw-cart-drawer"
|
|
28
|
+
data-fw-token={token}
|
|
29
|
+
data-fw-currency={currency}
|
|
30
|
+
data-checkout-path={checkoutPath}
|
|
31
|
+
data-image-transform={imageTransformEnabled ? 'true' : undefined}
|
|
32
|
+
class="fixed inset-y-0 right-0 z-50 hidden w-full max-w-md translate-x-full transform border-l border-[var(--border)] bg-[var(--card)] text-[var(--foreground)] shadow-lg transition-transform duration-200"
|
|
33
|
+
role="dialog"
|
|
34
|
+
aria-modal="true"
|
|
35
|
+
aria-labelledby="fw-cart-title"
|
|
36
|
+
aria-hidden="true"
|
|
37
|
+
>
|
|
38
|
+
<div class="flex h-full flex-col">
|
|
39
|
+
<header class="flex items-center justify-between border-b border-[var(--border)] p-4">
|
|
40
|
+
<h2 id="fw-cart-title" class="text-lg font-semibold">Your cart</h2>
|
|
41
|
+
<button
|
|
42
|
+
type="button"
|
|
43
|
+
id="fw-cart-close"
|
|
44
|
+
class="cursor-pointer text-[var(--muted-foreground)] transition-colors hover:text-[var(--primary,var(--foreground))] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]"
|
|
45
|
+
aria-label="Close cart"
|
|
46
|
+
>
|
|
47
|
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
48
|
+
<line x1="18" y1="6" x2="6" y2="18" />
|
|
49
|
+
<line x1="6" y1="6" x2="18" y2="18" />
|
|
50
|
+
</svg>
|
|
51
|
+
</button>
|
|
52
|
+
</header>
|
|
53
|
+
|
|
54
|
+
<div id="fw-cart-body" class="flex-1 overflow-y-auto p-4">
|
|
55
|
+
<p id="fw-cart-empty" class="text-sm text-[var(--muted-foreground)]">Your cart is empty.</p>
|
|
56
|
+
<ul id="fw-cart-lines" class="hidden space-y-4"></ul>
|
|
57
|
+
</div>
|
|
58
|
+
|
|
59
|
+
<footer class="space-y-3 border-t border-[var(--border)] p-4">
|
|
60
|
+
<div class="flex items-center justify-between text-sm">
|
|
61
|
+
<span class="text-[var(--muted-foreground)]">Subtotal</span>
|
|
62
|
+
<span id="fw-cart-subtotal" class="font-semibold">—</span>
|
|
63
|
+
</div>
|
|
64
|
+
<a
|
|
65
|
+
href={cartPath}
|
|
66
|
+
class="block w-full border border-[var(--border)] bg-[var(--background)] px-4 py-2 text-center text-sm font-semibold uppercase tracking-[0.14em] text-[var(--foreground)] hover:bg-[var(--muted)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]"
|
|
67
|
+
>
|
|
68
|
+
View cart
|
|
69
|
+
</a>
|
|
70
|
+
<button
|
|
71
|
+
type="button"
|
|
72
|
+
id="fw-cart-checkout"
|
|
73
|
+
class="w-full cursor-pointer bg-[var(--primary,var(--foreground))] px-4 py-3 text-sm font-bold uppercase tracking-[0.14em] text-[var(--primary-foreground,var(--background))] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))] disabled:cursor-not-allowed disabled:opacity-50"
|
|
74
|
+
disabled
|
|
75
|
+
>
|
|
76
|
+
Checkout
|
|
77
|
+
</button>
|
|
78
|
+
{fourthwallShopUrl && (
|
|
79
|
+
<noscript>
|
|
80
|
+
<a href={fourthwallShopUrl} class="block w-full text-center text-sm underline">Continue on Fourthwall</a>
|
|
81
|
+
</noscript>
|
|
82
|
+
)}
|
|
83
|
+
</footer>
|
|
84
|
+
</div>
|
|
85
|
+
</aside>
|
|
86
|
+
<div id="fw-cart-backdrop" class="fixed inset-0 z-40 hidden bg-black/50 transition-opacity"></div>
|
|
87
|
+
|
|
88
|
+
<script>
|
|
89
|
+
import { cart, type CartSnapshot } from '../cart-client.js'
|
|
90
|
+
import { initiateCheckout } from '../checkout-client.js'
|
|
91
|
+
import { optimizeFwImage } from '../image.js'
|
|
92
|
+
|
|
93
|
+
const drawer = document.getElementById('fw-cart-drawer')
|
|
94
|
+
const backdrop = document.getElementById('fw-cart-backdrop')
|
|
95
|
+
const closeBtn = document.getElementById('fw-cart-close')
|
|
96
|
+
const linesEl = document.getElementById('fw-cart-lines')
|
|
97
|
+
const emptyEl = document.getElementById('fw-cart-empty')
|
|
98
|
+
const subtotalEl = document.getElementById('fw-cart-subtotal')
|
|
99
|
+
const checkoutBtn = document.getElementById('fw-cart-checkout') as HTMLButtonElement | null
|
|
100
|
+
|
|
101
|
+
const token = drawer?.getAttribute('data-fw-token') ?? ''
|
|
102
|
+
const currency = drawer?.getAttribute('data-fw-currency') ?? 'USD'
|
|
103
|
+
const checkoutPath = drawer?.getAttribute('data-checkout-path') ?? '/api/store/checkout'
|
|
104
|
+
const imageTransformEnabled = drawer?.getAttribute('data-image-transform') === 'true'
|
|
105
|
+
if (token) {
|
|
106
|
+
cart.init({ token, currency })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function formatMoney(cents: number, cur: string): string {
|
|
110
|
+
try {
|
|
111
|
+
return new Intl.NumberFormat('en-US', { style: 'currency', currency: cur }).format(cents / 100)
|
|
112
|
+
} catch {
|
|
113
|
+
return `${cur} ${(cents / 100).toFixed(2)}`
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let pendingRefresh = false
|
|
118
|
+
|
|
119
|
+
function renderSkeleton(count: number): void {
|
|
120
|
+
if (!linesEl || !emptyEl || !subtotalEl || !checkoutBtn) return
|
|
121
|
+
emptyEl.classList.add('hidden')
|
|
122
|
+
linesEl.classList.remove('hidden')
|
|
123
|
+
const rows = Math.max(count || 0, 1)
|
|
124
|
+
linesEl.innerHTML = Array.from({ length: rows })
|
|
125
|
+
.map(
|
|
126
|
+
() => `
|
|
127
|
+
<li class="flex animate-pulse gap-3" aria-hidden="true">
|
|
128
|
+
<div class="h-16 w-16 bg-[var(--muted)]"></div>
|
|
129
|
+
<div class="flex-1 space-y-2 pt-1">
|
|
130
|
+
<div class="h-3 w-2/3 rounded bg-[var(--muted)]"></div>
|
|
131
|
+
<div class="h-3 w-1/3 rounded bg-[var(--muted)]"></div>
|
|
132
|
+
</div>
|
|
133
|
+
<div class="flex flex-col items-end justify-between">
|
|
134
|
+
<div class="h-3 w-12 rounded bg-[var(--muted)]"></div>
|
|
135
|
+
</div>
|
|
136
|
+
</li>`,
|
|
137
|
+
)
|
|
138
|
+
.join('')
|
|
139
|
+
subtotalEl.textContent = '—'
|
|
140
|
+
checkoutBtn.disabled = true
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function openDrawer(): void {
|
|
144
|
+
drawer?.classList.remove('hidden')
|
|
145
|
+
backdrop?.classList.remove('hidden')
|
|
146
|
+
requestAnimationFrame(() => {
|
|
147
|
+
drawer?.classList.remove('translate-x-full')
|
|
148
|
+
drawer?.setAttribute('aria-hidden', 'false')
|
|
149
|
+
})
|
|
150
|
+
const snap = cart.getSnapshot()
|
|
151
|
+
if (!snap.cartId) {
|
|
152
|
+
render(snap)
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
// Show a skeleton instead of the cached snapshot so users never see
|
|
156
|
+
// stale prices/quantities. Refresh resolves with live Fourthwall data.
|
|
157
|
+
pendingRefresh = true
|
|
158
|
+
renderSkeleton(snap.count)
|
|
159
|
+
cart
|
|
160
|
+
.refresh()
|
|
161
|
+
.catch(() => {
|
|
162
|
+
/* noop — refresh() already resets state on unrecoverable error */
|
|
163
|
+
})
|
|
164
|
+
.finally(() => {
|
|
165
|
+
pendingRefresh = false
|
|
166
|
+
render(cart.getSnapshot())
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function closeDrawer(): void {
|
|
171
|
+
drawer?.classList.add('translate-x-full')
|
|
172
|
+
drawer?.setAttribute('aria-hidden', 'true')
|
|
173
|
+
setTimeout(() => {
|
|
174
|
+
drawer?.classList.add('hidden')
|
|
175
|
+
backdrop?.classList.add('hidden')
|
|
176
|
+
}, 200)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function render(snap: CartSnapshot): void {
|
|
180
|
+
if (!linesEl || !emptyEl || !subtotalEl || !checkoutBtn) return
|
|
181
|
+
if (pendingRefresh) return
|
|
182
|
+
if (snap.items.length === 0) {
|
|
183
|
+
emptyEl.classList.remove('hidden')
|
|
184
|
+
linesEl.classList.add('hidden')
|
|
185
|
+
subtotalEl.textContent = '—'
|
|
186
|
+
checkoutBtn.disabled = true
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
emptyEl.classList.add('hidden')
|
|
190
|
+
linesEl.classList.remove('hidden')
|
|
191
|
+
linesEl.innerHTML = snap.items
|
|
192
|
+
.map((item) => {
|
|
193
|
+
const optSrc = optimizeFwImage(item.imageUrl, { width: 128, enabled: imageTransformEnabled }) ?? item.imageUrl
|
|
194
|
+
const img = item.imageUrl
|
|
195
|
+
? `<img src="${optSrc}" alt="" loading="lazy" decoding="async" class="h-16 w-16 object-cover" />`
|
|
196
|
+
: `<div class="h-16 w-16 bg-[var(--muted)]"></div>`
|
|
197
|
+
const lineTotal = formatMoney(Math.round(item.priceValue * 100) * item.quantity, item.priceCurrency)
|
|
198
|
+
return `
|
|
199
|
+
<li class="flex gap-3" data-line-id="${item.lineId}">
|
|
200
|
+
${img}
|
|
201
|
+
<div class="flex-1 space-y-1 text-sm">
|
|
202
|
+
<p class="font-semibold">${escapeHtml(item.name || 'Item')}</p>
|
|
203
|
+
<p class="text-[var(--muted-foreground)]">Qty ${item.quantity}</p>
|
|
204
|
+
</div>
|
|
205
|
+
<div class="flex flex-col items-end justify-between text-sm">
|
|
206
|
+
<span class="font-semibold">${lineTotal}</span>
|
|
207
|
+
<button type="button" class="cursor-pointer text-sm text-[var(--muted-foreground)] underline transition-colors hover:text-[var(--primary,var(--foreground))] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]" data-remove="${item.lineId}">Remove</button>
|
|
208
|
+
</div>
|
|
209
|
+
</li>`
|
|
210
|
+
})
|
|
211
|
+
.join('')
|
|
212
|
+
subtotalEl.textContent = formatMoney(snap.totalCents, snap.currency)
|
|
213
|
+
checkoutBtn.disabled = false
|
|
214
|
+
|
|
215
|
+
linesEl.querySelectorAll<HTMLButtonElement>('[data-remove]').forEach((btn) => {
|
|
216
|
+
btn.addEventListener('click', async () => {
|
|
217
|
+
const lineId = btn.getAttribute('data-remove')
|
|
218
|
+
if (!lineId) return
|
|
219
|
+
const removed = cart.getSnapshot().items.find((it) => it.lineId === lineId)
|
|
220
|
+
btn.disabled = true
|
|
221
|
+
try {
|
|
222
|
+
const next = await cart.remove(lineId)
|
|
223
|
+
if (removed?.productId) {
|
|
224
|
+
window.glAnalytics?.track('store_remove_from_cart', {
|
|
225
|
+
productId: removed.productId,
|
|
226
|
+
productName: removed.productName,
|
|
227
|
+
sku: removed.sku,
|
|
228
|
+
variant: removed.variantName,
|
|
229
|
+
price: removed.priceValue,
|
|
230
|
+
currency: removed.priceCurrency,
|
|
231
|
+
quantity: removed.quantity,
|
|
232
|
+
cartTotal: next.totalCents / 100,
|
|
233
|
+
source: 'cart-drawer',
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
} catch (err) {
|
|
237
|
+
console.error('cart remove failed', err)
|
|
238
|
+
} finally {
|
|
239
|
+
btn.disabled = false
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function escapeHtml(str: string): string {
|
|
246
|
+
return str
|
|
247
|
+
.replace(/&/g, '&')
|
|
248
|
+
.replace(/</g, '<')
|
|
249
|
+
.replace(/>/g, '>')
|
|
250
|
+
.replace(/"/g, '"')
|
|
251
|
+
.replace(/'/g, ''')
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
cart.on(render)
|
|
255
|
+
document.addEventListener('fw:cart-open', openDrawer)
|
|
256
|
+
closeBtn?.addEventListener('click', closeDrawer)
|
|
257
|
+
backdrop?.addEventListener('click', closeDrawer)
|
|
258
|
+
document.addEventListener('keydown', (e) => {
|
|
259
|
+
if (e.key === 'Escape') closeDrawer()
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
checkoutBtn?.addEventListener('click', async () => {
|
|
263
|
+
const snap = cart.getSnapshot()
|
|
264
|
+
if (!snap.cartId) return
|
|
265
|
+
checkoutBtn.disabled = true
|
|
266
|
+
const original = checkoutBtn.textContent
|
|
267
|
+
checkoutBtn.textContent = 'Redirecting…'
|
|
268
|
+
try {
|
|
269
|
+
await initiateCheckout(snap, { source: 'cart-drawer', checkoutPath })
|
|
270
|
+
} catch (err) {
|
|
271
|
+
console.error(err)
|
|
272
|
+
checkoutBtn.textContent = original
|
|
273
|
+
checkoutBtn.disabled = false
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
</script>
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Header cart icon with a live count badge. Listens for fw:cart-updated
|
|
4
|
+
* events emitted by cart-client and opens the drawer on click when there are
|
|
5
|
+
* items; otherwise navigates to the store index.
|
|
6
|
+
*
|
|
7
|
+
* Uses a single bundled module script and `data-fw-cart-icon` attributes so
|
|
8
|
+
* every CartIcon instance on the page (header + mobile dialog) is wired up
|
|
9
|
+
* without per-instance inline scripts.
|
|
10
|
+
*/
|
|
11
|
+
interface Props {
|
|
12
|
+
idSuffix?: string
|
|
13
|
+
class?: string
|
|
14
|
+
hideWhenEmpty?: boolean
|
|
15
|
+
storePath?: string
|
|
16
|
+
/** Fourthwall-hosted shop URL, rendered as a `noscript` fallback. */
|
|
17
|
+
fourthwallShopUrl?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { idSuffix = 'header', class: className = '', hideWhenEmpty = false, storePath = '/store/', fourthwallShopUrl } = Astro.props
|
|
21
|
+
const iconId = `fw-cart-icon-${idSuffix}`
|
|
22
|
+
const badgeId = `fw-cart-count-${idSuffix}`
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
<button
|
|
26
|
+
type="button"
|
|
27
|
+
id={iconId}
|
|
28
|
+
data-fw-cart-icon
|
|
29
|
+
data-fw-store-path={storePath}
|
|
30
|
+
data-fw-hide-when-empty={hideWhenEmpty ? 'true' : undefined}
|
|
31
|
+
hidden={hideWhenEmpty}
|
|
32
|
+
class:list={[
|
|
33
|
+
'relative cursor-pointer text-[var(--foreground)] transition hover:text-[var(--primary,var(--accent-foreground))] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-[var(--ring,var(--foreground))]',
|
|
34
|
+
className,
|
|
35
|
+
]}
|
|
36
|
+
aria-label="Open cart"
|
|
37
|
+
>
|
|
38
|
+
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
39
|
+
<path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z" />
|
|
40
|
+
<path d="M3 6h18" />
|
|
41
|
+
<path d="M16 10a4 4 0 0 1-8 0" />
|
|
42
|
+
</svg>
|
|
43
|
+
<span
|
|
44
|
+
id={badgeId}
|
|
45
|
+
data-fw-cart-badge
|
|
46
|
+
class="pointer-events-none absolute -right-2 -top-2 hidden min-w-[1.25rem] rounded-full bg-[var(--primary,#b42525)] px-1 text-center text-xs font-semibold leading-5 text-[var(--primary-foreground,white)]"
|
|
47
|
+
>0</span>
|
|
48
|
+
</button>
|
|
49
|
+
|
|
50
|
+
{fourthwallShopUrl && (
|
|
51
|
+
<noscript>
|
|
52
|
+
<a href={fourthwallShopUrl} class="text-sm underline">Cart</a>
|
|
53
|
+
</noscript>
|
|
54
|
+
)}
|
|
55
|
+
|
|
56
|
+
<script>
|
|
57
|
+
import { cart, type CartSnapshot } from '../cart-client.js'
|
|
58
|
+
|
|
59
|
+
function wireIcons() {
|
|
60
|
+
const icons = document.querySelectorAll<HTMLButtonElement>('[data-fw-cart-icon]')
|
|
61
|
+
if (!icons.length) return
|
|
62
|
+
|
|
63
|
+
function render(snap: CartSnapshot) {
|
|
64
|
+
icons.forEach((icon) => {
|
|
65
|
+
const badge = icon.querySelector<HTMLElement>('[data-fw-cart-badge]')
|
|
66
|
+
if (!badge) return
|
|
67
|
+
if (snap.count > 0) {
|
|
68
|
+
badge.textContent = String(snap.count)
|
|
69
|
+
badge.classList.remove('hidden')
|
|
70
|
+
if (icon.dataset.fwHideWhenEmpty === 'true') icon.hidden = false
|
|
71
|
+
} else {
|
|
72
|
+
badge.textContent = '0'
|
|
73
|
+
badge.classList.add('hidden')
|
|
74
|
+
if (icon.dataset.fwHideWhenEmpty === 'true') icon.hidden = true
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
cart.on(render)
|
|
80
|
+
|
|
81
|
+
icons.forEach((icon) => {
|
|
82
|
+
icon.addEventListener('click', (e) => {
|
|
83
|
+
e.stopPropagation()
|
|
84
|
+
const snapshot = cart.getSnapshot()
|
|
85
|
+
if (snapshot.count > 0) {
|
|
86
|
+
document.dispatchEvent(new CustomEvent('fw:cart-open'))
|
|
87
|
+
} else {
|
|
88
|
+
window.location.href = icon.dataset.fwStorePath ?? '/store/'
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
wireIcons()
|
|
95
|
+
document.addEventListener('astro:after-swap', wireIcons)
|
|
96
|
+
</script>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { optimizeFwImage } from '../image.js'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
handle: string
|
|
6
|
+
name: string
|
|
7
|
+
description?: string
|
|
8
|
+
coverImageUrl?: string | null
|
|
9
|
+
priority?: boolean
|
|
10
|
+
/** From `StoreOptions.imageTransform.enabled`. */
|
|
11
|
+
imageTransformEnabled?: boolean
|
|
12
|
+
/** Base path for collection routes; default `/store/collections`. */
|
|
13
|
+
basePath?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
handle,
|
|
18
|
+
name,
|
|
19
|
+
description,
|
|
20
|
+
coverImageUrl,
|
|
21
|
+
priority = false,
|
|
22
|
+
imageTransformEnabled = false,
|
|
23
|
+
basePath = '/store/collections',
|
|
24
|
+
} = Astro.props
|
|
25
|
+
|
|
26
|
+
const href = `${basePath}/${handle}/`
|
|
27
|
+
const isRemote = coverImageUrl?.startsWith('http')
|
|
28
|
+
const optimizedSrc = isRemote
|
|
29
|
+
? optimizeFwImage(coverImageUrl ?? null, { width: 640, enabled: imageTransformEnabled })
|
|
30
|
+
: (coverImageUrl ?? null)
|
|
31
|
+
const srcset =
|
|
32
|
+
isRemote && coverImageUrl
|
|
33
|
+
? [320, 480, 640, 960]
|
|
34
|
+
.map((w) => `${optimizeFwImage(coverImageUrl, { width: w, enabled: imageTransformEnabled })} ${w}w`)
|
|
35
|
+
.join(', ')
|
|
36
|
+
: undefined
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
<a
|
|
40
|
+
href={href}
|
|
41
|
+
class="group flex h-full flex-col overflow-hidden rounded-sm border border-[var(--border)] transition-colors hover:border-[var(--foreground)]"
|
|
42
|
+
>
|
|
43
|
+
<div class="relative aspect-[4/5] overflow-hidden bg-[var(--muted)]">
|
|
44
|
+
{coverImageUrl ? (
|
|
45
|
+
<img
|
|
46
|
+
src={optimizedSrc ?? coverImageUrl}
|
|
47
|
+
srcset={srcset}
|
|
48
|
+
sizes="(min-width: 1024px) 25vw, (min-width: 640px) 50vw, 100vw"
|
|
49
|
+
alt={name}
|
|
50
|
+
width="640"
|
|
51
|
+
height="800"
|
|
52
|
+
loading={priority ? 'eager' : 'lazy'}
|
|
53
|
+
decoding="async"
|
|
54
|
+
fetchpriority={priority ? 'high' : 'auto'}
|
|
55
|
+
class="h-full w-full object-cover transition-transform duration-500 group-hover:scale-[1.01]"
|
|
56
|
+
/>
|
|
57
|
+
) : (
|
|
58
|
+
<div class="flex h-full w-full items-center justify-center">
|
|
59
|
+
<span class="text-lg font-semibold text-[var(--muted-foreground)]">{name}</span>
|
|
60
|
+
</div>
|
|
61
|
+
)}
|
|
62
|
+
</div>
|
|
63
|
+
<div class="flex flex-1 flex-col gap-1.5 p-5">
|
|
64
|
+
<h3 class="mb-1 text-lg font-semibold uppercase tracking-wide text-[var(--foreground)] transition-colors">
|
|
65
|
+
{name}{' '}
|
|
66
|
+
<svg
|
|
67
|
+
class="ml-0.5 inline-block h-[0.9em] w-[0.9em] translate-y-[0.05em] align-baseline"
|
|
68
|
+
viewBox="0 0 24 24"
|
|
69
|
+
fill="none"
|
|
70
|
+
stroke="currentColor"
|
|
71
|
+
stroke-width="2"
|
|
72
|
+
stroke-linecap="round"
|
|
73
|
+
stroke-linejoin="round"
|
|
74
|
+
aria-hidden="true"
|
|
75
|
+
>
|
|
76
|
+
<path d="m9 18 6-6-6-6" />
|
|
77
|
+
</svg>
|
|
78
|
+
</h3>
|
|
79
|
+
{description && <p class="text-sm leading-relaxed text-[var(--muted-foreground)]">{description}</p>}
|
|
80
|
+
</div>
|
|
81
|
+
</a>
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
currentCurrency: string
|
|
4
|
+
currencies?: string[]
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const { currentCurrency, currencies = ['USD', 'GBP', 'EUR', 'CAD', 'AUD'] } = Astro.props
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
<div class="relative" data-currency-selector>
|
|
11
|
+
<button
|
|
12
|
+
type="button"
|
|
13
|
+
class="inline-flex items-center gap-1.5 rounded-sm px-2 py-1.5 text-xs font-semibold uppercase tracking-[0.1em] text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring,var(--foreground))] focus-visible:ring-offset-2"
|
|
14
|
+
aria-label="Select currency"
|
|
15
|
+
aria-haspopup="listbox"
|
|
16
|
+
aria-expanded="false"
|
|
17
|
+
data-currency-toggle
|
|
18
|
+
>
|
|
19
|
+
{currentCurrency}
|
|
20
|
+
<svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6" /></svg>
|
|
21
|
+
</button>
|
|
22
|
+
<div
|
|
23
|
+
class="absolute right-0 top-full z-[100] mt-2 hidden min-w-24 overflow-hidden rounded border border-[var(--border)] bg-[var(--card)] py-1 text-[var(--foreground)] shadow-xl"
|
|
24
|
+
role="listbox"
|
|
25
|
+
aria-label="Currency options"
|
|
26
|
+
data-currency-menu
|
|
27
|
+
>
|
|
28
|
+
{currencies.map((currency) => (
|
|
29
|
+
<button
|
|
30
|
+
type="button"
|
|
31
|
+
class="block w-full px-3 py-2 text-left text-xs font-semibold uppercase tracking-[0.1em] text-[var(--foreground)] transition-colors hover:bg-[var(--muted)] focus-visible:bg-[var(--muted)] focus-visible:outline-none"
|
|
32
|
+
role="option"
|
|
33
|
+
aria-selected={currency === currentCurrency}
|
|
34
|
+
data-currency-option={currency}
|
|
35
|
+
>
|
|
36
|
+
{currency}
|
|
37
|
+
</button>
|
|
38
|
+
))}
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<script>
|
|
43
|
+
document.querySelectorAll<HTMLElement>('[data-currency-selector]').forEach((selector) => {
|
|
44
|
+
const toggle = selector.querySelector<HTMLButtonElement>('[data-currency-toggle]')
|
|
45
|
+
const menu = selector.querySelector<HTMLElement>('[data-currency-menu]')
|
|
46
|
+
if (!toggle || !menu) return
|
|
47
|
+
|
|
48
|
+
const setOpen = (open: boolean) => {
|
|
49
|
+
menu.classList.toggle('hidden', !open)
|
|
50
|
+
toggle.setAttribute('aria-expanded', String(open))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
toggle.addEventListener('click', () => {
|
|
54
|
+
setOpen(menu.classList.contains('hidden'))
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
selector.querySelectorAll<HTMLButtonElement>('[data-currency-option]').forEach((option) => {
|
|
58
|
+
option.addEventListener('click', () => {
|
|
59
|
+
const currency = option.dataset.currencyOption
|
|
60
|
+
if (!currency) return
|
|
61
|
+
document.cookie = `fw_currency=${encodeURIComponent(currency)}; Max-Age=31536000; Path=/; SameSite=Lax`
|
|
62
|
+
window.location.reload()
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
document.addEventListener('click', (event) => {
|
|
67
|
+
if (!selector.contains(event.target as Node)) setOpen(false)
|
|
68
|
+
})
|
|
69
|
+
document.addEventListener('keydown', (event) => {
|
|
70
|
+
if (event.key === 'Escape') setOpen(false)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
</script>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
import type { FwPrice } from '../types.js'
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
price: FwPrice
|
|
6
|
+
compareAt?: FwPrice | null
|
|
7
|
+
class?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { price, compareAt, class: className = '' } = Astro.props
|
|
11
|
+
|
|
12
|
+
function format(p: FwPrice): string {
|
|
13
|
+
try {
|
|
14
|
+
return new Intl.NumberFormat('en-US', {
|
|
15
|
+
style: 'currency',
|
|
16
|
+
currency: p.currency,
|
|
17
|
+
maximumFractionDigits: Number.isInteger(p.value) ? 0 : 2,
|
|
18
|
+
}).format(p.value)
|
|
19
|
+
} catch {
|
|
20
|
+
return `${p.currency} ${p.value.toFixed(2)}`
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const onSale = compareAt ? compareAt.value > price.value : false
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
<span class:list={['inline-flex items-baseline gap-2', className]}>
|
|
28
|
+
<span class="font-semibold text-[var(--foreground)]">{format(price)}</span>
|
|
29
|
+
{onSale && compareAt && (
|
|
30
|
+
<span class="text-sm text-[var(--muted-foreground)] line-through">
|
|
31
|
+
{format(compareAt)}
|
|
32
|
+
</span>
|
|
33
|
+
)}
|
|
34
|
+
</span>
|