3
3
import * as React from "react" ;
4
4
5
5
import {
6
- ConnectWalletModal ,
7
- ConnectWalletModalBody ,
8
- ConnectWalletModalContent ,
9
- ConnectWalletModalDescription ,
10
- ConnectWalletModalFooter ,
11
- ConnectWalletModalHeader ,
12
- ConnectWalletModalTitle ,
13
- ConnectWalletModalTrigger ,
14
- } from "@/components/ui/connect-wallet-modal" ;
6
+ SimpleKitModal ,
7
+ SimpleKitModalBody ,
8
+ SimpleKitModalContent ,
9
+ SimpleKitModalDescription ,
10
+ SimpleKitModalFooter ,
11
+ SimpleKitModalHeader ,
12
+ SimpleKitModalTitle ,
13
+ } from "@/components/ui/simplekit-modal" ;
15
14
import { Button } from "@/components/ui/button" ;
16
15
import {
17
16
type Connector ,
@@ -27,7 +26,7 @@ import { Check, ChevronLeft, Copy, RotateCcw } from "lucide-react";
27
26
28
27
const MODAL_CLOSE_DURATION = 320 ;
29
28
30
- const ConnectWalletContext = React . createContext < {
29
+ const SimpleKitContext = React . createContext < {
31
30
pendingConnector : Connector | null ;
32
31
setPendingConnector : React . Dispatch < React . SetStateAction < Connector | null > > ;
33
32
isConnectorError : boolean ;
@@ -43,16 +42,12 @@ const ConnectWalletContext = React.createContext<{
43
42
setOpen : ( ) => false ,
44
43
} ) ;
45
44
46
- export function ConnectWallet ( ) {
45
+ export function SimpleKitProvider ( props : { children : React . ReactNode } ) {
47
46
const { status, address } = useAccount ( ) ;
48
47
const [ pendingConnector , setPendingConnector ] =
49
48
React . useState < Connector | null > ( null ) ;
50
49
const [ isConnectorError , setIsConnectorError ] = React . useState ( false ) ;
51
50
const [ open , setOpen ] = React . useState ( false ) ;
52
- const { data : ensName } = useEnsName ( { address } ) ;
53
- const { data : ensAvatar } = useEnsAvatar ( { name : ensName ! } ) ;
54
-
55
- const formattedAddress = address ?. slice ( 0 , 6 ) + "•••" + address ?. slice ( - 4 ) ;
56
51
const isConnected = address && ! pendingConnector ;
57
52
58
53
React . useEffect ( ( ) => {
@@ -69,7 +64,7 @@ export function ConnectWallet() {
69
64
} , [ status , setOpen , pendingConnector , setPendingConnector ] ) ;
70
65
71
66
return (
72
- < ConnectWalletContext . Provider
67
+ < SimpleKitContext . Provider
73
68
value = { {
74
69
pendingConnector,
75
70
setPendingConnector,
@@ -79,25 +74,35 @@ export function ConnectWallet() {
79
74
setOpen,
80
75
} }
81
76
>
82
- < ConnectWalletModal open = { open } onOpenChange = { setOpen } >
83
- < ConnectWalletModalTrigger asChild >
84
- { isConnected ? (
85
- < Button className = "rounded-xl" >
86
- { ensAvatar && < img src = { ensAvatar } alt = "ENS Avatar" /> }
87
- { address && (
88
- < span > { ensName ? `${ ensName } ` : formattedAddress } </ span >
89
- ) }
90
- </ Button >
91
- ) : (
92
- < Button className = "rounded-xl" > Connect Wallet</ Button >
93
- ) }
94
- </ ConnectWalletModalTrigger >
95
-
96
- < ConnectWalletModalContent >
77
+ { props . children }
78
+ < SimpleKitModal open = { open } onOpenChange = { setOpen } >
79
+ < SimpleKitModalContent >
97
80
{ isConnected ? < Account /> : < Connectors /> }
98
- </ ConnectWalletModalContent >
99
- </ ConnectWalletModal >
100
- </ ConnectWalletContext . Provider >
81
+ </ SimpleKitModalContent >
82
+ </ SimpleKitModal >
83
+ </ SimpleKitContext . Provider >
84
+ ) ;
85
+ }
86
+
87
+ export function ConnectWalletButton ( ) {
88
+ const simplekit = useSimpleKit ( ) ;
89
+ const { address } = useAccount ( ) ;
90
+ const { data : ensName } = useEnsName ( { address } ) ;
91
+ const { data : ensAvatar } = useEnsAvatar ( { name : ensName ! } ) ;
92
+
93
+ return (
94
+ < Button onClick = { simplekit . toggleModal } className = "rounded-xl" >
95
+ { simplekit . isConnected ? (
96
+ < >
97
+ { ensAvatar && < img src = { ensAvatar } alt = "ENS Avatar" /> }
98
+ { address && (
99
+ < span > { ensName ? `${ ensName } ` : simplekit . formattedAddress } </ span >
100
+ ) }
101
+ </ >
102
+ ) : (
103
+ "Connect Wallet"
104
+ ) }
105
+ </ Button >
101
106
) ;
102
107
}
103
108
@@ -106,7 +111,7 @@ function Account() {
106
111
const { disconnect } = useDisconnect ( ) ;
107
112
const { data : ensName } = useEnsName ( { address } ) ;
108
113
const { data : userBalance } = useBalance ( { address } ) ;
109
- const context = React . useContext ( ConnectWalletContext ) ;
114
+ const context = React . useContext ( SimpleKitContext ) ;
110
115
111
116
const formattedAddress = address ?. slice ( 0 , 6 ) + "•••" + address ?. slice ( - 4 ) ;
112
117
const formattedUserBalace = userBalance ?. value
@@ -122,13 +127,13 @@ function Account() {
122
127
123
128
return (
124
129
< >
125
- < ConnectWalletModalHeader >
126
- < ConnectWalletModalTitle > Connected</ ConnectWalletModalTitle >
127
- < ConnectWalletModalDescription className = "sr-only" >
130
+ < SimpleKitModalHeader >
131
+ < SimpleKitModalTitle > Connected</ SimpleKitModalTitle >
132
+ < SimpleKitModalDescription className = "sr-only" >
128
133
Account modal for your connected Web3 wallet.
129
- </ ConnectWalletModalDescription >
130
- </ ConnectWalletModalHeader >
131
- < ConnectWalletModalBody className = "h-[280px]" >
134
+ </ SimpleKitModalDescription >
135
+ </ SimpleKitModalHeader >
136
+ < SimpleKitModalBody className = "h-[280px]" >
132
137
< div className = "flex w-full flex-col items-center justify-center gap-8 md:pt-5" >
133
138
< div className = "size-24 flex items-center justify-center" >
134
139
< img
@@ -154,37 +159,37 @@ function Account() {
154
159
Disconnect
155
160
</ Button >
156
161
</ div >
157
- </ ConnectWalletModalBody >
162
+ </ SimpleKitModalBody >
158
163
</ >
159
164
) ;
160
165
}
161
166
162
167
function Connectors ( ) {
163
- const context = React . useContext ( ConnectWalletContext ) ;
168
+ const context = React . useContext ( SimpleKitContext ) ;
164
169
165
170
return (
166
171
< >
167
- < ConnectWalletModalHeader >
172
+ < SimpleKitModalHeader >
168
173
< BackChevron />
169
- < ConnectWalletModalTitle >
174
+ < SimpleKitModalTitle >
170
175
{ context . pendingConnector ?. name ?? "Connect Wallet" }
171
- </ ConnectWalletModalTitle >
172
- < ConnectWalletModalDescription className = "sr-only" >
176
+ </ SimpleKitModalTitle >
177
+ < SimpleKitModalDescription className = "sr-only" >
173
178
Connect your Web3 wallet or create a new one.
174
- </ ConnectWalletModalDescription >
175
- </ ConnectWalletModalHeader >
176
- < ConnectWalletModalBody >
179
+ </ SimpleKitModalDescription >
180
+ </ SimpleKitModalHeader >
181
+ < SimpleKitModalBody >
177
182
{ context . pendingConnector ? < WalletConnecting /> : < WalletOptions /> }
178
- </ ConnectWalletModalBody >
179
- < ConnectWalletModalFooter >
183
+ </ SimpleKitModalBody >
184
+ < SimpleKitModalFooter >
180
185
< div className = "h-0" />
181
- </ ConnectWalletModalFooter >
186
+ </ SimpleKitModalFooter >
182
187
</ >
183
188
) ;
184
189
}
185
190
186
191
function WalletConnecting ( ) {
187
- const context = React . useContext ( ConnectWalletContext ) ;
192
+ const context = React . useContext ( SimpleKitContext ) ;
188
193
189
194
return (
190
195
< div className = "flex w-full flex-col items-center justify-center gap-9 md:pt-5" >
@@ -215,7 +220,7 @@ function WalletConnecting() {
215
220
}
216
221
217
222
function WalletOptions ( ) {
218
- const context = React . useContext ( ConnectWalletContext ) ;
223
+ const context = React . useContext ( SimpleKitContext ) ;
219
224
const { connectors, connect } = useConnectors ( ) ;
220
225
221
226
return (
@@ -293,7 +298,7 @@ function CopyAddressButton() {
293
298
}
294
299
295
300
function BackChevron ( ) {
296
- const context = React . useContext ( ConnectWalletContext ) ;
301
+ const context = React . useContext ( SimpleKitContext ) ;
297
302
298
303
if ( ! context . pendingConnector ) {
299
304
return null ;
@@ -316,7 +321,7 @@ function BackChevron() {
316
321
}
317
322
318
323
function RetryConnectorButton ( ) {
319
- const context = React . useContext ( ConnectWalletContext ) ;
324
+ const context = React . useContext ( SimpleKitContext ) ;
320
325
const { connect } = useConnect ( {
321
326
mutation : {
322
327
onError : ( ) => context . setIsConnectorError ( true ) ,
@@ -343,7 +348,7 @@ function RetryConnectorButton() {
343
348
}
344
349
345
350
function useConnectors ( ) {
346
- const context = React . useContext ( ConnectWalletContext ) ;
351
+ const context = React . useContext ( SimpleKitContext ) ;
347
352
const { connect, connectors } = useConnect ( {
348
353
mutation : {
349
354
onError : ( ) => context . setIsConnectorError ( true ) ,
@@ -416,3 +421,33 @@ function useConnectors() {
416
421
417
422
return { connectors : sortedConnectors , connect } ;
418
423
}
424
+
425
+ export function useSimpleKit ( ) {
426
+ const { address } = useAccount ( ) ;
427
+ const context = React . useContext ( SimpleKitContext ) ;
428
+
429
+ const isModalOpen = context . open ;
430
+ const isConnected = address && ! context . pendingConnector ;
431
+ const formattedAddress = address ?. slice ( 0 , 6 ) + "•••" + address ?. slice ( - 4 ) ;
432
+
433
+ function open ( ) {
434
+ context . setOpen ( true ) ;
435
+ }
436
+
437
+ function close ( ) {
438
+ context . setOpen ( false ) ;
439
+ }
440
+
441
+ function toggleModal ( ) {
442
+ context . setOpen ( ( prevState ) => ! prevState ) ;
443
+ }
444
+
445
+ return {
446
+ isModalOpen,
447
+ isConnected,
448
+ formattedAddress,
449
+ open,
450
+ close,
451
+ toggleModal,
452
+ } ;
453
+ }
0 commit comments