@reown/appkit-react-native 2.0.1 → 2.0.3
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/lib/commonjs/AppKit.js +20 -6
- package/lib/commonjs/AppKit.js.map +1 -1
- package/lib/commonjs/hooks/useAccount.js +68 -5
- package/lib/commonjs/hooks/useAccount.js.map +1 -1
- package/lib/commonjs/hooks/useAppKit.js +41 -9
- package/lib/commonjs/hooks/useAppKit.js.map +1 -1
- package/lib/commonjs/hooks/useAppKitContext.js +46 -0
- package/lib/commonjs/hooks/useAppKitContext.js.map +1 -0
- package/lib/commonjs/hooks/useAppKitEvents.js +64 -4
- package/lib/commonjs/hooks/useAppKitEvents.js.map +1 -1
- package/lib/commonjs/hooks/useAppKitLogs.js +2 -8
- package/lib/commonjs/hooks/useAppKitLogs.js.map +1 -1
- package/lib/commonjs/hooks/useAppKitState.js +35 -2
- package/lib/commonjs/hooks/useAppKitState.js.map +1 -1
- package/lib/commonjs/hooks/useAppKitTheme.js +74 -0
- package/lib/commonjs/hooks/useAppKitTheme.js.map +1 -0
- package/lib/commonjs/hooks/useProvider.js +3 -4
- package/lib/commonjs/hooks/useProvider.js.map +1 -1
- package/lib/commonjs/hooks/useWalletInfo.js +33 -2
- package/lib/commonjs/hooks/useWalletInfo.js.map +1 -1
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/modal/w3m-modal/index.js +7 -7
- package/lib/commonjs/modal/w3m-modal/index.js.map +1 -1
- package/lib/commonjs/modal/w3m-modal/styles.js +2 -1
- package/lib/commonjs/modal/w3m-modal/styles.js.map +1 -1
- package/lib/commonjs/partials/w3m-connecting-body/index.js +2 -0
- package/lib/commonjs/partials/w3m-connecting-body/index.js.map +1 -1
- package/lib/commonjs/partials/w3m-connecting-mobile/components/StoreLink.js +4 -0
- package/lib/commonjs/partials/w3m-connecting-mobile/components/StoreLink.js.map +1 -1
- package/lib/commonjs/partials/w3m-connecting-qrcode/index.js +5 -1
- package/lib/commonjs/partials/w3m-connecting-qrcode/index.js.map +1 -1
- package/lib/commonjs/partials/w3m-header/index.js +1 -0
- package/lib/commonjs/partials/w3m-header/index.js.map +1 -1
- package/lib/commonjs/partials/w3m-header/styles.js +3 -0
- package/lib/commonjs/partials/w3m-header/styles.js.map +1 -1
- package/lib/commonjs/partials/w3m-send-input-token/index.js +6 -3
- package/lib/commonjs/partials/w3m-send-input-token/index.js.map +1 -1
- package/lib/commonjs/partials/w3m-swap-input/index.js +6 -3
- package/lib/commonjs/partials/w3m-swap-input/index.js.map +1 -1
- package/lib/commonjs/views/w3m-account-default-view/index.js +2 -1
- package/lib/commonjs/views/w3m-account-default-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-connecting-external-view/index.js +4 -8
- package/lib/commonjs/views/w3m-connecting-external-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-connecting-view/index.js +18 -12
- package/lib/commonjs/views/w3m-connecting-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-onramp-settings-view/index.js +2 -0
- package/lib/commonjs/views/w3m-onramp-settings-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-onramp-view/index.js +3 -2
- package/lib/commonjs/views/w3m-onramp-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-swap-view/components/select-token-modal/index.js +2 -1
- package/lib/commonjs/views/w3m-swap-view/components/select-token-modal/index.js.map +1 -1
- package/lib/commonjs/views/w3m-swap-view/index.js +4 -2
- package/lib/commonjs/views/w3m-swap-view/index.js.map +1 -1
- package/lib/commonjs/views/w3m-wallet-send-view/index.js +2 -1
- package/lib/commonjs/views/w3m-wallet-send-view/index.js.map +1 -1
- package/lib/module/AppKit.js +20 -6
- package/lib/module/AppKit.js.map +1 -1
- package/lib/module/hooks/useAccount.js +69 -5
- package/lib/module/hooks/useAccount.js.map +1 -1
- package/lib/module/hooks/useAppKit.js +43 -10
- package/lib/module/hooks/useAppKit.js.map +1 -1
- package/lib/module/hooks/useAppKitContext.js +42 -0
- package/lib/module/hooks/useAppKitContext.js.map +1 -0
- package/lib/module/hooks/useAppKitEvents.js +65 -4
- package/lib/module/hooks/useAppKitEvents.js.map +1 -1
- package/lib/module/hooks/useAppKitLogs.js +3 -9
- package/lib/module/hooks/useAppKitLogs.js.map +1 -1
- package/lib/module/hooks/useAppKitState.js +36 -2
- package/lib/module/hooks/useAppKitState.js.map +1 -1
- package/lib/module/hooks/useAppKitTheme.js +71 -0
- package/lib/module/hooks/useAppKitTheme.js.map +1 -0
- package/lib/module/hooks/useProvider.js +3 -4
- package/lib/module/hooks/useProvider.js.map +1 -1
- package/lib/module/hooks/useWalletInfo.js +34 -2
- package/lib/module/hooks/useWalletInfo.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/modal/w3m-modal/index.js +7 -7
- package/lib/module/modal/w3m-modal/index.js.map +1 -1
- package/lib/module/modal/w3m-modal/styles.js +2 -1
- package/lib/module/modal/w3m-modal/styles.js.map +1 -1
- package/lib/module/partials/w3m-connecting-body/index.js +2 -0
- package/lib/module/partials/w3m-connecting-body/index.js.map +1 -1
- package/lib/module/partials/w3m-connecting-mobile/components/StoreLink.js +4 -0
- package/lib/module/partials/w3m-connecting-mobile/components/StoreLink.js.map +1 -1
- package/lib/module/partials/w3m-connecting-qrcode/index.js +5 -1
- package/lib/module/partials/w3m-connecting-qrcode/index.js.map +1 -1
- package/lib/module/partials/w3m-header/index.js +1 -0
- package/lib/module/partials/w3m-header/index.js.map +1 -1
- package/lib/module/partials/w3m-header/styles.js +3 -0
- package/lib/module/partials/w3m-header/styles.js.map +1 -1
- package/lib/module/partials/w3m-send-input-token/index.js +6 -3
- package/lib/module/partials/w3m-send-input-token/index.js.map +1 -1
- package/lib/module/partials/w3m-swap-input/index.js +6 -3
- package/lib/module/partials/w3m-swap-input/index.js.map +1 -1
- package/lib/module/views/w3m-account-default-view/index.js +2 -1
- package/lib/module/views/w3m-account-default-view/index.js.map +1 -1
- package/lib/module/views/w3m-connecting-external-view/index.js +4 -8
- package/lib/module/views/w3m-connecting-external-view/index.js.map +1 -1
- package/lib/module/views/w3m-connecting-view/index.js +19 -13
- package/lib/module/views/w3m-connecting-view/index.js.map +1 -1
- package/lib/module/views/w3m-onramp-settings-view/index.js +2 -0
- package/lib/module/views/w3m-onramp-settings-view/index.js.map +1 -1
- package/lib/module/views/w3m-onramp-view/index.js +3 -2
- package/lib/module/views/w3m-onramp-view/index.js.map +1 -1
- package/lib/module/views/w3m-swap-view/components/select-token-modal/index.js +2 -1
- package/lib/module/views/w3m-swap-view/components/select-token-modal/index.js.map +1 -1
- package/lib/module/views/w3m-swap-view/index.js +4 -2
- package/lib/module/views/w3m-swap-view/index.js.map +1 -1
- package/lib/module/views/w3m-wallet-send-view/index.js +2 -1
- package/lib/module/views/w3m-wallet-send-view/index.js.map +1 -1
- package/lib/typescript/AppKit.d.ts +9 -2
- package/lib/typescript/AppKit.d.ts.map +1 -1
- package/lib/typescript/AppKitContext.d.ts +2 -2
- package/lib/typescript/AppKitContext.d.ts.map +1 -1
- package/lib/typescript/hooks/useAccount.d.ts +57 -33
- package/lib/typescript/hooks/useAccount.d.ts.map +1 -1
- package/lib/typescript/hooks/useAppKit.d.ts +42 -0
- package/lib/typescript/hooks/useAppKit.d.ts.map +1 -1
- package/lib/typescript/hooks/useAppKitContext.d.ts +27 -0
- package/lib/typescript/hooks/useAppKitContext.d.ts.map +1 -0
- package/lib/typescript/hooks/useAppKitEvents.d.ts +66 -0
- package/lib/typescript/hooks/useAppKitEvents.d.ts.map +1 -1
- package/lib/typescript/hooks/useAppKitLogs.d.ts.map +1 -1
- package/lib/typescript/hooks/useAppKitState.d.ts +32 -0
- package/lib/typescript/hooks/useAppKitState.d.ts.map +1 -1
- package/lib/typescript/hooks/useAppKitTheme.d.ts +59 -0
- package/lib/typescript/hooks/useAppKitTheme.d.ts.map +1 -0
- package/lib/typescript/hooks/useProvider.d.ts +1 -4
- package/lib/typescript/hooks/useProvider.d.ts.map +1 -1
- package/lib/typescript/hooks/useWalletInfo.d.ts +31 -0
- package/lib/typescript/hooks/useWalletInfo.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +3 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/modal/w3m-modal/index.d.ts +9 -1
- package/lib/typescript/modal/w3m-modal/index.d.ts.map +1 -1
- package/lib/typescript/modal/w3m-modal/styles.d.ts +1 -0
- package/lib/typescript/modal/w3m-modal/styles.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-connecting-body/index.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-connecting-qrcode/index.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-header/index.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-header/styles.d.ts +3 -0
- package/lib/typescript/partials/w3m-header/styles.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-send-input-token/index.d.ts +2 -1
- package/lib/typescript/partials/w3m-send-input-token/index.d.ts.map +1 -1
- package/lib/typescript/partials/w3m-swap-input/index.d.ts +2 -1
- package/lib/typescript/partials/w3m-swap-input/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-account-default-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-connecting-external-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-connecting-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-onramp-settings-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-onramp-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-swap-view/index.d.ts.map +1 -1
- package/lib/typescript/views/w3m-wallet-send-view/index.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/AppKit.ts +27 -7
- package/src/AppKitContext.tsx +1 -1
- package/src/hooks/useAccount.ts +93 -5
- package/src/hooks/useAppKit.ts +45 -12
- package/src/hooks/useAppKitContext.ts +43 -0
- package/src/hooks/useAppKitEvents.ts +63 -3
- package/src/hooks/useAppKitLogs.ts +3 -11
- package/src/hooks/useAppKitState.ts +35 -2
- package/src/hooks/useAppKitTheme.ts +82 -0
- package/src/hooks/useProvider.ts +3 -4
- package/src/hooks/useWalletInfo.ts +33 -2
- package/src/index.ts +8 -2
- package/src/modal/w3m-modal/index.tsx +16 -7
- package/src/modal/w3m-modal/styles.ts +2 -1
- package/src/partials/w3m-connecting-body/index.tsx +3 -1
- package/src/partials/w3m-connecting-mobile/components/StoreLink.tsx +4 -1
- package/src/partials/w3m-connecting-qrcode/index.tsx +10 -1
- package/src/partials/w3m-header/index.tsx +6 -1
- package/src/partials/w3m-header/styles.ts +3 -0
- package/src/partials/w3m-send-input-token/index.tsx +5 -1
- package/src/partials/w3m-swap-input/index.tsx +5 -1
- package/src/views/w3m-account-default-view/index.tsx +6 -1
- package/src/views/w3m-connecting-external-view/index.tsx +4 -8
- package/src/views/w3m-connecting-view/index.tsx +24 -14
- package/src/views/w3m-onramp-settings-view/index.tsx +7 -1
- package/src/views/w3m-onramp-view/index.tsx +3 -2
- package/src/views/w3m-swap-view/components/select-token-modal/index.tsx +1 -1
- package/src/views/w3m-swap-view/index.tsx +2 -0
- package/src/views/w3m-wallet-send-view/index.tsx +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reown/appkit-react-native",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"main": "lib/commonjs/index.js",
|
|
5
5
|
"types": "lib/typescript/index.d.ts",
|
|
6
6
|
"module": "lib/module/index.js",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "bob build",
|
|
11
11
|
"clean": "rm -rf lib",
|
|
12
|
+
"test": "jest",
|
|
12
13
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
@@ -39,9 +40,9 @@
|
|
|
39
40
|
"provenance": true
|
|
40
41
|
},
|
|
41
42
|
"dependencies": {
|
|
42
|
-
"@reown/appkit-common-react-native": "2.0.
|
|
43
|
-
"@reown/appkit-core-react-native": "2.0.
|
|
44
|
-
"@reown/appkit-ui-react-native": "2.0.
|
|
43
|
+
"@reown/appkit-common-react-native": "2.0.3",
|
|
44
|
+
"@reown/appkit-core-react-native": "2.0.3",
|
|
45
|
+
"@reown/appkit-ui-react-native": "2.0.3",
|
|
45
46
|
"@walletconnect/universal-provider": "2.21.10",
|
|
46
47
|
"valtio": "2.1.8"
|
|
47
48
|
},
|
package/src/AppKit.ts
CHANGED
|
@@ -271,29 +271,49 @@ export class AppKit {
|
|
|
271
271
|
return this.networks;
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Switches to a different network.
|
|
276
|
+
* @param network - Either an AppKitNetwork object or a CAIP network ID string (e.g., 'eip155:1')
|
|
277
|
+
* @throws {Error} When the network is not found in configured networks
|
|
278
|
+
* @throws {Error} When no active adapter is available (only when connected)
|
|
279
|
+
* @returns Promise that resolves when the network switch is complete
|
|
280
|
+
*/
|
|
281
|
+
async switchNetwork(network: AppKitNetwork | CaipNetworkId): Promise<void> {
|
|
275
282
|
const { isConnected } = ConnectionsController.state;
|
|
276
283
|
|
|
284
|
+
const appKitNetwork =
|
|
285
|
+
typeof network === 'string' ? this.networks.find(n => n.caipNetworkId === network) : network;
|
|
286
|
+
|
|
287
|
+
if (!appKitNetwork) {
|
|
288
|
+
const error = new Error(`Network not found: ${network}`);
|
|
289
|
+
LogController.sendError(`Network not found: ${network}`, 'AppKit.ts', 'switchNetwork');
|
|
290
|
+
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
|
|
277
294
|
if (!isConnected) {
|
|
278
|
-
OptionsController.setDefaultNetwork(
|
|
295
|
+
OptionsController.setDefaultNetwork(appKitNetwork);
|
|
279
296
|
|
|
280
|
-
return
|
|
297
|
+
return;
|
|
281
298
|
}
|
|
282
299
|
|
|
283
|
-
const adapter = this.getAdapterByNamespace(
|
|
300
|
+
const adapter = this.getAdapterByNamespace(appKitNetwork.chainNamespace);
|
|
284
301
|
if (!adapter) throw new Error('No active adapter');
|
|
285
302
|
|
|
286
|
-
await adapter.switchNetwork(
|
|
303
|
+
await adapter.switchNetwork(appKitNetwork);
|
|
287
304
|
|
|
288
305
|
EventsController.sendEvent({
|
|
289
306
|
type: 'track',
|
|
290
307
|
event: 'SWITCH_NETWORK',
|
|
291
308
|
properties: {
|
|
292
|
-
network:
|
|
309
|
+
network: appKitNetwork.caipNetworkId
|
|
293
310
|
}
|
|
294
311
|
});
|
|
295
312
|
|
|
296
|
-
ConnectionsController.setActiveNetwork(
|
|
313
|
+
ConnectionsController.setActiveNetwork(
|
|
314
|
+
appKitNetwork.chainNamespace,
|
|
315
|
+
appKitNetwork.caipNetworkId
|
|
316
|
+
);
|
|
297
317
|
}
|
|
298
318
|
|
|
299
319
|
open(options?: AppKitOpenOptions) {
|
package/src/AppKitContext.tsx
CHANGED
package/src/hooks/useAccount.ts
CHANGED
|
@@ -1,19 +1,106 @@
|
|
|
1
1
|
/* eslint-disable valtio/state-snapshot-rule */
|
|
2
|
+
import {
|
|
3
|
+
ConnectionsController,
|
|
4
|
+
CoreHelperUtil,
|
|
5
|
+
LogController
|
|
6
|
+
} from '@reown/appkit-core-react-native';
|
|
2
7
|
import { useMemo } from 'react';
|
|
3
8
|
import { useSnapshot } from 'valtio';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
9
|
+
import type { AccountType, AppKitNetwork } from '@reown/appkit-common-react-native';
|
|
10
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Represents a blockchain account with its associated metadata
|
|
14
|
+
*/
|
|
15
|
+
export interface Account {
|
|
16
|
+
/** The blockchain address of the account */
|
|
17
|
+
address: string;
|
|
18
|
+
/** The blockchain namespace (e.g., 'eip155' for Ethereum, 'solana' for Solana) */
|
|
19
|
+
namespace: string;
|
|
20
|
+
/** The chain ID where this account is active */
|
|
21
|
+
chainId: string;
|
|
22
|
+
/** Optional account type (e.g. 'eoa' or 'smartAccount') */
|
|
23
|
+
type?: AccountType;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Hook to access the current account state and connection information
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* This hook provides access to all connected accounts, the currently active account,
|
|
31
|
+
* connection status, and active chain information. It automatically subscribes to
|
|
32
|
+
* connection state changes via valtio.
|
|
33
|
+
*
|
|
34
|
+
* The hook parses account data from CAIP-10 format (namespace:chainId:address)
|
|
35
|
+
* and provides a normalized structure.
|
|
36
|
+
*
|
|
37
|
+
* @returns An object containing:
|
|
38
|
+
* - `allAccounts` - Array of all connected accounts across all connections
|
|
39
|
+
* - `address` - The plain address of the currently active account (without namespace or chain prefix)
|
|
40
|
+
* - `isConnected` - Boolean indicating if a wallet is currently connected
|
|
41
|
+
* - `chainId` - The ID of the currently active chain
|
|
42
|
+
* - `chain` - The full chain/network object of the currently active chain
|
|
43
|
+
* - `namespace` - The namespace of the currently active account (e.g. 'eip155', 'solana' or 'bip122')
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* function MyComponent() {
|
|
48
|
+
* const { address, isConnected, chainId, allAccounts } = useAccount();
|
|
49
|
+
*
|
|
50
|
+
* if (!isConnected) {
|
|
51
|
+
* return <Text>Not connected</Text>;
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* return (
|
|
55
|
+
* <View>
|
|
56
|
+
* <Text>Connected: {address}</Text>
|
|
57
|
+
* <Text>Chain: {chainId}</Text>
|
|
58
|
+
* <Text>Total accounts: {allAccounts.length}</Text>
|
|
59
|
+
* </View>
|
|
60
|
+
* );
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @throws Will log errors via LogController if account parsing fails
|
|
65
|
+
*/
|
|
7
66
|
export function useAccount() {
|
|
8
|
-
|
|
67
|
+
useAppKitContext();
|
|
9
68
|
|
|
10
69
|
const {
|
|
11
70
|
activeAddress: address,
|
|
12
71
|
activeNamespace,
|
|
13
72
|
connection,
|
|
73
|
+
connections,
|
|
14
74
|
networks
|
|
15
75
|
} = useSnapshot(ConnectionsController.state);
|
|
16
76
|
|
|
77
|
+
const allAccounts: Account[] = useMemo(() => {
|
|
78
|
+
if (!address) return [];
|
|
79
|
+
|
|
80
|
+
return Array.from(connections.values()).flatMap(
|
|
81
|
+
_connection =>
|
|
82
|
+
_connection.accounts
|
|
83
|
+
.map(account => {
|
|
84
|
+
const [namespace, chainId, plainAddress] = account.split(':');
|
|
85
|
+
if (!plainAddress || !namespace || !chainId) {
|
|
86
|
+
LogController.sendError('Invalid account', 'useAccount.ts', 'useAccount', {
|
|
87
|
+
account
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
address: plainAddress,
|
|
95
|
+
namespace,
|
|
96
|
+
chainId,
|
|
97
|
+
type: _connection.type
|
|
98
|
+
};
|
|
99
|
+
})
|
|
100
|
+
.filter(account => account !== undefined) as Account[]
|
|
101
|
+
);
|
|
102
|
+
}, [connections, address]);
|
|
103
|
+
|
|
17
104
|
const activeChain = useMemo(
|
|
18
105
|
() =>
|
|
19
106
|
connection?.caipNetwork
|
|
@@ -23,10 +110,11 @@ export function useAccount() {
|
|
|
23
110
|
);
|
|
24
111
|
|
|
25
112
|
return {
|
|
113
|
+
allAccounts,
|
|
26
114
|
address: CoreHelperUtil.getPlainAddress(address),
|
|
27
115
|
isConnected: !!address,
|
|
28
|
-
chainId: activeChain?.id,
|
|
29
|
-
chain: activeChain,
|
|
116
|
+
chainId: activeChain?.id !== undefined ? String(activeChain.id) : undefined,
|
|
117
|
+
chain: activeChain as AppKitNetwork | undefined,
|
|
30
118
|
namespace: activeNamespace
|
|
31
119
|
};
|
|
32
120
|
}
|
package/src/hooks/useAppKit.ts
CHANGED
|
@@ -1,27 +1,60 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
2
|
import type { ChainNamespace } from '@reown/appkit-common-react-native';
|
|
3
3
|
|
|
4
4
|
import type { AppKit } from '../AppKit';
|
|
5
|
-
import {
|
|
5
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Interface representing the return value of the useAppKit hook
|
|
9
|
+
*/
|
|
7
10
|
interface UseAppKitReturn {
|
|
11
|
+
/** Function to open the AppKit modal with optional view configuration */
|
|
8
12
|
open: AppKit['open'];
|
|
13
|
+
/** Function to close the AppKit modal */
|
|
9
14
|
close: AppKit['close'];
|
|
15
|
+
/** Function to disconnect the wallet, optionally scoped to a specific namespace */
|
|
10
16
|
disconnect: (namespace?: ChainNamespace) => void;
|
|
17
|
+
/** Function to switch to a different network */
|
|
11
18
|
switchNetwork: AppKit['switchNetwork'];
|
|
12
19
|
}
|
|
13
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Hook to access core AppKit functionality for controlling the modal
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* This hook provides access to the main AppKit instance methods for opening/closing
|
|
26
|
+
* the modal, disconnecting wallets, and switching networks. All functions are memoized
|
|
27
|
+
* and properly bound to ensure stable references across renders.
|
|
28
|
+
*
|
|
29
|
+
* @returns {UseAppKitReturn} An object containing:
|
|
30
|
+
* - `open`: Opens the AppKit modal, optionally with a specific view
|
|
31
|
+
* - `close`: Closes the AppKit modal
|
|
32
|
+
* - `disconnect`: Disconnects the current wallet connection (optionally for a specific namespace)
|
|
33
|
+
* - `switchNetwork`: Switches to a different blockchain network
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
36
|
+
* @throws {Error} If AppKit instance is not available in context
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```tsx
|
|
40
|
+
* function MyComponent() {
|
|
41
|
+
* const { open, close, disconnect, switchNetwork } = useAppKit();
|
|
42
|
+
*
|
|
43
|
+
* return (
|
|
44
|
+
* <View>
|
|
45
|
+
* <Button onPress={() => open()} title="Connect Wallet" />
|
|
46
|
+
* <Button onPress={() => disconnect()} title="Disconnect" />
|
|
47
|
+
* <Button
|
|
48
|
+
* onPress={() => switchNetwork('eip155:1')}
|
|
49
|
+
* title="Switch to Ethereum"
|
|
50
|
+
* />
|
|
51
|
+
* </View>
|
|
52
|
+
* );
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
14
56
|
export const useAppKit = (): UseAppKitReturn => {
|
|
15
|
-
const context =
|
|
16
|
-
|
|
17
|
-
if (context === undefined) {
|
|
18
|
-
throw new Error('useAppKit must be used within an AppKitProvider');
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (!context.appKit) {
|
|
22
|
-
// This might happen if the provider is rendered before AppKit is initialized
|
|
23
|
-
throw new Error('AppKit instance is not yet available in context.');
|
|
24
|
-
}
|
|
57
|
+
const context = useAppKitContext();
|
|
25
58
|
|
|
26
59
|
const stableFunctions = useMemo(() => {
|
|
27
60
|
if (!context.appKit) {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { AppKitContext, type AppKitContextType } from '../AppKitContext';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook to access the AppKit context
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This is an internal hook used by other AppKit hooks to ensure they're used within
|
|
10
|
+
* the AppKitProvider. You typically don't need to use this hook directly - use the
|
|
11
|
+
* higher-level hooks like `useAppKit`, `useAccount`, `useAppKitTheme`, etc. instead.
|
|
12
|
+
*
|
|
13
|
+
* @returns {AppKitContextType} The AppKit context containing the AppKit instance
|
|
14
|
+
*
|
|
15
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
16
|
+
* @throws {Error} If the AppKit instance is not yet available in context
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* // This is typically used internally by other hooks
|
|
23
|
+
* function MyCustomHook() {
|
|
24
|
+
* const context = useAppKitContext();
|
|
25
|
+
* // Use context.appKit...
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export const useAppKitContext = (): AppKitContextType => {
|
|
31
|
+
const context = useContext(AppKitContext);
|
|
32
|
+
|
|
33
|
+
if (context === undefined) {
|
|
34
|
+
throw new Error('useAppKitContext must be used within an AppKitProvider');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!context.appKit) {
|
|
38
|
+
// This might happen if the provider is rendered before AppKit is initialized
|
|
39
|
+
throw new Error('AppKit instance is not yet available in context.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return context;
|
|
43
|
+
};
|
|
@@ -2,10 +2,42 @@ import { useEffect } from 'react';
|
|
|
2
2
|
import { useSnapshot } from 'valtio';
|
|
3
3
|
import { EventsController, type EventsControllerState } from '@reown/appkit-core-react-native';
|
|
4
4
|
import { type EventName } from '@reown/appkit-common-react-native';
|
|
5
|
-
import {
|
|
5
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Hook to subscribe to all AppKit events
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This hook provides reactive access to AppKit's event system, allowing you to
|
|
12
|
+
* monitor all events that occur within the AppKit lifecycle (connections, disconnections,
|
|
13
|
+
* network changes, etc.). The callback is invoked whenever a new event is emitted.
|
|
14
|
+
*
|
|
15
|
+
* @param callback - Optional callback function invoked when any event occurs
|
|
16
|
+
*
|
|
17
|
+
* @returns An object containing:
|
|
18
|
+
* - `data`: The most recent event data
|
|
19
|
+
* - `timestamp`: The timestamp of the most recent event
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* function MyComponent() {
|
|
26
|
+
* const { data, timestamp } = useAppKitEvents((event) => {
|
|
27
|
+
* console.log('Event occurred:', event.data.event);
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <View>
|
|
32
|
+
* <Text>Last event: {data?.event}</Text>
|
|
33
|
+
* <Text>Time: {timestamp}</Text>
|
|
34
|
+
* </View>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
7
39
|
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
|
|
8
|
-
|
|
40
|
+
useAppKitContext();
|
|
9
41
|
const { data, timestamp } = useSnapshot(EventsController.state);
|
|
10
42
|
|
|
11
43
|
useEffect(() => {
|
|
@@ -21,11 +53,39 @@ export function useAppKitEvents(callback?: (newEvent: EventsControllerState) =>
|
|
|
21
53
|
return { data, timestamp };
|
|
22
54
|
}
|
|
23
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Hook to subscribe to a specific AppKit event
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* This hook allows you to listen for a specific event type rather than all events.
|
|
61
|
+
* It's more efficient than `useAppKitEvents` when you only care about a particular event.
|
|
62
|
+
*
|
|
63
|
+
* @param event - The specific event name to subscribe to (e.g., 'MODAL_OPEN', 'CONNECT_SUCCESS')
|
|
64
|
+
* @param callback - Callback function invoked when the specified event occurs
|
|
65
|
+
*
|
|
66
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* function MyComponent() {
|
|
71
|
+
* useAppKitEventSubscription('CONNECT_SUCCESS', (event) => {
|
|
72
|
+
* console.log('Wallet connected!', event.data);
|
|
73
|
+
* });
|
|
74
|
+
*
|
|
75
|
+
* useAppKitEventSubscription('DISCONNECT_SUCCESS', (event) => {
|
|
76
|
+
* console.log('Wallet disconnected!', event.data);
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* return <View>{/ Your component /}</View>;
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
|
|
24
84
|
export function useAppKitEventSubscription(
|
|
25
85
|
event: EventName,
|
|
26
86
|
callback: (newEvent: EventsControllerState) => void
|
|
27
87
|
) {
|
|
28
|
-
|
|
88
|
+
useAppKitContext();
|
|
29
89
|
|
|
30
90
|
useEffect(() => {
|
|
31
91
|
const unsubscribe = EventsController?.subscribeEvent(event, callback);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
2
|
import { useSnapshot } from 'valtio';
|
|
3
3
|
import { LogController, type LogEntry, type LogLevel } from '@reown/appkit-core-react-native';
|
|
4
|
-
import {
|
|
4
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
5
5
|
|
|
6
6
|
export interface UseAppKitLogsReturn {
|
|
7
7
|
/**
|
|
@@ -65,15 +65,7 @@ export interface UseAppKitLogsReturn {
|
|
|
65
65
|
* ```
|
|
66
66
|
*/
|
|
67
67
|
export const useAppKitLogs = (): UseAppKitLogsReturn => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (context === undefined) {
|
|
71
|
-
throw new Error('useAppKitLogs must be used within an AppKitProvider');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (!context.appKit) {
|
|
75
|
-
throw new Error('AppKit instance is not yet available in context.');
|
|
76
|
-
}
|
|
68
|
+
useAppKitContext();
|
|
77
69
|
|
|
78
70
|
const { logs } = useSnapshot(LogController.state);
|
|
79
71
|
|
|
@@ -2,10 +2,43 @@
|
|
|
2
2
|
import { useMemo } from 'react';
|
|
3
3
|
import { useSnapshot } from 'valtio';
|
|
4
4
|
import { ConnectionsController, ModalController } from '@reown/appkit-core-react-native';
|
|
5
|
-
import {
|
|
5
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook to access the overall state of the AppKit modal and connection
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This hook provides a high-level view of the AppKit's current state, including
|
|
12
|
+
* whether the modal is open, if it's loading, connection status, and the active chain.
|
|
13
|
+
* It's useful for coordinating UI elements with the AppKit's state.
|
|
14
|
+
*
|
|
15
|
+
* @returns An object containing:
|
|
16
|
+
* - `isOpen`: Whether the AppKit modal is currently open
|
|
17
|
+
* - `isLoading`: Whether the AppKit is in a loading state
|
|
18
|
+
* - `isConnected`: Whether a wallet is currently connected
|
|
19
|
+
* - `chain`: The currently active blockchain network
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* function MyComponent() {
|
|
26
|
+
* const { isOpen, isLoading, isConnected, chain } = useAppKitState();
|
|
27
|
+
*
|
|
28
|
+
* return (
|
|
29
|
+
* <View>
|
|
30
|
+
* <Text>Modal: {isOpen ? 'Open' : 'Closed'}</Text>
|
|
31
|
+
* <Text>Loading: {isLoading ? 'Yes' : 'No'}</Text>
|
|
32
|
+
* <Text>Connected: {isConnected ? 'Yes' : 'No'}</Text>
|
|
33
|
+
* {chain && <Text>Chain: {chain.name}</Text>}
|
|
34
|
+
* </View>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
6
39
|
|
|
7
40
|
export function useAppKitState() {
|
|
8
|
-
|
|
41
|
+
useAppKitContext();
|
|
9
42
|
const { activeAddress: address, connection, networks } = useSnapshot(ConnectionsController.state);
|
|
10
43
|
const { open, loading } = useSnapshot(ModalController.state);
|
|
11
44
|
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useSnapshot } from 'valtio';
|
|
3
|
+
import type { ThemeMode, ThemeVariables } from '@reown/appkit-common-react-native';
|
|
4
|
+
import { ThemeController } from '@reown/appkit-core-react-native';
|
|
5
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Interface representing the result of the useAppKitTheme hook
|
|
9
|
+
*/
|
|
10
|
+
export interface UseAppKitThemeReturn {
|
|
11
|
+
/** The current theme mode ('dark' or 'light') */
|
|
12
|
+
themeMode: ThemeMode;
|
|
13
|
+
/** The current theme variables, currently only supports 'accent' color */
|
|
14
|
+
themeVariables: ThemeVariables;
|
|
15
|
+
/** Function to set the theme mode */
|
|
16
|
+
setThemeMode: (themeMode: ThemeMode | undefined) => void;
|
|
17
|
+
/** Function to set theme variables */
|
|
18
|
+
setThemeVariables: (themeVariables: ThemeVariables | undefined) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Hook to control the visual appearance of the AppKit modal
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* This hook provides access to the theme mode and theme variables, allowing you to
|
|
26
|
+
* customize the modal's appearance. Use this hook when you need to implement dark/light
|
|
27
|
+
* mode or match the modal's appearance with your application's theme.
|
|
28
|
+
*
|
|
29
|
+
* Currently, the only supported theme variable is `accent`, which controls the primary
|
|
30
|
+
* accent color used throughout the modal interface.
|
|
31
|
+
*
|
|
32
|
+
* @returns {UseAppKitThemeReturn} An object containing:
|
|
33
|
+
* - `themeMode`: The current theme mode ('dark' or 'light')
|
|
34
|
+
* - `themeVariables`: The current theme variables (only 'accent' is supported)
|
|
35
|
+
* - `setThemeMode`: Function to change the theme mode
|
|
36
|
+
* - `setThemeVariables`: Function to update theme variables
|
|
37
|
+
*
|
|
38
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { useAppKitTheme } from '@reown/appkit-react-native';
|
|
43
|
+
*
|
|
44
|
+
* function MyComponent() {
|
|
45
|
+
* const { themeMode, themeVariables, setThemeMode, setThemeVariables } = useAppKitTheme();
|
|
46
|
+
*
|
|
47
|
+
* // Set theme to dark mode
|
|
48
|
+
* setThemeMode('dark');
|
|
49
|
+
*
|
|
50
|
+
* // Customize the accent color
|
|
51
|
+
* setThemeVariables({
|
|
52
|
+
* accent: '#00BB7F'
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* return (
|
|
56
|
+
* <View>
|
|
57
|
+
* <Text>Current theme: {themeMode}</Text>
|
|
58
|
+
* <Text>Accent color: {themeVariables?.accent}</Text>
|
|
59
|
+
* </View>
|
|
60
|
+
* );
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function useAppKitTheme(): UseAppKitThemeReturn {
|
|
65
|
+
useAppKitContext();
|
|
66
|
+
|
|
67
|
+
const { themeMode, themeVariables } = useSnapshot(ThemeController.state);
|
|
68
|
+
|
|
69
|
+
const stableFunctions = useMemo(
|
|
70
|
+
() => ({
|
|
71
|
+
setThemeMode: ThemeController.setDefaultThemeMode.bind(ThemeController),
|
|
72
|
+
setThemeVariables: ThemeController.setThemeVariables.bind(ThemeController)
|
|
73
|
+
}),
|
|
74
|
+
[]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
themeMode,
|
|
79
|
+
themeVariables,
|
|
80
|
+
...stableFunctions
|
|
81
|
+
};
|
|
82
|
+
}
|
package/src/hooks/useProvider.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { useMemo } from 'react';
|
|
|
3
3
|
import { useSnapshot } from 'valtio';
|
|
4
4
|
import { ConnectionsController, LogController } from '@reown/appkit-core-react-native';
|
|
5
5
|
import type { Provider, ChainNamespace } from '@reown/appkit-common-react-native';
|
|
6
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Interface representing the result of the useProvider hook
|
|
@@ -29,14 +30,12 @@ interface ProviderResult {
|
|
|
29
30
|
*
|
|
30
31
|
* if (provider) {
|
|
31
32
|
* // Use the provider for blockchain operations
|
|
32
|
-
* const balance = await provider.request({
|
|
33
|
-
* method: 'eth_getBalance',
|
|
34
|
-
* params: [address, 'latest']
|
|
35
|
-
* });
|
|
33
|
+
* const balance = await provider.request({...});
|
|
36
34
|
* }
|
|
37
35
|
* ```
|
|
38
36
|
*/
|
|
39
37
|
export function useProvider(): ProviderResult {
|
|
38
|
+
useAppKitContext();
|
|
40
39
|
const { connection } = useSnapshot(ConnectionsController.state);
|
|
41
40
|
|
|
42
41
|
const returnValue = useMemo(() => {
|
|
@@ -1,10 +1,41 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
2
|
import { useSnapshot } from 'valtio';
|
|
3
3
|
import { ConnectionsController } from '@reown/appkit-core-react-native';
|
|
4
|
-
import {
|
|
4
|
+
import { useAppKitContext } from './useAppKitContext';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Hook to access information about the currently connected wallet
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This hook provides access to metadata about the connected wallet, such as its name,
|
|
11
|
+
* icon, and other identifying information. It automatically subscribes to wallet info
|
|
12
|
+
* changes via valtio.
|
|
13
|
+
*
|
|
14
|
+
* @returns An object containing:
|
|
15
|
+
* - `walletInfo`: Metadata about the currently connected wallet (name, icon, etc.)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* function MyComponent() {
|
|
20
|
+
* const { walletInfo } = useWalletInfo();
|
|
21
|
+
*
|
|
22
|
+
* return (
|
|
23
|
+
* <View>
|
|
24
|
+
* {walletInfo && (
|
|
25
|
+
* <>
|
|
26
|
+
* <Image source={{ uri: walletInfo.icon }} />
|
|
27
|
+
* <Text>{walletInfo.name}</Text>
|
|
28
|
+
* </>
|
|
29
|
+
* )}
|
|
30
|
+
* </View>
|
|
31
|
+
* );
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} If used outside of an AppKitProvider
|
|
36
|
+
*/
|
|
6
37
|
export function useWalletInfo() {
|
|
7
|
-
|
|
38
|
+
useAppKitContext();
|
|
8
39
|
const { walletInfo: walletInfoSnapshot } = useSnapshot(ConnectionsController.state);
|
|
9
40
|
|
|
10
41
|
const walletInfo = useMemo(() => ({ walletInfo: walletInfoSnapshot }), [walletInfoSnapshot]);
|
package/src/index.ts
CHANGED
|
@@ -14,7 +14,12 @@ export {
|
|
|
14
14
|
NetworkButton as NetworkButton,
|
|
15
15
|
type NetworkButtonProps as NetworkButtonProps
|
|
16
16
|
} from './modal/w3m-network-button';
|
|
17
|
-
export {
|
|
17
|
+
export {
|
|
18
|
+
AppKit,
|
|
19
|
+
type AppKitProps,
|
|
20
|
+
type AppKitModalContentWrapperComponent,
|
|
21
|
+
type AppKitModalContentWrapperProps
|
|
22
|
+
} from './modal/w3m-modal';
|
|
18
23
|
|
|
19
24
|
/********** Types **********/
|
|
20
25
|
export type * from '@reown/appkit-core-react-native';
|
|
@@ -31,7 +36,8 @@ export type { AppKitConfig } from './types';
|
|
|
31
36
|
/****** Hooks *******/
|
|
32
37
|
export { useAppKit } from './hooks/useAppKit';
|
|
33
38
|
export { useProvider } from './hooks/useProvider';
|
|
34
|
-
export { useAccount } from './hooks/useAccount';
|
|
39
|
+
export { useAccount, type Account as UseAccountReturn } from './hooks/useAccount';
|
|
40
|
+
export { useAppKitTheme, type UseAppKitThemeReturn } from './hooks/useAppKitTheme';
|
|
35
41
|
export { useWalletInfo } from './hooks/useWalletInfo';
|
|
36
42
|
export { useAppKitEvents, useAppKitEventSubscription } from './hooks/useAppKitEvents';
|
|
37
43
|
export { useAppKitState } from './hooks/useAppKitState';
|