@godaddy/gd-commerce-storefront 0.1.3 → 0.3.0

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 CHANGED
@@ -84,9 +84,9 @@ A connection failure leaves the surrounding application and its state mounted. C
84
84
 
85
85
  The stylesheet includes all required utilities and scopes them to the package's surfaces. The build removes CSS layer wrappers in their declared order, so the exported CSS can pass through a host Tailwind v3 PostCSS pipeline without `@tailwind` directives. Import it directly; consumers do not need to copy or rewrite the CSS. It does not add a global reset or require dependency scanning by a host Tailwind build. The `theme` prop reaches the drawer even though it is portalled into `document.body`.
86
86
 
87
- By default, `.commerce-storefront` wrappers do not paint a background and inherit the host's text color. Set page and section backgrounds on the host application's layout elements. `--commerce-surface` colors controls and the opaque cart drawer, and supplies the base for muted colors; it does not color component wrappers. Set `--commerce-surface` and `--commerce-text` together to adapt controls and the drawer to a dark theme. Avoid setting wrapper backgrounds through CSS or the `theme` prop's `background`, `backgroundColor`, or `backgroundImage` properties, which can still override the transparent default. Descendants inherit the surface text and body font instead of global heading styles. Keep text, controls and focus indicators accessible when changing colors. Utility class names and internal markup are not a customization API.
87
+ By default, `.commerce-storefront` wrappers do not paint a background and inherit the host's text color. Set page and section backgrounds on the host application's layout elements. `--commerce-surface` colors controls and the opaque cart drawer, and supplies the base for muted colors; it does not color component wrappers. Set `--commerce-surface` and `--commerce-text` together to adapt controls and the drawer to a dark theme. Avoid setting wrapper backgrounds through CSS or the `theme` prop's `background`, `backgroundColor`, or `backgroundImage` properties, which can still override the transparent default. Descendants inherit the surface text and body font instead of global heading styles. Keep text, controls and focus indicators accessible when changing colors. Internal utility class names and markup are not a customization API; use the [product parts](#custom-product-layouts) to change a product page's layout.
88
88
 
89
- The first release uses English UI text and `en-US` currency formatting. Catalog title and description are configurable. Full localization and arbitrary component slots are outside this initial API.
89
+ The first release uses English UI text and `en-US` currency formatting. Catalog title and description are configurable. Full localization is outside this initial API.
90
90
 
91
91
  ## Compatibility
92
92
 
@@ -101,6 +101,7 @@ The repository example uses React Router 7. Follow each router version's own Rea
101
101
  | `CommerceStorefront` | Recommended integration: provider and one drawer |
102
102
  | `Catalog` | Six products per cursor page, with `title`, `description`, and `showHeader` props |
103
103
  | `ProductDetails` | Reads `:productId`; validates URL option selections against catalog results |
104
+ | `ProductDetailsRoot` and product parts | Build a [custom product layout](#custom-product-layouts) with the same rules as `ProductDetails` |
104
105
  | `ProductCard` | Renders one `SKUGroup` with direct add or a product-details link |
105
106
  | `CartButton` | Opens the shared drawer and displays item count |
106
107
  | `AddToCartButton` | Adds a verified `sku`, `name`, and optional integer `quantity` |
@@ -112,6 +113,71 @@ The package exports TypeScript catalog/cart response types and selection/summary
112
113
 
113
114
  `Catalog` renders its `title` as an H1 by default. When the host page owns its semantic heading, render that page H1 and pass `showHeader={false}` so the document still has exactly one H1.
114
115
 
116
+ ## Custom product layouts
117
+
118
+ `ProductDetails` is the recommended product page. When its layout does not fit, compose the same parts inside `ProductDetailsRoot`. The root loads the route's `:productId` and shows the connection, loading, error and not-found states itself, so parts render only once the product exists. This is the packaged layout, written out:
119
+
120
+ ```tsx
121
+ import {
122
+ ProductDetailsBackLink, ProductDetailsDescription, ProductDetailsGallery, ProductDetailsPrice,
123
+ ProductDetailsPurchase, ProductDetailsRoot, ProductDetailsTitle,
124
+ } from '@godaddy/gd-commerce-storefront';
125
+
126
+ <ProductDetailsRoot>
127
+ <ProductDetailsBackLink className='mb-8 sm:mb-10' />
128
+ <div className='grid items-start gap-8 lg:grid-cols-12 lg:gap-12'>
129
+ <ProductDetailsGallery className='min-w-0 lg:col-span-7' />
130
+ <div className='min-w-0 lg:col-span-5 lg:py-4'>
131
+ <ProductDetailsTitle />
132
+ <ProductDetailsPrice className='mt-6' />
133
+ <ProductDetailsDescription className='my-8' />
134
+ <ProductDetailsPurchase className='border-t border-neutral-200 pt-8' />
135
+ </div>
136
+ </div>
137
+ </ProductDetailsRoot>
138
+ ```
139
+
140
+ The grid, spacing and border classes come from your own stylesheet in a custom layout. Without them, the parts stack in one column with no space between them.
141
+
142
+ Add your own elements between parts, reorder them, or leave out display parts. `ProductDetailsPurchase` renders the purchase parts below in order. Replace it with individual parts to place options apart from the add button:
143
+
144
+ | Part | Renders |
145
+ | --- | --- |
146
+ | `ProductDetailsOptions` | A select for every attribute, or the SKU list for products without attributes |
147
+ | `ProductDetailsOption` | A select for one attribute, by `attribute` name; nothing when the product lacks it |
148
+ | `ProductDetailsPurchaseStatus` | The one message explaining why the selection is not purchasable yet; nothing when ready |
149
+ | `ProductDetailsAvailability`, `ProductDetailsQuantity` | Stock and quantity for the verified SKU; nothing before verification |
150
+ | `ProductDetailsAddToCart` | Adds the verified SKU and quantity; before a SKU is verified, its accessible description gives the reason |
151
+
152
+ Every part is named `ProductDetails*` because it works only inside `ProductDetailsRoot`. `AddToCartButton` is the standalone button for a SKU you already have.
153
+
154
+ Parts read state from the root and take no product, SKU, price or quantity props. Whatever the layout, only a server-verified SKU can be added, and changing a selection resets the quantity and image. Every part throws if it is rendered outside `ProductDetailsRoot`. In development builds, if no control is rendered for an attribute the product requires, a console warning names the missing ones. A control that mounts later, such as one in a closed accordion, can trigger it; the product becomes purchasable once the control renders. Products without attributes but with several SKUs need `ProductDetailsOptions` or `ProductDetailsPurchase`. Parts can be rendered more than once, such as in a sticky mobile purchase bar; each copy gets its own element IDs.
155
+
156
+ Every part accepts `className`, which is added to the part's outer element, a wrapper the package's CSS reset leaves alone. Host classes there control margin, padding, border, color, width and grid placement, including layered utilities such as Tailwind v4's. The packaged layout sets its spacing and grid placement this way. Elements inside the part, such as the title's `<h1>`, keep the package's own styles: a host font size on `ProductDetailsTitle` does not change the heading's size, but a host color does reach it, because the heading inherits its color.
157
+
158
+ Two hooks support custom parts inside the root:
159
+
160
+ - `useProductDetails()` returns read-only `product`, `name`, `selectedSku`, `quantity`, `availableQuantity`, `images` and `status` (`incomplete`, `checking`, `variant-error`, `unavailable`, `not-purchasable` or `ready`). `selectedSku` is set only when `status` is `ready`.
161
+ - `useProductDetailsOption(attribute)` returns `{ name, label, selected, values, select }` for building swatches or buttons, or `null` when the product has no such attribute. `select(value)` accepts only that attribute's value names, or `null` to clear. Other values are ignored. A hook-based control counts toward the layout check.
162
+
163
+ ```tsx
164
+ function ColorSwatches() {
165
+ const color = useProductDetailsOption('color');
166
+ if (!color) return null;
167
+ return (
168
+ <div role='group' aria-label={color.label}>
169
+ {color.values.map((value) => (
170
+ <button key={value.name} type='button' aria-pressed={value.selected} onClick={() => color.select(value.name)}>
171
+ {value.label}
172
+ </button>
173
+ ))}
174
+ </div>
175
+ );
176
+ }
177
+ ```
178
+
179
+ Parts are flat named exports, not properties such as `ProductDetails.Title`, so each one is an ordinary module export.
180
+
115
181
  ## How it works
116
182
 
117
183
  Products are SKU groups. The package purchases only an unambiguous SKU, verifies all selected attributes through the server, uses SKU prices/images, and distinguishes untracked inventory from sold-out inventory. Server-side inventory and pricing checks remain mandatory.
@@ -144,3 +210,45 @@ Build before testing: artifact tests check the compiled JavaScript and shipped C
144
210
  ## License
145
211
 
146
212
  [MIT](LICENSE.md).
213
+
214
+ ### Before a store is connected
215
+
216
+ A site can mount the same components before it has a connected store:
217
+
218
+ ```tsx
219
+ <CommerceStorefront theme={theme}>
220
+ <Header><CartButton /></Header>
221
+ <Catalog sampleProducts />
222
+ </CommerceStorefront>
223
+ ```
224
+
225
+ `Catalog.sampleProducts` defaults to `false`. It allows six built-in sample products
226
+ (Product 1–6, $10–$60 USD) **only** when `/api/commerce/config` reports `state: 'unbound'`.
227
+ When a store is connected, the server reports a different state and the components show real
228
+ products. You do not need to change props or the component tree. The prop has no effect while
229
+ a store is connected. Real empty catalogs and errors never fall back to samples.
230
+
231
+ The sample cart supports adding items, changing quantities (1–999), removing items, and an
232
+ example subtotal. It uses the existing drawer and theme, keeps its items during navigation under
233
+ the shared provider, and resets on reload, unmount, or any change away from `unbound`. It uses no
234
+ live cart storage and makes no product, cart, checkout, or upstream Commerce requests.
235
+ Configuration reads continue. Sample entries are not SKUs or draft orders and never move into a
236
+ real cart. Checkout is unavailable, including direct `useCommerce().checkout()` calls.
237
+
238
+ `useCommerce().connection` now includes `unbound` and `connecting` in addition to `loading`,
239
+ `ready`, and `error`. Only `ready` means a usable store connection. The public `cart` and live
240
+ mutation methods apply only to the connected store; sample cart data is internal. A catalog
241
+ without `sampleProducts`, and direct product-detail visits, show the unconnected state.
242
+ Sample grids and drawers have the attribute `data-commerce-source="sample"`.
243
+
244
+ The provider reads configuration on mount and focus, and every five seconds while the page is
245
+ visible and the state is `unbound` or `connecting`. The first failed refresh hides samples and
246
+ clears the sample cart; a later `unbound` response starts an empty sample cart. While
247
+ connecting, refresh failures keep the connecting state for up to 60 seconds before showing
248
+ the error. Polling stops
249
+ when the state is `ready`; focus and explicit retry still refresh configuration.
250
+
251
+ Use a server that reports connection state before you enable `sampleProducts`. Successful
252
+ config responses without `state` still mean ready. A server that does not report `unbound`
253
+ never shows samples. See the [server contract](docs/server-api.md) for the host's
254
+ responsibilities.
package/dist/index.d.ts CHANGED
@@ -325,12 +325,14 @@ interface CatalogProps {
325
325
  title?: string;
326
326
  description?: string;
327
327
  showHeader?: boolean;
328
+ /** Permit built-in examples only while the host confirms this site is unbound. */
329
+ sampleProducts?: boolean;
328
330
  }
329
331
  declare function Catalog(props: CatalogProps): ReactElement;
330
332
  //#endregion
331
333
  //#region src/commerce-provider.d.ts
332
334
  interface CommerceContextValue {
333
- connection: 'loading' | 'ready' | 'error';
335
+ connection: 'loading' | 'unbound' | 'connecting' | 'ready' | 'error';
334
336
  connectionError: string | null;
335
337
  retryConnection: () => void;
336
338
  theme: StorefrontTheme;
@@ -385,10 +387,115 @@ declare function CommerceStorefront({
385
387
  ...props
386
388
  }: CommerceProviderProps): react_jsx_runtime0.JSX.Element;
387
389
  //#endregion
390
+ //#region src/product-details-context.d.ts
391
+ /** Where the shopper is in choosing a purchasable SKU. Only `ready` has a verified `selectedSku`. */
392
+ type ProductDetailsStatus = 'incomplete' | 'checking' | 'variant-error' | 'unavailable' | 'not-purchasable' | 'ready';
393
+ /** Read-only product state for custom parts rendered inside `ProductDetailsRoot`. */
394
+ interface ProductDetailsState {
395
+ readonly product: SKUGroup;
396
+ readonly name: string;
397
+ /** Set only after the server verifies the full selection. */
398
+ readonly selectedSku: SKU | null;
399
+ readonly quantity: number;
400
+ /** `null` means inventory is not tracked; `0` means sold out. */
401
+ readonly availableQuantity: number | null;
402
+ readonly status: ProductDetailsStatus;
403
+ readonly images: readonly string[];
404
+ }
405
+ /** Props shared by every product part. `className` is added to the part's outer element. */
406
+ interface ProductDetailsPartProps {
407
+ className?: string;
408
+ }
409
+ /** Read the current product, verified SKU, and purchase status from inside `ProductDetailsRoot`. */
410
+ declare function useProductDetails(): ProductDetailsState;
411
+ interface ProductDetailsOptionValueState {
412
+ readonly name: string;
413
+ readonly label: string;
414
+ readonly selected: boolean;
415
+ }
416
+ interface ProductDetailsOptionState {
417
+ readonly name: string;
418
+ readonly label: string;
419
+ readonly selected: string | null;
420
+ readonly values: readonly ProductDetailsOptionValueState[];
421
+ /** Accepts only this attribute's own value names, or `null` to clear. Other values are ignored. */
422
+ readonly select: (value: string | null) => void;
423
+ }
424
+ /**
425
+ * Build a custom control for one product attribute. Returns `null` when the product has no such attribute.
426
+ * A selection is never purchasable until the server verifies it.
427
+ */
428
+ declare function useProductDetailsOption(attribute: string): ProductDetailsOptionState | null;
429
+ interface ProductDetailsRootProps {
430
+ children: ReactNode;
431
+ /** Added to the root `<article>`. */
432
+ className?: string;
433
+ }
434
+ /** Loads the product for the route's `:productId` and provides state to product parts. */
435
+ declare function ProductDetailsRoot({
436
+ children,
437
+ className
438
+ }: ProductDetailsRootProps): ReactElement;
439
+ //#endregion
388
440
  //#region src/product-details.d.ts
441
+ /** The packaged product page for `${productPath}/:productId`. Compose the parts for a custom layout. */
389
442
  declare function ProductDetails(): ReactElement;
443
+ declare function ProductDetailsBackLink({
444
+ className
445
+ }: ProductDetailsPartProps): ReactElement;
446
+ /** Main image with thumbnails; the selected SKU's images replace the product's when present. */
447
+ declare function ProductDetailsGallery({
448
+ className
449
+ }: ProductDetailsPartProps): ReactElement;
450
+ /** The product name as the page's H1. */
451
+ declare function ProductDetailsTitle({
452
+ className
453
+ }: ProductDetailsPartProps): ReactElement;
454
+ /** The verified SKU's price and compare-at price, or the product's starting price. */
455
+ declare function ProductDetailsPrice({
456
+ className
457
+ }: ProductDetailsPartProps): ReactElement;
458
+ /** The verified SKU's description, or the product's. */
459
+ declare function ProductDetailsDescription({
460
+ className
461
+ }: ProductDetailsPartProps): ReactElement;
462
+ //#endregion
463
+ //#region src/product-purchase.d.ts
464
+ /** The packaged purchase area: options, status, availability, quantity, and add to cart. */
465
+ declare function ProductDetailsPurchase({
466
+ className
467
+ }: ProductDetailsPartProps): ReactElement;
468
+ /** A select for every product attribute, or the SKU list for products without attributes. */
469
+ declare function ProductDetailsOptions({
470
+ className
471
+ }: ProductDetailsPartProps): ReactElement | null;
472
+ interface ProductDetailsOptionProps extends ProductDetailsPartProps {
473
+ /** The attribute `name`, such as `color`. Renders nothing when the product has no such attribute. */
474
+ attribute: string;
475
+ }
476
+ /** A select for one product attribute. Use `useProductDetailsOption` for swatches or other custom controls. */
477
+ declare function ProductDetailsOption({
478
+ attribute,
479
+ className
480
+ }: ProductDetailsOptionProps): ReactElement | null;
481
+ /** The single message explaining why the selection is not yet purchasable. Renders nothing when ready. */
482
+ declare function ProductDetailsPurchaseStatus({
483
+ className
484
+ }: ProductDetailsPartProps): ReactElement | null;
485
+ /** Stock for the verified SKU. Renders nothing until a SKU is verified. */
486
+ declare function ProductDetailsAvailability({
487
+ className
488
+ }: ProductDetailsPartProps): ReactElement | null;
489
+ /** Quantity for the verified SKU. Renders nothing until a SKU is verified. */
490
+ declare function ProductDetailsQuantity({
491
+ className
492
+ }: ProductDetailsPartProps): ReactElement | null;
493
+ /** Adds the verified SKU and quantity. While disabled, its accessible description says why. */
494
+ declare function ProductDetailsAddToCart({
495
+ className
496
+ }: ProductDetailsPartProps): ReactElement;
390
497
  //#endregion
391
498
  //#region src/storefront-surface.d.ts
392
499
  declare function CommerceStatus(): ReactElement | null;
393
500
  //#endregion
394
- export { AddToCartButton, AddToCartItemInput, ApiError, CartAddress, CartButton, CartDiscount, CartDrawer, CartLineItem, CartLineItemDetails, CartNote, CartOrder, CartSelectedAddon, CartSelectedAddonValue, CartSelectedOption, CartShippingInfo, CartSummaryTotals, CartTax, Catalog, type CatalogProps, type CommerceContextValue, CommerceProvider, type CommerceProviderProps, CommerceStatus, CommerceStorefront, Connection, ConnectionEdge, DraftOrderContext, InventoryCount, LabelFilter, LineItemTotals, MediaObject, Money, OrderTotals, PageInfo, PriceRange, ProductCard, ProductDetails, ProductDetailsVariablesInput, ProductGridVariablesInput, SKU, SKUAttributeValue, SKUGroup, SKUGroupAttribute, SKUGroupAttributeValue, SKUGroupSKU, SKUPrice, SkuGroupResult, SkuGroupVariables, SkuGroupsResult, SkuGroupsVariables, SkuResult, SkuVariables, type StorefrontConfig, StorefrontProductAttribute, StorefrontProductAttributeValue, type StorefrontTheme, StringInFilter, addToCart, getAvailableInventoryQuantity, getCartSummaryTotals, getImageUrls, getLabeledSkuOptions, getPrimaryImageUrl, getProductAttributes, getSingleMatchedSku, getSingleMatchedSkuId, money, useCommerce };
501
+ export { AddToCartButton, AddToCartItemInput, ApiError, CartAddress, CartButton, CartDiscount, CartDrawer, CartLineItem, CartLineItemDetails, CartNote, CartOrder, CartSelectedAddon, CartSelectedAddonValue, CartSelectedOption, CartShippingInfo, CartSummaryTotals, CartTax, Catalog, type CatalogProps, type CommerceContextValue, CommerceProvider, type CommerceProviderProps, CommerceStatus, CommerceStorefront, Connection, ConnectionEdge, DraftOrderContext, InventoryCount, LabelFilter, LineItemTotals, MediaObject, Money, OrderTotals, PageInfo, PriceRange, ProductCard, ProductDetails, ProductDetailsAddToCart, ProductDetailsAvailability, ProductDetailsBackLink, ProductDetailsDescription, ProductDetailsGallery, ProductDetailsOption, type ProductDetailsOptionProps, type ProductDetailsOptionState, type ProductDetailsOptionValueState, ProductDetailsOptions, type ProductDetailsPartProps, ProductDetailsPrice, ProductDetailsPurchase, ProductDetailsPurchaseStatus, ProductDetailsQuantity, ProductDetailsRoot, type ProductDetailsRootProps, type ProductDetailsState, type ProductDetailsStatus, ProductDetailsTitle, ProductDetailsVariablesInput, ProductGridVariablesInput, SKU, SKUAttributeValue, SKUGroup, SKUGroupAttribute, SKUGroupAttributeValue, SKUGroupSKU, SKUPrice, SkuGroupResult, SkuGroupVariables, SkuGroupsResult, SkuGroupsVariables, SkuResult, SkuVariables, type StorefrontConfig, StorefrontProductAttribute, StorefrontProductAttributeValue, type StorefrontTheme, StringInFilter, addToCart, getAvailableInventoryQuantity, getCartSummaryTotals, getImageUrls, getLabeledSkuOptions, getPrimaryImageUrl, getProductAttributes, getSingleMatchedSku, getSingleMatchedSkuId, money, useCommerce, useProductDetails, useProductDetailsOption };