Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaces resize lib with mediaQuery matcher #115

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"react-icons": "^4.4.0",
"react-loader-spinner": "^5.1.5",
"react-redux": "^8.0.2",
"react-resize-aware": "^3.1.1",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.5",
Expand Down
6 changes: 4 additions & 2 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useDispatch } from 'react-redux';
import 'react-toastify/dist/ReactToastify.css';
import { ToastContainer } from 'react-toastify';
import { authOperations } from 'redux/auth';
import { useSetDocumentTitle } from 'hooks/ui';
import { MediaContextProvider, useSetDocumentTitle } from 'hooks/ui';
import { AppRouter } from './AppRouter';
import '../../translation/i18n';
const App = () => {
Expand All @@ -22,7 +22,9 @@ const App = () => {
return (
<ThemeProvider theme={theme}>
<MuiThemeProvider theme={muiTheme}>
<AppRouter />
<MediaContextProvider>
<AppRouter />
</MediaContextProvider>
</MuiThemeProvider>
<GlobalStyle />
<ToastContainer />
Expand Down
7 changes: 1 addition & 6 deletions src/components/BackgroundLayout/BackgroundLayout.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import ImagesContainer from './ImagesContainer';
import { Background } from './BackgroundLayout.styled';

const BackgroundLayout = ({ children }) => {
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
const { pathname } = useLocation();
const isMainPage = pathname === '/';

return (
<Background>
{children}
{!isLoggedIn && (
<ImagesContainer isMainPage={isMainPage} isAuth={isLoggedIn} />
)}
{!isLoggedIn && <ImagesContainer isAuth={isLoggedIn} />}
</Background>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import leavesTabletAuth from 'images/background/leaves-tablet-auth.png';
import strawberry from 'images/background/strawberry.png';
import strawberryTablet from 'images/background/strawberry-tablet.png';
import greyBackground from 'images/background/grey-background.svg';
import useResizeAware from 'react-resize-aware';

import {
ImagesWrapper,
Expand All @@ -14,15 +13,20 @@ import {
BananaImg,
StrawberryImg,
} from './ImagesContainer.styled';
import { useLocation } from 'react-router-dom';
import { useMedia } from 'hooks/ui';

const DESKTOP_WIDTH_BREAKPOINT = 1280;
const DESKTOP_HEIGHT_BREAKPOINT = 850;

const ImagesContainer = ({ isMainPage = false }) => {
const [resizeListener, { width, height }] = useResizeAware();
const ImagesContainer = () => {
const { width, height } = useMedia();
const isTablet = width < DESKTOP_WIDTH_BREAKPOINT;
const shouldResizeBg = height > DESKTOP_HEIGHT_BREAKPOINT;

const { pathname } = useLocation();
const isMainPage = pathname === '/';

const strawberryImageSource = !isTablet
? strawberry
: isMainPage
Expand All @@ -37,7 +41,6 @@ const ImagesContainer = ({ isMainPage = false }) => {

return (
<ImagesWrapper>
{resizeListener}
<GreyBackgroundImg
shouldResizeBg={shouldResizeBg}
src={greyBackground}
Expand Down
11 changes: 3 additions & 8 deletions src/components/DiaryPageContent/DiaryPageContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import { useShowForm } from './hooks';
import { useTranslation } from 'react-i18next';
import EmptyListTitle from 'components/EmptyListTitle';
import { DiaryCalendarAndForm } from './DiaryCalendarAndForm';
import { useMobileModal } from 'hooks/ui';
import { useMedia, useMobileModal } from 'hooks/ui';
import {
AddProductButton,
AddProductButtonWrapper,
AddProductIcon,
} from 'components/Forms/DiaryAddProductForm/AddProduct.mui';
import useResizeAware from 'react-resize-aware';
import { AlertModal } from 'components/AlertModal';

const TABLET = 768;

export const DiaryPageContent = () => {
const { t } = useTranslation();
const [showModal, setShowModal] = useState(false);
Expand Down Expand Up @@ -50,14 +47,12 @@ export const DiaryPageContent = () => {
setShowModal(!showModal);
};

const [resizeListener, { width }] = useResizeAware();
const isMobile = width < TABLET;
const { isMobile } = useMedia();

const [, openMobileModal] = useMobileModal();

return (
<BlockWrapper>
{resizeListener}

<DiaryPageStyled>
<DiaryCalendarAndForm
addProduct={addProduct}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { getIsLoggedIn } from 'redux/auth/authSelector';
import { useTranslation } from 'react-i18next';
import { setUserData } from 'redux/auth/authSlice';
import { transformUserData } from './transformUserData';
import { useMobileModal } from 'hooks/ui';
import useResizeAware from 'react-resize-aware';
import { useMedia, useMobileModal } from 'hooks/ui';
import { Button } from 'components/Button';
import { Input } from 'components/Input';
import * as yup from 'yup';
Expand All @@ -25,10 +24,10 @@ const typeBlood = [1, 2, 3, 4];
const CalculatorСalorieForm = ({ openModal, getPrivatDailyNorma }) => {
const { t } = useTranslation();
const [selectedTypeBlood, setSelectedTypeBlood] = useState(1);
const [resizeListener, { width }] = useResizeAware();
const { isMobile } = useMedia();
const dispatch = useDispatch();
const isLoggedIn = useSelector(getIsLoggedIn);
const mobileWidth = width <= 767;

const [, openMobileModal] = useMobileModal();

const validationSchema = yup.object({
Expand Down Expand Up @@ -72,10 +71,10 @@ const CalculatorСalorieForm = ({ openModal, getPrivatDailyNorma }) => {
await getPrivatDailyNorma(paramsUser);
}

if (formik.dirty && !isLoggedIn && !mobileWidth) {
if (formik.dirty && !isLoggedIn && !isMobile) {
await openModal();
}
if (formik.dirty && !isLoggedIn && mobileWidth) {
if (formik.dirty && !isLoggedIn && isMobile) {
await openMobileModal();
}
resetForm();
Expand All @@ -84,7 +83,6 @@ const CalculatorСalorieForm = ({ openModal, getPrivatDailyNorma }) => {

return (
<Form onSubmit={formik.handleSubmit}>
{resizeListener}
<InputContainer>
<Wrapper>
<Input
Expand Down
23 changes: 7 additions & 16 deletions src/components/Header/Navigation/Navigation.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import useResizeAware from 'react-resize-aware';
import Logo from '../Logo';
import MenuIcon from '@material-ui/icons/Menu';
import CloseIcon from '@material-ui/icons/Close';
Expand All @@ -26,7 +25,7 @@ import { Languages } from '../Languages';
import IconButton from 'components/IconButton';
import { createPortal } from 'react-dom';
import { getRefs } from 'utils';
import { useMobileModal } from 'hooks/ui';
import { useMedia, useMobileModal } from 'hooks/ui';
import { ReturnButtonWrapper } from '../UserInfo/UserInfo.styled';
import { IconReturnLeft } from 'components/MobileModal/MobileModal.styled';

Expand Down Expand Up @@ -91,15 +90,9 @@ const NavigationOnMobile = ({ visibleMenu, handleMenuBtnClick }) => {
);
};

const TABLET = 768;
const DESKTOP = 1280;

const Header = () => {
const { t } = useTranslation();
const [resizeListener, { width }] = useResizeAware();
const mobileWidth = width < TABLET;
const tabletWidth = width >= TABLET && width < DESKTOP;
const desktopWidth = width >= DESKTOP;
const { isMobile, isTablet, isDesktop } = useMedia();

const isLogged = useSelector(getIsLoggedIn);
const [showMobileModal, , hideMobileModal] = useMobileModal();
Expand All @@ -109,8 +102,6 @@ const Header = () => {

return (
<div>
{resizeListener}

<HeaderStyled isLogged={isLogged}>
<Container>
<HeaderNavigation>
Expand All @@ -121,7 +112,7 @@ const Header = () => {
<HeaderLinksWrapper isLogged={isLogged}>
{!isLogged && <NavigationForGuest />}

{isLogged && mobileWidth && (
{isLogged && isMobile && (
<div>
<NavigationOnMobile
visibleMenu={visibleMenu}
Expand All @@ -130,7 +121,7 @@ const Header = () => {
</div>
)}

{isLogged && tabletWidth && (
{isLogged && isTablet && (
<>
<UserInfo />
<NavigationOnMobile
Expand All @@ -140,7 +131,7 @@ const Header = () => {
</>
)}

{isLogged && desktopWidth && (
{isLogged && isDesktop && (
<>
<div>
<Wrapp>
Expand All @@ -166,13 +157,13 @@ const Header = () => {
</Container>
</HeaderStyled>

{isLogged && mobileWidth && (
{isLogged && isMobile && (
<HeaderButtonsWrapper>
<UserInfo />
</HeaderButtonsWrapper>
)}

{!isLogged && mobileWidth && showMobileModal && (
{!isLogged && isMobile && showMobileModal && (
<HeaderButtonsWrapper>
<Container>
<ReturnButtonWrapper>
Expand Down
11 changes: 3 additions & 8 deletions src/components/Header/UserInfo/UserInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import useResizeAware from 'react-resize-aware';
import { useSelector } from 'react-redux';
import { getName } from 'redux/auth/authSelector';
import {
Expand All @@ -9,22 +8,18 @@ import {
ReturnButtonWrapper,
} from './UserInfo.styled';
import { Alert } from './Alert';
import { useMobileModal } from 'hooks/ui';
import { useMedia, useMobileModal } from 'hooks/ui';
import IconButton from 'components/IconButton';

const TABLET = 768;

export default function UserInfo() {
const [resizeListener, { width }] = useResizeAware();
const isMobile = width < TABLET;
const { isMobile } = useMedia();

const name = useSelector(getName);
const [showMobileModal, , hideMobileModal] = useMobileModal();

return (
<MobileContainer>
<HeaderNavButtonsContainer>
{resizeListener}

{showMobileModal && isMobile && (
<ReturnButtonWrapper>
<IconButton icon={<IconReturnLeft />} onClick={hideMobileModal} />
Expand Down
10 changes: 3 additions & 7 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';

import { ModalContent } from './ModalContent/ModalContent';
import useResizeAware from 'react-resize-aware';
import { MobileModal } from 'components/MobileModal';
import { useMobileModal } from 'hooks/ui';
import { useMedia, useMobileModal } from 'hooks/ui';

const MuiDialog = styled(Dialog)(({ theme }) => ({
'& .MuiDialogContent-root': {
Expand All @@ -15,8 +14,7 @@ const MuiDialog = styled(Dialog)(({ theme }) => ({
}));

export const Modal = ({ showModal, setShowModal }) => {
const [resizeListener, { width }] = useResizeAware();
const mobileWidth = width <= 767;
const { isMobile } = useMedia();

const [showMobileModal, , hideMobileModal] = useMobileModal();

Expand All @@ -30,9 +28,7 @@ export const Modal = ({ showModal, setShowModal }) => {

return (
<div>
{resizeListener}

{!mobileWidth ? (
{!isMobile ? (
<div>
<MuiDialog
onClose={handleClose}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './useMobileModal';
export * from './useSetDocumentTitle';
export * from './useToggleNoScroll';
export * from './useListenEscKeyDown';
export * from './useMedia';
Loading