create-skybridge 0.0.0-dev.94c22d0 → 0.0.0-dev.9520814
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/dist/index.js +107 -17
- package/package.json +2 -2
- package/templates/blank/README.md +2 -1
- package/templates/blank/node_modules/.bin/alpic +2 -2
- package/templates/blank/node_modules/.bin/tsx +2 -2
- package/templates/blank/node_modules/.bin/vite +2 -2
- package/templates/blank/package.json +8 -7
- package/templates/blank/src/index.ts +3 -0
- package/templates/blank/src/server.ts +7 -11
- package/templates/blank/vite.config.ts +7 -1
- package/templates/demo/README.md +3 -1
- package/templates/demo/evals/start.eval.ts +11 -0
- package/templates/demo/node_modules/.bin/alpic +2 -2
- package/templates/demo/node_modules/.bin/tsx +2 -2
- package/templates/demo/node_modules/.bin/vite +2 -2
- package/templates/demo/node_modules/.bin/vitest +21 -0
- package/templates/demo/package.json +19 -13
- package/templates/demo/src/index.ts +3 -0
- package/templates/demo/src/server.ts +86 -88
- package/templates/demo/src/views/onboarding.tsx +3 -3
- package/templates/demo/tsconfig.evals.json +11 -0
- package/templates/demo/vite.config.ts +9 -3
- package/templates/ecom/.env.template +5 -0
- package/templates/ecom/README.md +2 -1
- package/templates/ecom/node_modules/.bin/alpic +2 -2
- package/templates/ecom/node_modules/.bin/ladle +2 -2
- package/templates/ecom/node_modules/.bin/vite +2 -2
- package/templates/ecom/package.json +3 -2
- package/templates/ecom/src/catalog/index.ts +19 -0
- package/templates/ecom/src/catalog/mock.ts +134 -0
- package/templates/ecom/src/catalog/shopify.ts +264 -0
- package/templates/ecom/src/components/chip.css.ts +15 -8
- package/templates/ecom/src/components/chip.stories.tsx +2 -1
- package/templates/ecom/src/components/chip.tsx +16 -8
- package/templates/ecom/src/components/product-card.tsx +2 -2
- package/templates/ecom/src/components/variant-picker.stories.tsx +7 -4
- package/templates/ecom/src/components/variant-picker.tsx +17 -29
- package/templates/ecom/src/components/view-frame.tsx +4 -4
- package/templates/ecom/src/i18n.ts +1 -1
- package/templates/ecom/src/index.ts +3 -0
- package/templates/ecom/src/lib/variants.ts +100 -29
- package/templates/ecom/src/server.ts +17 -21
- package/templates/ecom/src/tools/render-carousel.ts +5 -85
- package/templates/ecom/src/tools/search-products.ts +32 -21
- package/templates/ecom/src/types.ts +72 -0
- package/templates/ecom/src/views/carousel/detail/detail.css.ts +2 -2
- package/templates/ecom/src/views/carousel/detail/detail.stories.tsx +26 -8
- package/templates/ecom/src/views/carousel/detail/index.tsx +43 -29
- package/templates/ecom/src/views/carousel/index.tsx +1 -2
- package/templates/ecom/vite.config.ts +1 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import type { SearchInput } from "../tools/search-products.js";
|
|
2
|
+
import type {
|
|
3
|
+
Option,
|
|
4
|
+
Price,
|
|
5
|
+
Product,
|
|
6
|
+
SearchResult,
|
|
7
|
+
Spec,
|
|
8
|
+
Variant,
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
|
|
11
|
+
// Shopify Storefront API provider. To use it:
|
|
12
|
+
// 1. re-export this module from `./index.js` instead of `./mock.js`
|
|
13
|
+
// 2. set SHOPIFY_STORE_DOMAIN and SHOPIFY_STOREFRONT_TOKEN in .env
|
|
14
|
+
// 3. uncomment the `csp` block in `src/tools/render-carousel.ts` with
|
|
15
|
+
// `https://cdn.shopify.com` and your store domain
|
|
16
|
+
//
|
|
17
|
+
// Product ids are Shopify handles, not GIDs: stable, readable, and far cheaper
|
|
18
|
+
// in model context than `gid://shopify/Product/8123456789`.
|
|
19
|
+
|
|
20
|
+
const API_VERSION = "2026-07";
|
|
21
|
+
const PAGE_SIZE = 20;
|
|
22
|
+
|
|
23
|
+
// Only the fields this app renders. Options are derived from the variants'
|
|
24
|
+
// `selectedOptions`, so the product-level `options` field is not queried.
|
|
25
|
+
const PRODUCT_FIELDS = `
|
|
26
|
+
fragment ProductFields on Product {
|
|
27
|
+
handle
|
|
28
|
+
title
|
|
29
|
+
description
|
|
30
|
+
vendor
|
|
31
|
+
productType
|
|
32
|
+
onlineStoreUrl
|
|
33
|
+
availableForSale
|
|
34
|
+
priceRange { minVariantPrice { amount currencyCode } }
|
|
35
|
+
images(first: 10) { nodes { url } }
|
|
36
|
+
variants(first: 100) {
|
|
37
|
+
nodes {
|
|
38
|
+
id
|
|
39
|
+
title
|
|
40
|
+
availableForSale
|
|
41
|
+
price { amount currencyCode }
|
|
42
|
+
image { url }
|
|
43
|
+
selectedOptions { name value }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}`;
|
|
47
|
+
|
|
48
|
+
type ShopifyMoney = { amount: string; currencyCode: string };
|
|
49
|
+
|
|
50
|
+
type ShopifyProduct = {
|
|
51
|
+
handle: string;
|
|
52
|
+
title: string;
|
|
53
|
+
description: string;
|
|
54
|
+
vendor: string;
|
|
55
|
+
productType: string;
|
|
56
|
+
onlineStoreUrl: string | null;
|
|
57
|
+
availableForSale: boolean;
|
|
58
|
+
priceRange: { minVariantPrice: ShopifyMoney };
|
|
59
|
+
images: { nodes: { url: string }[] };
|
|
60
|
+
variants: {
|
|
61
|
+
nodes: {
|
|
62
|
+
id: string;
|
|
63
|
+
title: string;
|
|
64
|
+
availableForSale: boolean;
|
|
65
|
+
price: ShopifyMoney;
|
|
66
|
+
image: { url: string } | null;
|
|
67
|
+
selectedOptions: { name: string; value: string }[];
|
|
68
|
+
}[];
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
async function storefront(query: string, variables: Record<string, unknown>) {
|
|
73
|
+
const domain = process.env.SHOPIFY_STORE_DOMAIN;
|
|
74
|
+
const token = process.env.SHOPIFY_STOREFRONT_TOKEN;
|
|
75
|
+
if (!domain || !token) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Missing SHOPIFY_STORE_DOMAIN or SHOPIFY_STOREFRONT_TOKEN in the environment.",
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const response = await fetch(
|
|
82
|
+
`https://${domain}/api/${API_VERSION}/graphql.json`,
|
|
83
|
+
{
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: {
|
|
86
|
+
"Content-Type": "application/json",
|
|
87
|
+
"X-Shopify-Storefront-Access-Token": token,
|
|
88
|
+
},
|
|
89
|
+
body: JSON.stringify({ query, variables }),
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw new Error(`Shopify ${response.status}: ${await response.text()}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const { data, errors } = await response.json();
|
|
97
|
+
if (errors) {
|
|
98
|
+
throw new Error(`Shopify: ${JSON.stringify(errors)}`);
|
|
99
|
+
}
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function toPrice(money: ShopifyMoney): Price {
|
|
104
|
+
return { amount: Number(money.amount), currency: money.currencyCode };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// @todo: Shopify has no generic "specs" field. Vendor and product type are a
|
|
108
|
+
// starting point; swap in your own metafields once you know their namespace.
|
|
109
|
+
function toSpecs(node: ShopifyProduct): Spec[] {
|
|
110
|
+
const specs: Spec[] = [];
|
|
111
|
+
if (node.vendor) {
|
|
112
|
+
specs.push({ label: "Brand", value: node.vendor });
|
|
113
|
+
}
|
|
114
|
+
if (node.productType) {
|
|
115
|
+
specs.push({ label: "Type", value: node.productType });
|
|
116
|
+
}
|
|
117
|
+
return specs;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function toProduct(node: ShopifyProduct): Product {
|
|
121
|
+
const specs = toSpecs(node);
|
|
122
|
+
const images: string[] = [];
|
|
123
|
+
for (const image of node.images.nodes) {
|
|
124
|
+
images.push(image.url);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Walk the variants once, building both the variants and the axes they vary
|
|
128
|
+
// on. Shopify option names are used as-is for ids, so a variant's
|
|
129
|
+
// `selectedOptions` maps straight onto `selection`.
|
|
130
|
+
const options: Option[] = [];
|
|
131
|
+
const variants: Variant[] = [];
|
|
132
|
+
for (const variant of node.variants.nodes) {
|
|
133
|
+
const selection: Record<string, string> = {};
|
|
134
|
+
for (const selected of variant.selectedOptions) {
|
|
135
|
+
selection[selected.name] = selected.value;
|
|
136
|
+
|
|
137
|
+
let option: Option | undefined;
|
|
138
|
+
for (const candidate of options) {
|
|
139
|
+
if (candidate.id === selected.name) {
|
|
140
|
+
option = candidate;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!option) {
|
|
145
|
+
option = { id: selected.name, label: selected.name, values: [] };
|
|
146
|
+
options.push(option);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let known = false;
|
|
150
|
+
for (const value of option.values) {
|
|
151
|
+
if (value.id === selected.value) {
|
|
152
|
+
known = true;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!known) {
|
|
157
|
+
option.values.push({
|
|
158
|
+
id: selected.value,
|
|
159
|
+
label: selected.value,
|
|
160
|
+
media: variant.image?.url,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
variants.push({
|
|
166
|
+
id: variant.id,
|
|
167
|
+
selection,
|
|
168
|
+
title: variant.title === "Default Title" ? node.title : variant.title,
|
|
169
|
+
description: node.description,
|
|
170
|
+
price: toPrice(variant.price),
|
|
171
|
+
media: variant.image ? [variant.image.url] : images,
|
|
172
|
+
url: node.onlineStoreUrl ?? undefined,
|
|
173
|
+
outOfStock: !variant.availableForSale,
|
|
174
|
+
specs,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// A single-variant product has no axes to pick from.
|
|
179
|
+
if (variants.length < 2) {
|
|
180
|
+
options.length = 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
id: node.handle,
|
|
185
|
+
options,
|
|
186
|
+
variants,
|
|
187
|
+
card: {
|
|
188
|
+
title: node.title,
|
|
189
|
+
description: node.description,
|
|
190
|
+
price: toPrice(node.priceRange.minVariantPrice),
|
|
191
|
+
media: images,
|
|
192
|
+
url: node.onlineStoreUrl ?? undefined,
|
|
193
|
+
outOfStock: !node.availableForSale,
|
|
194
|
+
specs,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function search({
|
|
200
|
+
keyword,
|
|
201
|
+
sort,
|
|
202
|
+
priceRange,
|
|
203
|
+
}: SearchInput): Promise<SearchResult> {
|
|
204
|
+
// Shopify's search DSL: a bare term matches title, vendor, type and tags;
|
|
205
|
+
// extra clauses narrow it. @todo: add your own (`tag:`, `product_type:`…).
|
|
206
|
+
let query = keyword;
|
|
207
|
+
if (priceRange) {
|
|
208
|
+
const [min, max] = priceRange.split("-");
|
|
209
|
+
query += ` variants.price:>=${min} variants.price:<=${max}`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const data = await storefront(
|
|
213
|
+
`${PRODUCT_FIELDS}
|
|
214
|
+
query Search($query: String!, $sortKey: ProductSortKeys!, $reverse: Boolean!, $first: Int!) {
|
|
215
|
+
products(query: $query, sortKey: $sortKey, reverse: $reverse, first: $first) {
|
|
216
|
+
nodes { ...ProductFields }
|
|
217
|
+
}
|
|
218
|
+
}`,
|
|
219
|
+
{
|
|
220
|
+
query,
|
|
221
|
+
sortKey: sort ? "PRICE" : "RELEVANCE",
|
|
222
|
+
reverse: sort === "price-desc",
|
|
223
|
+
first: PAGE_SIZE,
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
const products: Product[] = [];
|
|
228
|
+
for (const node of data.products.nodes) {
|
|
229
|
+
products.push(toProduct(node));
|
|
230
|
+
}
|
|
231
|
+
// Storefront paginates by cursor and reports no total, so both counts are
|
|
232
|
+
// left out rather than faked.
|
|
233
|
+
return { products };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function getProducts(ids: string[]): Promise<Product[]> {
|
|
237
|
+
// One aliased lookup per handle, so the response comes back in the order the
|
|
238
|
+
// model asked for.
|
|
239
|
+
const declarations: string[] = [];
|
|
240
|
+
const fields: string[] = [];
|
|
241
|
+
const variables: Record<string, string> = {};
|
|
242
|
+
for (let index = 0; index < ids.length; index++) {
|
|
243
|
+
declarations.push(`$h${index}: String!`);
|
|
244
|
+
fields.push(`p${index}: product(handle: $h${index}) { ...ProductFields }`);
|
|
245
|
+
variables[`h${index}`] = ids[index];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const data = await storefront(
|
|
249
|
+
`${PRODUCT_FIELDS}
|
|
250
|
+
query Products(${declarations.join(", ")}) {
|
|
251
|
+
${fields.join("\n ")}
|
|
252
|
+
}`,
|
|
253
|
+
variables,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
const products: Product[] = [];
|
|
257
|
+
for (let index = 0; index < ids.length; index++) {
|
|
258
|
+
const node = data[`p${index}`];
|
|
259
|
+
if (node) {
|
|
260
|
+
products.push(toProduct(node));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return products;
|
|
264
|
+
}
|
|
@@ -2,8 +2,9 @@ import { style } from "@vanilla-extract/css";
|
|
|
2
2
|
import { recipe } from "@vanilla-extract/recipes";
|
|
3
3
|
import { colors, primitives } from "../design/tokens";
|
|
4
4
|
|
|
5
|
-
// A selectable pill used for option values (sizes, colors…). `selected
|
|
6
|
-
// `
|
|
5
|
+
// A selectable pill used for option values (sizes, colors…). `selected`,
|
|
6
|
+
// `outOfStock` and `nonExistent` are driven by the variant picker from the
|
|
7
|
+
// sparse variant list.
|
|
7
8
|
// @todo: tune the chip to your brand (radius, borders, selected treatment).
|
|
8
9
|
export const chip = recipe({
|
|
9
10
|
base: {
|
|
@@ -32,18 +33,24 @@ export const chip = recipe({
|
|
|
32
33
|
},
|
|
33
34
|
false: {},
|
|
34
35
|
},
|
|
35
|
-
|
|
36
|
-
//
|
|
37
|
-
// visible so the client sees the axis exists (never silently hidden).
|
|
36
|
+
outOfStock: {
|
|
37
|
+
// Struck but still clickable; the buy CTA carries the state.
|
|
38
38
|
true: {
|
|
39
|
-
|
|
40
|
-
opacity: 0.4,
|
|
39
|
+
color: colors.content.subtle,
|
|
41
40
|
textDecoration: "line-through",
|
|
42
41
|
},
|
|
43
42
|
false: {},
|
|
44
43
|
},
|
|
44
|
+
nonExistent: {
|
|
45
|
+
// Faded and non-interactive, distinct from sold out above.
|
|
46
|
+
true: {
|
|
47
|
+
opacity: 0.35,
|
|
48
|
+
cursor: "default",
|
|
49
|
+
},
|
|
50
|
+
false: {},
|
|
51
|
+
},
|
|
45
52
|
},
|
|
46
|
-
defaultVariants: { selected: false,
|
|
53
|
+
defaultVariants: { selected: false, outOfStock: false, nonExistent: false },
|
|
47
54
|
});
|
|
48
55
|
|
|
49
56
|
// Small color/material swatch shown before the label on image chips.
|
|
@@ -14,7 +14,8 @@ export const States = () => (
|
|
|
14
14
|
<div style={row}>
|
|
15
15
|
<Chip label="M" />
|
|
16
16
|
<Chip label="L" selected />
|
|
17
|
-
<Chip label="XL"
|
|
17
|
+
<Chip label="XL" outOfStock /> {/* exists, sold out: clickable */}
|
|
18
|
+
<Chip label="XXL" nonExistent /> {/* combination does not exist */}
|
|
18
19
|
</div>
|
|
19
20
|
);
|
|
20
21
|
|
|
@@ -3,7 +3,11 @@ import * as styles from "./chip.css";
|
|
|
3
3
|
type ChipProps = {
|
|
4
4
|
label: string;
|
|
5
5
|
selected?: boolean;
|
|
6
|
-
|
|
6
|
+
// Exists but sold out: struck-through and greyed, still clickable
|
|
7
|
+
outOfStock?: boolean;
|
|
8
|
+
// The combination does not exist at all (hole in the variant matrix):
|
|
9
|
+
// faded and non-interactive
|
|
10
|
+
nonExistent?: boolean;
|
|
7
11
|
onSelect?: () => void;
|
|
8
12
|
// Optional swatch image (e.g. one per color); omit for a plain text chip.
|
|
9
13
|
media?: string;
|
|
@@ -12,25 +16,29 @@ type ChipProps = {
|
|
|
12
16
|
/**
|
|
13
17
|
* One option value as a selectable pill. Rendered as a `radio` inside the
|
|
14
18
|
* variant picker's `radiogroup`; the picker owns selection state and passes
|
|
15
|
-
* `selected`/`
|
|
19
|
+
* `selected`/`outOfStock`/`nonExistent` computed from the sparse variant list.
|
|
20
|
+
* An out-of-stock chip stays operable: aria-disabled announces the state to
|
|
21
|
+
* assistive tech without blocking activation. A nonexistent chip is natively
|
|
22
|
+
* disabled: the combination cannot be composed.
|
|
16
23
|
*/
|
|
17
24
|
export function Chip({
|
|
18
25
|
label,
|
|
19
26
|
selected,
|
|
20
|
-
|
|
27
|
+
outOfStock,
|
|
28
|
+
nonExistent,
|
|
21
29
|
onSelect,
|
|
22
30
|
media,
|
|
23
31
|
}: ChipProps) {
|
|
24
32
|
return (
|
|
25
|
-
// biome-ignore lint/a11y/useSemanticElements: custom radio; a styled button carries the swatch and
|
|
33
|
+
// biome-ignore lint/a11y/useSemanticElements: custom radio; a styled button carries the swatch and availability states a native radio cannot, inside the picker radiogroup.
|
|
26
34
|
<button
|
|
27
35
|
type="button"
|
|
28
36
|
role="radio"
|
|
29
37
|
aria-checked={selected ?? false}
|
|
30
|
-
aria-disabled={
|
|
31
|
-
disabled={
|
|
32
|
-
className={styles.chip({ selected,
|
|
33
|
-
onClick={
|
|
38
|
+
aria-disabled={outOfStock ?? false}
|
|
39
|
+
disabled={nonExistent ?? false}
|
|
40
|
+
className={styles.chip({ selected, outOfStock, nonExistent })}
|
|
41
|
+
onClick={onSelect}
|
|
34
42
|
>
|
|
35
43
|
{media ? (
|
|
36
44
|
<img className={styles.swatch} src={media} alt="" draggable={false} />
|
|
@@ -30,8 +30,8 @@ export function ProductCard({
|
|
|
30
30
|
}: ProductCardProps) {
|
|
31
31
|
const { locale } = useUser();
|
|
32
32
|
const labels = useLabels();
|
|
33
|
-
// Show the first image. @todo: the rest of `media` is available here, e.g.
|
|
34
|
-
//
|
|
33
|
+
// Show the first image. @todo: the rest of `media` is available here, e.g. to
|
|
34
|
+
// swap in media[1] on hover (do not cross-fade, breaks with transparency)
|
|
35
35
|
const cover = media?.[0];
|
|
36
36
|
return (
|
|
37
37
|
<article className={styles.card({ framed: FRAMED })}>
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
2
|
import { initialSelection, type Selection } from "../lib/variants.js";
|
|
3
|
-
import type { Product } from "../
|
|
3
|
+
import type { Product } from "../types.js";
|
|
4
4
|
import { VariantPicker } from "./variant-picker";
|
|
5
5
|
|
|
6
|
-
// A sparse catalog: {
|
|
7
|
-
// {
|
|
6
|
+
// A sparse catalog: {white,42} does not exist, so "42" is hard-disabled under
|
|
7
|
+
// "White"; {black,42} is sold out, so it renders struck (yet clickable) under
|
|
8
|
+
// "Black".
|
|
8
9
|
function variant(
|
|
9
10
|
id: string,
|
|
10
11
|
color: string,
|
|
11
12
|
size: string,
|
|
13
|
+
outOfStock = false,
|
|
12
14
|
): Product["variants"][number] {
|
|
13
15
|
return {
|
|
14
16
|
id,
|
|
@@ -17,6 +19,7 @@ function variant(
|
|
|
17
19
|
price: { amount: 120, currency: "EUR" },
|
|
18
20
|
media: [],
|
|
19
21
|
specs: [],
|
|
22
|
+
outOfStock,
|
|
20
23
|
};
|
|
21
24
|
}
|
|
22
25
|
|
|
@@ -42,7 +45,7 @@ const PRODUCT: Product = {
|
|
|
42
45
|
],
|
|
43
46
|
variants: [
|
|
44
47
|
variant("s-b-40", "black", "40"),
|
|
45
|
-
variant("s-b-42", "black", "42"),
|
|
48
|
+
variant("s-b-42", "black", "42", true),
|
|
46
49
|
variant("s-w-40", "white", "40"),
|
|
47
50
|
],
|
|
48
51
|
card: { title: "Sneaker", media: [], specs: [] },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { text } from "../design/tokens";
|
|
2
2
|
import { cx } from "../lib/cx";
|
|
3
|
-
import { type Selection
|
|
4
|
-
import type { Product } from "../
|
|
3
|
+
import { applyChoice, axisStates, type Selection } from "../lib/variants.js";
|
|
4
|
+
import type { Product } from "../types.js";
|
|
5
5
|
import { Chip } from "./chip";
|
|
6
6
|
import * as styles from "./variant-picker.css";
|
|
7
7
|
|
|
@@ -12,10 +12,11 @@ type VariantPickerProps = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* Renders one section per option axis
|
|
16
|
-
*
|
|
17
|
-
* the
|
|
18
|
-
*
|
|
15
|
+
* Renders one section per option axis. Chip states come from axisStates
|
|
16
|
+
* (lib/variants.ts, top-down): nonexistent values are hard-disabled, sold-out
|
|
17
|
+
* ones struck but clickable (the CTA names the cause), and an axis that does
|
|
18
|
+
* not apply hides its row. A pick goes through applyChoice, which keeps
|
|
19
|
+
* still-existing later choices and snaps the rest onto a real variant.
|
|
19
20
|
*
|
|
20
21
|
* This is the in-place model: every axis is local state, no remount. @todo: if
|
|
21
22
|
* a catalog models an axis (e.g. color) as separate products, promote it to a
|
|
@@ -26,32 +27,14 @@ export function VariantPicker({
|
|
|
26
27
|
selection,
|
|
27
28
|
onChange,
|
|
28
29
|
}: VariantPickerProps) {
|
|
29
|
-
function choose(axisId: string, valueId: string) {
|
|
30
|
-
const next: Selection = { ...selection, [axisId]: valueId };
|
|
31
|
-
// Repair: drop any other-axis choice the new pick just made unreachable.
|
|
32
|
-
for (const option of product.options) {
|
|
33
|
-
if (option.id === axisId) {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const chosen = next[option.id];
|
|
37
|
-
if (
|
|
38
|
-
chosen != null &&
|
|
39
|
-
!selectableValues(product, option.id, next).has(chosen)
|
|
40
|
-
) {
|
|
41
|
-
delete next[option.id];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
onChange(next);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
30
|
return (
|
|
48
31
|
<div className={styles.picker}>
|
|
49
32
|
{product.options.map((option) => {
|
|
50
|
-
//
|
|
51
|
-
|
|
33
|
+
// Empty map: the axis does not apply to the current configuration.
|
|
34
|
+
const states = axisStates(product, option.id, selection);
|
|
35
|
+
if (states.size === 0) {
|
|
52
36
|
return null;
|
|
53
37
|
}
|
|
54
|
-
const reachable = selectableValues(product, option.id, selection);
|
|
55
38
|
return (
|
|
56
39
|
<div
|
|
57
40
|
key={option.id}
|
|
@@ -69,8 +52,13 @@ export function VariantPicker({
|
|
|
69
52
|
label={value.label}
|
|
70
53
|
media={value.media}
|
|
71
54
|
selected={selection[option.id] === value.id}
|
|
72
|
-
|
|
73
|
-
|
|
55
|
+
nonExistent={!states.has(value.id)}
|
|
56
|
+
outOfStock={states.get(value.id) === "soldOut"}
|
|
57
|
+
onSelect={() =>
|
|
58
|
+
onChange(
|
|
59
|
+
applyChoice(product, selection, option.id, value.id),
|
|
60
|
+
)
|
|
61
|
+
}
|
|
74
62
|
/>
|
|
75
63
|
))}
|
|
76
64
|
</div>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { HTMLAttributes } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { useUser } from "skybridge/web";
|
|
3
3
|
import { darkTheme, lightTheme } from "../design/tokens";
|
|
4
4
|
import { cx } from "../lib/cx";
|
|
5
5
|
import { viewFrame } from "./view-frame.css";
|
|
@@ -10,8 +10,8 @@ import { viewFrame } from "./view-frame.css";
|
|
|
10
10
|
* variables the color contract declares) and paints the base surface + font +
|
|
11
11
|
* content color so descendants inherit from the DS instead of the host page.
|
|
12
12
|
*
|
|
13
|
-
* It follows the host theme via
|
|
14
|
-
* (e.g. always light), drop
|
|
13
|
+
* It follows the host theme via useUser(). To lock the widget to one palette
|
|
14
|
+
* (e.g. always light), drop useUser and hardcode the theme class.
|
|
15
15
|
*
|
|
16
16
|
* Keep view entry files thin: render <ViewFrame> at the root and let this carry
|
|
17
17
|
* the theme + base-style boilerplate.
|
|
@@ -19,7 +19,7 @@ import { viewFrame } from "./view-frame.css";
|
|
|
19
19
|
type ViewFrameProps = HTMLAttributes<HTMLDivElement>;
|
|
20
20
|
|
|
21
21
|
export function ViewFrame({ className, ...rest }: ViewFrameProps) {
|
|
22
|
-
const { theme } =
|
|
22
|
+
const { theme } = useUser();
|
|
23
23
|
const themeClass = theme === "dark" ? darkTheme : lightTheme;
|
|
24
24
|
|
|
25
25
|
return <div {...rest} className={cx(themeClass, viewFrame, className)} />;
|
|
@@ -8,6 +8,7 @@ import { useUser } from "skybridge/web";
|
|
|
8
8
|
const LABELS = {
|
|
9
9
|
en: {
|
|
10
10
|
outOfStock: "Out of stock",
|
|
11
|
+
combinationUnavailable: "Combination unavailable",
|
|
11
12
|
noProducts: "No products to show.",
|
|
12
13
|
carousel: "carousel",
|
|
13
14
|
products: "Products",
|
|
@@ -16,7 +17,6 @@ const LABELS = {
|
|
|
16
17
|
// Detail view.
|
|
17
18
|
reference: "Ref.",
|
|
18
19
|
viewOnSite: "View on site",
|
|
19
|
-
selectOptions: "Select options",
|
|
20
20
|
priceOnRequest: "Price on request",
|
|
21
21
|
specifications: "Specifications",
|
|
22
22
|
readMore: "Read more",
|