Skip to content

Commit

Permalink
feat: add different layout presentations
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed May 30, 2022
1 parent bbc3bcc commit 05cbcb6
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 70 deletions.
3 changes: 3 additions & 0 deletions packages/widget-embedded/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
},
"dependencies": {
"@lifinance/widget": "^0.1.0",
"@mui/icons-material": "^5.8.0",
"@mui/lab": "^5.0.0-alpha.83",
"@mui/material": "^5.8.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/widget-embedded/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ body {
margin: 0;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
background-color: #F4F5F6;
background-color: #F4F5F6 !important;
}

@media (prefers-color-scheme: dark) {
Expand Down
84 changes: 28 additions & 56 deletions packages/widget/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,42 @@
import { FC, PropsWithChildren } from 'react';
import { QueryClientProvider, QueryClientProviderProps } from 'react-query';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { WidgetConfig } from '.';
import { Route, Routes } from 'react-router-dom';
import { AppProps, AppProvider } from './AppProvider';
import { AppContainer } from './components/AppContainer';
import { Header } from './components/Header';
import { queryClient } from './config/queryClient';
import { MainPage } from './pages/MainPage';
import { SelectTokenPage } from './pages/SelectTokenPage';
import { SelectWalletPage } from './pages/SelectWalletPage';
import { SettingsPage } from './pages/SettingsPage';
import { SwapPage } from './pages/SwapPage';
import { SwapRoutesPage } from './pages/SwapRoutesPage';
import { SwapFormProvider } from './providers/SwapFormProvider';
import { ThemeProvider } from './providers/ThemeProvider';
import { WalletProvider } from './providers/WalletProvider';
import { WidgetProvider } from './providers/WidgetProvider';
import { routes } from './utils/routes';

interface AppProps {
config: WidgetConfig;
}

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

export const App: React.FC<AppProps> = ({ config }) => {
return (
<WidgetProvider config={config}>
<ThemeProvider>
<QueryProvider client={queryClient}>
<MemoryRouter>
<WalletProvider>
<SwapFormProvider>
<AppContainer
sx={config.containerStyle}
style={config.baselineStyle}
>
<Header />
<Routes>
<Route path={routes.home} element={<MainPage />} />
<Route
path={routes.selectWallet}
element={<SelectWalletPage />}
/>
<Route path={routes.settings} element={<SettingsPage />} />
<Route
path={routes.fromToken}
element={<SelectTokenPage formType="from" />}
/>
<Route
path={routes.toToken}
element={<SelectTokenPage formType="to" />}
/>
<Route
path={routes.swapRoutes}
element={<SwapRoutesPage />}
/>
<Route path={routes.swap} element={<SwapPage />} />
</Routes>
</AppContainer>
</SwapFormProvider>
</WalletProvider>
</MemoryRouter>
</QueryProvider>
</ThemeProvider>
</WidgetProvider>
<AppProvider config={config}>
<AppDefault />
</AppProvider>
);
};

export const AppDefault = () => {
return (
<AppContainer>
<Header />
<Routes>
<Route path={routes.home} element={<MainPage />} />
<Route path={routes.selectWallet} element={<SelectWalletPage />} />
<Route path={routes.settings} element={<SettingsPage />} />
<Route
path={routes.fromToken}
element={<SelectTokenPage formType="from" />}
/>
<Route
path={routes.toToken}
element={<SelectTokenPage formType="to" />}
/>
<Route path={routes.swapRoutes} element={<SwapRoutesPage />} />
<Route path={routes.swap} element={<SwapPage />} />
</Routes>
</AppContainer>
);
};
59 changes: 59 additions & 0 deletions packages/widget/src/AppDrawer.style.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Button, Typography } from '@mui/material';
import { styled } from '@mui/material/styles';

export const DrawerButton = styled(Button, {
shouldForwardProp: (prop) => prop !== 'open',
})<{ open?: boolean }>(({ theme, open }) => ({
background:
theme.palette.mode === 'light'
? theme.palette.common.black
: theme.palette.common.white,
color:
theme.palette.mode === 'light'
? theme.palette.common.white
: theme.palette.common.black,
alignItems: 'center',
borderRadius: `${theme.shape.borderRadius}px 0 0 ${theme.shape.borderRadius}px`,
display: 'flex',
flexDirection: 'column',
height: 148,
justifyContent: 'center',
minWidth: 40,
padding: theme.spacing(1, 0, 1, 0),
position: 'absolute',
right: 0,
top: 'calc(50% - 74px)',
transform: `translate3d(calc(${open ? '392px' : '0px'} * -1), 0, 0)`,
transition: theme.transitions.create(['transform'], {
duration: theme.transitions.duration.enteringScreen,
easing: theme.transitions.easing.easeOut,
}),
zIndex: 1500,
'&:hover': {
background:
theme.palette.mode === 'light'
? theme.palette.common.black
: theme.palette.common.white,
},
}));

export const DrawerButtonTypography = styled(Typography)(({ theme }) => ({
borderRadius: `0 ${theme.shape.borderRadius}px ${theme.shape.borderRadius}px 0`,
background:
theme.palette.mode === 'light'
? theme.palette.common.black
: theme.palette.common.white,
color:
theme.palette.mode === 'light'
? theme.palette.common.white
: theme.palette.common.black,
cursor: 'pointer',
display: 'flex',
flexDirection: 'row',
fontSize: '18px',
padding: theme.spacing(1),
position: 'relative',
textTransform: 'none',
transform: 'rotateZ(180deg)',
writingMode: 'vertical-rl',
}));
92 changes: 92 additions & 0 deletions packages/widget/src/AppDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
KeyboardArrowLeft as KeyboardArrowLeftIcon,
KeyboardArrowRight as KeyboardArrowRightIcon,
} from '@mui/icons-material';
import { Drawer, DrawerProps } from '@mui/material';
import {
forwardRef,
RefObject,
useCallback,
useImperativeHandle,
useRef,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { AppDefault } from './App';
import { DrawerButton, DrawerButtonTypography } from './AppDrawer.style';
import { AppProvider } from './AppProvider';
import { WidgetConfig } from './types';

export type AppDrawerProps = DrawerProps & {
elementRef?: RefObject<HTMLDivElement>;
config: WidgetConfig;
};

export interface AppDrawerBase {
isOpen(): void;
toggleDrawer(): void;
openDrawer(): void;
closeDrawer(): void;
}

export const AppDrawer = forwardRef<AppDrawerBase, AppDrawerProps>(
({ elementRef, open, config }, ref) => {
const { t } = useTranslation();
const openRef = useRef(open);
const [drawerOpen, setDrawerOpen] = useState(open);

const toggleDrawer = useCallback(() => {
setDrawerOpen((open) => !open);
}, []);

const openDrawer = useCallback(() => {
setDrawerOpen(true);
}, []);

const closeDrawer = useCallback(() => {
setDrawerOpen(false);
}, []);

useImperativeHandle(
ref,
() => ({
isOpen: () => openRef.current,
toggleDrawer,
openDrawer,
closeDrawer,
}),
[closeDrawer, openDrawer, toggleDrawer],
);

return (
<AppProvider config={config}>
<DrawerButton
variant="contained"
onClick={toggleDrawer}
open={drawerOpen}
disableElevation
>
{drawerOpen ? <KeyboardArrowRightIcon /> : <KeyboardArrowLeftIcon />}
<DrawerButtonTypography>
{drawerOpen ? t('button.hide') : t('button.lifiSwap')}
</DrawerButtonTypography>
</DrawerButton>
<Drawer
ref={elementRef}
anchor="right"
open={drawerOpen}
onClose={closeDrawer}
BackdropProps={{
sx: {
backgroundColor: 'rgb(0 0 0 / 48%)',
backdropFilter: 'blur(3px)',
},
}}
keepMounted
>
<AppDefault />
</Drawer>
</AppProvider>
);
},
);
36 changes: 36 additions & 0 deletions packages/widget/src/AppProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { FC, PropsWithChildren } from 'react';
import { QueryClientProvider, QueryClientProviderProps } from 'react-query';
import { MemoryRouter } from 'react-router-dom';
import { WidgetConfig } from '.';
import { queryClient } from './config/queryClient';
import { SwapFormProvider } from './providers/SwapFormProvider';
import { ThemeProvider } from './providers/ThemeProvider';
import { WalletProvider } from './providers/WalletProvider';
import { WidgetProvider } from './providers/WidgetProvider';

export interface AppProps {
config: WidgetConfig;
}

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

export const AppProvider: React.FC<PropsWithChildren<AppProps>> = ({
children,
config,
}) => {
return (
<WidgetProvider config={config}>
<ThemeProvider>
<QueryProvider client={queryClient}>
<MemoryRouter>
<WalletProvider>
<SwapFormProvider>{children}</SwapFormProvider>
</WalletProvider>
</MemoryRouter>
</QueryProvider>
</ThemeProvider>
</WidgetProvider>
);
};
12 changes: 6 additions & 6 deletions packages/widget/src/components/AppContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Box, BoxProps, ScopedCssBaseline } from '@mui/material';
import { Box, ScopedCssBaseline } from '@mui/material';
import { styled } from '@mui/material/styles';
import { PropsWithChildren, RefObject, useLayoutEffect, useRef } from 'react';
import { useLocation } from 'react-router-dom';
import { useWidgetConfig } from '../providers/WidgetProvider';
import { ElementId } from '../utils/elements';
import { PoweredBy } from './PoweredBy';

Expand All @@ -22,6 +23,7 @@ const RelativeContainer = styled(Box)(({ theme }) => ({
width: 392,
background: theme.palette.background.default,
overflow: 'auto',
flex: 1,
}));

const ScrollableContainer = styled(Box)({
Expand All @@ -32,13 +34,11 @@ const ScrollableContainer = styled(Box)({
display: 'flex',
});

export const AppContainer: React.FC<PropsWithChildren<BoxProps>> = ({
children,
sx,
}) => {
export const AppContainer: React.FC<PropsWithChildren<{}>> = ({ children }) => {
const ref = useRef<HTMLElement>(null);
const { containerStyle } = useWidgetConfig();
return (
<RelativeContainer sx={sx}>
<RelativeContainer sx={containerStyle}>
<ScrollableContainer id={ElementId.ScrollableContainer} ref={ref}>
<CssBaselineContainer enableColorScheme>
{children}
Expand Down
29 changes: 25 additions & 4 deletions packages/widget/src/config/theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { PaletteMode } from '@mui/material';
import {
PaletteMode,
PaletteOptions,
SimplePaletteColorOptions,
} from '@mui/material';
import {
alpha,
createTheme as createMuiTheme,
Expand Down Expand Up @@ -47,16 +51,34 @@ const palette = {
},
};

export const createTheme = (mode: PaletteMode) =>
export const createTheme = (
mode: PaletteMode,
paletteOptions: PaletteOptions = {},
) =>
createMuiTheme({
typography: {
fontFamily: 'Inter var, Inter, sans-serif',
},
palette: {
mode,
...palette,
primary: {
main:
(paletteOptions?.primary as SimplePaletteColorOptions)?.main ??
palette.primary.main,
light: lighten(
(paletteOptions?.primary as SimplePaletteColorOptions)?.main ??
palette.primary.main,
0.5,
),
dark: darken(
(paletteOptions?.primary as SimplePaletteColorOptions)?.main ??
palette.primary.main,
0.2,
),
},
...(mode === 'light'
? {
...palette,
text: {
primary: '#000',
secondary: '#52575b',
Expand All @@ -72,7 +94,6 @@ export const createTheme = (mode: PaletteMode) =>
},
}
: {
...palette,
background: {
paper: '#212121',
},
Expand Down
Loading

0 comments on commit 05cbcb6

Please sign in to comment.