1- import { CSSProperties , FunctionComponent , memo , useEffect } from 'react' ;
1+ import { CSSProperties , FunctionComponent , memo , useEffect , useRef } from 'react' ;
22import { useTonConnectUI } from '../hooks/useTonConnectUI' ;
3-
4- const buttonRootId = 'ton-connect-button' ;
3+ import { addButtonId , removeButtonId , getButtonIds , DEFAULT_BUTTON_ID } from '../utils/tonconnect-button-ids' ;
54
65export interface TonConnectButtonProps {
76 className ?: string ;
8-
97 style ?: CSSProperties ;
8+
9+ id ?: string ;
1010}
1111
1212/**
@@ -15,18 +15,28 @@ export interface TonConnectButtonProps {
1515 * @param [className] css class to add to the button container.
1616 * @param [style] style to add to the button container.
1717 * @constructor
18- */
19- const TonConnectButton : FunctionComponent < TonConnectButtonProps > = ( { className, style } ) => {
18+ */ const TonConnectButton : FunctionComponent < TonConnectButtonProps > = ( { className, style, id } ) => {
2019 const [ _ , setOptions ] = useTonConnectUI ( ) ;
20+ const buttonId = useRef < string > (
21+ id ??
22+ ( getButtonIds ( ) . length === 0
23+ ? DEFAULT_BUTTON_ID
24+ : `ton-connect-button-${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 10 ) } ` )
25+ ) ;
2126
2227 useEffect ( ( ) => {
23- setOptions ( { buttonRootId } ) ;
24- return ( ) => setOptions ( { buttonRootId : null } ) ;
28+ const newIds = addButtonId ( buttonId . current ) ;
29+ setOptions ( { buttonRootId : newIds } ) ;
30+
31+ return ( ) => {
32+ const newIds = removeButtonId ( buttonId . current ) ;
33+ setOptions ( { buttonRootId : newIds . length > 0 ? newIds : null } ) ;
34+ } ;
2535 } , [ setOptions ] ) ;
2636
2737 return (
2838 < div
29- id = { buttonRootId }
39+ id = { buttonId . current }
3040 className = { className }
3141 style = { { width : 'fit-content' , ...style } }
3242 > </ div >
0 commit comments