@usequeek/theme-kit 0.1.40 → 0.1.42
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 +1 -1
- package/apps/apps.css +3 -1
- package/hooks/use-variant-selection.ts +89 -9
- package/package.json +1 -1
- package/utils/match-variant.ts +21 -0
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ they keep the data already on screen. They return `isLoading`, except
|
|
|
133
133
|
| `useCart()` / `useShopCart(shopId)` | `hooks/use-cart`, `hooks/use-shop-cart` | The cart and its actions; one shop's lines and totals |
|
|
134
134
|
| `useAddAppBlockLines(shopId)` | `hooks/use-add-app-block-lines` | Adds the products of an add-to-cart app block: `{ addLines, pending }` |
|
|
135
135
|
| `useAuth()` | `hooks/use-auth` | The signed-in customer and sign-in modal controls |
|
|
136
|
-
| `useVariantSelection(product)` | `hooks/use-variant-selection` | Option selection state and the add-to-cart gate |
|
|
136
|
+
| `useVariantSelection(product, preferredVariantPId?)` | `hooks/use-variant-selection` | Option selection state and the add-to-cart gate; opens on a linked `?variant=` |
|
|
137
137
|
| `useSearch(query, products, collections)` | `hooks/use-search` | Instant search over data you hold |
|
|
138
138
|
| `useAddressAutocomplete(text)` / `useResolveCoordinates()` | `hooks/use-address-autocomplete`, `hooks/use-resolve-coordinates` | Delivery address lookup |
|
|
139
139
|
| `useHref()` | `hooks/use-href` | Builds a store-relative link |
|
package/apps/apps.css
CHANGED
|
@@ -407,7 +407,9 @@
|
|
|
407
407
|
width: 100%;
|
|
408
408
|
max-width: 400px;
|
|
409
409
|
background: var(--brand-background, #fff);
|
|
410
|
-
|
|
410
|
+
/* Capped at the `rounded` scale's lg: `pill` sets --radius-lg to 9999px,
|
|
411
|
+
which turns a 400px card into a circle that clips its own content. */
|
|
412
|
+
border-radius: min(var(--radius-lg, 12px), 28px);
|
|
411
413
|
padding: 2.5rem 2rem 2rem;
|
|
412
414
|
box-shadow: var(--shadow-3, 0 20px 60px rgba(0, 0, 0, 0.25));
|
|
413
415
|
animation: core-subscribe-modal-pop 0.25s ease;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { useCallback, useMemo, useState } from 'react';
|
|
3
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import type { ReactNode } from 'react';
|
|
4
5
|
import type { Product, ProductVariant } from '../types/product';
|
|
5
|
-
import { matchVariant, productRequiresVariant } from '../utils/match-variant';
|
|
6
|
+
import { findPreferredVariant, matchVariant, productRequiresVariant } from '../utils/match-variant';
|
|
6
7
|
|
|
7
8
|
export interface VariantSelection {
|
|
8
9
|
/** Current option name → value map, seeded from a real variant. */
|
|
@@ -25,14 +26,59 @@ export interface VariantSelection {
|
|
|
25
26
|
activeHasDiscount: boolean;
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
interface LinkedVariantValue {
|
|
30
|
+
/** The product page's `p_id`: only a picker for THIS product is the page's own picker. */
|
|
31
|
+
productPId: number | undefined;
|
|
32
|
+
variantPId: number | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const PreferredVariantContext = createContext<LinkedVariantValue | null>(null);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Tells the product page's own `useVariantSelection` which variant the page was linked to (`?variant=`), without the
|
|
39
|
+
* theme passing it down. A server product page reads the param, wraps the theme's product page in this with the
|
|
40
|
+
* page's product `p_id`, and the first HTML render already shows that variant: no hydration mismatch, and a crawler
|
|
41
|
+
* sees the linked variant's price. A theme that already calls `useVariantSelection(product)` needs no change.
|
|
42
|
+
*
|
|
43
|
+
* Only a picker for the page's own product (`product.p_id === productPId`) reads it or rewrites the address bar: a
|
|
44
|
+
* quick-view modal or an inline buy box for another product, mounted under the same page, never does.
|
|
45
|
+
*/
|
|
46
|
+
export function PreferredVariantProvider({
|
|
47
|
+
productPId,
|
|
48
|
+
variantPId,
|
|
49
|
+
children,
|
|
50
|
+
}: {
|
|
51
|
+
productPId: number | undefined;
|
|
52
|
+
variantPId: number | undefined;
|
|
53
|
+
children: ReactNode;
|
|
54
|
+
}): ReactNode {
|
|
55
|
+
const value = useMemo(() => ({ productPId, variantPId }), [productPId, variantPId]);
|
|
56
|
+
return createElement(PreferredVariantContext.Provider, { value }, children);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* What the page link says about THIS product: the linked variant's `p_id` (undefined when the link names none) and
|
|
61
|
+
* whether this picker is the page's own, which is the only one allowed to move `?variant=`. Outside a
|
|
62
|
+
* `PreferredVariantProvider`, or for any other product, it is `{ variantPId: undefined, ownsAddressBar: false }`.
|
|
63
|
+
*/
|
|
64
|
+
export function useLinkedVariant(product: Product): { variantPId: number | undefined; ownsAddressBar: boolean } {
|
|
65
|
+
const linked = useContext(PreferredVariantContext);
|
|
66
|
+
const ownsAddressBar = linked !== null && product.p_id !== undefined && product.p_id === linked.productPId;
|
|
67
|
+
return { variantPId: ownsAddressBar ? linked.variantPId : undefined, ownsAddressBar };
|
|
68
|
+
}
|
|
69
|
+
|
|
28
70
|
/**
|
|
29
71
|
* The opening selection. Seeded from a variant that actually exists, so the
|
|
30
|
-
* initial state is always resolvable
|
|
31
|
-
*
|
|
72
|
+
* initial state is always resolvable.
|
|
73
|
+
*
|
|
74
|
+
* `preferredVariantPId` (a product link's `?variant=`) wins when `findPreferredVariant` finds it. An unknown or
|
|
75
|
+
* inactive one falls back to the default rule: the first in-stock variant, since opening on a sold-out variant reads
|
|
76
|
+
* as "unavailable" for a product that is not.
|
|
32
77
|
*/
|
|
33
|
-
export function seedVariantOptions(product: Product): Record<string, string> {
|
|
78
|
+
export function seedVariantOptions(product: Product, preferredVariantPId?: number): Record<string, string> {
|
|
34
79
|
const variants = product.variants ?? [];
|
|
35
|
-
const
|
|
80
|
+
const preferred = findPreferredVariant(product, preferredVariantPId);
|
|
81
|
+
const seed = preferred ?? variants.find((variant) => variant.inventory?.in_stock) ?? variants[0];
|
|
36
82
|
if (seed) {
|
|
37
83
|
return Object.fromEntries(seed.option_values.map((ov) => [ov.option_name, ov.value]));
|
|
38
84
|
}
|
|
@@ -46,6 +92,24 @@ export function seedVariantOptions(product: Product): Record<string, string> {
|
|
|
46
92
|
return initial;
|
|
47
93
|
}
|
|
48
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Keeps the address bar's `?variant=` on the variant the shopper chose, so a copied link opens on it.
|
|
97
|
+
* `replaceState`: no navigation, no scroll, no history entry. Silent where there is no window.
|
|
98
|
+
*/
|
|
99
|
+
export function syncVariantParam(variantPId: number | undefined): void {
|
|
100
|
+
if (variantPId === undefined || typeof window === 'undefined') return;
|
|
101
|
+
try {
|
|
102
|
+
const url = new URL(window.location.href);
|
|
103
|
+
if (url.searchParams.get('variant') === String(variantPId)) return;
|
|
104
|
+
url.searchParams.set('variant', String(variantPId));
|
|
105
|
+
// `null` state, the pattern Next documents: Next's own entry state (`__NA`) would make it skip its router sync,
|
|
106
|
+
// leaving `useSearchParams` stale and letting a later `router.refresh()` put the old `?variant=` back.
|
|
107
|
+
window.history.replaceState(null, '', url);
|
|
108
|
+
} catch {
|
|
109
|
+
// A link in the address bar is a convenience; never break the page over it.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
49
113
|
/** Everything derived from a selection. Pure, so the rules are testable directly. */
|
|
50
114
|
export function resolveVariantState(
|
|
51
115
|
product: Product,
|
|
@@ -71,7 +135,7 @@ export function resolveVariantState(
|
|
|
71
135
|
* hand-roll, and which gated the add button on stock and add-ons but never on a
|
|
72
136
|
* resolved variant.
|
|
73
137
|
*
|
|
74
|
-
* It does
|
|
138
|
+
* It does things a hand-rolled version tends to get wrong:
|
|
75
139
|
*
|
|
76
140
|
* 1. It seeds from a real variant (first in-stock, else first) rather than
|
|
77
141
|
* from the first value of each option independently. On a sparse matrix —
|
|
@@ -82,6 +146,10 @@ export function resolveVariantState(
|
|
|
82
146
|
* (mid-change, or that sparse seed) disables the add instead of sending a
|
|
83
147
|
* line the backend rejects.
|
|
84
148
|
*
|
|
149
|
+
* 3. It opens on the variant a product link names (`?variant=<p_id>`, via `preferredVariantPId` or a
|
|
150
|
+
* `PreferredVariantProvider` above it) and, once the shopper picks another on the page's own product, moves the
|
|
151
|
+
* address bar's `?variant=` to the one they chose.
|
|
152
|
+
*
|
|
85
153
|
* Themes keep their own option markup and call `selectOption`: the kit owns the
|
|
86
154
|
* state, the theme owns the design.
|
|
87
155
|
*
|
|
@@ -89,14 +157,26 @@ export function resolveVariantState(
|
|
|
89
157
|
* const { selectedOptions, selectOption, variantSatisfied, activePrice } = useVariantSelection(product);
|
|
90
158
|
* <button disabled={!variantSatisfied || !activeInStock}>Add to cart</button>
|
|
91
159
|
*/
|
|
92
|
-
export function useVariantSelection(product: Product): VariantSelection {
|
|
93
|
-
const
|
|
160
|
+
export function useVariantSelection(product: Product, preferredVariantPId?: number): VariantSelection {
|
|
161
|
+
const link = useLinkedVariant(product);
|
|
162
|
+
const [selectedOptions, setSelectedOptions] = useState<Record<string, string>>(() =>
|
|
163
|
+
seedVariantOptions(product, preferredVariantPId ?? link.variantPId),
|
|
164
|
+
);
|
|
165
|
+
const chosenByShopper = useRef(false);
|
|
94
166
|
|
|
95
167
|
const selectOption = useCallback((optionName: string, value: string): void => {
|
|
168
|
+
chosenByShopper.current = true;
|
|
96
169
|
setSelectedOptions((previous) => ({ ...previous, [optionName]: value }));
|
|
97
170
|
}, []);
|
|
98
171
|
|
|
99
172
|
const derived = useMemo(() => resolveVariantState(product, selectedOptions), [product, selectedOptions]);
|
|
100
173
|
|
|
174
|
+
// Only a choice the shopper made, on the page's own product, rewrites the link: opening a page never adds a
|
|
175
|
+
// param to its URL, and a modal or buy box for another product (or one outside the provider) never touches it.
|
|
176
|
+
const matchedPId = derived.matchedVariant?.p_id;
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (chosenByShopper.current && link.ownsAddressBar) syncVariantParam(matchedPId);
|
|
179
|
+
}, [matchedPId, link.ownsAddressBar]);
|
|
180
|
+
|
|
101
181
|
return { selectedOptions, selectOption, ...derived };
|
|
102
182
|
}
|
package/package.json
CHANGED
package/utils/match-variant.ts
CHANGED
|
@@ -29,3 +29,24 @@ export function matchVariant(
|
|
|
29
29
|
export function productRequiresVariant(product: Product): boolean {
|
|
30
30
|
return (product.variants?.length ?? 0) > 0 || (product.variants_count ?? 0) > 0;
|
|
31
31
|
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The variant `p_id` a product link names (`?variant=1234`): a positive integer, else undefined. Takes what a
|
|
35
|
+
* Next `searchParams` value is (`string | string[] | undefined`; the first of several wins), so a server page can
|
|
36
|
+
* pass the raw param straight in and never has to trust it.
|
|
37
|
+
*/
|
|
38
|
+
export function parsePreferredVariantPId(raw: string | string[] | undefined | null): number | undefined {
|
|
39
|
+
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
40
|
+
if (typeof value !== 'string' || !/^[1-9]\d{0,15}$/.test(value.trim())) return undefined;
|
|
41
|
+
const parsed = Number(value.trim());
|
|
42
|
+
return Number.isSafeInteger(parsed) ? parsed : undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The ACTIVE variant of `product` whose `p_id` is `preferredVariantPId`, even when sold out (the page must show the
|
|
47
|
+
* linked variant and its availability); null for no preference, an unknown `p_id` or an inactive variant.
|
|
48
|
+
*/
|
|
49
|
+
export function findPreferredVariant(product: Product, preferredVariantPId: number | undefined): ProductVariant | null {
|
|
50
|
+
if (preferredVariantPId === undefined) return null;
|
|
51
|
+
return (product.variants ?? []).find((variant) => variant.p_id === preferredVariantPId && variant.is_active !== false) ?? null;
|
|
52
|
+
}
|