@usequeek/theme-kit 0.5.14 → 0.5.16
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 +10 -1
- package/client.ts +1 -0
- package/components/product-media-frame.tsx +10 -0
- package/components/search-loading.tsx +17 -0
- package/hooks/use-product-sections.ts +20 -2
- package/hooks/use-products.ts +52 -36
- package/hooks/use-related-products.ts +42 -12
- package/package.json +1 -1
- package/shared-blocks/core-blocks.css +5 -4
- package/utils/image-srcset.ts +49 -13
- package/utils/product-media.ts +7 -1
package/README.md
CHANGED
|
@@ -123,7 +123,9 @@ they keep the data already on screen. They return `isLoading`, except
|
|
|
123
123
|
| `useProducts(options?)` | `hooks/use-products` | A product list: `{ products, isLoading, error }` |
|
|
124
124
|
| `useProductBySlug(slug)` | `hooks/use-products` | One product by slug: `{ product, isLoading, error }` |
|
|
125
125
|
| `useProductDetail(listProduct, isOpen, vendorSlug?, locale?)` | `hooks/use-product-detail` | A list product completed with variants and add-ons |
|
|
126
|
-
| `useRelatedProducts(slug, limit?)` | `hooks/use-related-products` | Related products |
|
|
126
|
+
| `useRelatedProducts(slug, limit?, { fallback }?)` | `hooks/use-related-products` | Related products; `fallback` (e.g. `{ sort: 'popular', limit: 9 }`) is read only when none come back |
|
|
127
|
+
| `useProductSections({ enabled }?)` | `hooks/use-product-sections` | The catalogue in sections (render `SearchResultsLoading` from `components/search-loading` while it loads); `enabled: false` (a closed search sheet) reads nothing until it turns true |
|
|
128
|
+
| `logoSrc(url, variants)` | `utils/image-srcset` | The smallest copy of a store logo tall enough for a 120 px logo box on a 3x screen (`LOGO_MAX_CSS_HEIGHT`); a theme that draws a taller logo must raise it |
|
|
127
129
|
| `useShop(options?)` + `useShopParams()` | `hooks/use-shop`, `hooks/use-shop-params` | The shop catalogue page and its URL state |
|
|
128
130
|
| `useCollectionParams()` | `hooks/use-collection-params` | Search, sort and page state for a collection page |
|
|
129
131
|
| `useCategories(options?)` | `hooks/use-categories` | Categories, flattened |
|
|
@@ -141,6 +143,13 @@ they keep the data already on screen. They return `isLoading`, except
|
|
|
141
143
|
| `useStorefront()` / `useStorefrontLocale()` / `useThemeStrings()` | `provider` | The store, the request locale, the bound `t` |
|
|
142
144
|
| `useProductCount()` | `provider` | `(count) => number \| null`: the product count to print, `null` unless the store turned counts on (`config.display.show_product_counts`, off by default). Print every product count through it and render nothing for `null` |
|
|
143
145
|
|
|
146
|
+
### Image loading
|
|
147
|
+
|
|
148
|
+
`ProductMediaFrame` (and `buildMediaFrames`, which marks `MediaFrame.lazy` on every frame after the first) renders a product gallery's
|
|
149
|
+
later frames, thumbnails and avatar-sized frames with `loading="lazy"`. Whatever a product page shows first (the first frame, a selected
|
|
150
|
+
variant's photo, a modal hero) stays eager, without `fetchPriority="high"` (measured: it made LCP later). `logoSrc` renders the smallest
|
|
151
|
+
logo copy that still holds every pixel a 120 px logo box shows on a 3x screen (`LOGO_MAX_CSS_HEIGHT`).
|
|
152
|
+
|
|
144
153
|
### Cart pricing
|
|
145
154
|
|
|
146
155
|
`useShopCart(shopId)` and `CartPanelController` also return `pricing`: what the
|
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 './components/search-loading';
|
|
17
18
|
export * from './media-index';
|
|
18
19
|
export * from './components/product-metafields';
|
|
19
20
|
export * from './components/powered-by-queek';
|
|
@@ -23,6 +23,14 @@ interface ProductMediaFrameProps {
|
|
|
23
23
|
* One frame of a product's media gallery: an image through `Image`, or a video.
|
|
24
24
|
* Pass a frame from `buildMediaFrames(product)`. `variant="thumbnail"` renders a
|
|
25
25
|
* muted video with no controls, so the browser shows its first frame.
|
|
26
|
+
*
|
|
27
|
+
* Loading: a frame is lazy only when nothing about it can be the first thing a
|
|
28
|
+
* product page shows: a later frame of `buildMediaFrames` (`frame.lazy`), a
|
|
29
|
+
* thumbnail, or an avatar-sized frame. Every other frame (the first one, a
|
|
30
|
+
* selected variant's photo, a modal's hero, any frame a theme builds itself) loads
|
|
31
|
+
* eagerly as before. No `fetchPriority="high"`: on three live stores under
|
|
32
|
+
* Lighthouse Slow 4G it made the photo race the page's CSS and LCP came 0.4-1.0 s
|
|
33
|
+
* later than plain eager (26/11/26).
|
|
26
34
|
*/
|
|
27
35
|
export function ProductMediaFrame({
|
|
28
36
|
frame,
|
|
@@ -48,6 +56,7 @@ export function ProductMediaFrame({
|
|
|
48
56
|
// Thumbnail strips render at ~64-90px across every theme — the `thumb` tier
|
|
49
57
|
// (a fixed-src tier that is never part of a srcset) is exactly right there.
|
|
50
58
|
const resolvedIntent: ImageIntent = intent ?? (variant === 'thumbnail' ? 'avatar' : 'hero');
|
|
59
|
+
const lazy = frame.lazy === true || variant === 'thumbnail' || resolvedIntent === 'avatar';
|
|
51
60
|
|
|
52
61
|
return (
|
|
53
62
|
<Image
|
|
@@ -57,6 +66,7 @@ export function ProductMediaFrame({
|
|
|
57
66
|
placeholder={placeholder}
|
|
58
67
|
variants={frame.variants}
|
|
59
68
|
intent={resolvedIntent}
|
|
69
|
+
loading={lazy ? 'lazy' : undefined}
|
|
60
70
|
/>
|
|
61
71
|
);
|
|
62
72
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JSX } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The rows a search sheet shows in its results area while the catalogue is still being read: the first time a
|
|
5
|
+
* sheet opens, its list is not loaded yet (see `useProductSections({ enabled })`). Open the sheet at once and
|
|
6
|
+
* render this where the results go, never an empty list and never a "no matches" line. Uses the shell's
|
|
7
|
+
* `core-skeleton` shimmer when present and a plain tint otherwise; no text, so it needs no string.
|
|
8
|
+
*/
|
|
9
|
+
export function SearchResultsLoading({ rows = 4 }: { rows?: number }): JSX.Element {
|
|
10
|
+
return (
|
|
11
|
+
<div role="status" aria-busy="true" data-search-loading="" style={{ display: 'grid', gap: 12, width: '100%', gridColumn: '1 / -1' }}>
|
|
12
|
+
{Array.from({ length: rows }, (_, index) => (
|
|
13
|
+
<span key={index} className="core-skeleton" style={{ height: 56, backgroundColor: 'rgba(128, 128, 128, 0.12)' }} />
|
|
14
|
+
))}
|
|
15
|
+
</div>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -53,11 +53,27 @@ const EMPTY_RESULT: SectionsResult = { sections: [], groups: [] };
|
|
|
53
53
|
const sectionsCache = new Map<string, SectionsResult>();
|
|
54
54
|
const inflightSections = new Map<string, Promise<SectionsResult>>();
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
/**
|
|
57
|
+
* The store's catalogue grouped into sections (and, on the QR menu, its tabs),
|
|
58
|
+
* read once per store, platform and locale and then kept for the page.
|
|
59
|
+
*
|
|
60
|
+
* Pass `{ enabled: false }` from a component that is mounted but not showing
|
|
61
|
+
* yet (a search sheet that is closed): nothing is requested until it turns
|
|
62
|
+
* true, so a visitor who never opens it never pays for the read. Without the
|
|
63
|
+
* option it reads straight away, as it always did. Disabled, `sections` stay
|
|
64
|
+
* empty (or the cached result, when one exists) and `isLoading` is true until
|
|
65
|
+
* the first read lands, so a sheet that opens on it shows its loading state.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* const isOpen = useSearchModalStore((s) => s.isOpen);
|
|
69
|
+
* const { sections, isLoading } = useProductSections({ enabled: isOpen });
|
|
70
|
+
*/
|
|
71
|
+
export function useProductSections(options?: { enabled?: boolean }): {
|
|
57
72
|
sections: ProductSection[];
|
|
58
73
|
groups: MenuGroup[];
|
|
59
74
|
isLoading: boolean;
|
|
60
75
|
} {
|
|
76
|
+
const enabled = options?.enabled ?? true;
|
|
61
77
|
const { previewData, vendor } = useStorefront();
|
|
62
78
|
const locale = useStorefrontLocale();
|
|
63
79
|
// Cache per vendor AND platform — the grouped catalogue can differ between
|
|
@@ -72,6 +88,8 @@ export function useProductSections(): {
|
|
|
72
88
|
const [isLoading, setIsLoading] = useState(!sectionsCache.has(cacheKey));
|
|
73
89
|
|
|
74
90
|
useEffect(() => {
|
|
91
|
+
if (!enabled) return undefined;
|
|
92
|
+
|
|
75
93
|
let cancelled = false;
|
|
76
94
|
const previewProducts = previewData?.products;
|
|
77
95
|
|
|
@@ -164,7 +182,7 @@ export function useProductSections(): {
|
|
|
164
182
|
});
|
|
165
183
|
|
|
166
184
|
return () => { cancelled = true; };
|
|
167
|
-
}, [previewData?.products, vendor.slug, cacheKey, locale]);
|
|
185
|
+
}, [enabled, previewData?.products, vendor.slug, cacheKey, locale]);
|
|
168
186
|
|
|
169
187
|
return { sections: result.sections, groups: result.groups, isLoading };
|
|
170
188
|
}
|
package/hooks/use-products.ts
CHANGED
|
@@ -44,6 +44,57 @@ function resolvePreviewProducts(products: Product[], options: ProductQuery): Pro
|
|
|
44
44
|
return typeof options.limit === 'number' ? next.slice(0, options.limit) : next;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The one browser read behind a product list: a collection's products, or the
|
|
49
|
+
* store's products filtered by `sort`, `limit`, `ids` and `hasVideo`. Shared by
|
|
50
|
+
* {@link useProducts} and the fallback of `useRelatedProducts`, so the two can
|
|
51
|
+
* never ask the API differently.
|
|
52
|
+
*/
|
|
53
|
+
export async function fetchProductList(
|
|
54
|
+
client: ReturnType<typeof createBrowserClient>,
|
|
55
|
+
options: ProductQuery,
|
|
56
|
+
locale?: string | null,
|
|
57
|
+
): Promise<Product[]> {
|
|
58
|
+
let resolvedProducts: Product[];
|
|
59
|
+
|
|
60
|
+
if (options.collection) {
|
|
61
|
+
const response = await client.get<ApiResponse<unknown>>(
|
|
62
|
+
`/collections/${encodeURIComponent(options.collection)}/products`,
|
|
63
|
+
withLocaleQuery(undefined, locale),
|
|
64
|
+
);
|
|
65
|
+
resolvedProducts = extractProducts(response.data).map((p) => normalizeProduct(p));
|
|
66
|
+
} else {
|
|
67
|
+
const query: Record<string, string | number | string[]> = {};
|
|
68
|
+
if (options.sort) query.sort = options.sort;
|
|
69
|
+
if (options.limit) query.per_page = options.limit;
|
|
70
|
+
if (options.hasVideo) query.has_video = 1;
|
|
71
|
+
// Ask the API for exactly these products (by id) instead of
|
|
72
|
+
// fetching an arbitrary `per_page`-sized page of the default listing
|
|
73
|
+
// and hoping the requested ids happen to land in it — with a small
|
|
74
|
+
// limit (bundle/shop-the-look/featured's 1-3) they almost never did,
|
|
75
|
+
// silently dropping hand-picked products from the storefront.
|
|
76
|
+
if (options.ids && options.ids.length > 0) query.ids = options.ids;
|
|
77
|
+
|
|
78
|
+
const response = await client.get<ApiResponse<unknown, ProductListMeta>>('/products', withLocaleQuery(query, locale));
|
|
79
|
+
resolvedProducts = extractProducts(response.data).map((p) => normalizeProduct(p));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let next = resolvedProducts;
|
|
83
|
+
|
|
84
|
+
if (options.collection && options.ids && options.ids.length > 0) {
|
|
85
|
+
// The collection endpoint has no ids filter of its own — this branch
|
|
86
|
+
// fetches the full collection unbounded, so a client-side filter here
|
|
87
|
+
// is safe (nothing was truncated before it ran).
|
|
88
|
+
next = next.filter((product) => options.ids?.includes(product.id));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof options.limit === 'number') {
|
|
92
|
+
next = next.slice(0, options.limit);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return next;
|
|
96
|
+
}
|
|
97
|
+
|
|
47
98
|
/**
|
|
48
99
|
* A list of the store's products, fetched in the browser (or filtered from the
|
|
49
100
|
* seeded catalogue in preview).
|
|
@@ -106,42 +157,7 @@ export function useProducts(options?: ProductQuery & {
|
|
|
106
157
|
setIsLoading(true);
|
|
107
158
|
setError(null);
|
|
108
159
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (options?.collection) {
|
|
112
|
-
const response = await client.get<ApiResponse<unknown>>(
|
|
113
|
-
`/collections/${encodeURIComponent(options.collection)}/products`,
|
|
114
|
-
withLocaleQuery(undefined, locale),
|
|
115
|
-
);
|
|
116
|
-
resolvedProducts = extractProducts(response.data).map((p) => normalizeProduct(p));
|
|
117
|
-
} else {
|
|
118
|
-
const query: Record<string, string | number | string[]> = {};
|
|
119
|
-
if (options?.sort) query.sort = options.sort;
|
|
120
|
-
if (options?.limit) query.per_page = options.limit;
|
|
121
|
-
if (options?.hasVideo) query.has_video = 1;
|
|
122
|
-
// Ask the API for exactly these products (by id) instead of
|
|
123
|
-
// fetching an arbitrary `per_page`-sized page of the default listing
|
|
124
|
-
// and hoping the requested ids happen to land in it — with a small
|
|
125
|
-
// limit (bundle/shop-the-look/featured's 1-3) they almost never did,
|
|
126
|
-
// silently dropping hand-picked products from the storefront.
|
|
127
|
-
if (options?.ids && options.ids.length > 0) query.ids = options.ids;
|
|
128
|
-
|
|
129
|
-
const response = await client.get<ApiResponse<unknown, ProductListMeta>>('/products', withLocaleQuery(query, locale));
|
|
130
|
-
resolvedProducts = extractProducts(response.data).map((p) => normalizeProduct(p));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
let next = resolvedProducts;
|
|
134
|
-
|
|
135
|
-
if (options?.collection && options?.ids && options.ids.length > 0) {
|
|
136
|
-
// The collection endpoint has no ids filter of its own — this branch
|
|
137
|
-
// fetches the full collection unbounded, so a client-side filter here
|
|
138
|
-
// is safe (nothing was truncated before it ran).
|
|
139
|
-
next = next.filter((product) => options.ids?.includes(product.id));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (typeof options?.limit === 'number') {
|
|
143
|
-
next = next.slice(0, options.limit);
|
|
144
|
-
}
|
|
160
|
+
const next = await fetchProductList(client, options!, locale);
|
|
145
161
|
|
|
146
162
|
if (!cancelled) {
|
|
147
163
|
setProducts(next);
|
|
@@ -8,24 +8,38 @@ import { createBrowserClient } from '../sdk/client';
|
|
|
8
8
|
import { withLocaleQuery } from '../utils/locale';
|
|
9
9
|
import { extractProducts, normalizeProduct } from '../utils/product-normalizer';
|
|
10
10
|
import { toError } from '../utils/to-error';
|
|
11
|
+
import { fetchProductList, type ProductQuery } from './use-products';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Products related to the one being viewed (up to `limit`, default 4). The
|
|
14
15
|
* list stays empty when the request fails; `error` tells you why, and a theme
|
|
15
16
|
* can simply hide the section.
|
|
16
17
|
*
|
|
18
|
+
* Pass `fallback` (a {@link ProductQuery}, e.g. `{ sort: 'popular', limit: 9 }`)
|
|
19
|
+
* when the section should never be empty: it is read ONLY when the related list
|
|
20
|
+
* comes back empty (or fails), so a product that has related items costs one
|
|
21
|
+
* request, not two. The fallback list leaves out the viewed product and is cut
|
|
22
|
+
* to `limit`. `isLoading` stays true until the list you will show has arrived,
|
|
23
|
+
* and `usedFallback` says which list that is.
|
|
24
|
+
*
|
|
17
25
|
* @example
|
|
18
26
|
* const { products } = useRelatedProducts(product.slug, 4);
|
|
27
|
+
* const { products: rail } = useRelatedProducts(product.slug, 8, { fallback: { sort: 'popular', limit: 9 } });
|
|
19
28
|
*/
|
|
20
29
|
export function useRelatedProducts(
|
|
21
30
|
currentSlug: string,
|
|
22
31
|
limit = 4,
|
|
23
|
-
|
|
32
|
+
options?: { fallback?: ProductQuery },
|
|
33
|
+
): { products: Product[]; isLoading: boolean; error: Error | null; usedFallback: boolean } {
|
|
24
34
|
const { vendor, previewData } = useStorefront();
|
|
25
35
|
const locale = useStorefrontLocale();
|
|
26
36
|
const [products, setProducts] = useState<Product[]>([]);
|
|
27
37
|
const [isLoading, setIsLoading] = useState(true);
|
|
28
38
|
const [error, setError] = useState<Error | null>(null);
|
|
39
|
+
const [usedFallback, setUsedFallback] = useState(false);
|
|
40
|
+
// The fallback query as a plain string: a new object literal every render
|
|
41
|
+
// must not re-run the reads.
|
|
42
|
+
const fallbackKey = options?.fallback ? JSON.stringify(options.fallback) : '';
|
|
29
43
|
|
|
30
44
|
useEffect(() => {
|
|
31
45
|
let cancelled = false;
|
|
@@ -43,17 +57,33 @@ export function useRelatedProducts(
|
|
|
43
57
|
}
|
|
44
58
|
|
|
45
59
|
const client = createBrowserClient(vendor.slug ?? undefined);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
60
|
+
let related: Product[] = [];
|
|
61
|
+
let fromFallback = false;
|
|
62
|
+
try {
|
|
63
|
+
const response = await client.get<ApiResponse<unknown>>(
|
|
64
|
+
`/products/${encodeURIComponent(currentSlug)}/related`,
|
|
65
|
+
withLocaleQuery({ limit }, locale),
|
|
66
|
+
);
|
|
67
|
+
related = extractProducts(response.data)
|
|
68
|
+
.map((p) => normalizeProduct(p as Parameters<typeof normalizeProduct>[0]))
|
|
69
|
+
.slice(0, limit);
|
|
70
|
+
} catch (caught) {
|
|
71
|
+
// Non-critical: with a fallback the section is filled from it; either
|
|
72
|
+
// way the failure stays on `error`.
|
|
73
|
+
if (!fallbackKey) throw caught;
|
|
74
|
+
if (!cancelled) setError(toError(caught));
|
|
75
|
+
}
|
|
50
76
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.
|
|
54
|
-
|
|
77
|
+
if (related.length === 0 && fallbackKey) {
|
|
78
|
+
const popular = await fetchProductList(client, JSON.parse(fallbackKey) as ProductQuery, locale);
|
|
79
|
+
related = popular.filter((p) => p.slug !== currentSlug).slice(0, limit);
|
|
80
|
+
fromFallback = true;
|
|
81
|
+
}
|
|
55
82
|
|
|
56
|
-
if (!cancelled)
|
|
83
|
+
if (!cancelled) {
|
|
84
|
+
setProducts(related);
|
|
85
|
+
setUsedFallback(fromFallback);
|
|
86
|
+
}
|
|
57
87
|
} catch (caught) {
|
|
58
88
|
// Non-critical: the section can hide itself. The failure is on `error`.
|
|
59
89
|
if (!cancelled) setError(toError(caught));
|
|
@@ -64,7 +94,7 @@ export function useRelatedProducts(
|
|
|
64
94
|
|
|
65
95
|
void run();
|
|
66
96
|
return () => { cancelled = true; };
|
|
67
|
-
}, [currentSlug, limit, vendor.slug, previewData?.products, locale]);
|
|
97
|
+
}, [currentSlug, limit, fallbackKey, vendor.slug, previewData?.products, locale]);
|
|
68
98
|
|
|
69
|
-
return { products, isLoading, error };
|
|
99
|
+
return { products, isLoading, error, usedFallback };
|
|
70
100
|
}
|
package/package.json
CHANGED
|
@@ -1222,14 +1222,15 @@
|
|
|
1222
1222
|
line-height: 1.6;
|
|
1223
1223
|
}
|
|
1224
1224
|
|
|
1225
|
-
/* A formatted answer (markdown): paragraphs and lists sit flush with the answer box, one clear gap between them.
|
|
1226
|
-
*
|
|
1227
|
-
.
|
|
1225
|
+
/* A formatted answer (markdown): paragraphs and lists sit flush with the answer box, one clear gap between them. Scoped under
|
|
1226
|
+
* `.core-block-faq` (specificity 0,2,2) so the gap outranks a theme's prose resets — `.theme-x p { margin: 0 }` (0,1,1) and
|
|
1227
|
+
* `html :where(.theme-x) .content-stack :is(p, ul, ol) { margin: 0 }` (0,1,2), which loads later and won a 0,1,2 tie. */
|
|
1228
|
+
.core-block-faq .core-block-faq__answer > :is(p, ul, ol) {
|
|
1228
1229
|
margin-top: 0;
|
|
1229
1230
|
margin-bottom: 0;
|
|
1230
1231
|
}
|
|
1231
1232
|
|
|
1232
|
-
.core-block-faq__answer > :is(p, ul, ol) + :is(p, ul, ol) {
|
|
1233
|
+
.core-block-faq .core-block-faq__answer > :is(p, ul, ol) + :is(p, ul, ol) {
|
|
1233
1234
|
margin-top: 1em;
|
|
1234
1235
|
}
|
|
1235
1236
|
|
package/utils/image-srcset.ts
CHANGED
|
@@ -193,27 +193,63 @@ export function resolveImageSources(
|
|
|
193
193
|
return { src, srcSet, sizes: INTENT_SIZES[intent] };
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
+
/**
|
|
197
|
+
* The tallest box any theme draws a store logo in, in CSS px: Deluxr's merchant
|
|
198
|
+
* logo height (`header.logo_height`, 16..120). Every other header and footer logo
|
|
199
|
+
* box is 26..72 px tall, and every one is bounded by its height (a wide wordmark
|
|
200
|
+
* grows sideways, never taller). A theme that draws a logo taller than this must
|
|
201
|
+
* raise it, or `logoSrc` hands it a copy softer than the upload.
|
|
202
|
+
*/
|
|
203
|
+
export const LOGO_MAX_CSS_HEIGHT = 120;
|
|
204
|
+
|
|
205
|
+
/** The densest screen a logo copy is sized for (a 3x phone). */
|
|
206
|
+
const LOGO_MAX_DPR = 3;
|
|
207
|
+
|
|
208
|
+
/** Smallest first: the first copy tall enough wins. */
|
|
209
|
+
const LOGO_TIERS = ['card', 'card2x', 'view'] as const;
|
|
210
|
+
|
|
196
211
|
/**
|
|
197
212
|
* The one url a store logo renders from. A logo reaches a theme as a plain string
|
|
198
|
-
* (`HeaderProps.logo`), so it has no srcset
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
213
|
+
* (`HeaderProps.logo`), so it has no srcset: the url is the smallest copy that still
|
|
214
|
+
* holds every pixel the logo can show, i.e. at least as tall as the upload or as
|
|
215
|
+
* {@link LOGO_MAX_CSS_HEIGHT} x 3 (360 px), whichever is less. A copy keeps the
|
|
216
|
+
* upload's shape, so one tall enough is wide enough for a wordmark of any width: a
|
|
217
|
+
* square 1024 px JPG of 118 KB renders its 480 px `card` (23 KB), a 4:1 wordmark its
|
|
218
|
+
* `view`. Never softer than the upload. When the API did not measure the upload, the
|
|
219
|
+
* `view` copy (at most 1600 px), as before. The raw `url` when no copy is tall enough,
|
|
220
|
+
* there are no copies at all (an SVG, an off-host file) or copies of a different
|
|
221
|
+
* file, and when the upload is already a WebP no wider than the copy: the copy is
|
|
222
|
+
* then the same picture re-encoded, and often heavier (an 8 KB wordmark's `view` is
|
|
223
|
+
* 9.6 KB).
|
|
207
224
|
*/
|
|
208
225
|
export function logoSrc(url: string | null | undefined, variants: ImageVariants | null | undefined): string | null {
|
|
209
226
|
if (!url) {
|
|
210
227
|
return null;
|
|
211
228
|
}
|
|
212
229
|
const original = variants?.original;
|
|
213
|
-
|
|
214
|
-
|
|
230
|
+
if (!variants || !original || original.url !== url) {
|
|
231
|
+
return url;
|
|
232
|
+
}
|
|
233
|
+
const copy = smallestLogoCopy(variants, original);
|
|
234
|
+
if (!copy) {
|
|
215
235
|
return url;
|
|
216
236
|
}
|
|
217
|
-
const samePixels = typeof
|
|
218
|
-
return samePixels && /\.webp(?:[?#]|$)/i.test(url) ? url :
|
|
237
|
+
const samePixels = typeof copy.w === 'number' && typeof original.w === 'number' && copy.w >= original.w;
|
|
238
|
+
return samePixels && /\.webp(?:[?#]|$)/i.test(url) ? url : copy.url;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function smallestLogoCopy(variants: ImageVariants, original: ImageVariantTier): ImageVariantTier | null {
|
|
242
|
+
const measured = typeof original.h === 'number' && Number.isFinite(original.h) && original.h > 0;
|
|
243
|
+
if (!measured) {
|
|
244
|
+
return usableTier(variants.view) ? variants.view : null;
|
|
245
|
+
}
|
|
246
|
+
const needed = Math.min(original.h as number, LOGO_MAX_CSS_HEIGHT * LOGO_MAX_DPR);
|
|
247
|
+
for (const name of LOGO_TIERS) {
|
|
248
|
+
const tier = variants[name];
|
|
249
|
+
if (usableTier(tier) && typeof tier.h === 'number' && tier.h >= needed) {
|
|
250
|
+
return tier;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return null;
|
|
219
255
|
}
|
package/utils/product-media.ts
CHANGED
|
@@ -10,6 +10,12 @@ export interface MediaFrame {
|
|
|
10
10
|
* ProductMediaFrame → <Image variants>. Absent for video frames and for
|
|
11
11
|
* non-Media-backed images. */
|
|
12
12
|
variants?: ImageVariants | null;
|
|
13
|
+
/** True on every frame `buildMediaFrames` returns after the first: a gallery's
|
|
14
|
+
* later photos start off-screen (below the first, or a slide not yet shown), so
|
|
15
|
+
* ProductMediaFrame loads them lazily. Never set on the first frame, nor on a
|
|
16
|
+
* frame a theme builds itself (a selected variant's photo, a modal's hero):
|
|
17
|
+
* whatever a page shows first is never lazy. */
|
|
18
|
+
lazy?: boolean;
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
/**
|
|
@@ -123,5 +129,5 @@ export function buildMediaFrames(product: Product): MediaFrame[] {
|
|
|
123
129
|
frames.push({ id: 'video', url: product.media.video_url, alt: product.title, type: 'video' });
|
|
124
130
|
}
|
|
125
131
|
|
|
126
|
-
return frames;
|
|
132
|
+
return frames.map((frame, index) => (index === 0 ? frame : { ...frame, lazy: true }));
|
|
127
133
|
}
|