@shake-defi/react 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +124 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # @shake-defi/react
2
+
3
+ React components for [`@shake-defi/js`](https://www.npmjs.com/package/@shake-defi/js) — a self-contained, embeddable widget for accepting ERC-20 stablecoin payments in exchange for tokens on EVM-compatible chains (Base mainnet / Base Sepolia). This package wraps the vanilla-JS SDK in a `<PlatformProvider>` + `<TokenPurchaseWidget>` pair so it drops cleanly into a React tree.
4
+
5
+ If you're not using React, use [`@shake-defi/js`](https://www.npmjs.com/package/@shake-defi/js) directly.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @shake-defi/react @shake-defi/js
11
+ ```
12
+
13
+ `react` and `react-dom` (>=18) are peer dependencies; `@shake-defi/js` is a direct dependency and doesn't need to be initialized separately.
14
+
15
+ ## How it fits together
16
+
17
+ 1. Your **backend** creates a `TokenPurchaseIntent` (server-side, using your secret key) and returns the `_widget_params` from that response to your frontend.
18
+ 2. Your React app wraps its checkout UI in `<PlatformProvider publishableKey="pk_live_…">`.
19
+ 3. `<TokenPurchaseWidget params={widgetParams} />` renders the actual payment UI and drives wallet connection, approval, and confirmation.
20
+ 4. Your backend receives the final result via **webhook** — treat `onSuccess` as a UX signal, not a fulfillment trigger.
21
+
22
+ See the [`@shake-defi/js` README](https://www.npmjs.com/package/@shake-defi/js) for the backend (customer + intent creation) side of this flow, the full appearance/theming reference, and the error code table — this README covers the React-specific API only.
23
+
24
+ ## Quickstart
25
+
26
+ ```jsx
27
+ import { PlatformProvider, TokenPurchaseWidget } from '@shake-defi/react';
28
+
29
+ function App() {
30
+ return (
31
+ <PlatformProvider publishableKey={process.env.NEXT_PUBLIC_SHAKE_PUBLISHABLE_KEY}>
32
+ <Checkout />
33
+ </PlatformProvider>
34
+ );
35
+ }
36
+
37
+ function Checkout() {
38
+ // widgetParams and intentId come from your backend's
39
+ // POST /token_purchase_intents response (intent._widget_params, intent.id)
40
+ const { widgetParams, intentId } = useCheckoutIntent();
41
+
42
+ return (
43
+ <TokenPurchaseWidget
44
+ params={widgetParams}
45
+ intentId={intentId}
46
+ appearance={{ theme: 'stripe' }}
47
+ onSuccess={({ txHash }) => {
48
+ // Optimistic UI update only — confirm fulfillment via webhook.
49
+ }}
50
+ onError={({ message }) => {
51
+ console.error(message);
52
+ }}
53
+ />
54
+ );
55
+ }
56
+ ```
57
+
58
+ ## API reference
59
+
60
+ ### `<PlatformProvider>`
61
+
62
+ Initializes the SDK once at the root of your tree. Every `<TokenPurchaseWidget>` underneath shares this initialization.
63
+
64
+ ```jsx
65
+ <PlatformProvider
66
+ publishableKey={process.env.NEXT_PUBLIC_SHAKE_PUBLISHABLE_KEY}
67
+ baseUrl={process.env.NEXT_PUBLIC_SHAKE_API_URL}
68
+ >
69
+ {children}
70
+ </PlatformProvider>
71
+ ```
72
+
73
+ | Prop | Type | Required | Description |
74
+ |---|---|---|---|
75
+ | `publishableKey` | `string` | Yes | Your `pk_live_…` / `pk_test_…` key. Safe to expose client-side. |
76
+ | `baseUrl` | `string` | No | Root URL of the Shake DeFi API (e.g. `https://api.shake-defi.com`), matching your key's mode. Only needed if a descendant widget uses the `clientSecret` init pattern — it's **not** your own backend's URL. |
77
+ | `options` | `object` | No | Passed through to the underlying `loadPlatformClient` call (e.g. `walletConnect`). |
78
+ | `children` | `ReactNode` | No | — |
79
+
80
+ A `<TokenPurchaseWidget>` rendered outside a `<PlatformProvider>` throws.
81
+
82
+ ### `<TokenPurchaseWidget>`
83
+
84
+ Renders the payment widget into a `<div>` it manages internally.
85
+
86
+ ```jsx
87
+ <TokenPurchaseWidget
88
+ params={widgetParams} // or clientSecret — not both
89
+ intentId={intent.id}
90
+ appearance={{ theme: 'night' }}
91
+ className="my-widget"
92
+ style={{ maxWidth: 420 }}
93
+ onReady={() => {}}
94
+ onChange={({ state, step }) => {}}
95
+ onSuccess={({ intentId, txHash }) => {}}
96
+ onError={({ code, message, retryable }) => {}}
97
+ onPriceChange={({ oldPrice, newPrice, confirmed, canceled }) => {}}
98
+ />
99
+ ```
100
+
101
+ | Prop | Type | Description |
102
+ |---|---|---|
103
+ | `params` | `object` | The `_widget_params` object from your create-intent response. Use this **or** `clientSecret`. |
104
+ | `clientSecret` | `string` | The `client_secret` from your create-intent response. Requires `baseUrl` (Shake DeFi API root) on the parent `<PlatformProvider>`. |
105
+ | `intentId` | `string` | Echoed back in `onSuccess`. |
106
+ | `appearance` | `object` | `{ theme, variables, rules }` — see the `@shake-defi/js` README for the full theme/variable reference. |
107
+ | `onReady` | `() => void` | Widget has rendered and is interactive. |
108
+ | `onChange` | `({ state, step }) => void` | Fired on every internal state transition. |
109
+ | `onSuccess` | `({ intentId, txHash }) => void` | Purchase confirmed on-chain. |
110
+ | `onError` | `({ code, message, retryable }) => void` | Terminal or retryable error. Codes are documented in the `@shake-defi/js` README. |
111
+ | `onPriceChange` | `({ oldPrice, newPrice, confirmed, canceled }) => void` | Live price moved before confirmation; call `confirmed()` or `canceled()`. |
112
+ | `className` | `string` | Applied to the host `<div>`. |
113
+ | `style` | `React.CSSProperties` | Applied to the host `<div>`. |
114
+
115
+ The component remounts the underlying widget whenever `params` or `clientSecret` changes identity, and calls `.destroy()` automatically on unmount.
116
+
117
+ ## Requirements
118
+
119
+ - React and ReactDOM >=18
120
+ - Node >=18 for build tooling
121
+
122
+ ## License
123
+
124
+ Apache-2.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shake-defi/react",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "React component wrapper for the token purchase widget",
5
5
  "author": "Matthew Anderson",
6
6
  "repository": {
@@ -45,7 +45,7 @@
45
45
  "react-dom": ">=18.0.0"
46
46
  },
47
47
  "dependencies": {
48
- "@shake-defi/js": "^1.0.0"
48
+ "@shake-defi/js": "^1.0.1"
49
49
  },
50
50
  "devDependencies": {
51
51
  "esbuild": "^0.21.0"