Skip to content

Commit

Permalink
fix: connection button
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini committed Feb 24, 2024
1 parent 4d19476 commit 8b71b28
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/dapp-kit-ui/src/components/buttons/button.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { html, LitElement, type TemplateResult } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { subscribeKey } from 'valtio/vanilla/utils';
import { DAppKitUI } from '../../client';
import { defaultI18n, type I18n, type ThemeMode } from '../../constants';
import { subscribeToCustomEvent } from '../../utils';
Expand All @@ -9,10 +8,17 @@ import { subscribeToCustomEvent } from '../../utils';
export class Button extends LitElement {
constructor() {
super();

if (DAppKitUI.initialized) {
this.initAddressListener();
this.setAddressFromState();
this.configureButtonUI();
this.initAddressListener();

// this subscribeToCustomEvent need to be done if the DappKitUI button is reconfigured and so recreated after the initial configuration
subscribeToCustomEvent('vdk-dapp-kit-configured', () => {
this.setAddressFromState();
this.initAddressListener();
});
} else {
subscribeToCustomEvent('vdk-dapp-kit-configured', () => {
this.setAddressFromState();
Expand All @@ -23,19 +29,24 @@ export class Button extends LitElement {

private setAddressFromState(): void {
this.address = DAppKitUI.wallet.state.address ?? '';
this.requestUpdate();
}

private configureButtonUI(): void {
this.mode = DAppKitUI.configuration?.themeMode ?? 'LIGHT';
this.i18n = DAppKitUI.configuration?.i18n ?? defaultI18n;
this.language = DAppKitUI.configuration?.language ?? 'en';
this.requestUpdate();
}

private initAddressListener(): void {
subscribeKey(DAppKitUI.wallet.state, 'address', (v) => {
this.address = v ?? '';
this.requestUpdate();
});
DAppKitUI.wallet.subscribeToKey(
'address',
(_address: string | null) => {
this.address = _address ?? '';
this.requestUpdate();
},
);
}

@property()
Expand Down

0 comments on commit 8b71b28

Please sign in to comment.