@base44/app-plugin-commerce 0.3.4 → 0.4.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 +2 -2
- package/base44/functions/commerce/storefront-cart/cart-pricing.ts +1 -0
- package/base44/functions/commerce/storefront-checkout/cart-pricing.ts +1 -0
- package/package.json +1 -1
- package/skills/commerce/SKILL.md +46 -50
- package/skills/commerce/docs/api-storefront.md +1 -1
- package/skills/commerce/install/01-install.md +4 -1
- package/skills/commerce/install/02-storefront.md +169 -184
- package/skills/commerce/references/storefront-custom.md +150 -0
- package/skills/commerce/references/storefront-styling.md +99 -0
- package/skills/commerce/references/storefront-verification.md +4 -0
- package/src/commerce/storefront/StorefrontProvider.jsx +10 -3
- package/src/commerce/storefront/index.js +29 -0
- package/src/commerce/storefront/parts/cart.jsx +167 -0
- package/src/commerce/storefront/parts/checkout.jsx +504 -0
- package/src/commerce/storefront/parts/drawer.jsx +119 -0
- package/src/commerce/storefront/parts/labels.jsx +140 -0
- package/src/commerce/storefront/parts/orderReceived.jsx +188 -0
- package/src/commerce/storefront/parts/parts.css +232 -0
- package/src/commerce/storefront/parts/shared.jsx +127 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useId,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
import { addressFieldSpec, attributesLabel } from "@/commerce/utils";
|
|
12
|
+
import { CheckoutProvider, useCheckoutContext } from "../useCheckout";
|
|
13
|
+
import { ShippingMethodPicker, PaymentMethodPicker } from "../pickers";
|
|
14
|
+
import { useCart, useCountries, useFormatMoney } from "../StorefrontProvider";
|
|
15
|
+
import { REQUIRED_BILLING_FIELDS } from "../address";
|
|
16
|
+
import { LabelsScope, label, useResolvedLabels } from "./labels";
|
|
17
|
+
import { CouponField, Totals } from "./shared";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Checkout parts — the checkout's sections as unstyled, unworded components
|
|
21
|
+
* the store places into ITS OWN layout markup. Contract, styling
|
|
22
|
+
* (`data-part`), labels and placement: the commerce skill's
|
|
23
|
+
* install/02-storefront.md. Each part is a thin composition over `useCheckout`
|
|
24
|
+
* and the pickers; mixing a part with a custom section built on
|
|
25
|
+
* `useCheckoutContext()` is normal, and the parts are the app's source —
|
|
26
|
+
* editable when a requirement outgrows their props.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const CheckoutPartsContext = createContext(null);
|
|
30
|
+
|
|
31
|
+
/** Layout effect where there is a DOM; plain effect on the server (no warning). */
|
|
32
|
+
const useCommitEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Mounts `CheckoutProvider`, resolves the page's phase once (`submitted` >
|
|
36
|
+
* `loading` > `empty` > `form` — the ordering that kills the empty-bag flash
|
|
37
|
+
* over a just-placed order), and scopes `labels`. `orderReceivedPath` is
|
|
38
|
+
* REQUIRED: the one navigation this flow performs is an explicit decision —
|
|
39
|
+
* pass the store's own receipt route, or `null` (+ `onPlaced(result)`) to own
|
|
40
|
+
* the step entirely.
|
|
41
|
+
*/
|
|
42
|
+
function Root({ labels, orderReceivedPath, onPlaced, options, checkout, children }) {
|
|
43
|
+
if (checkout == null && orderReceivedPath === undefined) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
"<Checkout.Root> requires orderReceivedPath — the route the store's order-received page is mounted at (or null, with onPlaced, to handle the post-order step yourself). The kit never assumes a route.",
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return (
|
|
49
|
+
<CheckoutProvider checkout={checkout} options={{ ...options, orderReceivedPath }}>
|
|
50
|
+
<LabelsScope labels={labels}>
|
|
51
|
+
<RootPhase onPlaced={onPlaced}>{children}</RootPhase>
|
|
52
|
+
</LabelsScope>
|
|
53
|
+
</CheckoutProvider>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function RootPhase({ onPlaced, children }) {
|
|
58
|
+
const checkout = useCheckoutContext();
|
|
59
|
+
const { status } = useCart();
|
|
60
|
+
const phase =
|
|
61
|
+
checkout.stage === "submitted"
|
|
62
|
+
? "submitted"
|
|
63
|
+
: status === "loading"
|
|
64
|
+
? "loading"
|
|
65
|
+
: status === "empty"
|
|
66
|
+
? "empty"
|
|
67
|
+
: "form";
|
|
68
|
+
// A page that places <Checkout.Blockers> itself must not ALSO get the copy
|
|
69
|
+
// `PlaceOrder` renders by default — the same three reasons printed twice is a
|
|
70
|
+
// real defect an eval caught. Standalone instances register here (before
|
|
71
|
+
// paint), and the button's own copy stands down while one exists.
|
|
72
|
+
const [ownBlockers, setOwnBlockers] = useState(0);
|
|
73
|
+
const registerBlockers = useCallback((delta) => setOwnBlockers((n) => n + delta), []);
|
|
74
|
+
const value = useMemo(
|
|
75
|
+
() => ({ phase, onPlaced, hasOwnBlockers: ownBlockers > 0, registerBlockers }),
|
|
76
|
+
[phase, onPlaced, ownBlockers, registerBlockers],
|
|
77
|
+
);
|
|
78
|
+
return <CheckoutPartsContext.Provider value={value}>{children}</CheckoutPartsContext.Provider>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function usePartsContext(name) {
|
|
82
|
+
const ctx = useContext(CheckoutPartsContext);
|
|
83
|
+
if (!ctx) throw new Error(`<Checkout.${name}> works inside <Checkout.Root> only.`);
|
|
84
|
+
return ctx;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Gates: children render only in the matching phase (node, or function of `{ checkout, cart }`). */
|
|
88
|
+
function gate(name, match) {
|
|
89
|
+
function Gate({ children }) {
|
|
90
|
+
const { phase } = usePartsContext(name);
|
|
91
|
+
const checkout = useCheckoutContext();
|
|
92
|
+
if (phase !== match) return null;
|
|
93
|
+
return typeof children === "function"
|
|
94
|
+
? (children({ checkout, cart: checkout.cart }) ?? null)
|
|
95
|
+
: (children ?? null);
|
|
96
|
+
}
|
|
97
|
+
Gate.displayName = `Checkout.${name}`;
|
|
98
|
+
return Gate;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const Submitted = gate("Submitted", "submitted");
|
|
102
|
+
const Loading = gate("Loading", "loading");
|
|
103
|
+
const Empty = gate("Empty", "empty");
|
|
104
|
+
const Form = gate("Form", "form");
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The address form off `addressFieldSpec`: state/province appears with the
|
|
108
|
+
* right options once a country is picked, `autoComplete` tokens stay on,
|
|
109
|
+
* required marks arm on first blur, and the server's "we don't ship there"
|
|
110
|
+
* lands on the country field. `which="shipping"` renders null until
|
|
111
|
+
* `shipToDifferent`. Overrides: `inputRender` swaps the control only,
|
|
112
|
+
* `fieldRender` the whole labeled block.
|
|
113
|
+
*/
|
|
114
|
+
function AddressFields({
|
|
115
|
+
which = "billing",
|
|
116
|
+
include,
|
|
117
|
+
omit,
|
|
118
|
+
inputRender: InputRender,
|
|
119
|
+
fieldRender,
|
|
120
|
+
className,
|
|
121
|
+
classes,
|
|
122
|
+
labels: partLabels,
|
|
123
|
+
}) {
|
|
124
|
+
const checkout = useCheckoutContext();
|
|
125
|
+
const L = useResolvedLabels(partLabels);
|
|
126
|
+
const { countries } = useCountries();
|
|
127
|
+
const idBase = useId();
|
|
128
|
+
const [touched, setTouched] = useState({});
|
|
129
|
+
|
|
130
|
+
const isBilling = which === "billing";
|
|
131
|
+
if (!isBilling && !checkout.shipToDifferent) return null;
|
|
132
|
+
|
|
133
|
+
const values = isBilling ? checkout.billing : checkout.shipping;
|
|
134
|
+
const set = isBilling ? checkout.updateBilling : checkout.updateShipping;
|
|
135
|
+
const fields = addressFieldSpec({
|
|
136
|
+
countries,
|
|
137
|
+
country: values.country,
|
|
138
|
+
required: isBilling ? REQUIRED_BILLING_FIELDS : ["country", "city"],
|
|
139
|
+
includeEmail: isBilling,
|
|
140
|
+
includeCompany: include ? include.includes("company") : false,
|
|
141
|
+
includePhone: include ? include.includes("phone") : true,
|
|
142
|
+
}).filter((f) => !(omit ?? []).includes(f.key));
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div data-part="address-fields" data-which={which} className={className}>
|
|
146
|
+
{fields.map((f) => {
|
|
147
|
+
const id = `${idBase}-${which}-${f.key}`;
|
|
148
|
+
const value = values[f.key] ?? "";
|
|
149
|
+
const invalid = Boolean(touched[f.key] && f.required && !String(value).trim());
|
|
150
|
+
const dom = {
|
|
151
|
+
id,
|
|
152
|
+
value,
|
|
153
|
+
autoComplete: f.autoComplete,
|
|
154
|
+
onChange: (e) => set({ [f.key]: e && e.target ? e.target.value : e }),
|
|
155
|
+
onBlur: () => setTouched((t) => (t[f.key] ? t : { ...t, [f.key]: true })),
|
|
156
|
+
};
|
|
157
|
+
const labelText = label(L, `fields.${f.key}`);
|
|
158
|
+
if (fieldRender) {
|
|
159
|
+
return (
|
|
160
|
+
<React.Fragment key={f.key}>
|
|
161
|
+
{fieldRender({ field: f, label: labelText, invalid, options: f.options, ...dom })}
|
|
162
|
+
</React.Fragment>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
return (
|
|
166
|
+
<div
|
|
167
|
+
key={f.key}
|
|
168
|
+
data-part="field"
|
|
169
|
+
data-key={f.key}
|
|
170
|
+
data-span={f.colSpan}
|
|
171
|
+
data-invalid={invalid || undefined}
|
|
172
|
+
className={classes?.field}
|
|
173
|
+
>
|
|
174
|
+
<label data-part="label" className={classes?.label} htmlFor={id}>
|
|
175
|
+
{labelText}
|
|
176
|
+
{f.required && (
|
|
177
|
+
<span data-part="required" aria-hidden="true">
|
|
178
|
+
*
|
|
179
|
+
</span>
|
|
180
|
+
)}
|
|
181
|
+
</label>
|
|
182
|
+
{InputRender ? (
|
|
183
|
+
<InputRender field={f} options={f.options} invalid={invalid} {...dom} />
|
|
184
|
+
) : f.type === "select" ? (
|
|
185
|
+
<select
|
|
186
|
+
data-part="control"
|
|
187
|
+
className={classes?.control}
|
|
188
|
+
aria-invalid={invalid || undefined}
|
|
189
|
+
aria-required={f.required || undefined}
|
|
190
|
+
{...dom}
|
|
191
|
+
>
|
|
192
|
+
<option value="">{label(L, "fields.select_placeholder")}</option>
|
|
193
|
+
{f.options.map((o) => (
|
|
194
|
+
<option key={o.value} value={o.value}>
|
|
195
|
+
{o.label}
|
|
196
|
+
</option>
|
|
197
|
+
))}
|
|
198
|
+
</select>
|
|
199
|
+
) : (
|
|
200
|
+
<input
|
|
201
|
+
data-part="control"
|
|
202
|
+
className={classes?.control}
|
|
203
|
+
type={f.type}
|
|
204
|
+
aria-invalid={invalid || undefined}
|
|
205
|
+
aria-required={f.required || undefined}
|
|
206
|
+
{...dom}
|
|
207
|
+
/>
|
|
208
|
+
)}
|
|
209
|
+
{f.key === "country" && checkout.addressError && (
|
|
210
|
+
<p data-part="error" role="alert" className={classes?.error}>
|
|
211
|
+
{checkout.addressError.message}
|
|
212
|
+
</p>
|
|
213
|
+
)}
|
|
214
|
+
</div>
|
|
215
|
+
);
|
|
216
|
+
})}
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** The deliver-elsewhere toggle. Children are its label — the store's words. */
|
|
222
|
+
function ShipToDifferent({ children, className, classes }) {
|
|
223
|
+
const checkout = useCheckoutContext();
|
|
224
|
+
return (
|
|
225
|
+
<label data-part="ship-to-different" className={className}>
|
|
226
|
+
<input
|
|
227
|
+
data-part="control"
|
|
228
|
+
className={classes?.control}
|
|
229
|
+
type="checkbox"
|
|
230
|
+
checked={checkout.shipToDifferent}
|
|
231
|
+
onChange={(e) => checkout.setShipToDifferent(e.target.checked)}
|
|
232
|
+
/>
|
|
233
|
+
<span data-part="label" className={classes?.label}>{children}</span>
|
|
234
|
+
</label>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The delivery choice, every branch premade: the hint line (`serverMessage`
|
|
240
|
+
* preferred over the store's own words), a radio list while there is a real
|
|
241
|
+
* choice, the chosen/auto-selected method *displayed* when there is one option
|
|
242
|
+
* (never a picker of one), null for a virtual cart. `optionRender(option)`
|
|
243
|
+
* replaces an option's content; the radio stays wired.
|
|
244
|
+
*/
|
|
245
|
+
function ShippingMethods({ optionRender, className, classes, labels: partLabels }) {
|
|
246
|
+
const L = useResolvedLabels(partLabels);
|
|
247
|
+
const groupName = useId();
|
|
248
|
+
return (
|
|
249
|
+
<ShippingMethodPicker>
|
|
250
|
+
{({ status, methods, chosen, syncing, hint }) => {
|
|
251
|
+
const showList = methods.length > 1;
|
|
252
|
+
return (
|
|
253
|
+
<fieldset
|
|
254
|
+
data-part="shipping-methods"
|
|
255
|
+
data-state={status}
|
|
256
|
+
data-syncing={syncing || undefined}
|
|
257
|
+
className={className}
|
|
258
|
+
aria-label={label(L, "aria.shipping_group")}
|
|
259
|
+
>
|
|
260
|
+
{hint && (
|
|
261
|
+
<p
|
|
262
|
+
data-part="hint"
|
|
263
|
+
data-severity={hint.severity}
|
|
264
|
+
className={classes?.hint}
|
|
265
|
+
role={hint.severity === "error" ? "alert" : "status"}
|
|
266
|
+
>
|
|
267
|
+
{hint.serverMessage ?? label(L, `shipping.${hint.code}`)}
|
|
268
|
+
</p>
|
|
269
|
+
)}
|
|
270
|
+
{showList &&
|
|
271
|
+
methods.map((m) => (
|
|
272
|
+
<label
|
|
273
|
+
key={m.id}
|
|
274
|
+
data-part="option"
|
|
275
|
+
data-state={m.selected ? "selected" : undefined}
|
|
276
|
+
className={classes?.option}
|
|
277
|
+
>
|
|
278
|
+
<input
|
|
279
|
+
data-part="option-input"
|
|
280
|
+
type="radio"
|
|
281
|
+
name={groupName}
|
|
282
|
+
checked={m.selected}
|
|
283
|
+
onChange={() => Promise.resolve(m.select()).catch(() => {})}
|
|
284
|
+
/>
|
|
285
|
+
{optionRender ? (
|
|
286
|
+
optionRender(m)
|
|
287
|
+
) : (
|
|
288
|
+
<>
|
|
289
|
+
<span data-part="option-label" className={classes?.["option-label"]}>{m.title}</span>
|
|
290
|
+
<span data-part="option-cost" className={classes?.["option-cost"]}>{m.costLabel}</span>
|
|
291
|
+
</>
|
|
292
|
+
)}
|
|
293
|
+
</label>
|
|
294
|
+
))}
|
|
295
|
+
{!showList && chosen && (
|
|
296
|
+
<p data-part="chosen" className={classes?.chosen}>
|
|
297
|
+
{optionRender ? (
|
|
298
|
+
optionRender(chosen)
|
|
299
|
+
) : (
|
|
300
|
+
<>
|
|
301
|
+
<span data-part="option-label" className={classes?.["option-label"]}>{chosen.title}</span>
|
|
302
|
+
<span data-part="option-cost" className={classes?.["option-cost"]}>{chosen.costLabel}</span>
|
|
303
|
+
</>
|
|
304
|
+
)}
|
|
305
|
+
</p>
|
|
306
|
+
)}
|
|
307
|
+
</fieldset>
|
|
308
|
+
);
|
|
309
|
+
}}
|
|
310
|
+
</ShippingMethodPicker>
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* How to pay — every ENABLED gateway, titles and descriptions in the admin's
|
|
316
|
+
* own words. One gateway renders as the selection it already is; none renders
|
|
317
|
+
* the store's checkout-unavailable line. `optionRender(gateway)` replaces an
|
|
318
|
+
* option's content; the radio stays wired.
|
|
319
|
+
*/
|
|
320
|
+
function PaymentMethods({ optionRender, className, classes, labels: partLabels }) {
|
|
321
|
+
const L = useResolvedLabels(partLabels);
|
|
322
|
+
const groupName = useId();
|
|
323
|
+
return (
|
|
324
|
+
<PaymentMethodPicker>
|
|
325
|
+
{({ gateways, selected, single, hint }) => (
|
|
326
|
+
<fieldset data-part="payment-methods" className={className} aria-label={label(L, "aria.payment_group")}>
|
|
327
|
+
{hint && (
|
|
328
|
+
<p data-part="hint" data-severity={hint.severity} className={classes?.hint} role="alert">
|
|
329
|
+
{label(L, "payment.none_available")}
|
|
330
|
+
</p>
|
|
331
|
+
)}
|
|
332
|
+
{!single &&
|
|
333
|
+
gateways.map((g) => (
|
|
334
|
+
<label
|
|
335
|
+
key={g.slug}
|
|
336
|
+
data-part="option"
|
|
337
|
+
data-state={g.selected ? "selected" : undefined}
|
|
338
|
+
className={classes?.option}
|
|
339
|
+
>
|
|
340
|
+
<input
|
|
341
|
+
data-part="option-input"
|
|
342
|
+
type="radio"
|
|
343
|
+
name={groupName}
|
|
344
|
+
checked={g.selected}
|
|
345
|
+
onChange={g.select}
|
|
346
|
+
/>
|
|
347
|
+
{optionRender ? (
|
|
348
|
+
optionRender(g)
|
|
349
|
+
) : (
|
|
350
|
+
<>
|
|
351
|
+
<span data-part="option-label" className={classes?.["option-label"]}>{g.title}</span>
|
|
352
|
+
{g.description && (
|
|
353
|
+
<span data-part="option-description" className={classes?.["option-description"]}>
|
|
354
|
+
{g.description}
|
|
355
|
+
</span>
|
|
356
|
+
)}
|
|
357
|
+
</>
|
|
358
|
+
)}
|
|
359
|
+
</label>
|
|
360
|
+
))}
|
|
361
|
+
{single && selected && (
|
|
362
|
+
<p data-part="chosen" className={classes?.chosen}>
|
|
363
|
+
{optionRender ? (
|
|
364
|
+
optionRender(selected)
|
|
365
|
+
) : (
|
|
366
|
+
<>
|
|
367
|
+
<span data-part="option-label" className={classes?.["option-label"]}>{selected.title}</span>
|
|
368
|
+
{selected.description && (
|
|
369
|
+
<span data-part="option-description" className={classes?.["option-description"]}>
|
|
370
|
+
{selected.description}
|
|
371
|
+
</span>
|
|
372
|
+
)}
|
|
373
|
+
</>
|
|
374
|
+
)}
|
|
375
|
+
</p>
|
|
376
|
+
)}
|
|
377
|
+
</fieldset>
|
|
378
|
+
)}
|
|
379
|
+
</PaymentMethodPicker>
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** The mini summary: read-only rows of what is being bought. `itemRender(item)` replaces a row. */
|
|
384
|
+
function Items({ itemRender, className, classes }) {
|
|
385
|
+
const checkout = useCheckoutContext();
|
|
386
|
+
const formatMoney = useFormatMoney();
|
|
387
|
+
const items = checkout.cart?.items ?? [];
|
|
388
|
+
if (!items.length) return null;
|
|
389
|
+
return (
|
|
390
|
+
<ul data-part="items" className={className}>
|
|
391
|
+
{items.map((item) => (
|
|
392
|
+
<li key={item.item_key} data-part="row" className={classes?.row}>
|
|
393
|
+
{itemRender ? (
|
|
394
|
+
itemRender(item)
|
|
395
|
+
) : (
|
|
396
|
+
<>
|
|
397
|
+
{item.image ? (
|
|
398
|
+
<img data-part="media" className={classes?.media} src={item.image} alt="" loading="lazy" />
|
|
399
|
+
) : (
|
|
400
|
+
<div data-part="media" data-empty="" className={classes?.media} />
|
|
401
|
+
)}
|
|
402
|
+
<span data-part="content" className={classes?.content}>
|
|
403
|
+
<span data-part="name" className={classes?.name}>{item.name}</span>
|
|
404
|
+
{attributesLabel(item.attributes) ? (
|
|
405
|
+
<span data-part="attributes" className={classes?.attributes}>
|
|
406
|
+
{attributesLabel(item.attributes)}
|
|
407
|
+
</span>
|
|
408
|
+
) : null}
|
|
409
|
+
</span>
|
|
410
|
+
<span data-part="quantity" className={classes?.quantity}>×{item.quantity}</span>
|
|
411
|
+
<span data-part="line-total" className={classes?.["line-total"]}>
|
|
412
|
+
{formatMoney(item.total)}
|
|
413
|
+
</span>
|
|
414
|
+
</>
|
|
415
|
+
)}
|
|
416
|
+
</li>
|
|
417
|
+
))}
|
|
418
|
+
</ul>
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* The blocker lines — what still stands between the customer and the order,
|
|
424
|
+
* one line per code in the store's words, gone when the order can be placed.
|
|
425
|
+
* `PlaceOrder` renders these next to the button by default; placing this part
|
|
426
|
+
* anywhere under `Checkout.Root` moves them there instead — the button's own
|
|
427
|
+
* copy stands down automatically, so the reasons are never printed twice.
|
|
428
|
+
*/
|
|
429
|
+
function Blockers({ standalone = true, className, classes, labels: partLabels }) {
|
|
430
|
+
const checkout = useCheckoutContext();
|
|
431
|
+
const { registerBlockers } = usePartsContext("Blockers");
|
|
432
|
+
const L = useResolvedLabels(partLabels);
|
|
433
|
+
// Registered before paint, so the button's built-in copy never flashes.
|
|
434
|
+
useCommitEffect(() => {
|
|
435
|
+
if (!standalone) return undefined;
|
|
436
|
+
registerBlockers(1);
|
|
437
|
+
return () => registerBlockers(-1);
|
|
438
|
+
}, [standalone, registerBlockers]);
|
|
439
|
+
if (checkout.canPlaceOrder) return null;
|
|
440
|
+
return (
|
|
441
|
+
<div data-part="blockers" className={className}>
|
|
442
|
+
{checkout.blockers.map((code) => (
|
|
443
|
+
<p key={code} data-part="blocker" data-code={code} className={classes?.blocker}>
|
|
444
|
+
{label(L, `blockers.${code}`)}
|
|
445
|
+
</p>
|
|
446
|
+
))}
|
|
447
|
+
</div>
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The gate, the button and the reasons, together so a disabled button always
|
|
453
|
+
* says why. Label and placing-label are the store's; the order error is the
|
|
454
|
+
* server's own words. On success `Checkout.Root`'s `onPlaced` fires (when the
|
|
455
|
+
* page owns the post-order step).
|
|
456
|
+
*/
|
|
457
|
+
function PlaceOrder({ showBlockers = true, className, classes, labels: partLabels }) {
|
|
458
|
+
const checkout = useCheckoutContext();
|
|
459
|
+
const { onPlaced, hasOwnBlockers } = usePartsContext("PlaceOrder");
|
|
460
|
+
const L = useResolvedLabels(partLabels);
|
|
461
|
+
const place = useCallback(async () => {
|
|
462
|
+
const res = await checkout.placeOrder(); // resolves {ok:false} — never throws
|
|
463
|
+
if (res?.ok && onPlaced) onPlaced(res.result);
|
|
464
|
+
}, [checkout, onPlaced]);
|
|
465
|
+
return (
|
|
466
|
+
<>
|
|
467
|
+
<button
|
|
468
|
+
type="button"
|
|
469
|
+
data-part="place-order"
|
|
470
|
+
data-state={checkout.placing ? "placing" : undefined}
|
|
471
|
+
className={className}
|
|
472
|
+
disabled={!checkout.canPlaceOrder || checkout.placing}
|
|
473
|
+
onClick={place}
|
|
474
|
+
>
|
|
475
|
+
{checkout.placing ? label(L, "placeOrder.placing") : label(L, "placeOrder.label")}
|
|
476
|
+
</button>
|
|
477
|
+
{checkout.orderError && (
|
|
478
|
+
<p data-part="order-error" role="alert" className={classes?.["order-error"]}>
|
|
479
|
+
{checkout.orderError.message}
|
|
480
|
+
</p>
|
|
481
|
+
)}
|
|
482
|
+
{showBlockers && !hasOwnBlockers && (
|
|
483
|
+
<Blockers standalone={false} className={classes?.blockers} labels={partLabels} />
|
|
484
|
+
)}
|
|
485
|
+
</>
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export const Checkout = {
|
|
490
|
+
Root,
|
|
491
|
+
Submitted,
|
|
492
|
+
Loading,
|
|
493
|
+
Empty,
|
|
494
|
+
Form,
|
|
495
|
+
AddressFields,
|
|
496
|
+
ShipToDifferent,
|
|
497
|
+
ShippingMethods,
|
|
498
|
+
PaymentMethods,
|
|
499
|
+
Items,
|
|
500
|
+
CouponField,
|
|
501
|
+
Totals,
|
|
502
|
+
PlaceOrder,
|
|
503
|
+
Blockers,
|
|
504
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from "react";
|
|
2
|
+
import { useCart } from "../StorefrontProvider";
|
|
3
|
+
import { useCartUI } from "../cartUI";
|
|
4
|
+
import { label, useResolvedLabels } from "./labels";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cart-drawer parts. The open/close state (plus Esc, open-on-add and
|
|
8
|
+
* close-on-navigate) stays on `<CartUIProvider>` — mount it as before; these
|
|
9
|
+
* parts render the trigger and the dialog correctly so the classic drawer
|
|
10
|
+
* bugs (a hidden panel that stays clickable, an unnamed icon button, focus
|
|
11
|
+
* lost on close) cannot be written. Everything inside the panel is the
|
|
12
|
+
* store's markup — typically `Cart.*` parts plus its own checkout link.
|
|
13
|
+
* Contract, styling and labels: the commerce skill's install/02-storefront.md.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const PANEL_ID = "commerce-cart-drawer";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The header's cart button, wired (toggle, `aria-expanded`, dialog name from
|
|
20
|
+
* `labels.aria.trigger`). The icon is the store's: pass children, or a render
|
|
21
|
+
* prop `({ count, open }) => …` to badge it with the live item count.
|
|
22
|
+
*/
|
|
23
|
+
function Trigger({ children, className, labels: partLabels }) {
|
|
24
|
+
const L = useResolvedLabels(partLabels);
|
|
25
|
+
const ui = useCartUI();
|
|
26
|
+
const { itemCount } = useCart();
|
|
27
|
+
const content = typeof children === "function" ? children({ count: itemCount, open: ui.open }) : children;
|
|
28
|
+
return (
|
|
29
|
+
<button
|
|
30
|
+
type="button"
|
|
31
|
+
data-part="trigger"
|
|
32
|
+
data-state={ui.open ? "open" : "closed"}
|
|
33
|
+
className={className}
|
|
34
|
+
aria-haspopup="dialog"
|
|
35
|
+
aria-expanded={ui.open}
|
|
36
|
+
aria-controls={PANEL_ID}
|
|
37
|
+
aria-label={label(L, "aria.trigger")}
|
|
38
|
+
onClick={ui.toggleCart}
|
|
39
|
+
>
|
|
40
|
+
{content}
|
|
41
|
+
</button>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The dialog done right: click-away overlay (not the close control — put a
|
|
47
|
+
* named `<CartDrawer.Close>` inside), `role="dialog"` + `aria-modal`, focus
|
|
48
|
+
* moved in on open and restored on close. Unmounted while closed by default;
|
|
49
|
+
* pass `keepMounted` to animate the slide and the closed panel is made
|
|
50
|
+
* `inert` for you. Position, size and motion are entirely the store's CSS on
|
|
51
|
+
* `[data-part="panel"]` / `[data-state]`.
|
|
52
|
+
*/
|
|
53
|
+
function Panel({ children, keepMounted = false, className, classes, labels: partLabels }) {
|
|
54
|
+
const L = useResolvedLabels(partLabels);
|
|
55
|
+
const ui = useCartUI();
|
|
56
|
+
const open = ui.open;
|
|
57
|
+
const rootRef = useRef(null);
|
|
58
|
+
const panelRef = useRef(null);
|
|
59
|
+
const restoreRef = useRef(null);
|
|
60
|
+
|
|
61
|
+
// Focus in on open; hand it back on close.
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (open) {
|
|
64
|
+
restoreRef.current = typeof document !== "undefined" ? document.activeElement : null;
|
|
65
|
+
panelRef.current?.focus?.();
|
|
66
|
+
} else if (restoreRef.current) {
|
|
67
|
+
restoreRef.current.focus?.();
|
|
68
|
+
restoreRef.current = null;
|
|
69
|
+
}
|
|
70
|
+
}, [open]);
|
|
71
|
+
|
|
72
|
+
// keepMounted: a closed panel must not be clickable, tab-able or readable.
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!keepMounted || !rootRef.current) return;
|
|
75
|
+
rootRef.current.inert = !open;
|
|
76
|
+
}, [keepMounted, open]);
|
|
77
|
+
|
|
78
|
+
if (!keepMounted && !open) return null;
|
|
79
|
+
return (
|
|
80
|
+
<div ref={rootRef} data-part="drawer" data-state={open ? "open" : "closed"} className={className}>
|
|
81
|
+
<div data-part="overlay" className={classes?.overlay} aria-hidden="true" onClick={ui.closeCart} />
|
|
82
|
+
<aside
|
|
83
|
+
ref={panelRef}
|
|
84
|
+
id={PANEL_ID}
|
|
85
|
+
data-part="panel"
|
|
86
|
+
className={classes?.panel}
|
|
87
|
+
role="dialog"
|
|
88
|
+
aria-modal="true"
|
|
89
|
+
aria-label={label(L, "aria.drawer")}
|
|
90
|
+
tabIndex={-1}
|
|
91
|
+
>
|
|
92
|
+
{children}
|
|
93
|
+
</aside>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The named close control. Children are the store's icon or text. */
|
|
99
|
+
function Close({ children, className, labels: partLabels }) {
|
|
100
|
+
const L = useResolvedLabels(partLabels);
|
|
101
|
+
const ui = useCartUI();
|
|
102
|
+
return (
|
|
103
|
+
<button
|
|
104
|
+
type="button"
|
|
105
|
+
data-part="close"
|
|
106
|
+
className={className}
|
|
107
|
+
aria-label={label(L, "aria.close")}
|
|
108
|
+
onClick={ui.closeCart}
|
|
109
|
+
>
|
|
110
|
+
{children}
|
|
111
|
+
</button>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const CartDrawer = {
|
|
116
|
+
Trigger,
|
|
117
|
+
Panel,
|
|
118
|
+
Close,
|
|
119
|
+
};
|