@txnlab/use-wallet-ui-react 0.1.0 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +459 -127
- package/dist/cjs/index.cjs +9 -9
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +8148 -3276
- package/dist/esm/index.js.map +1 -1
- package/dist/style.css +1214 -0
- package/dist/types/index.d.cts +111 -3
- package/dist/types/index.d.ts +111 -3
- package/package.json +11 -5
- package/dist/cjs/assets/use-wallet-ui-react.css +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,133 @@
|
|
|
1
1
|
# @txnlab/use-wallet-ui-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ready-to-use UI components for Algorand wallet integration, built as a companion to [@txnlab/use-wallet](https://github.com/TxnLab/use-wallet).
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@txnlab/use-wallet-ui-react)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Quick Start
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @txnlab/use-wallet-ui-react
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
This package provides polished UI components that work on top of `@txnlab/use-wallet-react`. Choose the setup that matches your project:
|
|
15
|
+
|
|
16
|
+
### If you're using Tailwind CSS
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
import {
|
|
20
|
+
NetworkId,
|
|
21
|
+
WalletId,
|
|
22
|
+
WalletManager,
|
|
23
|
+
WalletProvider,
|
|
24
|
+
} from '@txnlab/use-wallet-react'
|
|
25
|
+
import { WalletUIProvider, WalletButton } from '@txnlab/use-wallet-ui-react'
|
|
26
|
+
|
|
27
|
+
// Configure the wallets you want to use
|
|
28
|
+
const walletManager = new WalletManager({
|
|
29
|
+
wallets: [
|
|
30
|
+
WalletId.PERA,
|
|
31
|
+
WalletId.DEFLY,
|
|
32
|
+
WalletId.LUTE,
|
|
33
|
+
// Add more wallets as needed
|
|
34
|
+
],
|
|
35
|
+
defaultNetwork: NetworkId.MAINNET,
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function App() {
|
|
39
|
+
return (
|
|
40
|
+
<WalletProvider manager={walletManager}>
|
|
41
|
+
<WalletUIProvider>
|
|
42
|
+
<div>
|
|
43
|
+
<WalletButton />
|
|
44
|
+
</div>
|
|
45
|
+
</WalletUIProvider>
|
|
46
|
+
</WalletProvider>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### If you're NOT using Tailwind CSS
|
|
52
|
+
|
|
53
|
+
```jsx
|
|
54
|
+
import {
|
|
55
|
+
NetworkId,
|
|
56
|
+
WalletId,
|
|
57
|
+
WalletManager,
|
|
58
|
+
WalletProvider,
|
|
59
|
+
} from '@txnlab/use-wallet-react'
|
|
60
|
+
import { WalletUIProvider, WalletButton } from '@txnlab/use-wallet-ui-react'
|
|
61
|
+
// Import our pre-built styles
|
|
62
|
+
import '@txnlab/use-wallet-ui-react/dist/style.css'
|
|
63
|
+
|
|
64
|
+
// Configure the wallets you want to use
|
|
65
|
+
const walletManager = new WalletManager({
|
|
66
|
+
wallets: [
|
|
67
|
+
WalletId.PERA,
|
|
68
|
+
WalletId.DEFLY,
|
|
69
|
+
WalletId.LUTE,
|
|
70
|
+
// Add more wallets as needed
|
|
71
|
+
],
|
|
72
|
+
defaultNetwork: NetworkId.MAINNET,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
function App() {
|
|
76
|
+
return (
|
|
77
|
+
<WalletProvider manager={walletManager}>
|
|
78
|
+
<WalletUIProvider>
|
|
79
|
+
{/* Add data-wallet-ui attribute when not using Tailwind */}
|
|
80
|
+
<div data-wallet-ui>
|
|
81
|
+
<WalletButton />
|
|
82
|
+
</div>
|
|
83
|
+
</WalletUIProvider>
|
|
84
|
+
</WalletProvider>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
That's it! You now have a fully functional wallet connection system with:
|
|
90
|
+
|
|
91
|
+
- Clean, accessible UI components
|
|
92
|
+
- NFD integration
|
|
93
|
+
- ALGO balance display
|
|
94
|
+
- Account switching
|
|
95
|
+
- Dark/light mode support
|
|
96
|
+
|
|
97
|
+
> **Note:** This is the React implementation of the UI components. Future versions will support Vue and SolidJS to match all frameworks supported by the core `@txnlab/use-wallet` library.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Table of Contents
|
|
102
|
+
|
|
103
|
+
- [Dependencies](#dependencies)
|
|
104
|
+
- [Installation](#installation)
|
|
105
|
+
- [Styling Options](#styling-options)
|
|
106
|
+
- [Without Tailwind CSS](#without-tailwind-css)
|
|
107
|
+
- [With Tailwind CSS](#with-tailwind-css)
|
|
108
|
+
- [Basic Usage](#basic-usage)
|
|
109
|
+
- [Component API](#component-api)
|
|
110
|
+
- [NFD Integration](#nfd-integration)
|
|
111
|
+
- [Account Information](#account-information)
|
|
112
|
+
- [Advanced Customization](#advanced-customization)
|
|
113
|
+
- [Integration with Tanstack Query](#integration-with-tanstack-query)
|
|
114
|
+
- [How It Works](#how-it-works)
|
|
115
|
+
- [License](#license)
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Dependencies
|
|
120
|
+
|
|
121
|
+
### Required
|
|
122
|
+
|
|
123
|
+
- `@txnlab/use-wallet-react` v4
|
|
124
|
+
- `algosdk` v3 (required for use-wallet v4)
|
|
125
|
+
- React v18 or v19
|
|
126
|
+
|
|
127
|
+
### Optional & Integrated
|
|
128
|
+
|
|
129
|
+
- **Tanstack Query**: Used internally for NFD lookups (built-in, but can integrate with your existing setup)
|
|
130
|
+
- **Tailwind CSS**: Supported but not required (the library works with or without it)
|
|
4
131
|
|
|
5
132
|
## Installation
|
|
6
133
|
|
|
@@ -15,219 +142,424 @@ yarn add @txnlab/use-wallet-ui-react
|
|
|
15
142
|
pnpm add @txnlab/use-wallet-ui-react
|
|
16
143
|
```
|
|
17
144
|
|
|
18
|
-
##
|
|
145
|
+
## Styling Options
|
|
19
146
|
|
|
20
|
-
|
|
147
|
+
The library supports two styling approaches:
|
|
21
148
|
|
|
22
|
-
###
|
|
149
|
+
### Option 1: Using Tailwind CSS (Recommended)
|
|
150
|
+
|
|
151
|
+
Using [Tailwind CSS](https://tailwindcss.com/) provides the richest customization experience:
|
|
152
|
+
|
|
153
|
+
- Full access to Tailwind's utility classes for customization
|
|
154
|
+
- Hover, focus, and active states with simple modifiers
|
|
155
|
+
- Dark mode support with the `dark:` variant
|
|
156
|
+
- Responsive design with breakpoint prefixes
|
|
157
|
+
- Animation and transition utilities
|
|
158
|
+
- Theme customization through your Tailwind config
|
|
159
|
+
|
|
160
|
+
#### With Tailwind CSS v4
|
|
161
|
+
|
|
162
|
+
```css
|
|
163
|
+
/* In your CSS file */
|
|
164
|
+
@import 'tailwindcss';
|
|
165
|
+
@source "../node_modules/@txnlab/use-wallet-ui-react";
|
|
166
|
+
```
|
|
23
167
|
|
|
24
|
-
|
|
168
|
+
This uses the `@source` directive to tell Tailwind to scan our library for classes. See the [Tailwind CSS v4 Installation Guide](https://tailwindcss.com/docs/installation) for setup instructions.
|
|
169
|
+
|
|
170
|
+
#### With Tailwind CSS v3
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
// tailwind.config.js
|
|
174
|
+
module.exports = {
|
|
175
|
+
content: [
|
|
176
|
+
'./src/**/*.{js,ts,jsx,tsx}',
|
|
177
|
+
// Add this line to scan our components
|
|
178
|
+
'./node_modules/@txnlab/use-wallet-ui-react/dist/**/*.{js,ts,jsx,tsx}',
|
|
179
|
+
],
|
|
180
|
+
// ...rest of your config
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
See the [Tailwind CSS v3 Installation Guide](https://v3.tailwindcss.com/docs/installation) for setup instructions.
|
|
185
|
+
|
|
186
|
+
### Option 2: Using the Pre-built CSS
|
|
187
|
+
|
|
188
|
+
For projects that don't use Tailwind, we provide a pre-built CSS file:
|
|
25
189
|
|
|
26
190
|
```jsx
|
|
27
|
-
|
|
191
|
+
// 1. Import the pre-built CSS
|
|
192
|
+
import '@txnlab/use-wallet-ui-react/dist/style.css'
|
|
28
193
|
|
|
29
194
|
function App() {
|
|
30
195
|
return (
|
|
31
|
-
|
|
196
|
+
// 2. Add the data-wallet-ui attribute to any container with wallet components
|
|
197
|
+
<div data-wallet-ui>
|
|
32
198
|
<WalletButton />
|
|
33
199
|
</div>
|
|
34
200
|
)
|
|
35
201
|
}
|
|
36
202
|
```
|
|
37
203
|
|
|
38
|
-
|
|
204
|
+
**Important:** Add the `data-wallet-ui` attribute to scope our styles and prevent conflicts with your application's existing styles.
|
|
39
205
|
|
|
40
|
-
|
|
41
|
-
- Opens the wallet selection dialog when clicked
|
|
42
|
-
- Shows the connected wallet interface after connection
|
|
43
|
-
- Handles disconnection
|
|
206
|
+
While this approach offers less flexibility for customization compared to Tailwind, it provides a simple way to use the components with minimal setup.
|
|
44
207
|
|
|
45
|
-
|
|
208
|
+
## Basic Usage
|
|
46
209
|
|
|
47
|
-
|
|
210
|
+
This library builds on top of `@txnlab/use-wallet-react` to provide UI components for wallet connectivity. Here's the basic setup:
|
|
211
|
+
|
|
212
|
+
### 1. Set up the Providers
|
|
48
213
|
|
|
49
214
|
```jsx
|
|
50
|
-
import { useWallet } from '@txnlab/use-wallet-react'
|
|
51
215
|
import {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} from '@txnlab/use-wallet-
|
|
216
|
+
NetworkId,
|
|
217
|
+
WalletId,
|
|
218
|
+
WalletManager,
|
|
219
|
+
WalletProvider,
|
|
220
|
+
} from '@txnlab/use-wallet-react'
|
|
221
|
+
import { WalletUIProvider } from '@txnlab/use-wallet-ui-react'
|
|
222
|
+
|
|
223
|
+
// Create and configure the wallet manager
|
|
224
|
+
const walletManager = new WalletManager({
|
|
225
|
+
wallets: [
|
|
226
|
+
// Add the wallets you want to support
|
|
227
|
+
WalletId.PERA,
|
|
228
|
+
WalletId.DEFLY,
|
|
229
|
+
WalletId.LUTE,
|
|
230
|
+
WalletId.EXODUS,
|
|
231
|
+
// For WalletConnect, you'll need a project ID
|
|
232
|
+
{
|
|
233
|
+
id: WalletId.WALLETCONNECT,
|
|
234
|
+
options: { projectId: 'your-project-id' },
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
defaultNetwork: NetworkId.TESTNET, // Or MAINNET for production
|
|
238
|
+
})
|
|
57
239
|
|
|
58
240
|
function App() {
|
|
59
|
-
|
|
241
|
+
return (
|
|
242
|
+
<WalletProvider manager={walletManager}>
|
|
243
|
+
<WalletUIProvider>{/* Your app content */}</WalletUIProvider>
|
|
244
|
+
</WalletProvider>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The `WalletProvider` from `@txnlab/use-wallet-react` manages the wallet connections using the provided `WalletManager` configuration, while `WalletUIProvider` adds UI-specific features like NFD lookups and data prefetching.
|
|
60
250
|
|
|
251
|
+
### 2. Add the Wallet Button
|
|
252
|
+
|
|
253
|
+
The simplest way to enable wallet connectivity is with the `WalletButton` component:
|
|
254
|
+
|
|
255
|
+
```jsx
|
|
256
|
+
import { WalletButton } from '@txnlab/use-wallet-ui-react'
|
|
257
|
+
|
|
258
|
+
function MyNav() {
|
|
61
259
|
return (
|
|
62
|
-
<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
<ConnectedWalletMenu>
|
|
66
|
-
<ConnectedWalletButton
|
|
67
|
-
className="border-2 border-blue-500"
|
|
68
|
-
showBalance={false}
|
|
69
|
-
/>
|
|
70
|
-
</ConnectedWalletMenu>
|
|
71
|
-
) : (
|
|
72
|
-
// Disconnected state with customized button
|
|
73
|
-
<ConnectWalletMenu>
|
|
74
|
-
<ConnectWalletButton className="bg-green-500 hover:bg-green-600">
|
|
75
|
-
Connect your wallet
|
|
76
|
-
</ConnectWalletButton>
|
|
77
|
-
</ConnectWalletMenu>
|
|
78
|
-
)}
|
|
79
|
-
</div>
|
|
260
|
+
<nav>
|
|
261
|
+
<WalletButton />
|
|
262
|
+
</nav>
|
|
80
263
|
)
|
|
81
264
|
}
|
|
82
265
|
```
|
|
83
266
|
|
|
84
|
-
|
|
267
|
+
The `WalletButton` is an all-in-one solution that:
|
|
268
|
+
|
|
269
|
+
- Shows a connect button when disconnected
|
|
270
|
+
- Opens the wallet selection dialog when clicked
|
|
271
|
+
- Displays the connected wallet after connection
|
|
272
|
+
- Shows NFD names and avatars when available
|
|
273
|
+
- Provides access to account switching and disconnection
|
|
85
274
|
|
|
86
|
-
|
|
275
|
+
## Component API
|
|
87
276
|
|
|
88
|
-
|
|
277
|
+
The library provides several components that can be used independently or together:
|
|
278
|
+
|
|
279
|
+
### WalletUIProvider
|
|
280
|
+
|
|
281
|
+
Required wrapper that enables NFD lookups and data prefetching:
|
|
89
282
|
|
|
90
283
|
```jsx
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
284
|
+
<WalletUIProvider
|
|
285
|
+
// Optional configurations
|
|
286
|
+
enablePrefetching={false} // Prefetch data for all accounts in a wallet (default: true)
|
|
287
|
+
prefetchNfdView="brief" // Data view for NFD prefetching (default: 'thumbnail')
|
|
288
|
+
queryClient={yourQueryClient} // Optional: integrate with existing Tanstack Query
|
|
289
|
+
>
|
|
290
|
+
{/* Your app content */}
|
|
291
|
+
</WalletUIProvider>
|
|
292
|
+
```
|
|
96
293
|
|
|
97
|
-
|
|
98
|
-
|
|
294
|
+
### WalletButton
|
|
295
|
+
|
|
296
|
+
All-in-one solution for wallet connectivity - combines the connect and connected states:
|
|
297
|
+
|
|
298
|
+
```jsx
|
|
299
|
+
<WalletButton />
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Connection Components
|
|
303
|
+
|
|
304
|
+
For more customization, use these component pairs:
|
|
305
|
+
|
|
306
|
+
#### Connect State (when disconnected)
|
|
307
|
+
|
|
308
|
+
```jsx
|
|
309
|
+
import { ConnectWalletButton, ConnectWalletMenu } from '@txnlab/use-wallet-ui-react'
|
|
310
|
+
|
|
311
|
+
// Just the menu with default button:
|
|
312
|
+
<ConnectWalletMenu />
|
|
313
|
+
|
|
314
|
+
// Customized button:
|
|
315
|
+
<ConnectWalletMenu>
|
|
316
|
+
<ConnectWalletButton className="bg-blue-500">
|
|
317
|
+
Connect Wallet
|
|
318
|
+
</ConnectWalletButton>
|
|
319
|
+
</ConnectWalletMenu>
|
|
320
|
+
|
|
321
|
+
// Fully custom trigger:
|
|
322
|
+
<ConnectWalletMenu>
|
|
323
|
+
<button className="my-custom-button">Connect</button>
|
|
324
|
+
</ConnectWalletMenu>
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
#### Connected State (when wallet is connected)
|
|
328
|
+
|
|
329
|
+
```jsx
|
|
330
|
+
import { ConnectedWalletButton, ConnectedWalletMenu } from '@txnlab/use-wallet-ui-react'
|
|
331
|
+
|
|
332
|
+
// Just the menu with default button:
|
|
333
|
+
<ConnectedWalletMenu />
|
|
334
|
+
|
|
335
|
+
// Customized button:
|
|
336
|
+
<ConnectedWalletMenu>
|
|
337
|
+
<ConnectedWalletButton className="border-2 border-green-500" />
|
|
338
|
+
</ConnectedWalletMenu>
|
|
339
|
+
|
|
340
|
+
// Fully custom trigger:
|
|
341
|
+
<ConnectedWalletMenu>
|
|
342
|
+
<div className="flex items-center gap-2">
|
|
343
|
+
<span>My Wallet</span>
|
|
344
|
+
</div>
|
|
345
|
+
</ConnectedWalletMenu>
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
### NfdAvatar
|
|
349
|
+
|
|
350
|
+
Renders NFD avatars with automatic IPFS gateway handling:
|
|
351
|
+
|
|
352
|
+
```jsx
|
|
353
|
+
import { useNfd, NfdAvatar } from '@txnlab/use-wallet-ui-react'
|
|
354
|
+
|
|
355
|
+
function Profile() {
|
|
356
|
+
const nfdQuery = useNfd()
|
|
99
357
|
|
|
100
358
|
return (
|
|
101
|
-
<
|
|
102
|
-
{
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
</button>
|
|
108
|
-
</ConnectedWalletMenu>
|
|
109
|
-
) : (
|
|
110
|
-
// Disconnected state - completely custom button
|
|
111
|
-
<ConnectWalletMenu>
|
|
112
|
-
<button className="custom-connect-button">
|
|
113
|
-
My Custom Connect Button
|
|
114
|
-
</button>
|
|
115
|
-
</ConnectWalletMenu>
|
|
116
|
-
)}
|
|
117
|
-
</div>
|
|
359
|
+
<NfdAvatar
|
|
360
|
+
nfd={nfdQuery.data}
|
|
361
|
+
size={48}
|
|
362
|
+
className="rounded-full border-2"
|
|
363
|
+
alt="User profile"
|
|
364
|
+
/>
|
|
118
365
|
)
|
|
119
366
|
}
|
|
120
367
|
```
|
|
121
368
|
|
|
122
|
-
|
|
369
|
+
## NFD Integration
|
|
123
370
|
|
|
124
|
-
|
|
371
|
+
This library includes built-in support for [NFD (Non-Fungible Domains)](https://app.nf.domains/) - Algorand's naming service that provides human-readable identities for wallet addresses.
|
|
372
|
+
|
|
373
|
+
### The useNfd Hook
|
|
125
374
|
|
|
126
375
|
```jsx
|
|
127
|
-
import {
|
|
128
|
-
import {
|
|
129
|
-
ConnectWalletButton,
|
|
130
|
-
ConnectedWalletButton,
|
|
131
|
-
} from '@txnlab/use-wallet-ui-react'
|
|
376
|
+
import { useNfd } from '@txnlab/use-wallet-ui-react'
|
|
132
377
|
|
|
133
|
-
function
|
|
134
|
-
|
|
378
|
+
function Profile() {
|
|
379
|
+
// Gets NFD data for the currently connected address
|
|
380
|
+
const nfdQuery = useNfd()
|
|
381
|
+
|
|
382
|
+
if (nfdQuery.isLoading) return <div>Loading...</div>
|
|
383
|
+
|
|
384
|
+
// Access NFD properties
|
|
385
|
+
const name = nfdQuery.data?.name
|
|
386
|
+
const userProperties = nfdQuery.data?.properties?.userDefined
|
|
135
387
|
|
|
136
388
|
return (
|
|
137
389
|
<div>
|
|
138
|
-
{
|
|
139
|
-
|
|
140
|
-
className="custom-class"
|
|
141
|
-
showBalance={false}
|
|
142
|
-
onClick={() => console.log('Connected button clicked')}
|
|
143
|
-
/>
|
|
144
|
-
) : (
|
|
145
|
-
<ConnectWalletButton
|
|
146
|
-
className="custom-class"
|
|
147
|
-
onClick={() => console.log('Connect button clicked')}
|
|
148
|
-
/>
|
|
149
|
-
)}
|
|
390
|
+
<h2>{name || 'No NFD found'}</h2>
|
|
391
|
+
<p>{userProperties?.bio || 'No bio'}</p>
|
|
150
392
|
</div>
|
|
151
393
|
)
|
|
152
394
|
}
|
|
153
395
|
```
|
|
154
396
|
|
|
155
|
-
|
|
397
|
+
### NFD Features
|
|
156
398
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
399
|
+
- Automatic lookup for the active wallet address
|
|
400
|
+
- Support for different data views: 'tiny', 'thumbnail', 'brief', 'full'
|
|
401
|
+
- Efficient caching via Tanstack Query
|
|
402
|
+
- Works with both MainNet and TestNet
|
|
160
403
|
|
|
161
|
-
|
|
162
|
-
|
|
404
|
+
```jsx
|
|
405
|
+
// Customizing the NFD lookup
|
|
406
|
+
const nfdQuery = useNfd({
|
|
407
|
+
enabled: true, // Whether to enable the lookup
|
|
408
|
+
view: 'brief', // Data view to request (default: 'thumbnail')
|
|
409
|
+
})
|
|
410
|
+
```
|
|
163
411
|
|
|
164
|
-
|
|
412
|
+
## Account Information
|
|
165
413
|
|
|
166
|
-
|
|
414
|
+
Get account data like ALGO balance and asset holdings using the `useAccountInfo` hook:
|
|
167
415
|
|
|
168
416
|
```jsx
|
|
169
|
-
|
|
417
|
+
import { useAccountInfo } from '@txnlab/use-wallet-ui-react'
|
|
418
|
+
|
|
419
|
+
function Balance() {
|
|
420
|
+
// Gets account data for the connected address
|
|
421
|
+
const accountQuery = useAccountInfo()
|
|
422
|
+
|
|
423
|
+
if (accountQuery.isLoading) return <div>Loading...</div>
|
|
424
|
+
if (!accountQuery.data) return <div>No account data</div>
|
|
425
|
+
|
|
426
|
+
// Convert microAlgos to Algos (1 ALGO = 1,000,000 microAlgos)
|
|
427
|
+
const algoBalance = Number(accountQuery.data.amount) / 1_000_000
|
|
428
|
+
|
|
429
|
+
// Available balance (total minus minimum required)
|
|
430
|
+
const minBalance = Number(accountQuery.data.minBalance) / 1_000_000
|
|
431
|
+
const availableBalance = Math.max(0, algoBalance - minBalance)
|
|
432
|
+
|
|
433
|
+
return (
|
|
434
|
+
<div>
|
|
435
|
+
<div>Total: {algoBalance.toFixed(2)} ALGO</div>
|
|
436
|
+
<div>Available: {availableBalance.toFixed(2)} ALGO</div>
|
|
437
|
+
</div>
|
|
438
|
+
)
|
|
439
|
+
}
|
|
170
440
|
```
|
|
171
441
|
|
|
172
|
-
|
|
442
|
+
## Advanced Customization
|
|
443
|
+
|
|
444
|
+
### Styling Without Tailwind
|
|
445
|
+
|
|
446
|
+
When not using Tailwind, you have two options for customizing components:
|
|
447
|
+
|
|
448
|
+
#### 1. Using the `style` prop
|
|
173
449
|
|
|
174
450
|
```jsx
|
|
175
|
-
<
|
|
176
|
-
|
|
177
|
-
|
|
451
|
+
<ConnectWalletButton
|
|
452
|
+
style={{
|
|
453
|
+
backgroundColor: '#3366FF',
|
|
454
|
+
color: 'white',
|
|
455
|
+
fontWeight: 'bold',
|
|
456
|
+
}}
|
|
457
|
+
>
|
|
458
|
+
Connect
|
|
459
|
+
</ConnectWalletButton>
|
|
178
460
|
```
|
|
179
461
|
|
|
180
|
-
|
|
462
|
+
#### 2. Using CSS selectors
|
|
181
463
|
|
|
182
|
-
|
|
464
|
+
First, add a custom class to the button:
|
|
183
465
|
|
|
184
466
|
```jsx
|
|
185
|
-
<
|
|
467
|
+
<ConnectWalletButton className="connect-button">Connect</ConnectWalletButton>
|
|
186
468
|
```
|
|
187
469
|
|
|
188
|
-
|
|
470
|
+
Then target it in your CSS:
|
|
471
|
+
|
|
472
|
+
```css
|
|
473
|
+
/* In your CSS */
|
|
474
|
+
[data-wallet-ui] .connect-button {
|
|
475
|
+
font-family: 'Your Custom Font', sans-serif;
|
|
476
|
+
background-color: #3366ff;
|
|
477
|
+
color: white;
|
|
478
|
+
padding: 8px 16px;
|
|
479
|
+
border-radius: 4px;
|
|
480
|
+
transition: background-color 0.2s;
|
|
481
|
+
}
|
|
189
482
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
</ConnectedWalletMenu>
|
|
483
|
+
[data-wallet-ui] .connect-button:hover {
|
|
484
|
+
background-color: #2952cc;
|
|
485
|
+
}
|
|
194
486
|
```
|
|
195
487
|
|
|
196
|
-
###
|
|
488
|
+
### Building Custom Wallet UI
|
|
197
489
|
|
|
198
|
-
|
|
490
|
+
For complete control, use the menu components with your own UI elements:
|
|
199
491
|
|
|
200
492
|
```jsx
|
|
201
|
-
|
|
493
|
+
import { useWallet } from '@txnlab/use-wallet-react'
|
|
494
|
+
import {
|
|
495
|
+
ConnectWalletMenu,
|
|
496
|
+
ConnectedWalletMenu,
|
|
497
|
+
} from '@txnlab/use-wallet-ui-react'
|
|
498
|
+
|
|
499
|
+
function CustomWalletButton() {
|
|
500
|
+
const { activeAddress } = useWallet()
|
|
501
|
+
|
|
502
|
+
return activeAddress ? (
|
|
503
|
+
<ConnectedWalletMenu>
|
|
504
|
+
<YourCustomConnectedButton />
|
|
505
|
+
</ConnectedWalletMenu>
|
|
506
|
+
) : (
|
|
507
|
+
<ConnectWalletMenu>
|
|
508
|
+
<YourCustomConnectButton />
|
|
509
|
+
</ConnectWalletMenu>
|
|
510
|
+
)
|
|
511
|
+
}
|
|
202
512
|
```
|
|
203
513
|
|
|
204
|
-
|
|
514
|
+
## Integration with Tanstack Query
|
|
205
515
|
|
|
206
|
-
|
|
516
|
+
This library uses [Tanstack Query](https://tanstack.com/query/latest) internally for data fetching. If your application already uses Tanstack Query, you can integrate the two to avoid duplicate caches:
|
|
207
517
|
|
|
208
518
|
```jsx
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
519
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
520
|
+
import { WalletManager, WalletProvider } from '@txnlab/use-wallet-react'
|
|
521
|
+
import { WalletUIProvider } from '@txnlab/use-wallet-ui-react'
|
|
522
|
+
|
|
523
|
+
const queryClient = new QueryClient()
|
|
524
|
+
const walletManager = new WalletManager({...})
|
|
214
525
|
|
|
215
|
-
|
|
526
|
+
function App() {
|
|
527
|
+
return (
|
|
528
|
+
<QueryClientProvider client={queryClient}>
|
|
529
|
+
<WalletProvider manager={walletManager}>
|
|
530
|
+
<WalletUIProvider queryClient={queryClient}>
|
|
531
|
+
{/* Your app */}
|
|
532
|
+
</WalletUIProvider>
|
|
533
|
+
</WalletProvider>
|
|
534
|
+
</QueryClientProvider>
|
|
535
|
+
)
|
|
536
|
+
}
|
|
537
|
+
```
|
|
216
538
|
|
|
217
|
-
|
|
539
|
+
By sharing the QueryClient, both your application and the wallet UI components will use the same query cache.
|
|
218
540
|
|
|
219
541
|
## How It Works
|
|
220
542
|
|
|
221
|
-
|
|
543
|
+
The library follows a simple workflow:
|
|
544
|
+
|
|
545
|
+
1. **Disconnected State**: `WalletButton` shows a connect button
|
|
546
|
+
2. **Connection Dialog**: Clicking opens a dropdown with available Algorand wallets
|
|
547
|
+
3. **Connected State**: After connecting, it displays the wallet address/NFD and balance
|
|
548
|
+
4. **Account Management**: The dropdown provides options to switch accounts or disconnect
|
|
549
|
+
|
|
550
|
+
Behind the scenes, `WalletUIProvider` handles:
|
|
222
551
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
552
|
+
- Prefetching NFD data for all accounts in the wallet
|
|
553
|
+
- Converting IPFS URLs to HTTPS for avatars
|
|
554
|
+
- Caching API responses for better performance
|
|
555
|
+
- Handling network-specific behavior (MainNet/TestNet)
|
|
227
556
|
|
|
228
|
-
##
|
|
557
|
+
## Related Resources
|
|
229
558
|
|
|
230
|
-
|
|
559
|
+
- [use-wallet Documentation](https://github.com/TxnLab/use-wallet)
|
|
560
|
+
- [use-wallet-react NPM Package](https://www.npmjs.com/package/@txnlab/use-wallet-react)
|
|
561
|
+
- [NFD (Non-Fungible Domains)](https://nf.domains/)
|
|
562
|
+
- [Algorand Developer Portal](https://developer.algorand.org/)
|
|
231
563
|
|
|
232
564
|
## License
|
|
233
565
|
|