Skip to content

Commit

Permalink
fix: refactor modal name
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini committed Jan 10, 2024
1 parent 506b5b1 commit 3ef307d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,71 +1,54 @@
import type { WalletManager, WalletSource } from '@vechain/dapp-kit';
import { DAppKitLogger } from '@vechain/dapp-kit';
import { subscribeKey } from 'valtio/vanilla/utils';
import { dispatchCustomEvent } from '../utils';
import { createModalIfNotPresent, dispatchCustomEvent } from '../utils';

const getModal = (): HTMLElement | null => document.querySelector('vdk-modal');

const createModalIfNotPresent = (options?: {
modalParent?: HTMLElement;
}): HTMLElement => {
const modal = getModal();

if (modal) {
return modal;
}

DAppKitLogger.debug('DAppKitModal', 'creating a new modal');

const newModal = document.createElement('vdk-modal');

(options?.modalParent ?? document.body).appendChild(newModal);

return newModal;
};

export interface DAppKitModalOptions {
export interface ConnectModalManagerOptions {
modalParent?: HTMLElement;
}

export class DAppKitModal {
private static instance: DAppKitModal | null = null;
export class ConnectModalManager {
private static instance: ConnectModalManager | null = null;

private constructor(
private walletManager: WalletManager,
options?: DAppKitModalOptions,
options?: ConnectModalManagerOptions,
) {
createModalIfNotPresent(options);
}

public static getInstance(
walletManager: WalletManager,
options?: DAppKitModalOptions,
): DAppKitModal {
if (!DAppKitModal.instance) {
DAppKitModal.instance = new DAppKitModal(walletManager, options);
options?: ConnectModalManagerOptions,
): ConnectModalManager {
if (!ConnectModalManager.instance) {
ConnectModalManager.instance = new ConnectModalManager(
walletManager,
options,
);
}

return DAppKitModal.instance;
return ConnectModalManager.instance;
}

open(): void {
DAppKitLogger.debug('DAppKitModal', 'opening the modal');
DAppKitLogger.debug('ConnectModalManager', 'opening the modal');
dispatchCustomEvent('vdk-open-wallet-modal');
}

close(): void {
DAppKitLogger.debug('DAppKitModal', 'closing the modal');
DAppKitLogger.debug('ConnectModalManager', 'closing the modal');
dispatchCustomEvent('vdk-close-wallet-modal');
}

closeWalletConnect(): void {
DAppKitLogger.debug('DAppKitModal', 'closing wallet connect');
DAppKitLogger.debug('ConnectModalManager', 'closing wallet connect');
dispatchCustomEvent('vdk-close-wc-qrcode');
}

closeConnectionCertificateRequest(): void {
DAppKitLogger.debug(
'DAppKitModal',
'ConnectModalManager',
'closing connection certificate request',
);
dispatchCustomEvent('vdk-close-connection-certificate-request');
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/src/class/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './dapp-kit-modal';
export * from './connect-modal-manager';
export * from './custom-wallet-connect-modal';
10 changes: 5 additions & 5 deletions packages/dapp-kit-ui/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="@vechain/connex" />
import type { DAppKitOptions, WalletManager } from '@vechain/dapp-kit';
import { DAppKit } from '@vechain/dapp-kit';
import { CustomWalletConnectModal, DAppKitModal } from './class';
import { CustomWalletConnectModal, ConnectModalManager } from './class';
import {
type CustomizedStyle,
dispatchCustomEvent,
Expand Down Expand Up @@ -34,8 +34,8 @@ export const DAppKitUI = {
dappKitOptions = options;
if (!dappKit) dappKit = new DAppKit(options);

// init modal so on the first opening it doesn't have to create it
DAppKitModal.getInstance(this.wallet, {
// init modal so that on the first opening it doesn't have to create it
ConnectModalManager.getInstance(this.wallet, {
modalParent: options.modalParent,
});

Expand Down Expand Up @@ -64,8 +64,8 @@ export const DAppKitUI = {
return this.get().wallet;
},

get modal(): DAppKitModal {
return DAppKitModal.getInstance(this.wallet, {
get modal(): ConnectModalManager {
return ConnectModalManager.getInstance(this.wallet, {
modalParent: dappKitOptions?.modalParent,
});
},
Expand Down
18 changes: 18 additions & 0 deletions packages/dapp-kit-ui/src/utils/ui-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
import { type DAppKitUIOptions } from '../client';
import { Colors, Font, modalZIndex, defaultI18n } from '../constants';

const getModal = (): HTMLElement | null => document.querySelector('vdk-modal');

export const createModalIfNotPresent = (options?: {
modalParent?: HTMLElement;
}): HTMLElement => {
const modal = getModal();

if (modal) {
return modal;
}

const newModal = document.createElement('vdk-modal');

(options?.modalParent ?? document.body).appendChild(newModal);

return newModal;
};

const ThemeVariables = {
'--vdk-color-dark-primary': Colors.Dark.Primary.toString(),
'--vdk-color-dark-primary-hover': Colors.Dark.PrimaryHover.toString(),
Expand Down

0 comments on commit 3ef307d

Please sign in to comment.