@mysten/dapp-kit-react 2.0.2 → 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/dist/index.d.mts +9 -9
- package/docs/actions/connect-wallet.md +69 -0
- package/docs/actions/disconnect-wallet.md +39 -0
- package/docs/actions/sign-and-execute-transaction.md +96 -0
- package/docs/actions/sign-personal-message.md +58 -0
- package/docs/actions/sign-transaction.md +65 -0
- package/docs/actions/switch-account.md +37 -0
- package/docs/actions/switch-network.md +37 -0
- package/docs/dapp-kit-instance.md +163 -0
- package/docs/getting-started/create-dapp.md +151 -0
- package/docs/getting-started/next-js.md +163 -0
- package/docs/getting-started/react.md +172 -0
- package/docs/getting-started/vue.md +193 -0
- package/docs/index.md +70 -0
- package/docs/llms-index.md +33 -0
- package/docs/react/components/connect-button.md +42 -0
- package/docs/react/components/connect-modal.md +51 -0
- package/docs/react/components/index.md +13 -0
- package/docs/react/dapp-kit-provider.md +86 -0
- package/docs/react/hooks/index.md +26 -0
- package/docs/react/hooks/use-current-account.md +33 -0
- package/docs/react/hooks/use-current-client.md +36 -0
- package/docs/react/hooks/use-current-network.md +28 -0
- package/docs/react/hooks/use-current-wallet.md +36 -0
- package/docs/react/hooks/use-dapp-kit.md +100 -0
- package/docs/react/hooks/use-wallet-connection.md +48 -0
- package/docs/react/hooks/use-wallets.md +33 -0
- package/docs/state.md +169 -0
- package/docs/theming.md +196 -0
- package/docs/web-components/connect-button.md +89 -0
- package/docs/web-components/connect-modal.md +177 -0
- package/package.json +2 -2
package/docs/state.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# State
|
|
2
|
+
|
|
3
|
+
> Access and subscribe to wallet connection state in dApp Kit.
|
|
4
|
+
|
|
5
|
+
The Sui dApp Kit SDK uses [nanostores](https://github.com/nanostores/nanostores) for state
|
|
6
|
+
management, providing a lightweight, framework-agnostic solution that works across React, Vue,
|
|
7
|
+
Vanilla JavaScript, and other frameworks.
|
|
8
|
+
|
|
9
|
+
## Available stores
|
|
10
|
+
|
|
11
|
+
The dApp Kit exposes four reactive stores that you can subscribe to:
|
|
12
|
+
|
|
13
|
+
- **`$connection`:** Current wallet connection state
|
|
14
|
+
- **`$currentNetwork`:** Currently selected network
|
|
15
|
+
- **`$currentClient`:** Client instance for the current network
|
|
16
|
+
- **`$wallets`:** List of available wallets
|
|
17
|
+
|
|
18
|
+
## Accessing stores
|
|
19
|
+
|
|
20
|
+
All stores are available through the `dAppKit.stores` property:
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
|
|
24
|
+
const dAppKit = createDAppKit({
|
|
25
|
+
/* config */
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Access stores
|
|
29
|
+
const connection = dAppKit.stores.$connection.get();
|
|
30
|
+
const currentNetwork = dAppKit.stores.$currentNetwork.get();
|
|
31
|
+
const client = dAppKit.stores.$currentClient.get();
|
|
32
|
+
const wallets = dAppKit.stores.$wallets.get();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Subscribing to state changes
|
|
36
|
+
|
|
37
|
+
Subscribe to any store to react to state changes:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Subscribe to connection changes
|
|
41
|
+
const unsubscribe = dAppKit.stores.$connection.subscribe((connection) => {
|
|
42
|
+
if (connection.isConnected && connection.wallet && connection.account) {
|
|
43
|
+
console.log('Connected to:', connection.wallet.name);
|
|
44
|
+
console.log('Account:', connection.account.address);
|
|
45
|
+
} else {
|
|
46
|
+
console.log('Not connected');
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Clean up subscription when done
|
|
51
|
+
unsubscribe();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Store details
|
|
55
|
+
|
|
56
|
+
### Connection store (`$connection`)
|
|
57
|
+
|
|
58
|
+
Contains the current wallet connection state with status flags:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const connection = dAppKit.stores.$connection.get();
|
|
62
|
+
|
|
63
|
+
// Connection has these properties:
|
|
64
|
+
// - wallet: UiWallet | null
|
|
65
|
+
// - account: UiWalletAccount | null
|
|
66
|
+
// - status: 'connected' | 'connecting' | 'reconnecting' | 'disconnected'
|
|
67
|
+
// - supportedIntents: string[]
|
|
68
|
+
// - isConnected: boolean
|
|
69
|
+
// - isConnecting: boolean
|
|
70
|
+
// - isReconnecting: boolean
|
|
71
|
+
// - isDisconnected: boolean
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Example usage:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const connection = dAppKit.stores.$connection.get();
|
|
78
|
+
|
|
79
|
+
if (connection.isConnected && connection.wallet && connection.account) {
|
|
80
|
+
console.log('Address:', connection.account.address);
|
|
81
|
+
console.log('Wallet:', connection.wallet.name);
|
|
82
|
+
console.log('Available accounts:', connection.wallet.accounts);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Current network store (`$currentNetwork`)
|
|
87
|
+
|
|
88
|
+
Contains the currently selected network as a string:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const currentNetwork = dAppKit.stores.$currentNetwork.get(); // 'mainnet' | 'testnet' | ...
|
|
92
|
+
|
|
93
|
+
// Subscribe to network changes
|
|
94
|
+
dAppKit.stores.$currentNetwork.subscribe((network) => {
|
|
95
|
+
console.log('Switched to network:', network);
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This store is read-only. Use `dAppKit.switchNetwork()` to change networks.
|
|
100
|
+
|
|
101
|
+
### Current client store (`$currentClient`)
|
|
102
|
+
|
|
103
|
+
Contains the SuiClient instance for the current network:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const client = dAppKit.stores.$currentClient.get();
|
|
107
|
+
|
|
108
|
+
// Use the client to query the blockchain
|
|
109
|
+
const balance = await client.getBalance({
|
|
110
|
+
owner: '0x...',
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
This store automatically updates when the network changes.
|
|
115
|
+
|
|
116
|
+
### Wallets store (`$wallets`)
|
|
117
|
+
|
|
118
|
+
Contains the list of available wallets:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const wallets = dAppKit.stores.$wallets.get();
|
|
122
|
+
|
|
123
|
+
wallets.forEach((wallet) => {
|
|
124
|
+
console.log('Wallet:', wallet.name);
|
|
125
|
+
console.log('Icon:', wallet.icon);
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Subscribing to state changes
|
|
130
|
+
|
|
131
|
+
You can subscribe directly to stores:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// Subscribe to connection changes
|
|
135
|
+
dAppKit.stores.$connection.subscribe((connection) => {
|
|
136
|
+
const statusElement = document.getElementById('connection-status');
|
|
137
|
+
if (statusElement === null) return;
|
|
138
|
+
if (connection.isConnected && connection.account) {
|
|
139
|
+
statusElement.textContent = `Connected: ${connection.account.address}`;
|
|
140
|
+
} else {
|
|
141
|
+
statusElement.textContent = 'Not connected';
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// Subscribe to network changes
|
|
146
|
+
dAppKit.stores.$currentNetwork.subscribe((network) => {
|
|
147
|
+
const networkElement = document.getElementById('current-network');
|
|
148
|
+
if (networkElement === null) return;
|
|
149
|
+
networkElement.textContent = `Network: ${network}`;
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## React
|
|
154
|
+
|
|
155
|
+
React users can use the [provided hooks](./react/hooks/use-wallet-connection) instead of direct
|
|
156
|
+
store access.
|
|
157
|
+
|
|
158
|
+
## Properties
|
|
159
|
+
|
|
160
|
+
Besides stores, some values are available as direct properties:
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
// Get available networks (not a store, just a property)
|
|
164
|
+
const networks = dAppKit.networks; // ['mainnet', 'testnet', ...]
|
|
165
|
+
|
|
166
|
+
// Get client for specific network
|
|
167
|
+
const mainnetClient = dAppKit.getClient('mainnet');
|
|
168
|
+
const currentClient = dAppKit.getClient(); // Current network's client
|
|
169
|
+
```
|
package/docs/theming.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# Theming
|
|
2
|
+
|
|
3
|
+
> Customize the appearance of dApp Kit UI components.
|
|
4
|
+
|
|
5
|
+
The dApp Kit web components use a theming system compatible with
|
|
6
|
+
[shadcn/ui](https://ui.shadcn.com/docs/theming). Components are customized using CSS custom
|
|
7
|
+
properties (CSS variables), allowing you to match them to your application's design system.
|
|
8
|
+
|
|
9
|
+
> **Note:** **shadcn/ui Compatibility**: The dApp Kit uses the same CSS custom property naming convention as
|
|
10
|
+
> shadcn/ui. If your application already uses shadcn/ui themes, the dApp Kit components will
|
|
11
|
+
> automatically inherit your theme. You can also use [shadcn/ui's theme
|
|
12
|
+
> builder](https://ui.shadcn.com/themes) to create custom themes.
|
|
13
|
+
|
|
14
|
+
## CSS custom properties
|
|
15
|
+
|
|
16
|
+
All dApp Kit web components support the following CSS custom properties:
|
|
17
|
+
|
|
18
|
+
### Colors
|
|
19
|
+
|
|
20
|
+
These color variables follow the shadcn/ui naming convention. Colors can be defined using any valid
|
|
21
|
+
CSS color format (hex, rgb, hsl, oklch, and others).
|
|
22
|
+
|
|
23
|
+
- **`--background`:** Background color of the component. Used as the default background for UI
|
|
24
|
+
elements.
|
|
25
|
+
- **`--foreground`:** Foreground color of the component. Used as the default text color.
|
|
26
|
+
- **`--primary`:** Primary color used for interactive elements such as buttons and links.
|
|
27
|
+
- **`--primary-foreground`:** Text or icon color placed on top of primary elements.
|
|
28
|
+
- **`--secondary`:** Secondary color used for less prominent interactive elements.
|
|
29
|
+
- **`--secondary-foreground`:** Text or icon color placed on top of secondary elements.
|
|
30
|
+
- **`--border`:** Border color for UI elements.
|
|
31
|
+
- **`--accent`:** Accent color used for highlights and decorative elements.
|
|
32
|
+
- **`--accent-foreground`:** Text or icon color placed on top of accent elements.
|
|
33
|
+
- **`--muted`:** Background color for subtle or muted UI elements (for example, placeholders and
|
|
34
|
+
disabled states).
|
|
35
|
+
- **`--muted-foreground`:** Text or icon color for muted UI elements.
|
|
36
|
+
- **`--popover`:** Background color for floating elements such as popovers, dropdowns, or tooltips.
|
|
37
|
+
- **`--popover-foreground`:** Text or icon color inside popover elements.
|
|
38
|
+
- **`--ring`:** Color used for focus rings (visible focus indicators on interactive elements).
|
|
39
|
+
|
|
40
|
+
### Typography
|
|
41
|
+
|
|
42
|
+
- **`--font-sans`:** Font family used for text content.
|
|
43
|
+
- **`--font-weight-medium`:** Medium font weight for text (typically used for buttons and
|
|
44
|
+
interactive elements).
|
|
45
|
+
- **`--font-weight-semibold`:** Semibold font weight for text (typically used for headings or
|
|
46
|
+
emphasized text).
|
|
47
|
+
|
|
48
|
+
### Layout
|
|
49
|
+
|
|
50
|
+
- **`--radius`:** Border radius used for UI elements.
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Global theming
|
|
55
|
+
|
|
56
|
+
Apply custom properties to affect all dApp Kit components:
|
|
57
|
+
|
|
58
|
+
```css
|
|
59
|
+
:root {
|
|
60
|
+
--primary: #4f46e5;
|
|
61
|
+
--primary-foreground: #ffffff;
|
|
62
|
+
--background: #ffffff;
|
|
63
|
+
--foreground: #0f172a;
|
|
64
|
+
--border: #e2e8f0;
|
|
65
|
+
--radius: 0.5rem;
|
|
66
|
+
--font-sans: 'Inter', sans-serif;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Component-specific theming
|
|
71
|
+
|
|
72
|
+
Target specific components:
|
|
73
|
+
|
|
74
|
+
```css
|
|
75
|
+
mysten-dapp-kit-connect-button {
|
|
76
|
+
--primary: #10b981;
|
|
77
|
+
--primary-foreground: #ffffff;
|
|
78
|
+
--radius: 0.75rem;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
mysten-dapp-kit-connect-modal {
|
|
82
|
+
--background: #f8fafc;
|
|
83
|
+
--popover: #ffffff;
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Dark mode
|
|
88
|
+
|
|
89
|
+
Implement dark mode by overriding the custom properties:
|
|
90
|
+
|
|
91
|
+
```css
|
|
92
|
+
:root {
|
|
93
|
+
--background: #ffffff;
|
|
94
|
+
--foreground: #0f172a;
|
|
95
|
+
--primary: #4f46e5;
|
|
96
|
+
--border: #e2e8f0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@media (prefers-color-scheme: dark) {
|
|
100
|
+
:root {
|
|
101
|
+
--background: #0f172a;
|
|
102
|
+
--foreground: #f1f5f9;
|
|
103
|
+
--primary: #818cf8;
|
|
104
|
+
--border: #334155;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Or use a class-based approach:
|
|
110
|
+
|
|
111
|
+
```css
|
|
112
|
+
.light-theme {
|
|
113
|
+
--background: #ffffff;
|
|
114
|
+
--foreground: #0f172a;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.dark-theme {
|
|
118
|
+
--background: #0f172a;
|
|
119
|
+
--foreground: #f1f5f9;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
<div className={isDark ? 'dark-theme' : 'light-theme'}>
|
|
125
|
+
<ConnectButton />
|
|
126
|
+
</div>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Example: complete theme
|
|
130
|
+
|
|
131
|
+
Here's a complete theme example matching a custom design system:
|
|
132
|
+
|
|
133
|
+
```css
|
|
134
|
+
:root {
|
|
135
|
+
/* Colors */
|
|
136
|
+
--background: hsl(0 0% 100%);
|
|
137
|
+
--foreground: hsl(222.2 84% 4.9%);
|
|
138
|
+
--primary: hsl(221.2 83.2% 53.3%);
|
|
139
|
+
--primary-foreground: hsl(210 40% 98%);
|
|
140
|
+
--secondary: hsl(210 40% 96.1%);
|
|
141
|
+
--secondary-foreground: hsl(222.2 47.4% 11.2%);
|
|
142
|
+
--accent: hsl(210 40% 96.1%);
|
|
143
|
+
--accent-foreground: hsl(222.2 47.4% 11.2%);
|
|
144
|
+
--muted: hsl(210 40% 96.1%);
|
|
145
|
+
--muted-foreground: hsl(215.4 16.3% 46.9%);
|
|
146
|
+
--border: hsl(214.3 31.8% 91.4%);
|
|
147
|
+
--popover: hsl(0 0% 100%);
|
|
148
|
+
--popover-foreground: hsl(222.2 84% 4.9%);
|
|
149
|
+
--ring: hsl(221.2 83.2% 53.3%);
|
|
150
|
+
|
|
151
|
+
/* Typography */
|
|
152
|
+
--font-sans:
|
|
153
|
+
system-ui, -apple-system, 'Segoe UI', 'Roboto', 'Ubuntu', 'Cantarell', 'Noto Sans', sans-serif;
|
|
154
|
+
--font-weight-medium: 500;
|
|
155
|
+
--font-weight-semibold: 600;
|
|
156
|
+
|
|
157
|
+
/* Layout */
|
|
158
|
+
--radius: 0.5rem;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Framework integration
|
|
163
|
+
|
|
164
|
+
### React
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
|
|
168
|
+
return <ConnectButton />;
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Vue
|
|
173
|
+
|
|
174
|
+
```vue
|
|
175
|
+
<template>
|
|
176
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
177
|
+
</template>
|
|
178
|
+
|
|
179
|
+
<style>
|
|
180
|
+
@import './theme.css';
|
|
181
|
+
</style>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Vanilla JavaScript
|
|
185
|
+
|
|
186
|
+
```html
|
|
187
|
+
<!doctype html>
|
|
188
|
+
<html>
|
|
189
|
+
<head>
|
|
190
|
+
<link rel="stylesheet" href="theme.css" />
|
|
191
|
+
</head>
|
|
192
|
+
<body>
|
|
193
|
+
<mysten-dapp-kit-connect-button></mysten-dapp-kit-connect-button>
|
|
194
|
+
</body>
|
|
195
|
+
</html>
|
|
196
|
+
```
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Connect Button
|
|
2
|
+
|
|
3
|
+
> Framework-agnostic web component providing a complete wallet connection UI for Sui dApps.
|
|
4
|
+
|
|
5
|
+
The `<mysten-dapp-kit-connect-button>` Web Component provides a complete wallet connection UI. It
|
|
6
|
+
displays a "Connect Wallet" button when no wallet is connected, and shows the connected account with
|
|
7
|
+
a menu when a wallet is active.
|
|
8
|
+
|
|
9
|
+
> Auto-connect is enabled by default and will attempt to restore the previous wallet connection on
|
|
10
|
+
> page reload. This provides a seamless user experience but can be
|
|
11
|
+
> [disabled](../dapp-kit-instance#disabling-auto-connect) if needed.
|
|
12
|
+
|
|
13
|
+
## Basic usage
|
|
14
|
+
|
|
15
|
+
### Vanilla
|
|
16
|
+
|
|
17
|
+
```html
|
|
18
|
+
<mysten-dapp-kit-connect-button></mysten-dapp-kit-connect-button>
|
|
19
|
+
|
|
20
|
+
<script>
|
|
21
|
+
const button = document.querySelector('mysten-dapp-kit-connect-button');
|
|
22
|
+
button!.instance = dAppKit;
|
|
23
|
+
</script>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Vue
|
|
27
|
+
|
|
28
|
+
```vue
|
|
29
|
+
<script setup>
|
|
30
|
+
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<template>
|
|
34
|
+
<mysten-dapp-kit-connect-button :instance="dAppKit" />
|
|
35
|
+
</template>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### React
|
|
39
|
+
|
|
40
|
+
DApp Kit React bindings provide a [React wrapper](../react/components/connect-button) for the
|
|
41
|
+
button.
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
The connect button component automatically handles:
|
|
46
|
+
|
|
47
|
+
- **Wallet Connection**: Opens a modal to select and connect a wallet
|
|
48
|
+
- **Account Display**: Shows the connected account address
|
|
49
|
+
- **Account Switching**: Provides a menu to switch between multiple accounts when connected
|
|
50
|
+
- **Disconnection**: Includes a disconnect option in the menu
|
|
51
|
+
|
|
52
|
+
## Properties
|
|
53
|
+
|
|
54
|
+
- **instance:** The dApp Kit instance
|
|
55
|
+
- **modalOptions** (optional) - Configuration options for the connect modal
|
|
56
|
+
- `filterFn`: Function to filter available wallets
|
|
57
|
+
- `sortFn`: Function to sort available wallets
|
|
58
|
+
|
|
59
|
+
## Custom button text
|
|
60
|
+
|
|
61
|
+
Use the default slot to customize the button text when not connected:
|
|
62
|
+
|
|
63
|
+
```html
|
|
64
|
+
<mysten-dapp-kit-connect-button>
|
|
65
|
+
<span>Sign In</span>
|
|
66
|
+
</mysten-dapp-kit-connect-button>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Modal integration
|
|
70
|
+
|
|
71
|
+
The connect button automatically manages its own modal. You don't need to add a separate
|
|
72
|
+
`<mysten-dapp-kit-connect-modal>` component when using the connect button.
|
|
73
|
+
|
|
74
|
+
## Connected state
|
|
75
|
+
|
|
76
|
+
When a wallet is connected, the button transforms into an account menu that displays:
|
|
77
|
+
|
|
78
|
+
- Account address (truncated)
|
|
79
|
+
- SUI balance
|
|
80
|
+
- Network indicator
|
|
81
|
+
- Menu options for:
|
|
82
|
+
- Switching accounts (if multiple accounts available)
|
|
83
|
+
- Managing connection
|
|
84
|
+
- Disconnecting
|
|
85
|
+
|
|
86
|
+
## Server-side rendering (SSR)
|
|
87
|
+
|
|
88
|
+
The `<mysten-dapp-kit-connect-button>` Web Component can be only client-side rendered, as wallets
|
|
89
|
+
are detected in the browser. For Next.js guide see [here](../getting-started/next-js).
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Connect Modal
|
|
2
|
+
|
|
3
|
+
> Framework-agnostic web component for wallet selection and connection in Sui dApps.
|
|
4
|
+
|
|
5
|
+
The `<mysten-dapp-kit-connect-modal>` Web Component provides a modal dialog for wallet connection.
|
|
6
|
+
While most apps should use the [`Connect Button`](./connect-button) which includes an integrated
|
|
7
|
+
modal, the standalone modal is useful for advanced scenarios requiring custom trigger mechanisms.
|
|
8
|
+
|
|
9
|
+
## When to use
|
|
10
|
+
|
|
11
|
+
Use the standalone modal when you need:
|
|
12
|
+
|
|
13
|
+
- **Custom trigger elements:** Menu items, keyboard shortcuts, or other non-button triggers
|
|
14
|
+
- **Complex UI flows:** Integration into existing navigation or custom wallet management interfaces
|
|
15
|
+
- **Programmatic control:** Opening the modal based on application logic or user actions
|
|
16
|
+
- **Multiple triggers:** Different parts of your UI that can open the same modal
|
|
17
|
+
|
|
18
|
+
For standard wallet connection with a button, use the [Connect Button](./connect-button) component
|
|
19
|
+
instead.
|
|
20
|
+
|
|
21
|
+
## Basic usage
|
|
22
|
+
|
|
23
|
+
### Vanilla
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<button id="custom-trigger">Open Wallet Selector</button>
|
|
27
|
+
<mysten-dapp-kit-connect-modal></mysten-dapp-kit-connect-modal>
|
|
28
|
+
|
|
29
|
+
<script>
|
|
30
|
+
import { createDAppKit } from '@mysten/dapp-kit-core';
|
|
31
|
+
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
32
|
+
|
|
33
|
+
const GRPC_URLS = {
|
|
34
|
+
mainnet: 'https://fullnode.mainnet.sui.io:443',
|
|
35
|
+
testnet: 'https://fullnode.testnet.sui.io:443',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const dAppKit = createDAppKit({
|
|
39
|
+
enableBurnerWallet: import.meta.env.DEV,
|
|
40
|
+
networks: ['mainnet', 'testnet'],
|
|
41
|
+
defaultNetwork: 'testnet',
|
|
42
|
+
createClient(network) {
|
|
43
|
+
return new SuiGrpcClient({ network, baseUrl: GRPC_URLS[network] });
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const modal = document.querySelector('mysten-dapp-kit-connect-modal');
|
|
48
|
+
modal!.instance = dAppKit;
|
|
49
|
+
|
|
50
|
+
const trigger = document.getElementById('custom-trigger');
|
|
51
|
+
trigger?.addEventListener('click', () => {
|
|
52
|
+
modal!.show();
|
|
53
|
+
});
|
|
54
|
+
</script>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Vue
|
|
58
|
+
|
|
59
|
+
```vue
|
|
60
|
+
<script setup lang="ts">
|
|
61
|
+
|
|
62
|
+
const modalRef = ref();
|
|
63
|
+
const isModalOpen = ref(false);
|
|
64
|
+
|
|
65
|
+
const openModal = () => {
|
|
66
|
+
// Using property binding
|
|
67
|
+
isModalOpen.value = true;
|
|
68
|
+
// Or using method
|
|
69
|
+
modalRef.value.show();
|
|
70
|
+
};
|
|
71
|
+
</script>
|
|
72
|
+
|
|
73
|
+
<template>
|
|
74
|
+
<!-- Custom trigger -->
|
|
75
|
+
<button @click="openModal">Select Wallet</button>
|
|
76
|
+
|
|
77
|
+
<!-- Modal component -->
|
|
78
|
+
<mysten-dapp-kit-connect-modal
|
|
79
|
+
ref="modalRef"
|
|
80
|
+
:instance="dAppKit"
|
|
81
|
+
:open="isModalOpen"
|
|
82
|
+
@closed="isModalOpen = false"
|
|
83
|
+
/>
|
|
84
|
+
</template>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### React
|
|
88
|
+
|
|
89
|
+
DApp Kit React bindings provide a [React wrapper](../react/components/connect-modal) for the modal.
|
|
90
|
+
|
|
91
|
+
## Properties
|
|
92
|
+
|
|
93
|
+
- **instance:** The dApp Kit instance
|
|
94
|
+
- **open:** Boolean to control modal visibility programmatically
|
|
95
|
+
- **filterFn** (optional) - Function to filter available wallets
|
|
96
|
+
- Type: `(wallet: UiWallet, index: number, array: UiWallet[]) => boolean`
|
|
97
|
+
- **sortFn** (optional) - Function to sort available wallets
|
|
98
|
+
- Type: `(a: UiWallet, b: UiWallet) => number`
|
|
99
|
+
|
|
100
|
+
## Methods
|
|
101
|
+
|
|
102
|
+
### show()
|
|
103
|
+
|
|
104
|
+
Opens the modal programmatically:
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
const modal = document.querySelector('mysten-dapp-kit-connect-modal');
|
|
108
|
+
await modal?.show();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### close(returnValue: string)
|
|
112
|
+
|
|
113
|
+
Closes the modal with an optional return value:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
const modal = document.querySelector('mysten-dapp-kit-connect-modal');
|
|
117
|
+
await modal?.close('user-cancelled');
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Events
|
|
121
|
+
|
|
122
|
+
The modal fires several events during its lifecycle:
|
|
123
|
+
|
|
124
|
+
- **open:** Fired before the modal opens (cancelable)
|
|
125
|
+
- **opened:** Fired after the modal has fully opened
|
|
126
|
+
- **close:** Fired before the modal closes (cancelable)
|
|
127
|
+
- **closed:** Fired after the modal has fully closed
|
|
128
|
+
- **cancel:** Fired when clicking backdrop or pressing Escape (cancelable)
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
const modal = document.querySelector('mysten-dapp-kit-connect-modal');
|
|
132
|
+
|
|
133
|
+
modal?.addEventListener('open', (event) => {
|
|
134
|
+
console.log('Modal is about to open');
|
|
135
|
+
// Prevent opening if needed
|
|
136
|
+
// event.preventDefault();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
modal?.addEventListener('opened', () => {
|
|
140
|
+
console.log('Modal is now open');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
modal?.addEventListener('closed', () => {
|
|
144
|
+
console.log('Modal has closed');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
modal?.addEventListener('cancel', (event) => {
|
|
148
|
+
console.log('User clicked backdrop or pressed Escape');
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Examples
|
|
153
|
+
|
|
154
|
+
### Programmatic control
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
const modal = document.querySelector('mysten-dapp-kit-connect-modal');
|
|
158
|
+
modal.instance = dAppKit;
|
|
159
|
+
|
|
160
|
+
// Open modal when user tries to perform an action that requires connection
|
|
161
|
+
async function performAction() {
|
|
162
|
+
const connection = dAppKit.stores.$connection.get();
|
|
163
|
+
|
|
164
|
+
if (!connection.account) {
|
|
165
|
+
await modal.show();
|
|
166
|
+
// Wait for connection or cancellation
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Proceed with action...
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Server-side rendering (SSR)
|
|
175
|
+
|
|
176
|
+
The `<mysten-dapp-kit-connect-modal>` Web Component can be only client-side rendered, as wallets are
|
|
177
|
+
detected in the browser. For Next.js guide see [here](../getting-started/next-js).
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@mysten/dapp-kit-react",
|
|
3
3
|
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
4
4
|
"description": "React bindings for @mysten/dapp-kit-core",
|
|
5
|
-
"version": "2.0.
|
|
5
|
+
"version": "2.0.3",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"files": [
|
|
8
8
|
"LICENSE",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@lit/react": "^1.0.8",
|
|
45
45
|
"@nanostores/react": "^1.0.0",
|
|
46
46
|
"nanostores": "^1.1.0",
|
|
47
|
-
"@mysten/dapp-kit-core": "^1.3.
|
|
47
|
+
"@mysten/dapp-kit-core": "^1.3.2"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@types/react": ">=17.0.0",
|