chore: updated example and connect-wallet for callbacks and types
b095e565
5 file(s) · +89 −14
| 1 | + | import { useEffect, useRef } from "react"; |
|
| 1 | 2 | import reactLogo from "./assets/react.svg"; |
|
| 2 | 3 | import viteLogo from "/vite.svg"; |
|
| 3 | 4 | import "./App.css"; |
|
| 4 | 5 | import "./components/connect-wallet"; |
|
| 5 | 6 | ||
| 6 | 7 | function App() { |
|
| 8 | + | const walletRef = useRef<any>(null); |
|
| 9 | + | ||
| 10 | + | // Example of using callbacks |
|
| 11 | + | useEffect(() => { |
|
| 12 | + | const walletElement = walletRef.current; |
|
| 13 | + | if (!walletElement) return; |
|
| 14 | + | ||
| 15 | + | walletElement.onWalletConnected = (detail: any) => { |
|
| 16 | + | console.log("connected via callback: ", detail); |
|
| 17 | + | }; |
|
| 18 | + | ||
| 19 | + | walletElement.onWalletDisconnected = () => { |
|
| 20 | + | console.log("disconnected via callback"); |
|
| 21 | + | }; |
|
| 22 | + | ||
| 23 | + | walletElement.onWalletError = (detail: any) => { |
|
| 24 | + | console.log("error via callback: ", detail); |
|
| 25 | + | }; |
|
| 26 | + | }, []); |
|
| 27 | + | ||
| 7 | 28 | return ( |
|
| 8 | 29 | <> |
|
| 9 | 30 | <div> |
|
| 16 | 37 | </div> |
|
| 17 | 38 | <h1>Vite + React</h1> |
|
| 18 | 39 | <div className="card"> |
|
| 19 | - | <connect-wallet></connect-wallet> |
|
| 40 | + | <connect-wallet ref={walletRef}></connect-wallet> |
|
| 20 | 41 | </div> |
|
| 21 | 42 | <p className="read-the-docs"> |
|
| 22 | 43 | Click on the Vite and React logos to learn more |
|
| 51 | 51 | this.showPopover = false; |
|
| 52 | 52 | this.balance = "0"; |
|
| 53 | 53 | this.copySuccess = false; |
|
| 54 | + | ||
| 55 | + | // React-friendly callback properties |
|
| 56 | + | this.onWalletConnected = null; |
|
| 57 | + | this.onWalletDisconnected = null; |
|
| 58 | + | this.onWalletError = null; |
|
| 54 | 59 | } |
|
| 55 | 60 | ||
| 56 | 61 | static get observedAttributes() { |
|
| 117 | 122 | this.loading = false; |
|
| 118 | 123 | this.render(); |
|
| 119 | 124 | ||
| 125 | + | const eventDetail = { |
|
| 126 | + | address: this.address, |
|
| 127 | + | ensData: this.ensData, |
|
| 128 | + | chainId: this.currentChainId, |
|
| 129 | + | }; |
|
| 130 | + | ||
| 131 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 120 | 132 | this.dispatchEvent( |
|
| 121 | 133 | new CustomEvent("wallet-connected", { |
|
| 122 | - | detail: { |
|
| 123 | - | address: this.address, |
|
| 124 | - | ensData: this.ensData, |
|
| 125 | - | chainId: this.currentChainId, |
|
| 126 | - | }, |
|
| 134 | + | detail: eventDetail, |
|
| 127 | 135 | }), |
|
| 128 | 136 | ); |
|
| 137 | + | ||
| 138 | + | // Call callback property if set (React-friendly) |
|
| 139 | + | if (typeof this.onWalletConnected === "function") { |
|
| 140 | + | this.onWalletConnected(eventDetail); |
|
| 141 | + | } |
|
| 129 | 142 | } catch (error) { |
|
| 130 | 143 | console.error("Connection failed", error); |
|
| 131 | 144 | this.loading = false; |
|
| 132 | 145 | this.render(); |
|
| 133 | 146 | ||
| 147 | + | const errorDetail = { error: error.message }; |
|
| 148 | + | ||
| 149 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 134 | 150 | this.dispatchEvent( |
|
| 135 | 151 | new CustomEvent("wallet-error", { |
|
| 136 | - | detail: { error: error.message }, |
|
| 152 | + | detail: errorDetail, |
|
| 137 | 153 | }), |
|
| 138 | 154 | ); |
|
| 155 | + | ||
| 156 | + | // Call callback property if set (React-friendly) |
|
| 157 | + | if (typeof this.onWalletError === "function") { |
|
| 158 | + | this.onWalletError(errorDetail); |
|
| 159 | + | } |
|
| 139 | 160 | } |
|
| 140 | 161 | } else { |
|
| 141 | 162 | alert("Please install a wallet extension like MetaMask"); |
|
| 152 | 173 | this.copySuccess = false; |
|
| 153 | 174 | this.render(); |
|
| 154 | 175 | ||
| 176 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 155 | 177 | this.dispatchEvent(new CustomEvent("wallet-disconnected")); |
|
| 178 | + | ||
| 179 | + | // Call callback property if set (React-friendly) |
|
| 180 | + | if (typeof this.onWalletDisconnected === "function") { |
|
| 181 | + | this.onWalletDisconnected(); |
|
| 182 | + | } |
|
| 156 | 183 | } |
|
| 157 | 184 | ||
| 158 | 185 | // Chain management methods |
|
| 22 | 22 | "noFallthroughCasesInSwitch": true, |
|
| 23 | 23 | "noUncheckedSideEffectImports": true |
|
| 24 | 24 | }, |
|
| 25 | - | "include": ["src", "custom-elements-jsx.d.ts"] |
|
| 25 | + | "include": ["src"] |
|
| 26 | 26 | } |
| 51 | 51 | this.showPopover = false; |
|
| 52 | 52 | this.balance = "0"; |
|
| 53 | 53 | this.copySuccess = false; |
|
| 54 | + | ||
| 55 | + | // React-friendly callback properties |
|
| 56 | + | this.onWalletConnected = null; |
|
| 57 | + | this.onWalletDisconnected = null; |
|
| 58 | + | this.onWalletError = null; |
|
| 54 | 59 | } |
|
| 55 | 60 | ||
| 56 | 61 | static get observedAttributes() { |
|
| 117 | 122 | this.loading = false; |
|
| 118 | 123 | this.render(); |
|
| 119 | 124 | ||
| 125 | + | const eventDetail = { |
|
| 126 | + | address: this.address, |
|
| 127 | + | ensData: this.ensData, |
|
| 128 | + | chainId: this.currentChainId, |
|
| 129 | + | }; |
|
| 130 | + | ||
| 131 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 120 | 132 | this.dispatchEvent( |
|
| 121 | 133 | new CustomEvent("wallet-connected", { |
|
| 122 | - | detail: { |
|
| 123 | - | address: this.address, |
|
| 124 | - | ensData: this.ensData, |
|
| 125 | - | chainId: this.currentChainId, |
|
| 126 | - | }, |
|
| 134 | + | detail: eventDetail, |
|
| 127 | 135 | }), |
|
| 128 | 136 | ); |
|
| 137 | + | ||
| 138 | + | // Call callback property if set (React-friendly) |
|
| 139 | + | if (typeof this.onWalletConnected === "function") { |
|
| 140 | + | this.onWalletConnected(eventDetail); |
|
| 141 | + | } |
|
| 129 | 142 | } catch (error) { |
|
| 130 | 143 | console.error("Connection failed", error); |
|
| 131 | 144 | this.loading = false; |
|
| 132 | 145 | this.render(); |
|
| 133 | 146 | ||
| 147 | + | const errorDetail = { error: error.message }; |
|
| 148 | + | ||
| 149 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 134 | 150 | this.dispatchEvent( |
|
| 135 | 151 | new CustomEvent("wallet-error", { |
|
| 136 | - | detail: { error: error.message }, |
|
| 152 | + | detail: errorDetail, |
|
| 137 | 153 | }), |
|
| 138 | 154 | ); |
|
| 155 | + | ||
| 156 | + | // Call callback property if set (React-friendly) |
|
| 157 | + | if (typeof this.onWalletError === "function") { |
|
| 158 | + | this.onWalletError(errorDetail); |
|
| 159 | + | } |
|
| 139 | 160 | } |
|
| 140 | 161 | } else { |
|
| 141 | 162 | alert("Please install a wallet extension like MetaMask"); |
|
| 152 | 173 | this.copySuccess = false; |
|
| 153 | 174 | this.render(); |
|
| 154 | 175 | ||
| 176 | + | // Dispatch custom event for vanilla JS and other frameworks |
|
| 155 | 177 | this.dispatchEvent(new CustomEvent("wallet-disconnected")); |
|
| 178 | + | ||
| 179 | + | // Call callback property if set (React-friendly) |
|
| 180 | + | if (typeof this.onWalletDisconnected === "function") { |
|
| 181 | + | this.onWalletDisconnected(); |
|
| 182 | + | } |
|
| 156 | 183 | } |
|
| 157 | 184 | ||
| 158 | 185 | // Chain management methods |
|