@usequeek/theme-kit 0.4.1 → 0.5.1
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 +8 -0
- package/apps/apps.css +37 -7
- package/apps/consent-preferences-link.tsx +2 -1
- package/apps/follow-card.tsx +4 -1
- package/apps/use-analytics-consent.ts +63 -14
- package/client.ts +1 -0
- package/components/blog/post-meta.tsx +4 -0
- package/components/blog/related-posts.tsx +2 -0
- package/components/image.tsx +23 -3
- package/hooks/use-categories.ts +3 -0
- package/index.ts +1 -0
- package/media-index.tsx +76 -0
- package/package.json +1 -1
- package/page-renderer.tsx +6 -1
- package/types/theme.ts +1 -1
- package/types/vendor.ts +5 -0
- package/utils/image-srcset.ts +40 -3
package/README.md
CHANGED
|
@@ -590,6 +590,14 @@ the footer control live in the kit so every theme gets them:
|
|
|
590
590
|
ids from `config.apps.analytics` (the patterns are exported too); an id that
|
|
591
591
|
fails its pattern counts as not set.
|
|
592
592
|
|
|
593
|
+
`config.apps.consent_banner` (typed in `types/vendor.ts`) carries the store's own `message` (replaces the banner's lead sentence) and `show` (`required` or `everywhere`); the storefront reads them.
|
|
594
|
+
|
|
595
|
+
`ConsentRegimeProvider` (`.../apps/use-analytics-consent`) takes `required`. The storefront sets it per request (the
|
|
596
|
+
visitor's country, the store's own `show` setting). Without it every shopper must answer. With `required={false}` a shopper
|
|
597
|
+
with no stored answer reads as granted for both categories (nothing is written to the record), Global Privacy Control still
|
|
598
|
+
reads as declined, and `reset()` does not delete the stored answer: it marks the question reopened (`decision` and
|
|
599
|
+
`marketing` read `null`) until `decide()` runs. The footer link therefore shows in that region and reopens the banner.
|
|
600
|
+
|
|
593
601
|
Style the button through `core-consent-link`; the kit ships a neutral style.
|
|
594
602
|
|
|
595
603
|
## Locales
|
package/apps/apps.css
CHANGED
|
@@ -162,7 +162,11 @@
|
|
|
162
162
|
margin: 0;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
|
|
165
|
+
/* One class and two elements on purpose (`div button.…`, specificity 0,1,2): it beats a theme's `button { color: inherit }` reset (0,1,1), which
|
|
166
|
+
left the label in the card's text colour on the accent fill (dark maroon on crimson, Beaurify, 16/11/26), and still loses to a theme's own
|
|
167
|
+
two-class rule such as `.theme-carat .core-follow-card__btn` (0,2,0), which is meant to restyle it. The fill and the label are the readable
|
|
168
|
+
pair the tokens guarantee at 4.5:1: `--brand-accent-fill` (the accent, deepened only when it had to be) and `--brand-on-accent`. */
|
|
169
|
+
div button.core-follow-card__btn {
|
|
166
170
|
width: 100%;
|
|
167
171
|
padding: 0.5rem 1rem;
|
|
168
172
|
border: 0;
|
|
@@ -171,8 +175,8 @@
|
|
|
171
175
|
font-weight: 600;
|
|
172
176
|
cursor: pointer;
|
|
173
177
|
transition: background 0.15s, transform 0.1s;
|
|
174
|
-
background: var(--brand-
|
|
175
|
-
color: var(--brand-
|
|
178
|
+
background: var(--brand-accent-fill, var(--brand-accent, #000));
|
|
179
|
+
color: var(--brand-on-accent, #fff);
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
.core-follow-card__btn:hover {
|
|
@@ -313,7 +317,8 @@
|
|
|
313
317
|
outline-offset: 1px;
|
|
314
318
|
}
|
|
315
319
|
|
|
316
|
-
|
|
320
|
+
/* `form button.…` (0,1,2) for the same reason as the follow card button: beats a theme's `button` reset, yields to a theme's own two-class rule. */
|
|
321
|
+
form button.core-subscribe__button {
|
|
317
322
|
padding: 0.7rem 1.25rem;
|
|
318
323
|
font: inherit;
|
|
319
324
|
font-size: 0.9375rem;
|
|
@@ -326,8 +331,8 @@
|
|
|
326
331
|
transition: opacity 0.2s ease;
|
|
327
332
|
}
|
|
328
333
|
|
|
329
|
-
.core-subscribe__button:hover:not(:disabled) { opacity: 0.88; }
|
|
330
|
-
.core-subscribe__button:disabled { opacity: 0.6; cursor: default; }
|
|
334
|
+
form button.core-subscribe__button:hover:not(:disabled) { opacity: 0.88; }
|
|
335
|
+
form button.core-subscribe__button:disabled { opacity: 0.6; cursor: default; }
|
|
331
336
|
|
|
332
337
|
.core-subscribe__error {
|
|
333
338
|
margin: 0.6rem 0 0;
|
|
@@ -392,6 +397,23 @@
|
|
|
392
397
|
width: auto;
|
|
393
398
|
bottom: 5rem;
|
|
394
399
|
}
|
|
400
|
+
|
|
401
|
+
/* One floating card at a time on a phone: the follow card and this one are both full width at the same height, and the shell
|
|
402
|
+
renders them as siblings. A full follow card keeps the bottom; this card shows once it is dismissed or the shopper follows. */
|
|
403
|
+
.core-follow-card--floating:not(.core-follow-card--compact) ~ .core-subscribe-floating {
|
|
404
|
+
display: none;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/* The small "Following" chip sits at the right edge at the same height: lift this card above it. */
|
|
408
|
+
.core-follow-card--compact.core-follow-card--floating ~ .core-subscribe-floating {
|
|
409
|
+
bottom: 8rem;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/* Another prompt is open (the storefront's overlay gate sets the attribute): the floating cards wait, the compact "Following" chip stays. */
|
|
414
|
+
[data-overlay-held] .core-follow-card--floating:not(.core-follow-card--compact),
|
|
415
|
+
[data-overlay-held] .core-subscribe-floating {
|
|
416
|
+
display: none;
|
|
395
417
|
}
|
|
396
418
|
|
|
397
419
|
/* ── Subscribe — modal variant ───────────────────────────── */
|
|
@@ -595,8 +617,16 @@
|
|
|
595
617
|
.core-consent-banner__text {
|
|
596
618
|
flex: 0 1 auto;
|
|
597
619
|
}
|
|
620
|
+
/* Stacked full width, in markup order (Accept first, Decline last, so focus order is the visual order): the labels ("Accept analytics
|
|
621
|
+
only", "Decline all") never wrap in cramped columns, and every button is the same size, Decline as easy to press as Accept. */
|
|
598
622
|
.core-consent-banner__actions {
|
|
599
|
-
|
|
623
|
+
flex-direction: column;
|
|
624
|
+
align-items: stretch;
|
|
625
|
+
}
|
|
626
|
+
.core-consent-banner .core-consent-banner__decline,
|
|
627
|
+
.core-consent-banner .core-consent-banner__accept {
|
|
628
|
+
width: 100%;
|
|
629
|
+
text-align: center;
|
|
600
630
|
}
|
|
601
631
|
}
|
|
602
632
|
|
|
@@ -23,7 +23,8 @@ function ConsentPreferencesButton({ onReset }: { onReset: () => void }): JSX.Ele
|
|
|
23
23
|
* Shown only when the store uses analytics or an ad pixel, the shopper has
|
|
24
24
|
* answered every question it asks, and the browser is not sending Global
|
|
25
25
|
* Privacy Control (that is already a decline: there is nothing to reopen).
|
|
26
|
-
* Clicking it
|
|
26
|
+
* Clicking it brings the banner back for this store only: where consent is required it forgets the stored choice, and outside
|
|
27
|
+
* those regions (`ConsentRegimeProvider required={false}`) it marks the question reopened and keeps the stored choice until the shopper decides.
|
|
27
28
|
* Renders nothing on the server and while hydrating (the decision is read from
|
|
28
29
|
* the browser), outside a `StorefrontProvider`, and for a store with no consent
|
|
29
30
|
* category. Reads the context directly so it never throws without a provider.
|
package/apps/follow-card.tsx
CHANGED
|
@@ -7,6 +7,7 @@ import { useUserStore } from '../stores/user-store';
|
|
|
7
7
|
import { useAuthModalStore } from '../stores/auth-modal-store';
|
|
8
8
|
import { getQueekClient } from '../sdk/queek-client';
|
|
9
9
|
import { formatCount } from '../utils/format';
|
|
10
|
+
import { Image } from '../components/image';
|
|
10
11
|
|
|
11
12
|
interface FollowData {
|
|
12
13
|
following: boolean;
|
|
@@ -228,8 +229,10 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
228
229
|
{previewMessage && <p className="core-follow-card__notice" role="status">{t('preview.action')}</p>}
|
|
229
230
|
<div className="core-follow-card__body">
|
|
230
231
|
{vendor.logo && (
|
|
231
|
-
<
|
|
232
|
+
<Image
|
|
232
233
|
src={vendor.logo}
|
|
234
|
+
variants={vendor.logo_variants}
|
|
235
|
+
intent="avatar"
|
|
233
236
|
alt={vendor.name ?? ''}
|
|
234
237
|
className="core-follow-card__logo"
|
|
235
238
|
/>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { useCallback, useSyncExternalStore } from 'react';
|
|
3
|
+
import { createContext, createElement, useCallback, useContext, useLayoutEffect, useSyncExternalStore, type JSX, type ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Consent is keyed by vendor id inside one shared localStorage record — the
|
|
@@ -75,11 +75,50 @@ function subscribe(onChange: () => void): () => void {
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Whether the shopper must answer before anything non-essential runs. The storefront decides it per request
|
|
80
|
+
* (the visitor's country, the store's own `show` setting) and tells the kit through `ConsentRegimeProvider`;
|
|
81
|
+
* without a provider every visitor must answer, exactly as before.
|
|
82
|
+
*/
|
|
83
|
+
const ConsentRegimeContext = createContext<boolean>(true);
|
|
84
|
+
|
|
85
|
+
/** The React-free readers' copy of the regime (the ad-pixel event subscribers fire outside React). Set only in the browser. */
|
|
86
|
+
let regimeRequired = true;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Stores whose shopper opened "Cookie preferences" while consent is not required. A reopened question must read
|
|
90
|
+
* unanswered even though an earlier answer is still stored: the stored answer stays untouched until the shopper
|
|
91
|
+
* decides again, so closing the tab mid-review loses nothing. In memory on purpose.
|
|
92
|
+
*/
|
|
93
|
+
const reviewing = new Set<string>();
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Tells the kit whether this visitor must answer before analytics or ad pixels run. `required={false}` means a
|
|
97
|
+
* shopper with no stored answer reads as granted (never written to the record: only a click writes it), Global
|
|
98
|
+
* Privacy Control still reads as declined, and "Cookie preferences" in the footer reopens the question so the
|
|
99
|
+
* shopper can decline. Default (no provider) is `required`.
|
|
100
|
+
*/
|
|
101
|
+
export function ConsentRegimeProvider({ required, children }: { required: boolean; children: ReactNode }): JSX.Element {
|
|
102
|
+
useLayoutEffect(() => {
|
|
103
|
+
regimeRequired = required;
|
|
104
|
+
return () => {
|
|
105
|
+
regimeRequired = true;
|
|
106
|
+
};
|
|
107
|
+
}, [required]);
|
|
108
|
+
|
|
109
|
+
return createElement(ConsentRegimeContext.Provider, { value: required }, children);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* One category's answer. Order matters: Global Privacy Control declines; a question the shopper reopened is
|
|
114
|
+
* unanswered; a stored answer wins; with no answer, a region that does not require consent grants and the rest ask.
|
|
115
|
+
*/
|
|
116
|
+
function readDecision(vendorId: string, category: keyof ConsentChoice, required: boolean): boolean | null {
|
|
79
117
|
if (hasGlobalPrivacyControl()) return false;
|
|
118
|
+
if (reviewing.has(vendorId)) return null;
|
|
80
119
|
const record = readStore()[vendorId];
|
|
81
|
-
if (
|
|
82
|
-
return
|
|
120
|
+
if (record && record.version === CONSENT_VERSION && typeof record[category] === 'boolean') return record[category];
|
|
121
|
+
return required ? null : true;
|
|
83
122
|
}
|
|
84
123
|
|
|
85
124
|
/**
|
|
@@ -90,16 +129,20 @@ function readDecision(vendorId: string, category: keyof ConsentChoice): boolean
|
|
|
90
129
|
*/
|
|
91
130
|
export function readStoredMarketingConsent(vendorId: string | undefined): boolean {
|
|
92
131
|
if (!vendorId || typeof window === 'undefined') return false;
|
|
93
|
-
return readDecision(vendorId, 'marketing') === true;
|
|
132
|
+
return readDecision(vendorId, 'marketing', regimeRequired) === true;
|
|
94
133
|
}
|
|
95
134
|
|
|
96
135
|
/**
|
|
97
136
|
* Per-vendor consent in two independent categories. `decision` (analytics) and
|
|
98
137
|
* `marketing` are each `null` until the visitor has chosen (or GPC decided for
|
|
99
138
|
* them) — the banner and Consent Mode's default state both key off that.
|
|
100
|
-
* Granting analytics never grants marketing.
|
|
101
|
-
* default
|
|
102
|
-
* vendor's record
|
|
139
|
+
* Granting analytics never grants marketing. Where consent is required (the
|
|
140
|
+
* default, no provider) marketing (ad pixels) is denied until an explicit accept
|
|
141
|
+
* and `reset` forgets this vendor's record, which reopens the banner. Under
|
|
142
|
+
* `ConsentRegimeProvider required={false}` a category with no stored answer reads
|
|
143
|
+
* as granted (marketing included, nothing is written to the record), GPC still
|
|
144
|
+
* declines, and `reset` leaves the stored record in place and marks the question
|
|
145
|
+
* reopened (both read `null`) until `decide` runs.
|
|
103
146
|
*/
|
|
104
147
|
export function useAnalyticsConsent(vendorId: string | undefined): {
|
|
105
148
|
decision: boolean | null;
|
|
@@ -119,14 +162,15 @@ export function useAnalyticsConsent(vendorId: string | undefined): {
|
|
|
119
162
|
// change event — a decision in the banner reaches the analytics loader
|
|
120
163
|
// without a reload, which per-instance state never did. `null` on the
|
|
121
164
|
// server and during hydration.
|
|
165
|
+
const required = useContext(ConsentRegimeContext);
|
|
122
166
|
const decision = useSyncExternalStore(
|
|
123
167
|
subscribe,
|
|
124
|
-
() => (vendorId ? readDecision(vendorId, 'analytics') : null),
|
|
168
|
+
() => (vendorId ? readDecision(vendorId, 'analytics', required) : null),
|
|
125
169
|
() => null,
|
|
126
170
|
);
|
|
127
171
|
const marketing = useSyncExternalStore(
|
|
128
172
|
subscribe,
|
|
129
|
-
() => (vendorId ? readDecision(vendorId, 'marketing') : null),
|
|
173
|
+
() => (vendorId ? readDecision(vendorId, 'marketing', required) : null),
|
|
130
174
|
() => null,
|
|
131
175
|
);
|
|
132
176
|
|
|
@@ -135,6 +179,7 @@ export function useAnalyticsConsent(vendorId: string | undefined): {
|
|
|
135
179
|
const decide = useCallback(
|
|
136
180
|
(choice: ConsentChoice) => {
|
|
137
181
|
if (!vendorId) return;
|
|
182
|
+
reviewing.delete(vendorId);
|
|
138
183
|
const store = readStore();
|
|
139
184
|
const previous = store[vendorId]?.version === CONSENT_VERSION ? store[vendorId] : undefined;
|
|
140
185
|
store[vendorId] = {
|
|
@@ -152,11 +197,15 @@ export function useAnalyticsConsent(vendorId: string | undefined): {
|
|
|
152
197
|
|
|
153
198
|
const reset = useCallback(() => {
|
|
154
199
|
if (!vendorId) return;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
200
|
+
if (required) {
|
|
201
|
+
const store = readStore();
|
|
202
|
+
delete store[vendorId];
|
|
203
|
+
writeStore(store);
|
|
204
|
+
} else {
|
|
205
|
+
reviewing.add(vendorId);
|
|
206
|
+
}
|
|
158
207
|
window.dispatchEvent(new Event(CHANGE_EVENT));
|
|
159
|
-
}, [vendorId]);
|
|
208
|
+
}, [vendorId, required]);
|
|
160
209
|
|
|
161
210
|
return { decision, marketing, gpc, decide, reset };
|
|
162
211
|
}
|
package/client.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from './theme-variant-resolver';
|
|
|
14
14
|
export * from './shared-blocks';
|
|
15
15
|
export * from './components/blog';
|
|
16
16
|
export * from './components/image';
|
|
17
|
+
export * from './media-index';
|
|
17
18
|
export * from './components/product-metafields';
|
|
18
19
|
export * from './components/powered-by-queek';
|
|
19
20
|
export * from './components/language-switcher';
|
|
@@ -18,6 +18,8 @@ export function PostMeta({ post, className }: { post: Post; className?: string }
|
|
|
18
18
|
|
|
19
19
|
const authorName = post.author?.name ?? vendor.name ?? null;
|
|
20
20
|
const authorAvatar = post.author?.avatar_url ?? vendor.logo ?? null;
|
|
21
|
+
// The vendor's tier map belongs to the vendor's logo only, never to an author's own avatar.
|
|
22
|
+
const avatarVariants = post.author?.avatar_url ? null : vendor.logo_variants;
|
|
21
23
|
const published = post.published_at
|
|
22
24
|
? formatShopperDate(post.published_at, display.locale, { year: 'numeric', month: 'long', day: 'numeric' }, display.timeZone)
|
|
23
25
|
: '';
|
|
@@ -29,6 +31,8 @@ export function PostMeta({ post, className }: { post: Post; className?: string }
|
|
|
29
31
|
<span className="core-post-meta__author">
|
|
30
32
|
<Image
|
|
31
33
|
src={authorAvatar || undefined}
|
|
34
|
+
variants={avatarVariants}
|
|
35
|
+
intent="avatar"
|
|
32
36
|
alt={authorName}
|
|
33
37
|
className="core-post-meta__avatar"
|
|
34
38
|
placeholder={{ label: authorName, width: 64, height: 64, variant: 'square' }}
|
|
@@ -36,6 +36,8 @@ export function RelatedPosts({
|
|
|
36
36
|
<div className="core-related__media">
|
|
37
37
|
<Image
|
|
38
38
|
src={post.cover_image_url || undefined}
|
|
39
|
+
variants={post.cover_image_variants}
|
|
40
|
+
intent="card2x"
|
|
39
41
|
alt={post.title}
|
|
40
42
|
className="core-related__img"
|
|
41
43
|
placeholder={{ label: post.title, width: 600, height: 400, variant: 'card' }}
|
package/components/image.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import { useContext, useState, type ImgHTMLAttributes } from 'react';
|
|
4
|
+
import { lookupMediaIndex, useMediaIndex } from '../media-index';
|
|
4
5
|
import { StorefrontContext } from '../provider';
|
|
5
6
|
import type { ImageVariants } from '../types/media';
|
|
6
7
|
import { resolveBrandPalette } from '../utils/brand-palette';
|
|
@@ -41,13 +42,21 @@ type ImageProps = ImgHTMLAttributes<HTMLImageElement> & {
|
|
|
41
42
|
* Purely additive: when absent — and whenever the API legitimately sends null
|
|
42
43
|
* (off-host image, SVG, legacy upload) — this component renders the plain
|
|
43
44
|
* image URL.
|
|
45
|
+
*
|
|
46
|
+
* Omit it (undefined) and a string `src` that is a key of the page's media
|
|
47
|
+
* index (`MediaIndexProvider`) uses that entry. Pass `null` to say "no
|
|
48
|
+
* variants" and skip the lookup.
|
|
44
49
|
*/
|
|
45
50
|
variants?: ImageVariants | null;
|
|
46
51
|
/**
|
|
47
52
|
* What surface this image renders on. Drives which tier is preferred and what
|
|
48
53
|
* `sizes` is emitted. Only meaningful alongside `variants`. Defaults to 'raw'
|
|
49
54
|
* ("best available single url, no responsive selection") so passing `variants`
|
|
50
|
-
* without an `intent` can never accidentally downgrade a hero.
|
|
55
|
+
* without an `intent` can never accidentally downgrade a hero. When the
|
|
56
|
+
* variants come from the page's media index instead (see `variants`), the
|
|
57
|
+
* intent is ALWAYS 'section' and this prop is ignored: call sites picked
|
|
58
|
+
* theirs when the image rendered the original and the intent did nothing,
|
|
59
|
+
* so honouring it would load a smaller copy than the box needs.
|
|
51
60
|
*/
|
|
52
61
|
intent?: ImageIntent;
|
|
53
62
|
};
|
|
@@ -69,12 +78,21 @@ type LoadStage = 'primary' | 'original' | 'failed';
|
|
|
69
78
|
* @example
|
|
70
79
|
* <Image src={product.media.image ?? ''} variants={product.media.image_variants} intent="card" alt={product.title} />
|
|
71
80
|
*/
|
|
72
|
-
export function Image({ fallback, placeholder, variants, intent
|
|
81
|
+
export function Image({ fallback, placeholder, variants: variantsProp, intent: intentProp, onError, ...props }: ImageProps) {
|
|
73
82
|
const [stage, setStage] = useState<LoadStage>('primary');
|
|
74
83
|
// Read context directly — returns null outside a StorefrontProvider
|
|
75
84
|
// (test environments, isolated captures) without throwing.
|
|
76
85
|
const storefront = useContext(StorefrontContext);
|
|
77
86
|
|
|
87
|
+
// A call site that passes no `variants` (undefined; an explicit null means
|
|
88
|
+
// "none") gets the page's media index entry for its src and the full-bleed-safe
|
|
89
|
+
// 'section' intent, whatever intent the call site named (it was chosen when the
|
|
90
|
+
// image rendered the original). An explicit `variants` keeps its own `intent`.
|
|
91
|
+
const mediaIndex = useMediaIndex();
|
|
92
|
+
const indexed = variantsProp === undefined ? lookupMediaIndex(mediaIndex, props.src) : null;
|
|
93
|
+
const variants = indexed ?? variantsProp;
|
|
94
|
+
const intent = indexed ? 'section' : (intentProp ?? 'raw');
|
|
95
|
+
|
|
78
96
|
const resolved = resolveImageSources(variants, intent);
|
|
79
97
|
|
|
80
98
|
// The resolved tier WINS over an explicitly-passed `src`. That's the whole
|
|
@@ -155,7 +173,9 @@ export function Image({ fallback, placeholder, variants, intent = 'raw', onError
|
|
|
155
173
|
loading={loading}
|
|
156
174
|
src={effectiveSrc}
|
|
157
175
|
srcSet={effectiveSrcSet}
|
|
158
|
-
sizes
|
|
176
|
+
// A call site's own `sizes` wins whenever a srcset is emitted: only it knows a box that is cropped
|
|
177
|
+
// (`object-fit: cover`) and drawn wider than the viewport. Without a srcset `sizes` means nothing.
|
|
178
|
+
sizes={effectiveSrcSet ? (props.sizes ?? effectiveSizes) : undefined}
|
|
159
179
|
onError={(e) => {
|
|
160
180
|
// 2-step chain: preferred tier → variants.original → placeholder.
|
|
161
181
|
if (stage === 'primary') {
|
package/hooks/use-categories.ts
CHANGED
|
@@ -5,12 +5,15 @@ import { useStorefront, useStorefrontLocale } from '../provider';
|
|
|
5
5
|
import { createBrowserClient } from '../sdk/client';
|
|
6
6
|
import { withLocaleQuery } from '../utils/locale';
|
|
7
7
|
import { toError } from '../utils/to-error';
|
|
8
|
+
import type { ImageVariants } from '../types/media';
|
|
8
9
|
|
|
9
10
|
export interface Category {
|
|
10
11
|
id: string;
|
|
11
12
|
slug: string;
|
|
12
13
|
name: string;
|
|
13
14
|
image?: string | null;
|
|
15
|
+
/** The API's tier map for `image` (`/collections` sends it); null when the image is not Media-backed. */
|
|
16
|
+
image_variants?: ImageVariants | null;
|
|
14
17
|
products_count?: number;
|
|
15
18
|
children?: Category[];
|
|
16
19
|
}
|
package/index.ts
CHANGED
|
@@ -33,6 +33,7 @@ export * from './types/app-block';
|
|
|
33
33
|
export * from './types/order';
|
|
34
34
|
export * from './types/page';
|
|
35
35
|
export * from './types/menu';
|
|
36
|
+
export * from './types/media';
|
|
36
37
|
export * from './utils/format';
|
|
37
38
|
export * from './utils/markdown';
|
|
38
39
|
export * from './utils/menu-link';
|
package/media-index.tsx
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { JSX } from 'react';
|
|
4
|
+
import { createContext, useContext, useMemo, type ReactNode } from 'react';
|
|
5
|
+
import type { ImageVariants, MediaIndex } from './types/media';
|
|
6
|
+
|
|
7
|
+
const MediaIndexContext = createContext<MediaIndex | null>(null);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hands a page's `media_index` (the API's url → tier-map sidecar on every
|
|
11
|
+
* Page/Post) to every kit `<Image>` and section background below it. An image
|
|
12
|
+
* whose `src` is a key of the index and whose call site passes no `variants`
|
|
13
|
+
* then renders a small responsive copy instead of the original upload, so a
|
|
14
|
+
* theme's section photos need no code change.
|
|
15
|
+
*
|
|
16
|
+
* Nested providers merge, the inner page winning, so a render that combines
|
|
17
|
+
* several pages (page chrome + the page) mounts one provider per page.
|
|
18
|
+
* `index` undefined, null or empty leaves the parent index untouched.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* <MediaIndexProvider index={page.media_index}>
|
|
22
|
+
* <HomePage blocks={page.content} />
|
|
23
|
+
* </MediaIndexProvider>
|
|
24
|
+
*/
|
|
25
|
+
export function MediaIndexProvider({
|
|
26
|
+
index,
|
|
27
|
+
children,
|
|
28
|
+
}: {
|
|
29
|
+
index?: MediaIndex | null;
|
|
30
|
+
children: ReactNode;
|
|
31
|
+
}): JSX.Element {
|
|
32
|
+
const parent = useContext(MediaIndexContext);
|
|
33
|
+
const value = useMemo<MediaIndex | null>(() => {
|
|
34
|
+
if (!index || Object.keys(index).length === 0) return parent;
|
|
35
|
+
return parent ? { ...parent, ...index } : index;
|
|
36
|
+
}, [parent, index]);
|
|
37
|
+
|
|
38
|
+
return <MediaIndexContext.Provider value={value}>{children}</MediaIndexContext.Provider>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The media index of the page being rendered; null outside a {@link MediaIndexProvider}. */
|
|
42
|
+
export function useMediaIndex(): MediaIndex | null {
|
|
43
|
+
return useContext(MediaIndexContext);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The tier map for a raw url, or null when there is no index, the url is not a
|
|
48
|
+
* key of it, or the API sent null for it (an off-host image, an SVG, a legacy
|
|
49
|
+
* upload) — the caller then keeps rendering the raw url.
|
|
50
|
+
*/
|
|
51
|
+
export function lookupMediaIndex(index: MediaIndex | null | undefined, url: unknown): ImageVariants | null {
|
|
52
|
+
if (!index || typeof url !== 'string' || !Object.prototype.hasOwnProperty.call(index, url)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return index[url] ?? null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The url for a CSS `background-image`, which has no `srcset` to step up from:
|
|
60
|
+
* the `view` copy only when it holds as many pixels as the original (`view.w` is
|
|
61
|
+
* at least `original.w`, so the same picture in WebP), else the raw url. A
|
|
62
|
+
* background is drawn full-bleed, so a copy with fewer pixels would be softer than
|
|
63
|
+
* what it renders today.
|
|
64
|
+
*/
|
|
65
|
+
export function mediaIndexBackgroundUrl(index: MediaIndex | null | undefined, url: string): string {
|
|
66
|
+
const variants = lookupMediaIndex(index, url);
|
|
67
|
+
const view = variants?.view;
|
|
68
|
+
const original = variants?.original;
|
|
69
|
+
if (
|
|
70
|
+
view && typeof view.url === 'string' && view.url.trim() !== '' &&
|
|
71
|
+
typeof view.w === 'number' && typeof original?.w === 'number' && view.w >= original.w
|
|
72
|
+
) {
|
|
73
|
+
return view.url;
|
|
74
|
+
}
|
|
75
|
+
return url;
|
|
76
|
+
}
|
package/package.json
CHANGED
package/page-renderer.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import type { Block, BlockDataMap, MetaobjectsResolved } from './types/block';
|
|
|
6
6
|
import type { Product } from './types/product';
|
|
7
7
|
import { useTheme } from './theme-context';
|
|
8
8
|
import { useStorefront } from './provider';
|
|
9
|
+
import { mediaIndexBackgroundUrl, useMediaIndex } from './media-index';
|
|
9
10
|
import { expandDesignTokensPartial } from './utils/brand';
|
|
10
11
|
import {
|
|
11
12
|
CoreButtonBlock,
|
|
@@ -96,6 +97,7 @@ export function PageRenderer({ blocks }: { blocks: Block[] }): JSX.Element {
|
|
|
96
97
|
const { config } = useStorefront();
|
|
97
98
|
const defaultVariants = config.default_variants ?? {};
|
|
98
99
|
const editMode = useEditMode();
|
|
100
|
+
const mediaIndex = useMediaIndex();
|
|
99
101
|
|
|
100
102
|
const renderBlock = (block: Block, index: number): JSX.Element | null => {
|
|
101
103
|
const variant = block.variant ?? defaultVariants[block.type] ?? undefined;
|
|
@@ -162,7 +164,10 @@ export function PageRenderer({ blocks }: { blocks: Block[] }): JSX.Element {
|
|
|
162
164
|
...(hasTokenVars ? tokenVars : {}),
|
|
163
165
|
...(bgImage
|
|
164
166
|
? {
|
|
165
|
-
|
|
167
|
+
// Full-bleed, so the page's `view` copy only when it holds as many
|
|
168
|
+
// pixels as the original (a background has no srcset to step up
|
|
169
|
+
// from); the raw url otherwise.
|
|
170
|
+
backgroundImage: `url(${mediaIndexBackgroundUrl(mediaIndex, bgImage)})`,
|
|
166
171
|
backgroundSize: 'cover',
|
|
167
172
|
backgroundPosition: 'center',
|
|
168
173
|
color: '#fff',
|
package/types/theme.ts
CHANGED
|
@@ -221,7 +221,7 @@ export interface ProductsBlockProps extends ProductsBlockData {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
export interface CategoriesBlockProps extends CategoriesBlockData {
|
|
224
|
-
categories?: Array<{ id: string; slug: string; name: string; image?: string | null }>;
|
|
224
|
+
categories?: Array<{ id: string; slug: string; name: string; image?: string | null; image_variants?: ImageVariants | null }>;
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
export interface BlogBlockProps extends BlogBlockData {
|
package/types/vendor.ts
CHANGED
|
@@ -246,6 +246,11 @@ export interface StorefrontConfig {
|
|
|
246
246
|
/** `modal` variant only — when it should pop up. Defaults to 'scroll' when unset. */
|
|
247
247
|
trigger?: 'immediate' | 'delay' | 'scroll';
|
|
248
248
|
};
|
|
249
|
+
/**
|
|
250
|
+
* The cookie banner. `message` replaces the banner's lead sentence (plain text); `show` is `everywhere` when the merchant
|
|
251
|
+
* wants the banner for every visitor instead of only where consent is required.
|
|
252
|
+
*/
|
|
253
|
+
consent_banner?: { message?: string; show?: 'required' | 'everywhere' };
|
|
249
254
|
};
|
|
250
255
|
followers_count?: number;
|
|
251
256
|
}
|
package/utils/image-srcset.ts
CHANGED
|
@@ -11,8 +11,13 @@ import type { ImageVariantTier, ImageVariantTierName, ImageVariants } from '../t
|
|
|
11
11
|
* (~320px CSS) and the featured/poster card (~620px CSS). Both would
|
|
12
12
|
* otherwise get the identical 480px `card` url, visibly soft on the poster
|
|
13
13
|
* ones.
|
|
14
|
+
*
|
|
15
|
+
* `section` is the intent of a merchant-uploaded section photo (a gallery or
|
|
16
|
+
* banner slide, an image block) that may run full-bleed. The `<Image>` applies
|
|
17
|
+
* it by itself when it finds its `src` in the page's media index and the call
|
|
18
|
+
* site named no intent.
|
|
14
19
|
*/
|
|
15
|
-
export type ImageIntent = 'avatar' | 'card' | 'card2x' | 'modal' | 'hero' | 'raw';
|
|
20
|
+
export type ImageIntent = 'avatar' | 'card' | 'card2x' | 'modal' | 'hero' | 'section' | 'raw';
|
|
16
21
|
|
|
17
22
|
export interface ResolvedImageSources {
|
|
18
23
|
src: string | undefined;
|
|
@@ -25,7 +30,8 @@ const NONE: ResolvedImageSources = { src: undefined, srcSet: undefined, sizes: u
|
|
|
25
30
|
/** Ascending by cap. `thumb` is excluded because it has no width; `original` is
|
|
26
31
|
* excluded on purpose — it is uncapped and unoptimized (raw jpg/png, possibly
|
|
27
32
|
* 6000px), so letting a browser pick it off a srcset is the exact regression
|
|
28
|
-
* the tier map exists to prevent.
|
|
33
|
+
* the tier map exists to prevent. The single exception is the `section` intent,
|
|
34
|
+
* which appends it as the top candidate only when it is wider than `view`. `original` survives only as a last-resort
|
|
29
35
|
* `src` for the `raw` intent and as the onError step-2 retry. */
|
|
30
36
|
const SRCSET_TIERS = ['card', 'card2x', 'view'] as const;
|
|
31
37
|
type SrcSetTier = (typeof SRCSET_TIERS)[number];
|
|
@@ -42,6 +48,7 @@ const INTENT_CEILING: Record<ImageIntent, SrcSetTier | null> = {
|
|
|
42
48
|
card2x: 'view',
|
|
43
49
|
modal: 'card2x',
|
|
44
50
|
hero: 'view',
|
|
51
|
+
section: 'view',
|
|
45
52
|
raw: null, // "best single url, no responsive selection"
|
|
46
53
|
};
|
|
47
54
|
|
|
@@ -54,6 +61,18 @@ const INTENT_CEILING: Record<ImageIntent, SrcSetTier | null> = {
|
|
|
54
61
|
* card2x @768px→768css: DPR2→1536→view(1600) · desktop 620css: DPR2→1240→view
|
|
55
62
|
* modal 420css: DPR1→420→card(480) · DPR2→840→card2x(800, the ceiling)
|
|
56
63
|
* hero desktop 700css: DPR1→700→view · DPR2→1400→view(1600)
|
|
64
|
+
* section full-bleed `100vw`: phone 390css DPR2→780→card2x(800) · DPR3→1170→view ·
|
|
65
|
+
* 1440css desktop DPR1→1440→view(1600). Never the 2 MB original on a
|
|
66
|
+
* phone or an ordinary desktop.
|
|
67
|
+
* `section` is the ONE intent whose srcset may end in `original`, and
|
|
68
|
+
* its srcset is `view` + `original` ONLY (see resolveImageSources): a
|
|
69
|
+
* section photo must never look softer than the raw upload it replaces.
|
|
70
|
+
* A cropped (`object-fit: cover`) tall hero on a phone is drawn WIDER
|
|
71
|
+
* than 100vw (a 16:9 photo in a 523px-tall box is drawn ~930px wide), so
|
|
72
|
+
* `sizes="100vw"` would under-measure it and the small tiers would be
|
|
73
|
+
* too soft: a phone gets `view` (WebP, at most 1600px, still about five
|
|
74
|
+
* times lighter than a 2 MB PNG) and only a screen wider than `view`
|
|
75
|
+
* picks the original, which is exactly what that screen is served today.
|
|
57
76
|
*
|
|
58
77
|
* `modal` is written as a media query (`(max-width: 456px) 92vw, 420px`,
|
|
59
78
|
* arithmetically identical to `min(420px, 92vw)`: 420/0.92 = 456.5) rather
|
|
@@ -67,6 +86,7 @@ const INTENT_SIZES: Record<ImageIntent, string | undefined> = {
|
|
|
67
86
|
card2x: '(max-width: 768px) 100vw, (max-width: 1100px) 50vw, 620px',
|
|
68
87
|
modal: '(max-width: 456px) 92vw, 420px',
|
|
69
88
|
hero: '(max-width: 900px) calc(100vw - 32px), 700px',
|
|
89
|
+
section: '100vw',
|
|
70
90
|
raw: undefined,
|
|
71
91
|
};
|
|
72
92
|
|
|
@@ -85,6 +105,7 @@ const INTENT_SRC_ORDER: Record<ImageIntent, ImageVariantTierName[]> = {
|
|
|
85
105
|
// very fallback the caller carefully picked. Returning undefined instead
|
|
86
106
|
// lets the Image component's `resolved.src ?? props.src` defer to it.
|
|
87
107
|
hero: ['view', 'card2x'],
|
|
108
|
+
section: ['view', 'card2x', 'card'],
|
|
88
109
|
raw: ['original', 'view', 'card2x', 'card', 'thumb'],
|
|
89
110
|
};
|
|
90
111
|
|
|
@@ -99,6 +120,9 @@ function usableTier(tier: ImageVariantTier | null | undefined): tier is ImageVar
|
|
|
99
120
|
* - null/undefined `variants` → every field undefined, so the caller's own
|
|
100
121
|
* `src` prop keeps winning and behavior is byte-identical to today.
|
|
101
122
|
* - never emits `thumb` in a srcset.
|
|
123
|
+
* - `original` enters a srcset only for the `section` intent, and only as the
|
|
124
|
+
* top candidate, when its real width is strictly wider than `view`; the
|
|
125
|
+
* `section` srcset holds nothing but `view` and `original`.
|
|
102
126
|
* - never invents a url; null tiers are simply skipped.
|
|
103
127
|
* - never exceeds the intent's ceiling (downgrade-only).
|
|
104
128
|
* - dedupes candidates that collapsed to the same width on a small original,
|
|
@@ -130,7 +154,8 @@ export function resolveImageSources(
|
|
|
130
154
|
|
|
131
155
|
const limit = SRCSET_TIERS.indexOf(ceiling);
|
|
132
156
|
const byWidth = new Map<number, string>();
|
|
133
|
-
for
|
|
157
|
+
// `section` starts at `view`: card and card2x are too small for a box that can be drawn wider than the viewport.
|
|
158
|
+
for (let i = intent === 'section' ? limit : 0; i <= limit; i += 1) {
|
|
134
159
|
const tier = variants[SRCSET_TIERS[i]];
|
|
135
160
|
// A tier with no usable width can't carry a `w` descriptor, and mixing
|
|
136
161
|
// `w` and bare descriptors in one srcset makes the whole attribute invalid.
|
|
@@ -144,6 +169,18 @@ export function resolveImageSources(
|
|
|
144
169
|
}
|
|
145
170
|
}
|
|
146
171
|
|
|
172
|
+
if (intent === 'section') {
|
|
173
|
+
const original = variants.original;
|
|
174
|
+
if (
|
|
175
|
+
usableTier(original) &&
|
|
176
|
+
typeof original.w === 'number' &&
|
|
177
|
+
Number.isFinite(original.w) &&
|
|
178
|
+
original.w > Math.max(0, ...byWidth.keys())
|
|
179
|
+
) {
|
|
180
|
+
byWidth.set(original.w, original.url);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
147
184
|
if (byWidth.size < 2) {
|
|
148
185
|
return { src, srcSet: undefined, sizes: undefined };
|
|
149
186
|
}
|