-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Graph network dropdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 105 additions & 72 deletions
177
src/app/pages/HomePage/Graph/NetworkSelector/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,133 @@ | ||
import { FC, useState } from 'react' | ||
import { FC, ForwardedRef, forwardRef, ReactElement } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { useTheme } from '@mui/material/styles' | ||
import { useScreenSize } from '../../../../hooks/useScreensize' | ||
import AddIcon from '@mui/icons-material/Add' | ||
import Box from '@mui/material/Box' | ||
import Button from '@mui/material/Button' | ||
import IconButton from '@mui/material/IconButton' | ||
import RemoveIcon from '@mui/icons-material/Remove' | ||
import Typography from '@mui/material/Typography' | ||
import { styled } from '@mui/material/styles' | ||
import { COLORS } from '../../../../../styles/theme/colors' | ||
import { getNetworkNames, Network } from '../../../../../types/network' | ||
import Collapse from '@mui/material/Collapse' | ||
import { RouteUtils } from '../../../../utils/route-utils' | ||
import { | ||
Select, | ||
SelectOptionBase, | ||
StyledSelectButton, | ||
StyledSelectListbox, | ||
StyledSelectOption, | ||
} from '../../../../components/Select' | ||
import { SelectRootSlotProps } from '@mui/base/Select' | ||
import ExpandLessIcon from '@mui/icons-material/ExpandLess' | ||
import ExpandMoreIcon from '@mui/icons-material/ExpandMore' | ||
import { getNetworkIcons } from '../../../../utils/content' | ||
import { optionClasses } from '@mui/base/Option' | ||
|
||
const StyledNetworkSelector = styled(Box)(() => ({ | ||
interface NetworkOption extends SelectOptionBase { | ||
label: Network | ||
value: Network | ||
} | ||
|
||
const StyledNetworkSelector = styled(Select<NetworkOption>)(({ theme }) => ({ | ||
position: 'absolute', | ||
bottom: 0, | ||
bottom: theme.spacing(5), | ||
display: 'flex', | ||
width: '100%', | ||
justifyContent: 'center', | ||
})) | ||
|
||
const StyledBox = styled(Box)(({ theme }) => ({ | ||
const StyledButton = styled(StyledSelectButton)(({ theme }) => ({ | ||
height: '44px', | ||
minWidth: '200px', | ||
backgroundColor: theme.palette.layout.primaryBackground, | ||
border: `solid 3px ${theme.palette.layout.lightBorder}`, | ||
borderRadius: '45px', | ||
padding: theme.spacing(3, 4), | ||
display: 'inline-flex', | ||
alignItems: 'center', | ||
border: `2px solid ${theme.palette.layout.lightBorder}`, | ||
color: theme.palette.layout.main, | ||
'&:hover': { | ||
backgroundColor: theme.palette.layout.primaryBackground, | ||
}, | ||
})) | ||
|
||
export const SelectNetworkButton = styled(Button, { | ||
shouldForwardProp: prop => prop !== 'isSelectedNetwork', | ||
})<{ isSelectedNetwork: boolean }>(({ isSelectedNetwork, theme }) => ({ | ||
height: '30px', | ||
padding: theme.spacing(2, 3), | ||
textTransform: 'capitalize', | ||
fontSize: '16px', | ||
borderRadius: '9px', | ||
backgroundColor: isSelectedNetwork | ||
? theme.palette.layout.primaryBackground | ||
: theme.palette.layout.secondary, | ||
borderColor: isSelectedNetwork ? theme.palette.layout.hoverBorder : theme.palette.layout.secondary, | ||
borderWidth: theme.spacing(1), | ||
color: theme.palette.layout.main, | ||
'&:hover, &:focus-visible': { | ||
backgroundColor: theme.palette.layout.secondary, | ||
borderWidth: theme.spacing(1), | ||
borderColor: COLORS.white, | ||
const StyledListbox = styled(StyledSelectListbox)(() => ({ | ||
minWidth: '200px', | ||
background: COLORS.white, | ||
color: COLORS.grayDark, | ||
})) | ||
|
||
const StyledOption = styled(StyledSelectOption)(() => ({ | ||
height: '44px', | ||
color: COLORS.grayDark, | ||
svg: { | ||
color: COLORS.grayExtraDark, | ||
}, | ||
[`&:hover:not(.${optionClasses.disabled}), | ||
&.${optionClasses.highlighted}`]: { | ||
backgroundColor: 'transparent', | ||
}, | ||
})) | ||
SelectNetworkButton.defaultProps = { | ||
size: 'small', | ||
variant: 'outlined', | ||
sx: { ml: 4 }, | ||
|
||
const SelectOption = ({ value }: NetworkOption): ReactElement => { | ||
const { t } = useTranslation() | ||
|
||
const labels = getNetworkNames(t) | ||
const icons = getNetworkIcons() | ||
|
||
return ( | ||
<StyledOption key={value} value={value}> | ||
<Box | ||
sx={theme => ({ display: 'flex', gap: theme.spacing(3), pl: theme.spacing(3), alignItems: 'center' })} | ||
> | ||
{icons[value]} | ||
<Typography variant="inherit">{labels[value]}</Typography> | ||
</Box> | ||
</StyledOption> | ||
) | ||
} | ||
|
||
type NetworkSelectorProps = { | ||
const NetworkSelectorButton = forwardRef( | ||
(props: SelectRootSlotProps<Network, false>, ref: ForwardedRef<HTMLButtonElement>) => { | ||
const { ownerState, ...restProps } = props | ||
const { open, value } = ownerState | ||
// Expecting value as Network | ||
const networkValue = value as Network | ||
const { t } = useTranslation() | ||
const label = getNetworkNames(t) | ||
const icons = getNetworkIcons() | ||
|
||
return ( | ||
<StyledButton {...restProps} ref={ref} color="inherit"> | ||
<Box | ||
sx={theme => ({ | ||
display: 'flex', | ||
gap: theme.spacing(3), | ||
pl: theme.spacing(3), | ||
alignItems: 'center', | ||
})} | ||
> | ||
{icons[networkValue]} | ||
<Typography variant="inherit">{label[networkValue]}</Typography> | ||
</Box> | ||
{open ? <ExpandLessIcon /> : <ExpandMoreIcon />} | ||
</StyledButton> | ||
) | ||
}, | ||
) | ||
|
||
interface NetworkSelectProps { | ||
network: Network | ||
setNetwork: (network: Network) => void | ||
setNetwork: (network: Network | null) => void | ||
} | ||
|
||
export const NetworkSelector: FC<NetworkSelectorProps> = ({ network, setNetwork }) => { | ||
const { t } = useTranslation() | ||
const theme = useTheme() | ||
const { isMobile } = useScreenSize() | ||
const [open, setOpen] = useState(false) | ||
const options: Network[] = RouteUtils.getEnabledNetworks() | ||
const labels = getNetworkNames(t) | ||
export const NetworkSelector: FC<NetworkSelectProps> = ({ network, setNetwork }) => { | ||
const options = RouteUtils.getEnabledNetworks().map(network => ({ | ||
label: network, | ||
value: network, | ||
})) | ||
|
||
return ( | ||
<StyledNetworkSelector> | ||
<StyledBox> | ||
{!isMobile && ( | ||
<Typography component="span" sx={{ fontSize: '12px', color: theme.palette.layout.main }}> | ||
{t('home.selectNetwork')} | ||
</Typography> | ||
)} | ||
<Box sx={{ height: 30, display: 'flex' }}> | ||
{options.map(option => ( | ||
<Collapse orientation="horizontal" in={open || network === option} key={option}> | ||
<SelectNetworkButton onClick={() => setNetwork(option)} isSelectedNetwork={option === network}> | ||
{labels[option]} | ||
</SelectNetworkButton> | ||
</Collapse> | ||
))} | ||
</Box> | ||
<IconButton aria-label={t('home.selectNetworkAria')} onClick={() => setOpen(!open)}> | ||
{open ? ( | ||
<RemoveIcon fontSize="medium" sx={{ color: theme.palette.layout.main, fontSize: '18px' }} /> | ||
) : ( | ||
<AddIcon fontSize="medium" sx={{ color: theme.palette.layout.main, fontSize: '18px' }} /> | ||
)} | ||
</IconButton> | ||
</StyledBox> | ||
</StyledNetworkSelector> | ||
<StyledNetworkSelector | ||
defaultValue={network} | ||
handleChange={setNetwork} | ||
options={options} | ||
placement="top-start" | ||
root={NetworkSelectorButton} | ||
Option={SelectOption} | ||
listbox={StyledListbox} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.