Skip to content

Commit b43732a

Browse files
committed
% selector in asset input
1 parent 71e1637 commit b43732a

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

src/components/transactions/Swap/actions/ActionsSkeleton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const stateToLoadingType = (state: SwapState): LoadingType => {
1313

1414
export const ActionsLoading: React.FC<{ state: SwapState }> = ({ state }) => {
1515
const loadingType = stateToLoadingType(state);
16-
17-
// Timer logic for updating the loading text after 2 seconds when loadingType is 'quote'
1816
const [quoteTimeElapsed, setQuoteTimeElapsed] = useState(false);
1917
const timerRef = useRef<NodeJS.Timeout | null>(null);
2018

19+
// Timer logic for updating the loading text after 2 seconds when loadingType is 'quote'
20+
// Trick to change quote loading trick to make it feel more smooth
2121
useEffect(() => {
2222
if (loadingType === 'quote') {
2323
setQuoteTimeElapsed(false);

src/components/transactions/Swap/inputs/primitives/SwapAssetInput.tsx

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { valueToBigNumber } from '@aave/math-utils';
12
import { isAddress } from '@ethersproject/address';
23
import { formatUnits } from '@ethersproject/units';
34
import { ExclamationIcon } from '@heroicons/react/outline';
@@ -12,7 +13,10 @@ import {
1213
InputBase,
1314
ListItemText,
1415
MenuItem,
16+
Popover,
1517
SvgIcon,
18+
ToggleButton,
19+
ToggleButtonGroup,
1620
Typography,
1721
useTheme,
1822
} from '@mui/material';
@@ -530,10 +534,20 @@ export const SwitchAssetInput = ({
530534
sx={{ ml: 1 }}
531535
/>
532536
</Typography>
537+
{!disableInput && (
538+
<PercentSelector
539+
disabled={disabled || Number(selectedAsset.balance) === 0}
540+
onSelectPercent={(fraction) => {
541+
const maxBase = forcedMaxValue || selectedAsset.balance || '0';
542+
const next = valueToBigNumber(maxBase).multipliedBy(fraction).toString();
543+
onChange && onChange(next);
544+
}}
545+
/>
546+
)}
533547
{!disableInput && (
534548
<Button
535549
size="small"
536-
sx={{ minWidth: 0, ml: '7px', p: 0 }}
550+
sx={{ minWidth: 0, ml: '1px', pt: 0, pb: 0, mr: '-5px' }}
537551
onClick={() => {
538552
onChange && onChange(forcedMaxValue || '-1');
539553
}}
@@ -555,3 +569,85 @@ export const SwitchAssetInput = ({
555569
</Box>
556570
);
557571
};
572+
573+
const PercentSelector = ({
574+
disabled,
575+
onSelectPercent,
576+
}: {
577+
disabled?: boolean;
578+
onSelectPercent: (fraction: number) => void;
579+
}) => {
580+
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
581+
const open = Boolean(anchorEl);
582+
583+
const handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
584+
if (disabled) return;
585+
setAnchorEl(event.currentTarget);
586+
};
587+
const handleClose = () => setAnchorEl(null);
588+
589+
const handlePick = (fraction: number) => {
590+
onSelectPercent(fraction);
591+
handleClose();
592+
};
593+
594+
return (
595+
<>
596+
<Button
597+
size="small"
598+
sx={{ minWidth: 0, ml: '6px', py: 0, fontSize: '12px' }}
599+
onClick={handleOpen}
600+
disabled={disabled}
601+
>
602+
<Trans>%</Trans>
603+
</Button>
604+
<Popover
605+
open={open}
606+
anchorEl={anchorEl}
607+
onClose={handleClose}
608+
anchorOrigin={{ vertical: 'top', horizontal: 'left' }}
609+
transformOrigin={{ vertical: 'bottom', horizontal: 'left' }}
610+
PaperProps={{
611+
sx: {
612+
p: 1,
613+
backgroundColor: 'background.surface',
614+
border: '1px solid',
615+
borderColor: 'divider',
616+
},
617+
}}
618+
>
619+
<Box sx={{ display: 'flex', alignItems: 'center', p: 0.5 }}>
620+
<ToggleButtonGroup
621+
exclusive
622+
sx={{
623+
backgroundColor: 'background.surface',
624+
borderRadius: '6px',
625+
borderColor: 'background.surface',
626+
}}
627+
onChange={(_, v) => v && handlePick(v)}
628+
>
629+
{[0.25, 0.5, 0.75].map((fraction) => (
630+
<ToggleButton
631+
key={fraction}
632+
value={fraction}
633+
sx={{
634+
borderRadius: 1,
635+
py: 0.5,
636+
px: 1,
637+
borderWidth: 2,
638+
'&.Mui-selected': {
639+
backgroundColor: 'background.paper',
640+
},
641+
}}
642+
>
643+
<Typography variant="subheader2" color="primary.main">
644+
{Math.round(fraction * 100)}%
645+
</Typography>
646+
</ToggleButton>
647+
))}
648+
</ToggleButtonGroup>
649+
</Box>
650+
</Popover>
651+
</>
652+
);
653+
};

0 commit comments

Comments
 (0)