Skip to content

Commit

Permalink
feat: add drawer variant to main config
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Oct 13, 2022
1 parent e7e0079 commit 5407a34
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
18 changes: 9 additions & 9 deletions packages/widget-embedded/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
switchChain,
switchChainAndAddToken,
} from '@lifi/wallet-management';
import { LiFiWidget, LiFiWidgetDrawer } from '@lifi/widget';
import { LiFiWidget } from '@lifi/widget';
import {
Box,
// Button,
Expand All @@ -23,7 +23,7 @@ import { createTheme, ThemeProvider } from '@mui/material/styles';
import React, { useEffect, useState } from 'react';
import { WalletButtons } from './components/WalletButtons';
import { WidgetEvents } from './components/WidgetEvents';
import { widgetConfig, widgetDrawerConfig } from './config';
import { widgetBaseConfig, widgetConfig } from './config';
import './index.css';
import { useWallet } from './providers/WalletProvider';

Expand Down Expand Up @@ -67,7 +67,7 @@ export const App = () => {
useEffect(() => {
setConfig(() => ({
...(drawer
? widgetDrawerConfig
? widgetBaseConfig
: {
...widgetConfig,
appearance: systemColor ? 'auto' : darkMode ? 'dark' : 'light',
Expand Down Expand Up @@ -97,7 +97,11 @@ export const App = () => {
fontFamily,
},
},
variant: expandableRoutesView ? 'expandable' : 'default',
variant: drawer
? 'drawer'
: expandableRoutesView
? 'expandable'
: 'default',
}));
}, [
borderRadius,
Expand Down Expand Up @@ -311,11 +315,7 @@ export const App = () => {
</Box>
</Drawer>
<Box flex={1} margin="auto">
{drawer ? (
<LiFiWidgetDrawer config={config} open />
) : (
<LiFiWidget config={config} />
)}
<LiFiWidget config={config} open />
</Box>
</Box>
</ThemeProvider>
Expand Down
13 changes: 11 additions & 2 deletions packages/widget-embedded/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { WidgetConfig } from '@lifi/widget';
import './index.css';

export const widgetDrawerConfig: WidgetConfig = {
export const widgetBaseConfig: WidgetConfig = {
// fromChain: 137,
// toChain: 10,
// fromToken: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
Expand All @@ -22,6 +22,15 @@ export const widgetDrawerConfig: WidgetConfig = {
// },
tokens: {
featured: [
// {
// address: '0xba98c0fbebc892f5b07a42b0febd606913ebc981',
// symbol: 'MEH',
// decimals: 18,
// chainId: 1,
// name: 'meh',
// logoURI:
// 'https://s2.coinmarketcap.com/static/img/coins/64x64/22158.png',
// },
{
address: '0x195e3087ea4d7eec6e9c37e9640162fe32433d5e',
symbol: '$ALTI',
Expand Down Expand Up @@ -91,7 +100,7 @@ export const widgetDrawerConfig: WidgetConfig = {
};

export const widgetConfig: WidgetConfig = {
...widgetDrawerConfig,
...widgetBaseConfig,
containerStyle: {
// border: `1px solid ${
// window.matchMedia('(prefers-color-scheme: dark)').matches
Expand Down
28 changes: 20 additions & 8 deletions packages/widget/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { AppProps } from './AppProvider';
import { forwardRef } from 'react';
import type { WidgetDrawer } from './AppDrawer';
import { AppDrawer } from './AppDrawer';
import { AppProvider } from './AppProvider';
import { AppRoutes } from './AppRoutes';
import {
Expand All @@ -11,14 +13,24 @@ import { Initializer } from './components/Initializer';
import { PoweredBy } from './components/PoweredBy';
import { SwapRoutesExpanded } from './components/SwapRoutes';
import { useExpandableVariant } from './hooks';
import type { WidgetProps } from './types';

export const App: React.FC<AppProps> = ({ config }) => {
return (
<AppProvider config={config}>
<AppDefault />
</AppProvider>
);
};
export const App: React.FC<WidgetProps> = forwardRef<WidgetDrawer, WidgetProps>(
({ elementRef, open, config }, ref) => {
return config?.variant !== 'drawer' ? (
<AppProvider config={config}>
<AppDefault />
</AppProvider>
) : (
<AppDrawer
ref={ref}
elementRef={elementRef}
config={config}
open={open}
/>
);
},
);

export const AppDefault = () => {
const expandable = useExpandableVariant();
Expand Down
11 changes: 2 additions & 9 deletions packages/widget/src/AppDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import {
KeyboardArrowLeft as KeyboardArrowLeftIcon,
KeyboardArrowRight as KeyboardArrowRightIcon,
} from '@mui/icons-material';
import type { DrawerProps } from '@mui/material';
import { Drawer } from '@mui/material';
import type { RefObject } from 'react';
import {
forwardRef,
useCallback,
Expand All @@ -17,12 +15,7 @@ import { useTranslation } from 'react-i18next';
import { AppDefault } from './App';
import { DrawerButton, DrawerButtonTypography } from './AppDrawer.style';
import { AppProvider } from './AppProvider';
import type { WidgetConfig } from './types';

export type WidgetDrawerProps = DrawerProps & {
elementRef?: RefObject<HTMLDivElement>;
config?: WidgetConfig;
};
import type { WidgetConfig, WidgetProps } from './types';

export interface WidgetDrawer {
isOpen(): void;
Expand All @@ -31,7 +24,7 @@ export interface WidgetDrawer {
closeDrawer(): void;
}

export const AppDrawer = forwardRef<WidgetDrawer, WidgetDrawerProps>(
export const AppDrawer = forwardRef<WidgetDrawer, WidgetProps>(
({ elementRef, open, config }, ref) => {
const { t } = useTranslation();
const openRef = useRef(open);
Expand Down
8 changes: 2 additions & 6 deletions packages/widget/src/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { QueryClientProvider } from '@tanstack/react-query';
import type { FC, PropsWithChildren } from 'react';
import { Fragment } from 'react';
import { MemoryRouter, useInRouterContext } from 'react-router-dom';
import type { WidgetConfig } from '.';
import { queryClient } from './config/queryClient';
import {
SDKProvider,
Expand All @@ -13,16 +12,13 @@ import {
WalletProvider,
WidgetProvider,
} from './providers';

export interface AppProps {
config?: WidgetConfig;
}
import type { WidgetProps } from './types';

const QueryProvider = QueryClientProvider as FC<
PropsWithChildren<QueryClientProviderProps>
>;

export const AppProvider: React.FC<PropsWithChildren<AppProps>> = ({
export const AppProvider: React.FC<PropsWithChildren<WidgetProps>> = ({
children,
config,
}) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AppDrawer } from './AppDrawer';
import './fonts/inter.css';
import { configureReactI18next } from './i18n';

export type { WidgetDrawer, WidgetDrawerProps } from './AppDrawer';
export type { WidgetDrawer } from './AppDrawer';
export { useWidgetEvents } from './hooks';
export * from './types';

Expand All @@ -13,4 +13,8 @@ configureReactI18next();
// );

export const LiFiWidget = App;

/**
* @deprecated Use configuration { variant: 'drawer' } instead
*/
export const LiFiWidgetDrawer = AppDrawer;
8 changes: 7 additions & 1 deletion packages/widget/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ChainKey, ConfigUpdate, Token } from '@lifi/sdk';
import type { PaletteMode, PaletteOptions, Shape } from '@mui/material';
import type { TypographyOptions } from '@mui/material/styles/createTypography';
import type { Signer } from 'ethers';
import type { CSSProperties } from 'react';
import type { CSSProperties, RefObject } from 'react';

export type WidgetVariant = 'default' | 'expandable' | 'drawer';

Expand Down Expand Up @@ -69,3 +69,9 @@ export interface WidgetConfig {
deny?: (Partial<Token> & Pick<Token, 'address' | 'chainId'>)[];
};
}

export type WidgetProps = {
elementRef?: RefObject<HTMLDivElement>;
config?: WidgetConfig;
open?: boolean;
};

0 comments on commit 5407a34

Please sign in to comment.