Skip to content

Commit ffb373b

Browse files
committed
fixed dynamic window resizing using useEffect
1 parent 905fa50 commit ffb373b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/insight/src/components/pill.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { FC } from 'react';
1+
import {FC, useEffect, useState} from 'react';
22
import styled from 'styled-components';
33
import CloseLightSvg from 'src/assets/images/close-light.svg'
44
import {Black, White, Slate30} from '../assets/styles/colors';
5-
import { size } from 'src/utilities/constants';
5+
import {size} from 'src/utilities/constants';
66

77
const PillBubble = styled.div`
88
display: flex;
@@ -73,11 +73,18 @@ interface PillProps {
7373
}
7474

7575
export const Pill: FC<PillProps> = ({ currency, network, onCloseClick }) => {
76-
const isMobile = () => window.innerWidth < Number(size.mobileL.slice(0, -2));
76+
const [isMobile, setIsMobile] = useState(window.innerWidth < Number(size.mobileL.slice(0, -2)));
77+
78+
useEffect(() => {
79+
window.addEventListener('resize', () => {
80+
setIsMobile(window.innerWidth < Number(size.mobileL.slice(0, -2)));
81+
});
82+
}, []);
83+
7784
return (
7885
currency ?
7986
<PillBubble>
80-
<img src={`https://bitpay.com/img/icon/currencies/${currency}.svg`} alt={currency} style={{height: isMobile() ? '60%' : '75%'}} />
87+
<img src={`https://bitpay.com/img/icon/currencies/${currency}.svg`} alt={currency} style={{height: isMobile ? '60%' : '75%'}} />
8188
<NetworkLabel>{network}</NetworkLabel>
8289
<PillCloseButtonScope onClick={onCloseClick}>
8390
<PillCloseButtonCircle>

packages/insight/src/components/search.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FC, memo, useMemo} from 'react';
1+
import {FC, memo, useEffect, useMemo, useState} from 'react';
22
import {determineInputType, searchValue} from 'src/utilities/search-helper-methods';
33
import {useNavigate} from 'react-router-dom';
44
import styled, {useTheme} from 'styled-components';
@@ -8,7 +8,7 @@ import {LightBlack, Slate} from '../assets/styles/colors';
88
import {useAppDispatch, useAppSelector} from '../utilities/hooks';
99
import {changeCurrency, changeNetwork} from 'src/store/app.actions';
1010
import {Pill} from './pill';
11-
import { size } from 'src/utilities/constants';
11+
import {size} from 'src/utilities/constants';
1212

1313
const SearchInput = styled.input`
1414
background: none;
@@ -52,15 +52,21 @@ const Search: FC<SearchProps> = ({borderBottom, id, setErrorMessage}) => {
5252
const theme = useTheme();
5353
const dispatch = useAppDispatch();
5454
const {currency, network} = useAppSelector(({APP}) => APP);
55+
const [isMobile, setIsMobile] = useState(window.innerWidth < Number(size.mobileL.slice(0, -2)));
5556

5657
const searchIcon = theme.dark ? SearchDarkSvg : SearchLightSvg;
5758
const searchId = id || 'search';
5859

60+
useEffect(() => {
61+
window.addEventListener('resize', () => {
62+
setIsMobile(window.innerWidth < Number(size.mobileL.slice(0, -2)));
63+
});
64+
}, []);
65+
5966
const search = async (event: any) => {
6067
event.preventDefault();
6168
setErrorMessage('');
6269
const searchVal = event.target[searchId].value.replace(/\s/g, '');
63-
6470
const searchInputs = await determineInputType(searchVal);
6571
if (searchInputs.length) {
6672
try {
@@ -161,7 +167,6 @@ const Search: FC<SearchProps> = ({borderBottom, id, setErrorMessage}) => {
161167
}
162168

163169
const searchInputPlaceholder = useMemo(() => {
164-
const isMobile = window.innerWidth < Number(size.mobileL.slice(0, -2));
165170
let placeholder = 'Search for block, transaction, or address';
166171
if (currency && network) {
167172
if (isMobile) {
@@ -170,7 +175,7 @@ const Search: FC<SearchProps> = ({borderBottom, id, setErrorMessage}) => {
170175
placeholder = `${placeholder} on ${currency} ${network}`;
171176
}
172177
return placeholder;
173-
}, [currency, network, window.innerWidth]);
178+
}, [currency, network, isMobile]);
174179

175180
return (
176181
<SearchForm onSubmit={search} borderBottom={borderBottom}>

0 commit comments

Comments
 (0)