Skip to content

Commit

Permalink
fix: dialog positioning issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Nov 20, 2024
1 parent 7ee4e20 commit 5954fda
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable react/jsx-max-props-per-line */

import { Dialog, Grid, Slide, Typography, useTheme } from '@mui/material';
import React, { useCallback, useLayoutEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';

import Infotip2 from '../../../components/Infotip2';
import { getStorage } from '../../../components/Loading';
Expand All @@ -29,12 +29,19 @@ interface Props {

function Currency ({ borderColor, color, dialogLeft = 260, fontSize = '22px', height, minWidth }: Props): React.ReactElement {
const theme = useTheme();
const ref = useRef<DOMRect>();

const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
const [currencyToShow, setCurrencyToShow] = useState<CurrencyItemType | undefined>();

const textColor = useMemo(() => color || (theme.palette.mode === 'dark' ? theme.palette.text.primary : theme.palette.text.secondary), [color, theme]);

useEffect(() => {
if (anchorEl?.getBoundingClientRect()) {
ref.current = anchorEl.getBoundingClientRect();
}
}, [anchorEl]);

useLayoutEffect(() => {
if (currencyToShow) {
return;
Expand Down Expand Up @@ -71,39 +78,37 @@ function Currency ({ borderColor, color, dialogLeft = 260, fontSize = '22px', he
</Typography>
</Infotip2>
</Grid>
{anchorEl &&
<Dialog
PaperProps={{
<Dialog
PaperProps={{
sx: {
bgcolor: 'background.paper',
borderRadius: '7px',
boxShadow: theme.palette.mode === 'dark'
? '0px 4px 4px rgba(255, 255, 255, 0.25)'
: '0px 0px 25px 0px rgba(0, 0, 0, 0.50)',
left: ((anchorEl?.getBoundingClientRect() || ref.current)?.right ?? 0) - dialogLeft,
position: 'absolute',
top: ((anchorEl?.getBoundingClientRect() || ref.current)?.bottom ?? 0) - 30
}
}}
TransitionComponent={Slide}
onClose={handleClose}
open={!!anchorEl}
slotProps={{
backdrop: {
sx: {
bgcolor: 'background.paper',
borderRadius: '7px',
boxShadow: theme.palette.mode === 'dark'
? '0px 4px 4px rgba(255, 255, 255, 0.25)'
: '0px 0px 25px 0px rgba(0, 0, 0, 0.50)',
left: anchorEl?.getBoundingClientRect().right - dialogLeft,
position: 'absolute',
top: anchorEl?.getBoundingClientRect().bottom - 30
backdropFilter: 'blur(8px)',
backgroundColor: 'rgba(0, 0, 0, 0.3)'
}
}}
TransitionComponent={Slide}
onClose={handleClose}
open={!!anchorEl}
slotProps={{
backdrop: {
sx: {
backdropFilter: 'blur(8px)',
backgroundColor: 'rgba(0, 0, 0, 0.3)'
}
}
}}
>
<CurrencyList
anchorEl={anchorEl}
setAnchorEl={setAnchorEl}
setCurrencyToShow={setCurrencyToShow}
/>
</Dialog>
}
}
}}
>
<CurrencyList
anchorEl={anchorEl}
setAnchorEl={setAnchorEl}
setCurrencyToShow={setCurrencyToShow}
/>
</Dialog>
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { faSliders } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Badge, Dialog, Grid, Slide, useTheme } from '@mui/material';
import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { useIsTestnetEnabled, useSelectedChains } from '../../../hooks';
import { TEST_NETS } from '../../../util/constants';
Expand All @@ -19,11 +19,18 @@ export default function FavoriteChains (): React.ReactElement {
const theme = useTheme();
const selectedChains = useSelectedChains();
const isTestNetEnabled = useIsTestnetEnabled();
const ref = useRef<DOMRect>();

const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);

const color = theme.palette.mode === 'dark' ? theme.palette.text.primary : theme.palette.text.secondary;

useEffect(() => {
if (anchorEl?.getBoundingClientRect()) {
ref.current = anchorEl.getBoundingClientRect();
}
}, [anchorEl]);

const onChainListClick = useCallback((event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(anchorEl ? null : event.currentTarget);
}, [anchorEl]);
Expand Down Expand Up @@ -53,7 +60,7 @@ export default function FavoriteChains (): React.ReactElement {
sx={{
'& .MuiBadge-badge': {
color: 'white'
},
}
}}
>
<Grid
Expand All @@ -72,7 +79,6 @@ export default function FavoriteChains (): React.ReactElement {
icon={faSliders}
/>
</Grid>
{ anchorEl &&
<Dialog
PaperProps={{
sx: {
Expand All @@ -81,9 +87,9 @@ export default function FavoriteChains (): React.ReactElement {
boxShadow: theme.palette.mode === 'dark'
? '0px 4px 4px rgba(255, 255, 255, 0.25)'
: '0px 0px 25px 0px rgba(0, 0, 0, 0.50)',
left: anchorEl?.getBoundingClientRect().right - 310,
left: ((anchorEl?.getBoundingClientRect() || ref.current)?.right ?? 0) - 310,
position: 'absolute',
top: anchorEl?.getBoundingClientRect().bottom - 30
top: ((anchorEl?.getBoundingClientRect() || ref.current)?.bottom ?? 0) - 30
}
}}
TransitionComponent={Slide}
Expand All @@ -99,7 +105,7 @@ export default function FavoriteChains (): React.ReactElement {
}}
>
<ChainList anchorEl={anchorEl} />
</Dialog>}
</Dialog>
</Badge>
);
}

0 comments on commit 5954fda

Please sign in to comment.