@tonconnect/ui 2.0.0-beta.1 → 2.0.0-beta.10

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/README.md CHANGED
@@ -103,38 +103,95 @@ or
103
103
  const walletsList = await TonConnectUI.getWallets();
104
104
  ```
105
105
 
106
- ## Call connect
106
+ ## Open connect modal
107
107
  "TonConnect UI connect button" (which is added at `buttonRootId`) automatically handles clicks and calls connect.
108
108
  But you are still able to open "connect modal" programmatically, e.g. after click on your custom connect button.
109
109
 
110
110
  ```ts
111
- const connectedWallet = await tonConnectUI.connectWallet();
111
+ await tonConnectUI.openModal();
112
112
  ```
113
113
 
114
- If there is an error while wallet connecting, `TonConnectUIError` or `TonConnectError` will be thrown depends on situation.
114
+ This method opens the modal window and returns a promise that resolves after the modal window is opened.
115
+
116
+ If there is an error while modal opening, `TonConnectUIError` or `TonConnectError` will be thrown depends on situation.
117
+
118
+ ## Close connect modal
119
+
120
+ ```ts
121
+ tonConnectUI.closeModal();
122
+ ```
123
+
124
+ This method closes the modal window.
125
+
126
+ ## Get current modal state
127
+
128
+ This getter returns the current state of the modal window. The state will be an object with `status` and `closeReason` properties. The `status` can be either 'opened' or 'closed'. If the modal is closed, you can check the `closeReason` to find out the reason of closing.
129
+
130
+ ```ts
131
+ const currentModalState = tonConnectUI.modalState;
132
+ ```
133
+
134
+ ## Subscribe to the modal window state changes
135
+ To subscribe to the changes of the modal window state, you can use the `onModalStateChange` method. It returns a function which has to be called to unsubscribe.
136
+
137
+ ```js
138
+ const unsubscribeModal = tonConnectUI.onModalStateChange(
139
+ (state: WalletsModalState) => {
140
+ // update state/reactive variables to show updates in the ui
141
+ // state.status will be 'opened' or 'closed'
142
+ // if state.status is 'closed', you can check state.closeReason to find out the reason
143
+ }
144
+ );
145
+ ```
146
+
147
+ Call `unsubscribeModal()` later to save resources when you don't need to listen for updates anymore.
148
+
149
+ ## Wallets Modal Control
150
+
151
+ The `tonConnectUI` provides methods for managing the modal window, such as `openModal()`, `closeModal()` and other, which are designed for ease of use and cover most use cases.
152
+
153
+ ```typescript
154
+ const { modal } = tonConnectUI;
155
+
156
+ // Open and close the modal
157
+ await modal.open();
158
+ modal.close();
159
+
160
+ // Get the current modal state
161
+ const currentState = modal.state;
162
+
163
+ // Subscribe and unsubscribe to modal state changes
164
+ const unsubscribe = modal.onStateChange(state => { /* ... */ });
165
+ unsubscribe();
166
+ ```
167
+
168
+ While `tonConnectUI` internally delegates these calls to the `modal`, it is recommended to use the `tonConnectUI` methods for a more straightforward and consistent experience. The `modal` is exposed in case you need direct access to the modal window's state and behavior, but this should generally be avoided unless necessary.
115
169
 
116
170
  ## Get current connected Wallet and WalletInfo
117
171
  You can use special getters to read current connection state. Note that this getter only represents current value, so they are not reactive.
118
- To react and handle wallet changes use `onStatusChange` mathod.
172
+ To react and handle wallet changes use `onStatusChange` method.
119
173
 
120
174
  ```ts
121
- const currentWallet = tonConnectUI.wallet;
122
- const currentWalletInfo = tonConnectUI.walletInfo;
123
- const currentAccount = tonConnectUI.account;
124
- const currentIsConnectedStatus = tonConnectUI.connected;
175
+ const currentWallet = tonConnectUI.wallet;
176
+ const currentWalletInfo = tonConnectUI.walletInfo;
177
+ const currentAccount = tonConnectUI.account;
178
+ const currentIsConnectedStatus = tonConnectUI.connected;
125
179
  ```
126
180
 
127
181
  ## Subscribe to the connection status changes
128
- ```js
182
+
183
+ To subscribe to the changes of the connection status, you can use the `onStatusChange` method. It returns a function which has to be called to unsubscribe.
184
+
185
+ ```ts
129
186
  const unsubscribe = tonConnectUI.onStatusChange(
130
187
  walletAndwalletInfo => {
131
188
  // update state/reactive variables to show updates in the ui
132
189
  }
133
190
  );
134
-
135
- // call `unsubscribe()` later to save resources when you don't need to listen for updates anymore.
136
191
  ```
137
192
 
193
+ Call `unsubscribe()` later to save resources when you don't need to listen for updates anymore.
194
+
138
195
  ## Disconnect wallet
139
196
  Call to disconnect the wallet.
140
197
 
@@ -284,31 +341,51 @@ const result = await tonConnectUI.sendTransaction(defaultTx, {
284
341
  });
285
342
  ```
286
343
 
287
- ## Use inside TWA (Telegram web app)
288
- TonConnect UI will work in TWA in the same way as in a regular website!
344
+ ## Use inside TMA (Telegram Mini Apps)
345
+ TonConnect UI will work in TMA in the same way as in a regular website!
289
346
  Basically, no changes are required from the dApp's developers. The only thing you have to set is a dynamic return strategy.
290
347
 
291
- Currently, it is impossible for TWA-wallets to redirect back to previous opened TWA-dApp like native wallet-apps do.
292
- It means, that you need to specify the return strategy as a link to your TWA that will be only applied if the dApp is opened in TWA mode.
348
+ Currently, it is impossible for TMA-wallets to redirect back to previous opened TMA-dApp like native wallet-apps do.
349
+ It means, that you need to specify the return strategy as a link to your TMA that will be only applied if the dApp is opened in TMA mode.
293
350
 
294
351
  ```ts
295
352
  tonConnectUI.uiOptions = {
296
- twaReturnUrl: 'https://t.me/durov'
297
- };
353
+ twaReturnUrl: 'https://t.me/durov'
354
+ };
298
355
  ```
299
356
 
300
- In other words,
357
+ In other words, TonConnect UI will automatically handle the return strategy based on the environment. **The following pseudo-code demonstrates the internal logic of TonConnect UI**:
358
+
359
+ > Please note that you **don't need to add this code to your dApp**. It's just an example of how TonConnect UI processes the return strategy internally.
360
+
301
361
  ```ts
302
- if (isLinkToTelegram()) {
362
+ let finalReturnStrategy;
363
+
364
+ // Determine if the provided link opens Telegram (using protocols tg:// or domain t.me).
365
+ if (isLinkToTelegram('https://example.com')) {
366
+ // In the Telegram Mini Apps environment,
303
367
  if (isInTWA()) {
304
- FINAL_RETURN_STRATEGY = actionsConfiguration.twaReturnUrl || actionsConfiguration.returnStrategy;
305
- } else {
306
- FINAL_RETURN_STRATEGY = 'none';
368
+ // preference is given to 'twaReturnUrl',
369
+ finalReturnStrategy = actionsConfiguration.twaReturnUrl;
370
+
371
+ // but if it's not set, fallback to 'returnStrategy'.
372
+ if (!finalReturnStrategy) {
373
+ finalReturnStrategy = actionsConfiguration.returnStrategy;
374
+ }
375
+ }
376
+ // If not in a TMA environment,
377
+ else {
378
+ // the return strategy is set to 'none'.
379
+ finalReturnStrategy = 'none';
307
380
  }
308
- } else {
309
- FINAL_RETURN_STRATEGY = actionsConfiguration.returnStrategy;
381
+ }
382
+ // When the link does not open Telegram,
383
+ else {
384
+ // use the predefined 'returnStrategy'.
385
+ finalReturnStrategy = actionsConfiguration.returnStrategy;
310
386
  }
311
387
 
388
+ // Now, 'finalReturnStrategy' contains the correct strategy based on the link's destination and the dApp's environment.
312
389
  ```
313
390
 
314
391
  ## Detect end of the connection restoring process
@@ -639,3 +716,72 @@ tonConnectUI.onStatusChange(wallet => {
639
716
  }
640
717
  });
641
718
  ```
719
+
720
+ # Troubleshooting
721
+
722
+ ## Android Back Handler
723
+
724
+ If you encounter any issues with the Android back handler, such as modals not closing properly when the back button is pressed, or conflicts with `history.pushState()` if you are manually handling browser history in your application, you can disable the back handler by setting `enableAndroidBackHandler` to `false`:
725
+
726
+ ```ts
727
+ const tonConnectUI = new TonConnectUI({
728
+ // ...
729
+ enableAndroidBackHandler: false
730
+ });
731
+ ```
732
+
733
+ This will disable the custom back button behavior on Android, and you can then handle the back button press manually in your application.
734
+
735
+ While we do not foresee any problems arising with the Android back handler, but if you find yourself needing to disable it due to an issue, please describe the problem in on [GitHub Issues](https://github.com/ton-connect/sdk/issues), so we can assist you further.
736
+
737
+ ## Animations not working
738
+
739
+ If you are experiencing issues with animations not working in your environment, it might be due to a lack of support for the Web Animations API. To resolve this issue, you can use the `web-animations-js` polyfill.
740
+
741
+ ### Using npm
742
+
743
+ To install the polyfill, run the following command:
744
+
745
+ ```shell
746
+ npm install web-animations-js
747
+ ```
748
+
749
+ Then, import the polyfill in your project:
750
+
751
+ ```typescript
752
+ import 'web-animations-js';
753
+ ```
754
+
755
+ ### Using CDN
756
+
757
+ Alternatively, you can include the polyfill via CDN by adding the following script tag to your HTML:
758
+
759
+ ```html
760
+ <script src="https://www.unpkg.com/web-animations-js@latest/web-animations.min.js"></script>
761
+ ```
762
+
763
+ Both methods will provide a fallback implementation of the Web Animations API and should resolve the animation issues you are facing.
764
+
765
+ ## Warning about 'encoding' module in Next.js
766
+
767
+ If you are using Next.js and see a warning similar to the following:
768
+
769
+ ```
770
+ ⚠ ./node_modules/node-fetch/lib/index.js
771
+ Module not found: Can't resolve 'encoding' in '.../node_modules/node-fetch/lib'
772
+
773
+ Import trace for requested module:
774
+ ./node_modules/node-fetch/lib/index.js
775
+ ./node_modules/@tonconnect/isomorphic-fetch/index.mjs
776
+ ./node_modules/@tonconnect/sdk/lib/esm/index.mjs
777
+ ./node_modules/@tonconnect/ui/lib/esm/index.mjs
778
+ ```
779
+
780
+ Please note that this is just a warning and should not affect the functionality of your application. If you wish to suppress the warning, you have two options:
781
+
782
+ 1. (Recommended) Wait for us to remove the dependency on `@tonconnect/isomorphic-fetch` in future releases. This dependency will be removed when we drop support for Node.js versions below 18.
783
+
784
+ 2. (Optional) Install the `encoding` package, to resolve the warning:
785
+ ```shell
786
+ npm install encoding
787
+ ```