@lime-bundles/react 0.1.1 → 1.0.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 +132 -0
- package/dist/index.cjs +329 -230
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -14
- package/dist/index.d.ts +74 -14
- package/dist/index.js +315 -217
- package/dist/index.js.map +1 -1
- package/package.json +3 -7
- package/dist/hooks/useCart.cjs +0 -84
- package/dist/hooks/useCart.cjs.map +0 -1
- package/dist/hooks/useCart.d.cts +0 -19
- package/dist/hooks/useCart.d.ts +0 -19
- package/dist/hooks/useCart.js +0 -64
- package/dist/hooks/useCart.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @lime-bundles/react
|
|
2
|
+
|
|
3
|
+
React components and hooks for Lime Bundles on Hydrogen, Next.js, Vite, Remix, or any React renderer. BYO-cart: you pass an `onAddToCart` callback; Lime Bundles renders the UI and fires analytics.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lime-bundles/react @lime-bundles/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependencies: `react >=18`, `react-dom >=18`.
|
|
12
|
+
|
|
13
|
+
## Components
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { FixedBundle, VolumeBundle, MixMatchBundle } from "@lime-bundles/react";
|
|
17
|
+
|
|
18
|
+
<FixedBundle
|
|
19
|
+
shopDomain="my-shop.myshopify.com"
|
|
20
|
+
storefrontAccessToken={process.env.NEXT_PUBLIC_LIME_BUNDLES_TOKEN!}
|
|
21
|
+
bundleGid="gid://shopify/Metaobject/42"
|
|
22
|
+
onAddToCart={async (lines) => {
|
|
23
|
+
await fetch("/api/cart/add", { method: "POST", body: JSON.stringify(lines) });
|
|
24
|
+
}}
|
|
25
|
+
onError={(err) => console.error(err)}
|
|
26
|
+
/>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
All three components share the same `BundleComponentProps` shape:
|
|
30
|
+
|
|
31
|
+
| Prop | Type | Required | Notes |
|
|
32
|
+
|---|---|:-:|---|
|
|
33
|
+
| `shopDomain` | `string` | ✓ | Your shop domain. |
|
|
34
|
+
| `storefrontAccessToken` | `string` | ✓ | Public Storefront Access Token. |
|
|
35
|
+
| `bundleGid` | `string` | ✓ | Bundle metaobject GID. |
|
|
36
|
+
| `onAddToCart` | `(lines: CartLineInput[]) => Promise<void>` | ✓ | Must return a Promise. Components `await` it for loading states. |
|
|
37
|
+
| `appUrl` | `string` | | Required only if `analyticsEnabled !== false`. |
|
|
38
|
+
| `analyticsEnabled` | `boolean` | | Default `true`. Set `false` to disable impression / add-to-cart POSTs. |
|
|
39
|
+
| `onError` | `(error: Error) => void` | | Fires on `StorefrontApiError`, `BundleParseError`, or errors thrown from `onAddToCart`. |
|
|
40
|
+
| `locale` | `string` | | BCP-47 locale tag. |
|
|
41
|
+
| `className` | `string` | | Applied to the outer wrapper. |
|
|
42
|
+
|
|
43
|
+
The `CartLineInput` shape matches Shopify Hydrogen's. A structural-compat type test guards this across versions.
|
|
44
|
+
|
|
45
|
+
## Hooks
|
|
46
|
+
|
|
47
|
+
### `useBundleData`
|
|
48
|
+
|
|
49
|
+
Client-side hook. Returns a discriminated union locked at v1.0.0:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { useBundleData } from "@lime-bundles/react";
|
|
53
|
+
|
|
54
|
+
function MyBundle({ bundleGid }: { bundleGid: string }) {
|
|
55
|
+
const result = useBundleData({
|
|
56
|
+
shopDomain: "my-shop.myshopify.com",
|
|
57
|
+
storefrontAccessToken: TOKEN,
|
|
58
|
+
bundleGid,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (result.status === "loading") return <Skeleton />;
|
|
62
|
+
if (result.status === "error") return <Error message={result.error.message} />;
|
|
63
|
+
|
|
64
|
+
// result.status === "success": result.bundle is a ParsedBundle
|
|
65
|
+
return <pre>{JSON.stringify(result.bundle, null, 2)}</pre>;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Fetches custom CSS in parallel and injects a scoped `<style>` tag on success. No action needed from you.
|
|
70
|
+
|
|
71
|
+
Unmounting or changing the bundle GID cancels the in-flight request via `AbortController`.
|
|
72
|
+
|
|
73
|
+
## Async fetcher for RSC / SSR
|
|
74
|
+
|
|
75
|
+
Use `fetchBundleData` in a server component, Next.js `getServerSideProps`, or any non-React async context:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { fetchBundleData } from "@lime-bundles/react";
|
|
79
|
+
|
|
80
|
+
const bundle = await fetchBundleData({
|
|
81
|
+
shopDomain: "my-shop.myshopify.com",
|
|
82
|
+
storefrontAccessToken: process.env.LIME_BUNDLES_TOKEN!,
|
|
83
|
+
bundleGid: "gid://shopify/Metaobject/42",
|
|
84
|
+
buyerIp: request.headers.get("x-forwarded-for")?.split(",")[0].trim() ?? undefined,
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`buyerIp` is required on SSR. Shopify may return `430 Security Rejection` for server-originated traffic on a private token without it. Pull the real client IP from whichever header your host sets (`x-forwarded-for` on Oxygen/Vercel/most hosts, `cf-connecting-ip` on Cloudflare, etc).
|
|
89
|
+
|
|
90
|
+
Throws:
|
|
91
|
+
- `StorefrontApiError`: network, HTTP, or GraphQL errors.
|
|
92
|
+
- `BundleParseError`: metaobject missing, invalid type, inactive, not started, or expired. `error.reason` is a typed literal.
|
|
93
|
+
|
|
94
|
+
## The cart contract
|
|
95
|
+
|
|
96
|
+
`onAddToCart` receives Hydrogen-compatible cart lines:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
type CartLineInput = {
|
|
100
|
+
merchandiseId: string; // variant GID
|
|
101
|
+
quantity: number;
|
|
102
|
+
attributes: Array<{ key: string; value: string }>;
|
|
103
|
+
};
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Always preserve `attributes[].{key: "_lime_bundle_gid"}` on its way to Shopify. The `orders/create` webhook uses it for purchase attribution.
|
|
107
|
+
|
|
108
|
+
## Styling
|
|
109
|
+
|
|
110
|
+
Override CSS custom properties on any wrapping element:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
<div style={{ "--lb-primary-color": "#e91e63" } as React.CSSProperties}>
|
|
114
|
+
<FixedBundle ... />
|
|
115
|
+
</div>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Full reference: [css-variables.md](https://github.com/lime-app-dev/lime-bundles-app/blob/main/docs/headless/css-variables.md).
|
|
119
|
+
|
|
120
|
+
## Versioning
|
|
121
|
+
|
|
122
|
+
Major versions bump together across `core`, `react`, and `widget`. The hook's `UseBundleDataResult` type and `BundleComponentProps` are locked at v1.0.0.
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT. See repo root.
|
|
127
|
+
|
|
128
|
+
## Links
|
|
129
|
+
|
|
130
|
+
- [Hydrogen guide](https://github.com/lime-app-dev/lime-bundles-app/blob/main/docs/headless/hydrogen.md)
|
|
131
|
+
- [React / Next.js guide](https://github.com/lime-app-dev/lime-bundles-app/blob/main/docs/headless/react-nextjs.md)
|
|
132
|
+
- [Report issues](https://github.com/lime-app-dev/lime-bundles-app/issues)
|