@openzeppelin/ui-react 1.2.0 → 2.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 +53 -58
- package/dist/index.cjs +269 -189
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -50
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +61 -50
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +267 -187
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -25,74 +25,74 @@ pnpm add react react-dom @tanstack/react-query
|
|
|
25
25
|
|
|
26
26
|
## Overview
|
|
27
27
|
|
|
28
|
-
This package provides core React context providers and hooks that centralize the management of global wallet state, active network selection,
|
|
28
|
+
This package provides core React context providers and hooks that centralize the management of global wallet state, active network selection, **`EcosystemRuntime` instances** (capability bundles per network), and ecosystem-specific UI kit hooks.
|
|
29
29
|
|
|
30
|
-
It is a foundational package that can be used to ensure consistent wallet and
|
|
30
|
+
It is a foundational package that can be used to ensure consistent wallet and runtime integration patterns across applications.
|
|
31
31
|
|
|
32
32
|
## Core Responsibilities
|
|
33
33
|
|
|
34
|
-
- **
|
|
35
|
-
- **Global
|
|
36
|
-
-
|
|
37
|
-
- The active `
|
|
38
|
-
-
|
|
39
|
-
- Orchestration of
|
|
40
|
-
- **Consistent
|
|
34
|
+
- **Runtime instance management:** `RuntimeProvider` keeps a registry of `EcosystemRuntime` instances (one per network id, with disposal on teardown), using a `resolveRuntime` function you supply (typically delegating to an adapter package’s `ecosystemDefinition.createRuntime`).
|
|
35
|
+
- **Global wallet / network state:** `WalletStateProvider` manages:
|
|
36
|
+
- Globally selected network id and `NetworkConfig`
|
|
37
|
+
- The active `EcosystemRuntime` and its loading state (`isRuntimeLoading`)
|
|
38
|
+
- `EcosystemSpecificReactHooks` from the active runtime’s UI kit
|
|
39
|
+
- Orchestration of the active ecosystem’s React UI provider (when configured)
|
|
40
|
+
- **Consistent access:** Hooks expose this state to components.
|
|
41
41
|
|
|
42
42
|
## Key Exports
|
|
43
43
|
|
|
44
44
|
### Providers
|
|
45
45
|
|
|
46
|
-
-
|
|
47
|
-
-
|
|
46
|
+
- **`RuntimeProvider`**: Registry + loading state; requires `resolveRuntime: (networkConfig) => Promise<EcosystemRuntime>`.
|
|
47
|
+
- **`WalletStateProvider`**: Global active network / runtime / wallet facade hooks; requires `getNetworkConfigById` and optionally `loadConfigModule` for UI kit config modules.
|
|
48
48
|
|
|
49
49
|
### Hooks
|
|
50
50
|
|
|
51
|
-
-
|
|
52
|
-
-
|
|
51
|
+
- **`useRuntimeContext()`**: Low-level access to `getRuntimeForNetwork` / loading helpers from `RuntimeProvider`.
|
|
52
|
+
- **`useWalletState()`**: `activeNetworkId`, `activeNetworkConfig`, **`activeRuntime`**, **`isRuntimeLoading`**, `walletFacadeHooks`, `setActiveNetworkId`, `reconfigureActiveUiKit`.
|
|
53
53
|
|
|
54
|
-
### Derived
|
|
54
|
+
### Derived hooks
|
|
55
55
|
|
|
56
|
-
These
|
|
56
|
+
These abstract wallet interactions across ecosystems:
|
|
57
57
|
|
|
58
|
-
- `useDerivedAccountStatus()
|
|
59
|
-
- `useDerivedSwitchChainStatus()`: Returns the `switchChain` function and switching state.
|
|
60
|
-
- `useDerivedChainInfo()`: Returns current chain information.
|
|
61
|
-
- `useDerivedConnectStatus()`: Returns wallet connection functions and state.
|
|
62
|
-
- `useDerivedDisconnect()`: Returns the disconnect function.
|
|
63
|
-
- `useWalletReconnectionHandler()`: Detects wallet reconnection and triggers network switch re-queue.
|
|
58
|
+
- `useDerivedAccountStatus()`, `useDerivedSwitchChainStatus()`, `useDerivedChainInfo()`, `useDerivedConnectStatus()`, `useDerivedDisconnect()`, `useWalletReconnectionHandler()`.
|
|
64
59
|
|
|
65
|
-
### UI
|
|
60
|
+
### UI components
|
|
66
61
|
|
|
67
|
-
- `WalletConnectionHeader
|
|
68
|
-
-
|
|
69
|
-
- `WalletConnectionWithSettings`: Wallet connection UI with settings controls.
|
|
70
|
-
- `NetworkSwitchManager`: Handles automatic wallet network switching for EVM chains.
|
|
62
|
+
- `WalletConnectionHeader`, `WalletConnectionUI`, `WalletConnectionWithSettings`
|
|
63
|
+
- **`NetworkSwitchManager`**: Pass **`wallet`** and **`networkCatalog`** from the active runtime (see below), plus `targetNetworkId` and optional `onNetworkSwitchComplete`.
|
|
71
64
|
|
|
72
65
|
## Usage
|
|
73
66
|
|
|
74
|
-
### Application
|
|
67
|
+
### Application setup
|
|
75
68
|
|
|
76
69
|
```tsx
|
|
77
|
-
import {
|
|
70
|
+
import { RuntimeProvider, WalletStateProvider } from '@openzeppelin/ui-react';
|
|
78
71
|
|
|
79
|
-
import {
|
|
72
|
+
import { ecosystemDefinition } from '@openzeppelin/adapter-evm';
|
|
73
|
+
import { getNetworkById } from './core/ecosystemManager';
|
|
74
|
+
|
|
75
|
+
async function resolveRuntime(networkConfig) {
|
|
76
|
+
return ecosystemDefinition.createRuntime('composer', networkConfig, {
|
|
77
|
+
/* profile-specific options if needed */
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
80
|
|
|
81
81
|
function AppRoot() {
|
|
82
82
|
return (
|
|
83
|
-
<
|
|
83
|
+
<RuntimeProvider resolveRuntime={resolveRuntime}>
|
|
84
84
|
<WalletStateProvider
|
|
85
85
|
initialNetworkId="ethereum-mainnet"
|
|
86
86
|
getNetworkConfigById={getNetworkById}
|
|
87
87
|
>
|
|
88
|
-
{/* Your application
|
|
88
|
+
{/* Your application */}
|
|
89
89
|
</WalletStateProvider>
|
|
90
|
-
</
|
|
90
|
+
</RuntimeProvider>
|
|
91
91
|
);
|
|
92
92
|
}
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
### Consuming
|
|
95
|
+
### Consuming global state
|
|
96
96
|
|
|
97
97
|
```tsx
|
|
98
98
|
import { useWalletState } from '@openzeppelin/ui-react';
|
|
@@ -101,13 +101,13 @@ function MyWalletComponent() {
|
|
|
101
101
|
const {
|
|
102
102
|
activeNetworkId,
|
|
103
103
|
activeNetworkConfig,
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
activeRuntime,
|
|
105
|
+
isRuntimeLoading,
|
|
106
106
|
walletFacadeHooks,
|
|
107
107
|
setActiveNetworkId,
|
|
108
108
|
} = useWalletState();
|
|
109
109
|
|
|
110
|
-
if (
|
|
110
|
+
if (isRuntimeLoading || !activeRuntime) {
|
|
111
111
|
return <p>Loading wallet information...</p>;
|
|
112
112
|
}
|
|
113
113
|
|
|
@@ -116,33 +116,39 @@ function MyWalletComponent() {
|
|
|
116
116
|
|
|
117
117
|
return (
|
|
118
118
|
<div>
|
|
119
|
-
<p>Current Network: {activeNetworkConfig?.name
|
|
119
|
+
<p>Current Network: {activeNetworkConfig?.name ?? 'None'}</p>
|
|
120
120
|
<p>Wallet Connected: {isConnected ? 'Yes' : 'No'}</p>
|
|
121
121
|
</div>
|
|
122
122
|
);
|
|
123
123
|
}
|
|
124
124
|
```
|
|
125
125
|
|
|
126
|
-
###
|
|
126
|
+
### NetworkSwitchManager
|
|
127
|
+
|
|
128
|
+
Use capabilities from the active runtime — **not** a monolithic adapter instance:
|
|
127
129
|
|
|
128
130
|
```tsx
|
|
129
131
|
import { useState } from 'react';
|
|
130
132
|
|
|
131
|
-
import { NetworkSwitchManager } from '@openzeppelin/ui-react';
|
|
133
|
+
import { NetworkSwitchManager, useWalletState } from '@openzeppelin/ui-react';
|
|
132
134
|
|
|
133
135
|
function MyApp() {
|
|
134
136
|
const [networkToSwitchTo, setNetworkToSwitchTo] = useState<string | null>(null);
|
|
135
|
-
const
|
|
137
|
+
const { activeRuntime } = useWalletState();
|
|
136
138
|
|
|
137
139
|
const handleNetworkSwitchComplete = () => {
|
|
138
140
|
setNetworkToSwitchTo(null);
|
|
139
141
|
};
|
|
140
142
|
|
|
143
|
+
const wallet = activeRuntime?.wallet;
|
|
144
|
+
const networkCatalog = activeRuntime?.networkCatalog;
|
|
145
|
+
|
|
141
146
|
return (
|
|
142
147
|
<>
|
|
143
|
-
{
|
|
148
|
+
{wallet && networkCatalog && networkToSwitchTo && (
|
|
144
149
|
<NetworkSwitchManager
|
|
145
|
-
|
|
150
|
+
wallet={wallet}
|
|
151
|
+
networkCatalog={networkCatalog}
|
|
146
152
|
targetNetworkId={networkToSwitchTo}
|
|
147
153
|
onNetworkSwitchComplete={handleNetworkSwitchComplete}
|
|
148
154
|
/>
|
|
@@ -157,9 +163,9 @@ function MyApp() {
|
|
|
157
163
|
```text
|
|
158
164
|
react/
|
|
159
165
|
├── src/
|
|
160
|
-
│ ├── components/ #
|
|
161
|
-
│ ├── hooks/ #
|
|
162
|
-
│ └── index.ts
|
|
166
|
+
│ ├── components/ # WalletConnection*, NetworkSwitchManager
|
|
167
|
+
│ ├── hooks/ # RuntimeProvider, WalletStateProvider, derived hooks
|
|
168
|
+
│ └── index.ts
|
|
163
169
|
├── package.json
|
|
164
170
|
├── tsconfig.json
|
|
165
171
|
├── tsdown.config.ts
|
|
@@ -168,25 +174,14 @@ react/
|
|
|
168
174
|
|
|
169
175
|
## Dependencies
|
|
170
176
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
- **@openzeppelin/ui-types**: Shared type definitions
|
|
174
|
-
- **@openzeppelin/ui-utils**: Shared utility functions
|
|
175
|
-
- **@openzeppelin/ui-components**: UI components
|
|
176
|
-
- **react**: Peer dependency for React hooks and context
|
|
177
|
-
- **react-dom**: Peer dependency for React DOM utilities
|
|
178
|
-
- **@tanstack/react-query**: Peer dependency for data fetching
|
|
177
|
+
- **@openzeppelin/ui-types**, **@openzeppelin/ui-utils**, **@openzeppelin/ui-components**
|
|
178
|
+
- **react**, **react-dom**, **@tanstack/react-query** (peers)
|
|
179
179
|
|
|
180
180
|
## Development
|
|
181
181
|
|
|
182
182
|
```bash
|
|
183
|
-
# Build the package
|
|
184
183
|
pnpm build
|
|
185
|
-
|
|
186
|
-
# Run tests
|
|
187
184
|
pnpm test
|
|
188
|
-
|
|
189
|
-
# Lint
|
|
190
185
|
pnpm lint
|
|
191
186
|
```
|
|
192
187
|
|