Skip to content

Commit

Permalink
feat: add select token drawer, header improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 3, 2022
1 parent 165a549 commit 00c28a9
Show file tree
Hide file tree
Showing 14 changed files with 745 additions and 601 deletions.
8 changes: 4 additions & 4 deletions libs/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"types": "./lib/react-app.d.ts",
"dependencies": {
"@babel/core": "^7.16.0",
"@babel/core": "^7.17.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
"@svgr/webpack": "^6.2.1",
"babel-jest": "^27.4.2",
Expand All @@ -42,8 +42,8 @@
"case-sensitive-paths-webpack-plugin": "^2.4.0",
"css-loader": "^6.6.0",
"css-minimizer-webpack-plugin": "^3.2.0",
"dotenv": "^15.0.0",
"dotenv-expand": "^6.0.1",
"dotenv": "^16.0.0",
"dotenv-expand": "^7.0.0",
"eslint": "^8.8.0",
"eslint-config-react-app": "^7.0.0",
"eslint-webpack-plugin": "^3.1.1",
Expand All @@ -59,7 +59,7 @@
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-loader": "^6.2.1",
"postcss-normalize": "^10.0.1",
"postcss-preset-env": "^7.3.0",
"postcss-preset-env": "^7.3.1",
"prompts": "^2.4.2",
"react-app-polyfill": "^3.0.0",
"react-dev-utils": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "lerna run --parallel start"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
"@types/node": "^17.0.14",
Expand Down
31 changes: 27 additions & 4 deletions packages/widget/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import { ThemeProvider } from '@mui/material/styles';
import { Container } from '@mui/material';
import { styled, ThemeProvider } from '@mui/material/styles';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { NavigationHeader } from './components/NavigationHeader';
import { WalletHeader } from './components/WalletHeader';
import { SettingsPage } from './pages/SettingsPage';
import { SwapPage } from './pages/SwapPage';
import { SwapFormProvider } from './providers/SwapFormProvider';
import { theme } from './theme';
import { routes } from './utils/routes';

const MainContainer = styled(Container)(({ theme }) => ({
backgroundColor: theme.palette.background.default,
height: '100%',
display: 'flex',
flexDirection: 'column',
overflowX: 'clip',
}));

export function App() {
return (
<ThemeProvider theme={theme}>
<MemoryRouter>
<Routes>
<Route path="/" element={<SwapPage />} />
</Routes>
<MainContainer maxWidth="sm" disableGutters>
<WalletHeader />
<NavigationHeader />
<SwapFormProvider>
<Routes>
<Route path={routes.home} element={<SwapPage />}>
<Route path={routes.selectToken} element={null} />
</Route>
<Route path={routes.settings} element={<SettingsPage />} />
</Routes>
</SwapFormProvider>
</MainContainer>
</MemoryRouter>
</ThemeProvider>
);
Expand Down
35 changes: 1 addition & 34 deletions packages/widget/src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Tune as TuneIcon } from '@mui/icons-material';
import { AppBar, IconButton, Toolbar, Typography } from '@mui/material';
import { AppBar, Toolbar } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTranslation } from 'react-i18next';

const HeaderAppBar = styled(AppBar)(({ theme }) => ({
backgroundColor: theme.palette.common.black,
Expand All @@ -18,34 +16,3 @@ export const Header: React.FC = ({ children }) => (
<HeaderToolbar>{children}</HeaderToolbar>
</HeaderAppBar>
);

export const WalletHeader: React.FC = () => {
const { t } = useTranslation();
return (
<Header>
<Typography
variant="body2"
noWrap
align="right"
sx={{ flexGrow: 1 }}
color="grey.500"
>
{t(`swap.header.walletConnected`, { walletAddress: '0000000000' })}
</Typography>
</Header>
);
};

export const MainHeader: React.FC = () => {
const { t } = useTranslation();
return (
<Header>
<Typography variant="h6" noWrap sx={{ flexGrow: 1 }}>
{t(`swap.header.title`)}
</Typography>
<IconButton size="large" aria-label="settings" color="inherit" edge="end">
<TuneIcon />
</IconButton>
</Header>
);
};
70 changes: 70 additions & 0 deletions packages/widget/src/components/NavigationHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
ArrowBack as ArrowBackIcon,
Tune as TuneIcon,
} from '@mui/icons-material';
import { Collapse, IconButton, Typography } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { useLocation, useNavigate } from 'react-router-dom';
import { routes } from '../utils/routes';
import { Header } from './Header';

const routesWithBack = [routes.settings, routes.selectToken];

export const NavigationHeader: React.FC = () => {
const { t } = useTranslation();
const location = useLocation();
const navigate = useNavigate();

const handleSettings = () => {
navigate(routes.settings, { replace: true });
};

const handleBack = () => {
navigate(routes.home, { replace: true });
};

const handleHeaderTitle = () => {
switch (location.pathname) {
case routes.settings:
return t(`swap.header.settings`);
case routes.selectToken:
return t(`swap.header.iWouldLikeToSwap`);
default:
return t(`swap.header.swap`);
}
};

return (
<Header>
<Collapse
collapsedSize={0}
orientation="horizontal"
in={routesWithBack.includes(location.pathname)}
>
<IconButton
size="large"
aria-label="settings"
color="inherit"
edge="start"
onClick={handleBack}
>
<ArrowBackIcon />
</IconButton>
</Collapse>
<Typography variant="h6" noWrap sx={{ flexGrow: 1 }}>
{handleHeaderTitle()}
</Typography>
{location.pathname === '/' && (
<IconButton
size="large"
aria-label="settings"
color="inherit"
edge="end"
onClick={handleSettings}
>
<TuneIcon />
</IconButton>
)}
</Header>
);
};
116 changes: 116 additions & 0 deletions packages/widget/src/components/SelectTokenDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Inbox as InboxIcon } from '@mui/icons-material';
import {
Box,
Divider,
Drawer,
DrawerProps,
List,
ListItem,
ListItemIcon,
ListItemText
} from '@mui/material';
import {
forwardRef,
RefObject,
useCallback,
useEffect,
useImperativeHandle,
useState
} from 'react';
import { useMatch, useNavigate } from 'react-router-dom';
import { routes } from '../utils/routes';

export type SelectTokenDrawerProps = DrawerProps & {
tokens?: string[];
containerRef: RefObject<Element>;
};

export interface SelectTokenDrawerBase {
openDrawer(): void;
closeDrawer(): void;
}

export const SelectTokenDrawer = forwardRef<
SelectTokenDrawerBase,
SelectTokenDrawerProps
>(({ tokens, containerRef }, ref) => {
const navigate = useNavigate();
const [open, setOpen] = useState(false);
const homeMatch = useMatch(routes.home);

const openDrawer = useCallback(() => {
navigate(routes.selectToken, { replace: true });
setOpen(true);
}, [navigate]);

const closeDrawer = useCallback(() => {
setOpen(false);
navigate(routes.home, { replace: true });
}, [navigate]);

useImperativeHandle(
ref,
() => ({
openDrawer,
closeDrawer,
}),
[closeDrawer, openDrawer],
);

useEffect(() => {
if (homeMatch && open) {
setOpen(false);
}
}, [homeMatch, open]);

const list = () => (
<Box role="presentation" onClick={closeDrawer} onKeyDown={closeDrawer}>
<List>
<ListItem button>
<ListItemIcon>
<InboxIcon />
</ListItemIcon>
<ListItemText primary={'Test'} />
</ListItem>
</List>
<Divider />
<List>
<ListItem button>
<ListItemIcon>
<InboxIcon />
</ListItemIcon>
<ListItemText primary={'Test'} />
</ListItem>
</List>
</Box>
);

return (
<Drawer
container={containerRef.current}
anchor="right"
open={open}
onClose={closeDrawer}
ModalProps={{
sx: { position: 'absolute' },
// keepMounted: true,
}}
PaperProps={{
sx: {
position: 'absolute',
width: '85%',
},
}}
BackdropProps={{
sx: {
position: 'absolute',
backgroundColor: 'rgba(0,0,0,0.12)',
backdropFilter: 'blur(3px)',
},
}}
SlideProps={{ container: containerRef.current }}
>
{list()}
</Drawer>
);
});
Loading

0 comments on commit 00c28a9

Please sign in to comment.