@tonconnect/ui-react 0.0.3 → 0.0.4

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
@@ -1,3 +1,146 @@
1
- # TON Connect SDK UI React
1
+ # TON Connect UI React
2
2
 
3
- ⚠️ **API is work in progress right now.**
3
+ ⚠️ **API is work in progress right now. Don't use this package in production.**
4
+
5
+ TonConnect UI React is a React UI kit for TonConnect SDK. Use it to connect your app to TON wallets via TonConnect protocol in React apps.
6
+
7
+ If you don't use React for your app, take a look at [TonConnect UI](https://github.com/ton-connect/sdk/tree/main/packages/ui).
8
+
9
+ If you want to use TonConnect on the server side, you should use the [TonConnect SDK](https://github.com/ton-connect/sdk/tree/main/packages/sdk).
10
+
11
+ You can find more details and the protocol specification in the [docs](https://github.com/ton-connect/docs).
12
+
13
+ ---
14
+
15
+ # Getting started
16
+
17
+ [Latest API documentation](https://ton-connect.github.io/sdk/modules/_tonconnect_ui-react.html)
18
+
19
+ # Getting started
20
+
21
+ ## Installation with npm
22
+ `npm i @tonconnect/ui-react`
23
+
24
+ # Usage
25
+
26
+ ## Add TonConnectUIProvider
27
+ Add TonConnectUIProvider to the root of the app. You can specify UI options using props.
28
+ [See all available options](TODO)
29
+
30
+ All TonConnect UI hooks calls and `<TonConnectButton />` component must be placed inside `<TonConnectUIProvider>`.
31
+
32
+ ```tsx
33
+ import { TonConnectUIProvider } from '@tonconnect/ui-react';
34
+
35
+ export function App() {
36
+ return (
37
+ <TonConnectUIProvider manifestUrl="https://<YOUR_APP_URL>/tonconnect-manifest.json">
38
+ { /* Your app */ }
39
+ </TonConnectUIProvider>
40
+ );
41
+ }
42
+
43
+ ```
44
+
45
+ ## Add TonConnect Button
46
+ TonConnect Button is universal UI component for initializing connection. After wallet is connected it transforms to a wallet menu.
47
+ It is recommended to place it in the top right corner of your app.
48
+
49
+ ```tsx
50
+ export const Header = () => {
51
+ return (
52
+ <header>
53
+ <span>My App with React UI</span>
54
+ <TonConnectButton />
55
+ </header>
56
+ );
57
+ };
58
+
59
+ ```
60
+
61
+ You can add `className` and `style` props to the button as well. Note that you cannot pass child to the TonConnectButton.
62
+ `<TonConnectButton className="my-button-class" style={{ float: "right" }}/>`
63
+
64
+ ## Use TonConnect UI hooks
65
+
66
+ ### useTonAddress
67
+ Use it to get user's current ton wallet address. Pass boolean parameter isUserFriendly to choose format of the address. If wallet is not connected hook will return empty string.
68
+
69
+ ```tsx
70
+ import { useTonAddress } from '@tonconnect/ui-react';
71
+
72
+ export const Address = () => {
73
+ const userFriendlyAddress = useTonAddress();
74
+ const rawAddress = useTonAddress(false);
75
+
76
+ return (
77
+ address && (
78
+ <div>
79
+ <span>User-friendly address: {userFriendlyAddress}</span>
80
+ <span>Raw address: {rawAddress}</span>
81
+ </div>
82
+ )
83
+ );
84
+ };
85
+ ```
86
+
87
+ ### useTonWallet
88
+ Use it to get user's current ton wallet. If wallet is not connected hook will return null.
89
+
90
+ See all wallet's properties
91
+
92
+ [Wallet interface](https://ton-connect.github.io/sdk/interfaces/_tonconnect_sdk.Wallet.html)
93
+ [WalletInfo interface](https://ton-connect.github.io/sdk/types/_tonconnect_sdk.WalletInfo.html)
94
+
95
+ ```tsx
96
+ import { useTonWallet } from '@tonconnect/ui-react';
97
+
98
+ export const Wallet = () => {
99
+ const wallet = useTonWallet();
100
+
101
+ return (
102
+ wallet && (
103
+ <div>
104
+ <span>Connected wallet: {wallet.name}</span>
105
+ <span>Device: {wallet.device.appName}</span>
106
+ </div>
107
+ )
108
+ );
109
+ };
110
+ ```
111
+
112
+ ### useTonConnectUI
113
+ Use it to get access to the `TonConnectUI` instance and UI options updating function.
114
+
115
+ [See more about TonConnectUI instance methods](https://github.com/ton-connect/sdk/tree/main/packages/ui#send-transaction)
116
+
117
+ [See more about setOptions function](https://github.com/ton-connect/sdk/tree/main/packages/ui#change-options-if-needed)
118
+
119
+
120
+ ```tsx
121
+ import { Locales, useTonConnectUI } from '@tonconnect/ui-react';
122
+
123
+ export const Settings = () => {
124
+ const [tonConnectUI, setOptions] = useTonConnectUI();
125
+
126
+ const onLanguageChange = (lang: string) => {
127
+ setOptions({ language: lang as Locales });
128
+ };
129
+
130
+ return (
131
+ <div>
132
+ <button onClick={() => tonConnectUI.sendTransaction(myTransaction)}>
133
+ Send transaction
134
+ </button>
135
+
136
+ <div>
137
+ <label>language</label>
138
+ <select onChange={e => onLanguageChange(e.target.value)}>
139
+ <option value="en">en</option>
140
+ <option value="ru">ru</option>
141
+ </select>
142
+ </div>
143
+ </div>
144
+ );
145
+ };
146
+ ```
@@ -5,18 +5,45 @@ declare type Color = CSS.Property.Color;
5
5
  export declare const TonConnectUIContext: import("react").Context<TonConnectUI | null>;
6
6
  export declare type TonConnectUIProviderProps = {
7
7
  children: JSX.Element;
8
- } & Partial<TonConnectUIProviderPropsBase> & (TonConnectUIProviderPropsWithManifest | TonConnectUIProviderPropsWithConnector);
8
+ } & Partial<TonConnectUIProviderPropsBase> & Partial<TonConnectUIProviderPropsWithManifest | TonConnectUIProviderPropsWithConnector>;
9
9
  export interface TonConnectUIProviderPropsWithManifest {
10
+ /**
11
+ * Url to the [manifest]{@link https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest} with the Dapp metadata that will be displayed in the user's wallet.
12
+ * If not passed, manifest from `${window.location.origin}/tonconnect-manifest.json` will be taken.
13
+ */
10
14
  manifestUrl: string;
11
15
  }
12
16
  export interface TonConnectUIProviderPropsWithConnector {
17
+ /**
18
+ * TonConnect instance. Can be helpful if you use custom ITonConnect implementation, or use both of @tonconnect/sdk and @tonconnect/ui in your app.
19
+ */
13
20
  connector: ITonConnect;
14
21
  }
15
22
  export interface TonConnectUIProviderPropsBase {
23
+ /**
24
+ * Try to restore existing session and reconnect to the corresponding wallet.
25
+ * @default true.
26
+ */
16
27
  restoreConnection: boolean;
28
+ /**
29
+ * Color theme for the UI elements.
30
+ * @default SYSTEM theme.
31
+ */
17
32
  theme: Theme;
33
+ /**
34
+ * Accent color for the UI elements.
35
+ * @default #31A6F5 (blue).
36
+ */
18
37
  accentColor: Color;
38
+ /**
39
+ * Language for the phrases it the UI elements.
40
+ * @default system
41
+ */
19
42
  language: Locales;
43
+ /**
44
+ * HTML element id to attach the modal window element. If not passed, `div#tc-widget-root` in the end of the <body> will be added and used.
45
+ * @default `div#tc-widget-root`.
46
+ */
20
47
  widgetRootId: string;
21
48
  }
22
49
  declare const _default: import("react").NamedExoticComponent<TonConnectUIProviderProps>;
@@ -1,2 +1,2 @@
1
1
  export { TonConnectProviderNotSetError } from './ton-connect-provider-not-set.error';
2
- export { TonConnectUiReactError } from './ton-connect-ui-react.error';
2
+ export { TonConnectUIReactError } from './ton-connect-ui-react.error';
@@ -1,4 +1,8 @@
1
- import { TonConnectUiReactError } from './ton-connect-ui-react.error';
2
- export declare class TonConnectProviderNotSetError extends TonConnectUiReactError {
1
+ import { TonConnectUIReactError } from './ton-connect-ui-react.error';
2
+ /**
3
+ * Thrown when either <TonConnectProvider> not added to the top of the tags tree,
4
+ * either there is an attempt using TonConnect UI hook or <TonConnectButton> inside <TonConnectProvider>
5
+ */
6
+ export declare class TonConnectProviderNotSetError extends TonConnectUIReactError {
3
7
  constructor(...args: ConstructorParameters<typeof Error>);
4
8
  }
@@ -1,4 +1,7 @@
1
1
  import { TonConnectUIError } from '@tonconnect/ui';
2
- export declare class TonConnectUiReactError extends TonConnectUIError {
2
+ /**
3
+ * Base class for TonConnectUIReact errors. You can check if the error was triggered by the @tonconnect/ui-react using `err instanceof TonConnectUIReactError`.
4
+ */
5
+ export declare class TonConnectUIReactError extends TonConnectUIError {
3
6
  constructor(...args: ConstructorParameters<typeof Error>);
4
7
  }
@@ -1 +1,5 @@
1
+ /**
2
+ * Use it to get user's current ton wallet address. If wallet is not connected hook will return empty string.
3
+ * @param [userFriendly=true] allows to choose format of the address.
4
+ */
1
5
  export declare function useTonAddress(userFriendly?: boolean): string;
@@ -1,2 +1,5 @@
1
1
  import { TonConnectUI, TonConnectUiOptions } from '@tonconnect/ui';
2
+ /**
3
+ * Use it to get access to the `TonConnectUI` instance and UI options updating function.
4
+ */
2
5
  export declare function useTonConnectUI(): [TonConnectUI, (options: TonConnectUiOptions) => void];
@@ -1,2 +1,5 @@
1
1
  import { Wallet, WalletInfo } from '@tonconnect/sdk';
2
+ /**
3
+ * Use it to get user's current ton wallet. If wallet is not connected hook will return null.
4
+ */
2
5
  export declare function useTonWallet(): (Wallet & WalletInfo) | null;
package/lib/index.js CHANGED
@@ -7776,7 +7776,6 @@ const AccountButtonDropdown = (props) => {
7776
7776
  const connector = useContext(ConnectorContext);
7777
7777
  const [isCopiedShown, setIsCopiedShown] = createSignal(false);
7778
7778
  const onCopy = () => __async(void 0, null, function* () {
7779
- console.log(tonConnectUi.account.address);
7780
7779
  const userFriendlyAddress = toUserFriendlyAddress$1(tonConnectUi.account.address);
7781
7780
  yield copyToClipboard(userFriendlyAddress);
7782
7781
  setIsCopiedShown(true);
@@ -8826,7 +8825,7 @@ function P(l2, u2, e2) {
8826
8825
  const _tmpl$$3 = /* @__PURE__ */ template$1(`<div></div>`);
8827
8826
  const AccountButton = () => {
8828
8827
  const connector = useContext(ConnectorContext);
8829
- const tonConnectUI = useContext(TonConnectUiContext);
8828
+ const tonConnectUI2 = useContext(TonConnectUiContext);
8830
8829
  const [isOpened, setIsOpened] = createSignal(false);
8831
8830
  const [address, setAddress] = createSignal("");
8832
8831
  let dropDownRef;
@@ -8875,7 +8874,7 @@ const AccountButton = () => {
8875
8874
  get children() {
8876
8875
  return createComponent(AccountButtonStyled, {
8877
8876
  appearance: "flat",
8878
- onClick: () => tonConnectUI.connectWallet(),
8877
+ onClick: () => tonConnectUI2.connectWallet(),
8879
8878
  get children() {
8880
8879
  return [createComponent(TonIcon, {}), createComponent(Text, {
8881
8880
  translationKey: "button.connectWallet",
@@ -11048,8 +11047,8 @@ const WalletsModal = () => {
11048
11047
  } = useI18n()[1];
11049
11048
  createEffect(() => locale(appState.language));
11050
11049
  const connector = useContext(ConnectorContext);
11051
- const tonConnectUI = useContext(TonConnectUiContext);
11052
- const [walletsList] = createResource(() => tonConnectUI.getWallets());
11050
+ const tonConnectUI2 = useContext(TonConnectUiContext);
11051
+ const [walletsList] = createResource(() => tonConnectUI2.getWallets());
11053
11052
  const [selectedWalletInfo, setSelectedWalletInfo] = createSignal(null);
11054
11053
  const onClose = () => {
11055
11054
  setWalletsModalOpen(false);
@@ -11335,8 +11334,8 @@ const widgetController = {
11335
11334
  setAction: (action2) => void setTimeout(() => setAction(action2)),
11336
11335
  clearAction: () => void setTimeout(() => setAction(null)),
11337
11336
  getSelectedWalletInfo: () => lastSelectedWalletInfo(),
11338
- renderApp: (root, tonConnectUI) => render(() => createComponent(App, {
11339
- tonConnectUI
11337
+ renderApp: (root, tonConnectUI2) => render(() => createComponent(App, {
11338
+ tonConnectUI: tonConnectUI2
11340
11339
  }), document.getElementById(root))
11341
11340
  };
11342
11341
  class TonConnectUIError extends TonConnectError$1 {
@@ -11650,6 +11649,8 @@ class TonConnectUI {
11650
11649
  console.error(e2);
11651
11650
  throw new TonConnectUIError("Unhandled error:" + e2);
11652
11651
  }
11652
+ } finally {
11653
+ widgetController.clearAction();
11653
11654
  }
11654
11655
  });
11655
11656
  }
@@ -11690,23 +11691,26 @@ class TonConnectUI {
11690
11691
  }
11691
11692
  }
11692
11693
  const TonConnectUIContext = createContext$1(null);
11694
+ let tonConnectUI = null;
11693
11695
  const TonConnectUIProvider = (_a) => {
11694
11696
  var _b = _a, {
11695
11697
  children: children2
11696
11698
  } = _b, options = __objRest(_b, [
11697
11699
  "children"
11698
11700
  ]);
11699
- const tonConnectUI = new TonConnectUI(options);
11701
+ if (!tonConnectUI) {
11702
+ tonConnectUI = new TonConnectUI(options);
11703
+ }
11700
11704
  return /* @__PURE__ */ jsx(TonConnectUIContext.Provider, { value: tonConnectUI, children: children2 });
11701
11705
  };
11702
11706
  const TonConnectUIProvider$1 = memo$1(TonConnectUIProvider);
11703
- class TonConnectUiReactError extends TonConnectUIError {
11707
+ class TonConnectUIReactError extends TonConnectUIError {
11704
11708
  constructor(...args) {
11705
11709
  super(...args);
11706
- Object.setPrototypeOf(this, TonConnectUiReactError.prototype);
11710
+ Object.setPrototypeOf(this, TonConnectUIReactError.prototype);
11707
11711
  }
11708
11712
  }
11709
- class TonConnectProviderNotSetError extends TonConnectUiReactError {
11713
+ class TonConnectProviderNotSetError extends TonConnectUIReactError {
11710
11714
  constructor(...args) {
11711
11715
  super(...args);
11712
11716
  Object.setPrototypeOf(this, TonConnectProviderNotSetError.prototype);
@@ -11721,10 +11725,10 @@ function checkProvider(provider) {
11721
11725
  return true;
11722
11726
  }
11723
11727
  function useTonConnectUI() {
11724
- const tonConnectUI = useContext$1(TonConnectUIContext);
11725
- checkProvider(tonConnectUI);
11726
- const setOptions = (options) => void (tonConnectUI.uiOptions = options);
11727
- return [tonConnectUI, setOptions];
11728
+ const tonConnectUI2 = useContext$1(TonConnectUIContext);
11729
+ checkProvider(tonConnectUI2);
11730
+ const setOptions = (options) => void (tonConnectUI2.uiOptions = options);
11731
+ return [tonConnectUI2, setOptions];
11728
11732
  }
11729
11733
  const buttonRootId = "ton-connect-button";
11730
11734
  const TonConnectButton = ({ className: className2, style: style2 }) => {
@@ -11736,15 +11740,15 @@ const TonConnectButton = ({ className: className2, style: style2 }) => {
11736
11740
  };
11737
11741
  const TonConnectButton$1 = memo$1(TonConnectButton);
11738
11742
  function useTonWallet() {
11739
- const [tonConnectUI] = useTonConnectUI();
11743
+ const [tonConnectUI2] = useTonConnectUI();
11740
11744
  const [wallet, setWallet] = useState(
11741
- () => tonConnectUI.wallet && __spreadValues2(__spreadValues2({}, tonConnectUI.wallet), tonConnectUI.walletInfo)
11745
+ () => tonConnectUI2.wallet && __spreadValues2(__spreadValues2({}, tonConnectUI2.wallet), tonConnectUI2.walletInfo)
11742
11746
  );
11743
11747
  useEffect(
11744
- () => tonConnectUI.onStatusChange((value) => {
11748
+ () => tonConnectUI2.onStatusChange((value) => {
11745
11749
  setWallet(value);
11746
11750
  }),
11747
- [tonConnectUI]
11751
+ [tonConnectUI2]
11748
11752
  );
11749
11753
  return wallet;
11750
11754
  }
@@ -14613,7 +14617,7 @@ export {
14613
14617
  TonConnectUI,
14614
14618
  TonConnectUIError,
14615
14619
  TonConnectUIProvider$1 as TonConnectUIProvider,
14616
- TonConnectUiReactError,
14620
+ TonConnectUIReactError,
14617
14621
  useTonAddress,
14618
14622
  useTonConnectUI,
14619
14623
  useTonWallet