@usequeek/theme-kit 0.5.2 → 0.5.3
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/block-position.tsx +31 -0
- package/components/image.tsx +12 -2
- package/package.json +1 -1
- package/page-renderer.tsx +8 -1
- package/types/vendor.ts +2 -0
- package/utils/image-srcset.ts +18 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { JSX } from 'react';
|
|
4
|
+
import { createContext, useContext, type ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
const BlockPositionContext = createContext<number | null>(null);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How many blocks open a page. A page's hero is its first block, or its second
|
|
10
|
+
* after a short heading or banner, so only these may hold the image the page is
|
|
11
|
+
* judged by (its LCP image).
|
|
12
|
+
*/
|
|
13
|
+
export const TOP_BLOCK_COUNT = 2;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Tells every `<Image>` below it where its block sits on the page. `PageRenderer`
|
|
17
|
+
* mounts one per block; a theme never needs to.
|
|
18
|
+
*/
|
|
19
|
+
export function BlockPositionProvider({ index, children }: { index: number; children: ReactNode }): JSX.Element {
|
|
20
|
+
return <BlockPositionContext.Provider value={index}>{children}</BlockPositionContext.Provider>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The index of the page block being rendered; null outside a `PageRenderer` block. */
|
|
24
|
+
export function useBlockPosition(): number | null {
|
|
25
|
+
return useContext(BlockPositionContext);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Whether a block index is past the opening blocks of the page (never true outside a block). */
|
|
29
|
+
export function isBelowTopBlocks(position: number | null): boolean {
|
|
30
|
+
return position !== null && position >= TOP_BLOCK_COUNT;
|
|
31
|
+
}
|
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 { isBelowTopBlocks, useBlockPosition } from '../block-position';
|
|
4
5
|
import { lookupMediaIndex, useMediaIndex } from '../media-index';
|
|
5
6
|
import { StorefrontContext } from '../provider';
|
|
6
7
|
import type { ImageVariants } from '../types/media';
|
|
@@ -157,6 +158,14 @@ export function Image({ fallback, placeholder, variants: variantsProp, intent: i
|
|
|
157
158
|
effectiveSizes = undefined;
|
|
158
159
|
}
|
|
159
160
|
|
|
161
|
+
// A block past the page's opening blocks is below the fold, so its images are
|
|
162
|
+
// lazy and never high priority, whatever the call site asked. A slider marks its
|
|
163
|
+
// first slide eager and high priority because it may be the hero; reused lower
|
|
164
|
+
// on the page, that made the browser (and React's SSR preloads) fetch those
|
|
165
|
+
// photos with the hero: 1.9 MB of a 3.5 MB phone page load on a Beaurify store.
|
|
166
|
+
const belowTop = isBelowTopBlocks(useBlockPosition());
|
|
167
|
+
const fetchPriority = belowTop && props.fetchPriority === 'high' ? undefined : props.fetchPriority;
|
|
168
|
+
|
|
160
169
|
// Cards sit in grids and rails, mostly below the fold — and a card's hover
|
|
161
170
|
// image is invisible until hover. Eager, a server-rendered grid of 24 cards
|
|
162
171
|
// made React preload the first ~10 (also into the 103 Early Hints header) and
|
|
@@ -164,12 +173,13 @@ export function Image({ fallback, placeholder, variants: variantsProp, intent: i
|
|
|
164
173
|
// first paint went from 1.8 s to 2.9 s on Slow 4G. Lazy is next/image's
|
|
165
174
|
// default as well. A theme whose card IS the page's main image passes
|
|
166
175
|
// loading="eager" or fetchPriority="high".
|
|
167
|
-
const lazyByDefault = (intent === 'card' || intent === 'card2x') &&
|
|
168
|
-
const loading = props.loading ?? (lazyByDefault ? 'lazy' : undefined);
|
|
176
|
+
const lazyByDefault = (intent === 'card' || intent === 'card2x') && fetchPriority !== 'high';
|
|
177
|
+
const loading = belowTop ? 'lazy' : (props.loading ?? (lazyByDefault ? 'lazy' : undefined));
|
|
169
178
|
|
|
170
179
|
return (
|
|
171
180
|
<img
|
|
172
181
|
{...props}
|
|
182
|
+
fetchPriority={fetchPriority}
|
|
173
183
|
loading={loading}
|
|
174
184
|
src={effectiveSrc}
|
|
175
185
|
srcSet={effectiveSrcSet}
|
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 { BlockPositionProvider, useBlockPosition } from './block-position';
|
|
9
10
|
import { mediaIndexBackgroundUrl, useMediaIndex } from './media-index';
|
|
10
11
|
import { expandDesignTokensPartial } from './utils/brand';
|
|
11
12
|
import {
|
|
@@ -98,12 +99,18 @@ export function PageRenderer({ blocks }: { blocks: Block[] }): JSX.Element {
|
|
|
98
99
|
const defaultVariants = config.default_variants ?? {};
|
|
99
100
|
const editMode = useEditMode();
|
|
100
101
|
const mediaIndex = useMediaIndex();
|
|
102
|
+
// Where each block sits for its images' loading (block-position.tsx): only blocks that render count, and a
|
|
103
|
+
// renderer nested in a block starts at that block's position, so a hero after an empty block keeps its priority.
|
|
104
|
+
const positionOffset = useBlockPosition() ?? 0;
|
|
105
|
+
let renderedCount = 0;
|
|
101
106
|
|
|
102
107
|
const renderBlock = (block: Block, index: number): JSX.Element | null => {
|
|
103
108
|
const variant = block.variant ?? defaultVariants[block.type] ?? undefined;
|
|
104
109
|
const key = `${block.type}-${index}`;
|
|
105
110
|
const node = renderInner(block, variant, key);
|
|
106
111
|
if (node === null) return null;
|
|
112
|
+
const position = positionOffset + renderedCount;
|
|
113
|
+
renderedCount += 1;
|
|
107
114
|
|
|
108
115
|
// Section-scoped design-token override: only wraps in an extra div when
|
|
109
116
|
// the section actually sets `tokens` — no-override sections render
|
|
@@ -214,7 +221,7 @@ export function PageRenderer({ blocks }: { blocks: Block[] }): JSX.Element {
|
|
|
214
221
|
|
|
215
222
|
const wrapped = (
|
|
216
223
|
<EditableBlock key={key} index={index} type={block.type} variant={variant} enabled={editMode}>
|
|
217
|
-
{content}
|
|
224
|
+
<BlockPositionProvider index={position}>{content}</BlockPositionProvider>
|
|
218
225
|
</EditableBlock>
|
|
219
226
|
);
|
|
220
227
|
|
package/types/vendor.ts
CHANGED
|
@@ -160,6 +160,8 @@ export interface StorefrontConfig {
|
|
|
160
160
|
header?: {
|
|
161
161
|
variant?: string;
|
|
162
162
|
logo_url?: string | null;
|
|
163
|
+
/** The tier map of `logo_url`; null for an SVG or off-host logo. Render the logo through `logoSrc()`. */
|
|
164
|
+
logo_variants?: ImageVariants | null;
|
|
163
165
|
announcement?: {
|
|
164
166
|
enabled: boolean;
|
|
165
167
|
text: string;
|
package/utils/image-srcset.ts
CHANGED
|
@@ -192,3 +192,21 @@ export function resolveImageSources(
|
|
|
192
192
|
|
|
193
193
|
return { src, srcSet, sizes: INTENT_SIZES[intent] };
|
|
194
194
|
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 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 and the call site's box is unknown:
|
|
199
|
+
* the `view` copy (WebP q98, at most 1600 px) holds every pixel a logo box up to
|
|
200
|
+
* 800 CSS px on a 2x screen (533 px on 3x) can show, at a fraction of the upload
|
|
201
|
+
* (a 2048 px JPG of 139 KB is a 38 KB WebP). Never a smaller tier: a wide wordmark
|
|
202
|
+
* on a 3x phone would come out softer than the upload it replaces. The raw `url`
|
|
203
|
+
* when there is no `view` copy, no copies at all (an SVG, an off-host file) or
|
|
204
|
+
* copies of a different file.
|
|
205
|
+
*/
|
|
206
|
+
export function logoSrc(url: string | null | undefined, variants: ImageVariants | null | undefined): string | null {
|
|
207
|
+
if (!url) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
const view = variants && variants.original?.url === url ? variants.view : null;
|
|
211
|
+
return usableTier(view) ? view.url : url;
|
|
212
|
+
}
|