@spindlecommerce/storefront-react 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +26 -2
- package/dist/client.d.ts +19 -0
- package/dist/client.js +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/renderer.d.ts +37 -0
- package/dist/renderer.js +32 -0
- package/package.json +46 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The SpindleCommerce authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @spindlecommerce/storefront-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React bindings for [SpindleCommerce](https://spindlecommerce.com) storefronts, on [`@spindlecommerce/storefront-client`](https://www.npmjs.com/package/@spindlecommerce/storefront-client).
|
|
4
|
+
|
|
5
|
+
**Page builder content.** One component per code, with a context of your own. Components may be async Server Components.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { createRenderer, type NodeProps } from "@spindlecommerce/storefront-react";
|
|
9
|
+
|
|
10
|
+
type Own = { tone: "light" | "dark" };
|
|
11
|
+
|
|
12
|
+
const { RenderNode, contextFor } = createRenderer<Own>({
|
|
13
|
+
components: { hero: Hero, rich_text: RichText },
|
|
14
|
+
fallback: ({ node }) => null, // codes the storefront doesn't know yet
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
function Hero({ node, ctx }: NodeProps<HeroNode, Own>) {
|
|
18
|
+
return <section {...ctx.edit.node(node)}>{ctx.renderBlocks(node.fields.actions)}</section>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ctx = contextFor(page, draft, { tone: "light" });
|
|
22
|
+
page.content.fields.sections.map((section) => <RenderNode key={section.id} node={section} ctx={ctx} />);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`spindle-storefront components types` ([`@spindlecommerce/storefront-cli`](https://www.npmjs.com/package/@spindlecommerce/storefront-cli)) generates the node types, such as `HeroNode`.
|
|
26
|
+
|
|
27
|
+
**Variant pickers.** `useVariantSelection(variants)`, from `@spindlecommerce/storefront-react/client`, returns `selection`, `variant`, `choose(attribute, value)` and `available(attribute, value)`.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Selection } from "@spindlecommerce/storefront-client";
|
|
2
|
+
type Variant = {
|
|
3
|
+
isAvailable: boolean;
|
|
4
|
+
selectedOptions: {
|
|
5
|
+
attribute: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The shopper's options, starting from the first variant in stock. `choose` keeps the other
|
|
11
|
+
* choices when that combination exists, and returns the variant chosen.
|
|
12
|
+
*/
|
|
13
|
+
export declare const useVariantSelection: <V extends Variant>(variants: V[]) => {
|
|
14
|
+
selection: Selection;
|
|
15
|
+
variant: V | undefined;
|
|
16
|
+
choose: (attribute: string, value: string) => V | undefined;
|
|
17
|
+
available: (attribute: string, value: string) => boolean;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { chooseValue, inStock, selectionOf, variantFor } from "@spindlecommerce/storefront-client";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* The shopper's options, starting from the first variant in stock. `choose` keeps the other
|
|
6
|
+
* choices when that combination exists, and returns the variant chosen.
|
|
7
|
+
*/
|
|
8
|
+
export const useVariantSelection = (variants) => {
|
|
9
|
+
const [selection, setSelection] = useState(() => selectionOf(variants.find((v) => v.isAvailable) ?? variants[0]));
|
|
10
|
+
const variant = variants.length === 1 ? variants[0] : variantFor(variants, selection);
|
|
11
|
+
const choose = (attribute, value) => {
|
|
12
|
+
const next = chooseValue(variants, selection, attribute, value);
|
|
13
|
+
setSelection(next.selection);
|
|
14
|
+
return next.variant;
|
|
15
|
+
};
|
|
16
|
+
/** Whether choosing this value, keeping the other choices, gives a variant in stock. */
|
|
17
|
+
const available = (attribute, value) => inStock(variants, selection, attribute, value);
|
|
18
|
+
return { selection, variant, choose, available };
|
|
19
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRenderer, type NodeProps, type RenderContext, type Renderer } from "./renderer.tsx";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRenderer } from "./renderer.js";
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type CategoryLink, type ContentNode, type Editable, type ProductSummary } from "@spindlecommerce/storefront-client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
/** What every component gets besides its node. `X` is the storefront's own context. */
|
|
4
|
+
export type RenderContext<X extends object = object> = X & {
|
|
5
|
+
/** Visual editing attributes (empty outside draft mode). */
|
|
6
|
+
edit: Editable;
|
|
7
|
+
draft: boolean;
|
|
8
|
+
/** Objects the document refers to by global id. */
|
|
9
|
+
products: ReadonlyMap<string, ProductSummary>;
|
|
10
|
+
categories: ReadonlyMap<string, CategoryLink>;
|
|
11
|
+
/** Renders a blocks field's nodes, optionally with a changed context. */
|
|
12
|
+
renderBlocks: (nodes: readonly ContentNode[], overrides?: Partial<X>) => ReactNode;
|
|
13
|
+
};
|
|
14
|
+
export type NodeProps<N, X extends object = object> = {
|
|
15
|
+
node: N;
|
|
16
|
+
ctx: RenderContext<X>;
|
|
17
|
+
};
|
|
18
|
+
/** A component for one code. Server Components may be async. */
|
|
19
|
+
export type Renderer<N, X extends object = object> = (props: NodeProps<N, X>) => ReactNode | Promise<ReactNode>;
|
|
20
|
+
type Document = {
|
|
21
|
+
references: {
|
|
22
|
+
products: ProductSummary[];
|
|
23
|
+
categories: CategoryLink[];
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Renders builder content with one component per code. `fallback` renders codes without one, such
|
|
28
|
+
* as a component added in the admin after the storefront was built.
|
|
29
|
+
*/
|
|
30
|
+
export declare const createRenderer: <X extends object>(renderers: {
|
|
31
|
+
components: Partial<Record<string, Renderer<never, X>>>;
|
|
32
|
+
fallback: Renderer<ContentNode, X>;
|
|
33
|
+
}) => {
|
|
34
|
+
RenderNode: ({ node, ctx }: NodeProps<ContentNode, X>) => import("react").JSX.Element;
|
|
35
|
+
contextFor: (document: Document, draft: boolean, own: X) => RenderContext<X>;
|
|
36
|
+
};
|
|
37
|
+
export {};
|
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { editable } from "@spindlecommerce/storefront-client";
|
|
3
|
+
/**
|
|
4
|
+
* Renders builder content with one component per code. `fallback` renders codes without one, such
|
|
5
|
+
* as a component added in the admin after the storefront was built.
|
|
6
|
+
*/
|
|
7
|
+
export const createRenderer = (renderers) => {
|
|
8
|
+
function RenderNode({ node, ctx }) {
|
|
9
|
+
// The API delivers fields in the shape the component's generated type describes.
|
|
10
|
+
const Component = (renderers.components[node.component] ?? renderers.fallback);
|
|
11
|
+
return _jsx(Component, { node: node, ctx: ctx });
|
|
12
|
+
}
|
|
13
|
+
const withRenderer = (base) => {
|
|
14
|
+
const ctx = {
|
|
15
|
+
...base,
|
|
16
|
+
renderBlocks: (nodes, overrides) => {
|
|
17
|
+
const child = overrides ? withRenderer({ ...base, ...overrides }) : ctx;
|
|
18
|
+
return nodes.map((node) => _jsx(RenderNode, { node: node, ctx: child }, node.id));
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
return ctx;
|
|
22
|
+
};
|
|
23
|
+
/** The context for one document: a page, a template, the header or the footer. */
|
|
24
|
+
const contextFor = (document, draft, own) => withRenderer({
|
|
25
|
+
...own,
|
|
26
|
+
edit: editable(draft),
|
|
27
|
+
draft,
|
|
28
|
+
products: new Map(document.references.products.map((p) => [p.id, p])),
|
|
29
|
+
categories: new Map(document.references.categories.map((c) => [c.id, c])),
|
|
30
|
+
});
|
|
31
|
+
return { RenderNode, contextFor };
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spindlecommerce/storefront-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React bindings for SpindleCommerce storefronts: render page-builder content and pick variants.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"spindlecommerce",
|
|
7
|
+
"storefront"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/spindlecommerce/storefront-sdk/tree/main/packages/storefront-react#readme",
|
|
10
|
+
"bugs": "https://github.com/spindlecommerce/storefront-sdk/issues",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/spindlecommerce/storefront-sdk.git",
|
|
14
|
+
"directory": "packages/storefront-react"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./client": {
|
|
25
|
+
"types": "./dist/client.d.ts",
|
|
26
|
+
"default": "./dist/client.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@spindlecommerce/storefront-client": "^0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": "^19.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/react": "19.3.0",
|
|
43
|
+
"react": "19.3.1"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
47
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
48
|
+
}
|
|
6
49
|
}
|