Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions packages/ui-react/src/components/TonConnectButton.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CSSProperties, FunctionComponent, memo, useEffect } from 'react';
import { CSSProperties, FunctionComponent, memo, useEffect, useRef } from 'react';
import { useTonConnectUI } from '../hooks/useTonConnectUI';

const buttonRootId = 'ton-connect-button';
import { addButtonId, removeButtonId, getButtonIds, DEFAULT_BUTTON_ID } from '../utils/tonconnect-button-ids';

export interface TonConnectButtonProps {
className?: string;

style?: CSSProperties;

id?: string;
}

/**
Expand All @@ -15,18 +15,28 @@ export interface TonConnectButtonProps {
* @param [className] css class to add to the button container.
* @param [style] style to add to the button container.
* @constructor
*/
const TonConnectButton: FunctionComponent<TonConnectButtonProps> = ({ className, style }) => {
*/const TonConnectButton: FunctionComponent<TonConnectButtonProps> = ({ className, style, id }) => {
const [_, setOptions] = useTonConnectUI();
const buttonId = useRef<string>(
id ??
(getButtonIds().length === 0
? DEFAULT_BUTTON_ID
: `ton-connect-button-${Math.random().toString(36).slice(2, 10)}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must be a counter

);

useEffect(() => {
setOptions({ buttonRootId });
return () => setOptions({ buttonRootId: null });
const newIds = addButtonId(buttonId.current);
setOptions({ buttonRootId: newIds });

return () => {
const newIds = removeButtonId(buttonId.current);
setOptions({ buttonRootId: newIds.length > 0 ? newIds : null });
};
}, [setOptions]);

return (
<div
id={buttonRootId}
id={buttonId.current}
className={className}
style={{ width: 'fit-content', ...style }}
></div>
Expand Down
19 changes: 19 additions & 0 deletions packages/ui-react/src/utils/tonconnect-button-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const DEFAULT_BUTTON_ID = 'ton-connect-button';

let ids: string[] = [];

export function getButtonIds() {
return [...ids];
}

export function addButtonId(id: string) {
if (!ids.includes(id)) {
ids.push(id);
}
return getButtonIds();
}

export function removeButtonId(id: string) {
ids = ids.filter(existingId => existingId !== id);
return getButtonIds();
}
12 changes: 8 additions & 4 deletions packages/ui/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ const App: Component<AppProps> = props => {
<ConnectorContext.Provider value={appState.connector}>
<GlobalStyles />
<ThemeProvider theme={themeState}>
<Show when={appState.buttonRootId}>
<Portal mount={document.getElementById(appState.buttonRootId!)!}>
<AccountButton />
</Portal>
<Show when={Array.isArray(appState.buttonRootId) && appState.buttonRootId.length > 0}>
<>
{appState.buttonRootId && (appState.buttonRootId as string[]).map((id) => (
<Portal mount={document.getElementById(id)!}>
<AccountButton />
</Portal>
))}
</>
</Show>
<Dynamic component={globalStylesTag}>
<WalletsModal />
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/app/state/app.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Loadable } from 'src/models/loadable';

export type AppState = {
connector: ITonConnect;
buttonRootId: string | null;
buttonRootId: string | null | string[];
language: Locales;
walletsListConfiguration: WalletsListConfiguration | {};
connectRequestParameters?: Loadable<ConnectAdditionalRequest> | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/models/ton-connect-ui-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface TonConnectUiOptions {
* HTML element id to attach the wallet connect button. If not passed button won't appear.
* @default null.
*/
buttonRootId?: string | null;
buttonRootId?: string | null | string[];

/**
* Language for the phrases it the UI elements.
Expand Down
12 changes: 11 additions & 1 deletion packages/ui/src/ton-connect-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,21 @@ export class TonConnectUI {
return rootId;
}

private checkButtonRootExist(buttonRootId: string | null | undefined): void | never {
private checkButtonRootExist(buttonRootId: string | string[] | null | undefined): void | never {
if (buttonRootId == null) {
return;
}

if (Array.isArray(buttonRootId)) {
for (const buttonId of buttonRootId) {
if (document.getElementById(buttonId)) {
return;
}
}

throw new TonConnectUIError(`${buttonRootId} element not found in the document.`);
}

if (!document.getElementById(buttonRootId)) {
throw new TonConnectUIError(`${buttonRootId} element not found in the document.`);
}
Expand Down