Skip to content

Commit

Permalink
Fix: Trigger relayout when switching network connection between unsup…
Browse files Browse the repository at this point in the history
…ported and supported
  • Loading branch information
lochie committed Aug 1, 2022
1 parent 0046a31 commit 98fef08
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/components/Common/Modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { AnimatePresence, motion, Variants } from 'framer-motion';

Expand Down Expand Up @@ -34,6 +34,7 @@ import { supportedConnectors } from '../../..';
import usePrevious from '../../../hooks/usePrevious';
import { CustomTheme } from '../../../types';
import { useThemeContext } from '../../ConnectKitThemeProvider/ConnectKitThemeProvider';
import { useNetwork } from 'wagmi';

const InfoIcon = ({ ...props }) => (
<svg
Expand Down Expand Up @@ -189,29 +190,42 @@ const Modal: React.FC<ModalProps> = ({
});
const [inTransition, setInTransition] = useState<boolean>(false);

// Calculate new content bounds
const updateBounds = (node: any) => {
const bounds = {
width: node?.offsetWidth,
height: node?.offsetHeight,
};
setDimensions({
width: `${bounds?.width}px`,
height: `${bounds?.height}px`,
});
};

let blockTimeout: ReturnType<typeof setTimeout>;
const contentRef = useCallback(
(node: any) => {
if (!node) return;
ref.current = node;

// Avoid transition mixups
setInTransition(true);
clearTimeout(blockTimeout);
blockTimeout = setTimeout(() => setInTransition(false), 360);

// Calculate new content bounds
const bounds = {
width: node?.offsetWidth,
height: node?.offsetHeight,
};
setDimensions({
width: `${bounds?.width}px`,
height: `${bounds?.height}px`,
});
updateBounds(node);
},
[open]
);

// Update layout on chain/network switch to avoid clipping
const { chain } = useNetwork();
const ref = useRef<any>(null);
useEffect(() => {
if (ref.current) updateBounds(ref.current);
}, [chain]);

useEffect(() => {
if (!mounted) {
setDimensions({
Expand Down

0 comments on commit 98fef08

Please sign in to comment.