@txnlab/use-wallet-ui-react 0.1.0 → 0.2.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/LICENSE +21 -0
- package/README.md +458 -127
- package/dist/cjs/index.cjs +9 -9
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +8147 -3276
- package/dist/esm/index.js.map +1 -1
- package/dist/style.css +1221 -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 (Algorand Name Service) 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,423 @@ 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 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 avatar = nfdQuery.data?.avatar
|
|
135
387
|
|
|
136
388
|
return (
|
|
137
389
|
<div>
|
|
138
|
-
{
|
|
139
|
-
<ConnectedWalletButton
|
|
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>
|
|
150
391
|
</div>
|
|
151
392
|
)
|
|
152
393
|
}
|
|
153
394
|
```
|
|
154
395
|
|
|
155
|
-
|
|
396
|
+
### NFD Features
|
|
156
397
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
398
|
+
- Automatic lookup for the active wallet address
|
|
399
|
+
- Support for different data views: 'tiny', 'thumbnail', 'brief', 'full'
|
|
400
|
+
- Efficient caching via Tanstack Query
|
|
401
|
+
- Works with both MainNet and TestNet
|
|
160
402
|
|
|
161
|
-
|
|
162
|
-
|
|
403
|
+
```jsx
|
|
404
|
+
// Customizing the NFD lookup
|
|
405
|
+
const nfdQuery = useNfd({
|
|
406
|
+
enabled: true, // Whether to enable the lookup
|
|
407
|
+
view: 'brief', // Data view to request (default: 'thumbnail')
|
|
408
|
+
})
|
|
409
|
+
```
|
|
163
410
|
|
|
164
|
-
|
|
411
|
+
## Account Information
|
|
165
412
|
|
|
166
|
-
|
|
413
|
+
Get account data like ALGO balance and asset holdings using the `useAccountInfo` hook:
|
|
167
414
|
|
|
168
415
|
```jsx
|
|
169
|
-
|
|
416
|
+
import { useAccountInfo } from '@txnlab/use-wallet-ui-react'
|
|
417
|
+
|
|
418
|
+
function Balance() {
|
|
419
|
+
// Gets account data for the connected address
|
|
420
|
+
const accountQuery = useAccountInfo()
|
|
421
|
+
|
|
422
|
+
if (accountQuery.isLoading) return <div>Loading...</div>
|
|
423
|
+
if (!accountQuery.data) return <div>No account data</div>
|
|
424
|
+
|
|
425
|
+
// Convert microAlgos to Algos (1 ALGO = 1,000,000 microAlgos)
|
|
426
|
+
const algoBalance = Number(accountQuery.data.amount) / 1_000_000
|
|
427
|
+
|
|
428
|
+
// Available balance (total minus minimum required)
|
|
429
|
+
const minBalance = Number(accountQuery.data.minBalance) / 1_000_000
|
|
430
|
+
const availableBalance = Math.max(0, algoBalance - minBalance)
|
|
431
|
+
|
|
432
|
+
return (
|
|
433
|
+
<div>
|
|
434
|
+
<div>Total: {algoBalance.toFixed(2)} ALGO</div>
|
|
435
|
+
<div>Available: {availableBalance.toFixed(2)} ALGO</div>
|
|
436
|
+
</div>
|
|
437
|
+
)
|
|
438
|
+
}
|
|
170
439
|
```
|
|
171
440
|
|
|
172
|
-
|
|
441
|
+
## Advanced Customization
|
|
442
|
+
|
|
443
|
+
### Styling Without Tailwind
|
|
444
|
+
|
|
445
|
+
When not using Tailwind, you have two options for customizing components:
|
|
446
|
+
|
|
447
|
+
#### 1. Using the `style` prop
|
|
173
448
|
|
|
174
449
|
```jsx
|
|
175
|
-
<
|
|
176
|
-
|
|
177
|
-
|
|
450
|
+
<ConnectWalletButton
|
|
451
|
+
style={{
|
|
452
|
+
backgroundColor: '#3366FF',
|
|
453
|
+
color: 'white',
|
|
454
|
+
fontWeight: 'bold',
|
|
455
|
+
}}
|
|
456
|
+
>
|
|
457
|
+
Connect
|
|
458
|
+
</ConnectWalletButton>
|
|
178
459
|
```
|
|
179
460
|
|
|
180
|
-
|
|
461
|
+
#### 2. Using CSS selectors
|
|
181
462
|
|
|
182
|
-
|
|
463
|
+
First, add a custom class to the button:
|
|
183
464
|
|
|
184
465
|
```jsx
|
|
185
|
-
<
|
|
466
|
+
<ConnectWalletButton className="connect-button">Connect</ConnectWalletButton>
|
|
186
467
|
```
|
|
187
468
|
|
|
188
|
-
|
|
469
|
+
Then target it in your CSS:
|
|
470
|
+
|
|
471
|
+
```css
|
|
472
|
+
/* In your CSS */
|
|
473
|
+
[data-wallet-ui] .connect-button {
|
|
474
|
+
font-family: 'Your Custom Font', sans-serif;
|
|
475
|
+
background-color: #3366ff;
|
|
476
|
+
color: white;
|
|
477
|
+
padding: 8px 16px;
|
|
478
|
+
border-radius: 4px;
|
|
479
|
+
transition: background-color 0.2s;
|
|
480
|
+
}
|
|
189
481
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
</ConnectedWalletMenu>
|
|
482
|
+
[data-wallet-ui] .connect-button:hover {
|
|
483
|
+
background-color: #2952cc;
|
|
484
|
+
}
|
|
194
485
|
```
|
|
195
486
|
|
|
196
|
-
###
|
|
487
|
+
### Building Custom Wallet UI
|
|
197
488
|
|
|
198
|
-
|
|
489
|
+
For complete control, use the menu components with your own UI elements:
|
|
199
490
|
|
|
200
491
|
```jsx
|
|
201
|
-
|
|
492
|
+
import { useWallet } from '@txnlab/use-wallet-react'
|
|
493
|
+
import {
|
|
494
|
+
ConnectWalletMenu,
|
|
495
|
+
ConnectedWalletMenu,
|
|
496
|
+
} from '@txnlab/use-wallet-ui-react'
|
|
497
|
+
|
|
498
|
+
function CustomWalletButton() {
|
|
499
|
+
const { activeAddress } = useWallet()
|
|
500
|
+
|
|
501
|
+
return activeAddress ? (
|
|
502
|
+
<ConnectedWalletMenu>
|
|
503
|
+
<YourCustomConnectedButton />
|
|
504
|
+
</ConnectedWalletMenu>
|
|
505
|
+
) : (
|
|
506
|
+
<ConnectWalletMenu>
|
|
507
|
+
<YourCustomConnectButton />
|
|
508
|
+
</ConnectWalletMenu>
|
|
509
|
+
)
|
|
510
|
+
}
|
|
202
511
|
```
|
|
203
512
|
|
|
204
|
-
|
|
513
|
+
## Integration with Tanstack Query
|
|
205
514
|
|
|
206
|
-
|
|
515
|
+
This library uses Tanstack Query internally for data fetching. If your application already uses Tanstack Query, you can integrate the two to avoid duplicate caches:
|
|
207
516
|
|
|
208
517
|
```jsx
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
518
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
519
|
+
import { WalletManager, WalletProvider } from '@txnlab/use-wallet-react'
|
|
520
|
+
import { WalletUIProvider } from '@txnlab/use-wallet-ui-react'
|
|
521
|
+
|
|
522
|
+
const queryClient = new QueryClient()
|
|
523
|
+
const walletManager = new WalletManager({...})
|
|
214
524
|
|
|
215
|
-
|
|
525
|
+
function App() {
|
|
526
|
+
return (
|
|
527
|
+
<QueryClientProvider client={queryClient}>
|
|
528
|
+
<WalletProvider manager={walletManager}>
|
|
529
|
+
<WalletUIProvider queryClient={queryClient}>
|
|
530
|
+
{/* Your app */}
|
|
531
|
+
</WalletUIProvider>
|
|
532
|
+
</WalletProvider>
|
|
533
|
+
</QueryClientProvider>
|
|
534
|
+
)
|
|
535
|
+
}
|
|
536
|
+
```
|
|
216
537
|
|
|
217
|
-
|
|
538
|
+
By sharing the QueryClient, both your application and the wallet UI components will use the same query cache.
|
|
218
539
|
|
|
219
540
|
## How It Works
|
|
220
541
|
|
|
221
|
-
|
|
542
|
+
The library follows a simple workflow:
|
|
543
|
+
|
|
544
|
+
1. **Disconnected State**: `WalletButton` shows a connect button
|
|
545
|
+
2. **Connection Dialog**: Clicking opens a dropdown with available Algorand wallets
|
|
546
|
+
3. **Connected State**: After connecting, it displays the wallet address/NFD and balance
|
|
547
|
+
4. **Account Management**: The dropdown provides options to switch accounts or disconnect
|
|
548
|
+
|
|
549
|
+
Behind the scenes, `WalletUIProvider` handles:
|
|
222
550
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
551
|
+
- Prefetching NFD data for all accounts in the wallet
|
|
552
|
+
- Converting IPFS URLs to HTTPS for avatars
|
|
553
|
+
- Caching API responses for better performance
|
|
554
|
+
- Handling network-specific behavior (MainNet/TestNet)
|
|
227
555
|
|
|
228
|
-
##
|
|
556
|
+
## Related Resources
|
|
229
557
|
|
|
230
|
-
|
|
558
|
+
- [use-wallet Documentation](https://github.com/TxnLab/use-wallet)
|
|
559
|
+
- [use-wallet-react NPM Package](https://www.npmjs.com/package/@txnlab/use-wallet-react)
|
|
560
|
+
- [NFD (Non-Fungible Domains)](https://nf.domains/)
|
|
561
|
+
- [Algorand Developer Portal](https://developer.algorand.org/)
|
|
231
562
|
|
|
232
563
|
## License
|
|
233
564
|
|