@tonconnect/ui-react 0.0.2 → 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
+ ```
@@ -1,22 +1,49 @@
1
- import { Locales, Theme, TonConnectUi } from '@tonconnect/ui';
1
+ import { Locales, Theme, TonConnectUI } from '@tonconnect/ui';
2
2
  import { ITonConnect } from '@tonconnect/sdk';
3
3
  import type * as CSS from 'csstype';
4
4
  declare type Color = CSS.Property.Color;
5
- export declare const TonConnectUIContext: import("react").Context<TonConnectUi | null>;
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
- import { TonConnectUi, TonConnectUiOptions } from '@tonconnect/ui';
2
- export declare function useTonConnectUI(): [TonConnectUi, (options: TonConnectUiOptions) => void];
1
+ import { TonConnectUI, TonConnectUiOptions } from '@tonconnect/ui';
2
+ /**
3
+ * Use it to get access to the `TonConnectUI` instance and UI options updating function.
4
+ */
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;