Skip to content

Commit

Permalink
feat: Add more directive methods for open different modals (#17)
Browse files Browse the repository at this point in the history
* feat: Add test example

* feat: Update examples

* feat: Add disclaimer & download url

* docs: Update example

* docs: Update docs

* docs: Update docs

* docs: Update gitignore

* docs: Update docs

* feat: Add zIndices theme config

* feat: Update examples

* feat: Refactor switch network modal

* refactor: Clean code

* feat: Update examples

* refactor: Update dependencies

* feat: Update version

* docs: Update docs
  • Loading branch information
wenty22 authored Nov 9, 2023
1 parent 192bd40 commit a3f373c
Show file tree
Hide file tree
Showing 43 changed files with 729 additions and 1,783 deletions.
4 changes: 2 additions & 2 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"react-dom": "^18",
"next": "^13",
"@totejs/walletkit": "workspace:*",
"viem": "^1.16.6",
"wagmi": "^1.4.4"
"viem": "^1.0.0",
"wagmi": "^1.0.0"
},
"devDependencies": {
"typescript": "^5",
Expand Down
4 changes: 2 additions & 2 deletions examples/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"react-dom": "^18",
"next": "^13",
"@totejs/walletkit": "workspace:*",
"viem": "^1.16.6",
"wagmi": "^1.4.4"
"viem": "^1.0.0",
"wagmi": "^1.0.0"
},
"devDependencies": {
"typescript": "^5",
Expand Down
14 changes: 14 additions & 0 deletions examples/test/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WalletKitOptions,
WalletKitProvider,
getDefaultConfig,
useModal,
} from '@totejs/walletkit';
import { trustWallet, metaMask, walletConnect } from '@totejs/walletkit/wallets';
import { useState } from 'react';
Expand Down Expand Up @@ -44,9 +45,22 @@ export default function App({ Component, pageProps }: AppProps) {

<WalletKitProvider options={options} mode={mode} debugMode>
<WalletKitButton />
<Example />
<Component {...pageProps} />
<SwitchNetworkModal />
</WalletKitProvider>
</WagmiConfig>
);
}

function Example() {
const { onOpen, onOpenProfile, onOpenSwitchNetwork } = useModal();

return (
<>
<button onClick={onOpen}>Open Connect Modal</button>
<button onClick={onOpenProfile}>Open Profile Modal</button>
<button onClick={onOpenSwitchNetwork}>Open SwitchNetwork Modal</button>
</>
);
}
4 changes: 2 additions & 2 deletions examples/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@totejs/walletkit": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"viem": "^1.16.6",
"wagmi": "^1.4.4"
"viem": "^1.0.0",
"wagmi": "^1.0.0"
},
"devDependencies": {
"@types/react": "^18.2.15",
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "pnpm --parallel --filter example-test --filter @totejs/walletkit dev"
"dev": "pnpm --recursive --parallel --filter example-test --filter @totejs/walletkit dev"
},
"repository": {
"type": "git",
"url": "git+https://github.com/node-real/walletkit.git"
},
"license": "MIT",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"eslint": "^8.38.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.53.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"prettier": "^3.0.3",
"rollup": "^4.2.0"
"eslint-plugin-react-refresh": "^0.3.5",
"prettier": "^3.0.3"
}
}
23 changes: 7 additions & 16 deletions packages/walletkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@totejs/walletkit",
"version": "1.0.0",
"version": "1.0.2",
"type": "module",
"files": [
"dist",
Expand All @@ -21,30 +21,21 @@
"wagmi": "^1.0.0"
},
"devDependencies": {
"@emotion/babel-preset-css-prop": "^11.11.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/qrcode": "^1.5.4",
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@types/qrcode": "^1.5.5",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@vanilla-extract/css": "^1.13.0",
"@vanilla-extract/rollup-plugin": "^1.3.0",
"@vanilla-extract/vite-plugin": "^3.9.0",
"@vitejs/plugin-react": "^4.1.1",
"autoprefixer": "^10.4.16",
"postcss": "^8.4.31",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rollup-plugin-embed-css": "^1.0.26",
"rollup-plugin-import-css": "^3.3.5",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.0.2",
"viem": "^1.16.6",
"typescript": "^5.2.2",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.3",
"wagmi": "^1.4.4"
"viem": "^1.0.0",
"wagmi": "^1.0.0"
},
"dependencies": {
"qrcode": "^1.5.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/walletkit/src/base/components/Modal/styles.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { style } from '@vanilla-extract/css';
import { cssVar } from '../../utils/css';

export const clsModal = style({
zIndex: 10000,
zIndex: cssVar('modal', 'zIndices'),
position: 'fixed',
left: 0,
top: 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/walletkit/src/base/components/toast/styles.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const clsToastRoot = style({
top: 0,
left: '50%',
transform: 'translateX(-50%)',
zIndex: 10001,
zIndex: cssVar('toast', 'zIndices'),
padding: 8,
display: 'flex',
flexDirection: 'column',
Expand Down
10 changes: 6 additions & 4 deletions packages/walletkit/src/components/DisconnectButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useConnect, useDisconnect } from 'wagmi';
import { Button, ButtonProps } from '../../base/components/Button';
import { ExitIcon } from '../../base/icons/ExitIcon';
import { useWalletKitContext } from '../WalletKitProvider/context';
import { cx } from '../../base/utils/css';
import { clsContainer } from './styles.css';
import { useModal } from '../..';

export type DisconnectButtonProps = ButtonProps;

Expand All @@ -12,12 +12,14 @@ export function DisconnectButton(props: DisconnectButtonProps) {

const { reset } = useConnect();
const { disconnect } = useDisconnect();
const { onClose } = useWalletKitContext();
const { onClose } = useModal();

const onDisconnect = () => {
disconnect();
reset();
onClose();
setTimeout(() => {
disconnect();
reset();
}, 100);
};

return (
Expand Down
21 changes: 21 additions & 0 deletions packages/walletkit/src/components/ModalProvider/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { useContext } from 'react';

export interface OpenSwitchNetworkOptions extends React.MouseEvent {
isClosable?: boolean;
}

export interface ModalContextProps {
isClosable: boolean;
isOpen: boolean;
onClose: () => void;
onOpen: () => void;
onOpenProfile: () => void;
onOpenSwitchNetwork: (options?: OpenSwitchNetworkOptions) => void;
}

export const ModalContext = React.createContext({} as ModalContextProps);

export function useModal() {
const context = useContext(ModalContext);
return context;
}
75 changes: 75 additions & 0 deletions packages/walletkit/src/components/ModalProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useEffect, useMemo, useState } from 'react';
import { useDisclosure } from '../../base/hooks/useDisclosure';
import { ModalContext, OpenSwitchNetworkOptions } from './context';
import { useRouter } from '../RouteProvider/context';
import { useAccount, useNetwork } from 'wagmi';
import { routes } from '../RouteProvider';
import { toast } from '../../base/components/toast';

export interface ModalProviderProps {
children: React.ReactNode;
}

export function ModalProvider(props: ModalProviderProps) {
const { children } = props;

const [isClosable, setIsClosable] = useState(true);

const { isOpen, onClose, onOpen } = useDisclosure();
const { isConnected } = useAccount();
const { chain } = useNetwork();
const router = useRouter();

useEffect(() => {
if (router.route === routes.SWITCH_NETWORK && isConnected && !chain?.unsupported) {
setIsClosable(true);
}
}, [chain?.unsupported, isConnected, router.route]);

const value = useMemo(() => {
return {
isClosable,
isOpen,
onClose() {
router.reset();
setIsClosable(true);
onClose();
},
onOpen() {
router.push(routes.CONNECTORS);
onOpen();
},
onOpenProfile() {
if (isConnected) {
router.push(routes.CONNECTED);
onOpen();
} else {
toast.info({
description: 'Please connect a wallet first.',
});
}
},
onOpenSwitchNetwork(options?: OpenSwitchNetworkOptions) {
const { isClosable = true } = options ?? {};

if (isConnected) {
router.push(routes.SWITCH_NETWORK);
setIsClosable(isClosable);
onOpen();
} else {
toast.info({
description: 'Please connect a wallet first.',
});
}
},
};
}, [isClosable, isConnected, isOpen, onClose, onOpen, router]);

useEffect(() => {
if ([routes.CONNECTING, routes.CONNECT_WITH_QRCODE].includes(router.route) && isConnected) {
onClose();
}
}, [isConnected, onClose, router.route]);

return <ModalContext.Provider value={value}>{children}</ModalContext.Provider>;
}
16 changes: 5 additions & 11 deletions packages/walletkit/src/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import { cx } from '../../base/utils/css';
import { useRouter } from '../RouteProvider/context';
import { useWalletKitContext } from '../WalletKitProvider/context';
import { Box, BoxProps } from '../../base/components/Box';
import { IconButton } from '../../base/components/IconButton';
import { BackIcon } from '../../base/icons/BackIcon';
import { CloseIcon } from '../../base/icons/CloseIcon';
import { clsNavbar } from './styles.css';
import { useModal } from '../..';

export interface NavbarProps extends BoxProps {
showBack?: boolean;
onBack?: () => void;
onClose?: () => void;
}

export function Navbar(props: NavbarProps) {
const { className, showBack = false, onBack, onClose, ...restProps } = props;
const { className, showBack = false, onBack, ...restProps } = props;

const { onClose: onCloseModal } = useWalletKitContext();
const { onClose: onCloseModal } = useModal();
const router = useRouter();

const onBeforeBack = () => {
onBack?.();
router.back();
};

const onBeforeClose = () => {
onClose?.();
onCloseModal();
};

return (
<Box className={cx('wk-navbar', clsNavbar, className)} {...restProps}>
{showBack && (
<IconButton className="wk-back-button" icon={<BackIcon />} onClick={onBeforeBack} />
<IconButton className="wk-navbar-back-button" icon={<BackIcon />} onClick={onBeforeBack} />
)}
<Box style={{ flex: 1 }} />
<IconButton className="wk-close-button" icon={<CloseIcon />} onClick={onBeforeClose} />
<IconButton className="wk-navbar-close-button" icon={<CloseIcon />} onClick={onCloseModal} />
</Box>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface RouteContextProps {
back: () => void;
replace: (nextRoute: string) => void;
push: (nextRoute: string) => void;
reset: () => void;
}

export const RouteContext = React.createContext({} as RouteContextProps);
Expand Down
Loading

0 comments on commit a3f373c

Please sign in to comment.