Skip to content

Commit

Permalink
add mobile sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
Victoria-Fedkova committed Sep 21, 2023
1 parent 7cdfeb2 commit 0c148f0
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 13 deletions.
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react-input-slider": "^6.0.1",
"react-number-format": "^5.3.1",
"react-redux": "^8.1.2",
"react-responsive": "^9.0.2",
"react-router-dom": "^6.16.0",
"react-select": "^5.7.4",
"react-slick": "^0.29.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/CarCard/CarCard.styled.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ImgWrapper = styled.div`
right: 14px;
border: none;
background: transparent;
z-index: 1000;
z-index: 2;
padding: 0;
margin: 0;
transition: all 250ms linear;
Expand Down
1 change: 1 addition & 0 deletions src/components/PageFooter/PageFooter.styled.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const FooterTopWrapper = styled.div`
padding: 34px 0;
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
@media (min-width: 768px) {
Expand Down
23 changes: 20 additions & 3 deletions src/components/PageHeader/PageHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faHeart, faCar, faHome } from '@fortawesome/free-solid-svg-icons';
import {
faHeart,
faCar,
faHome,
faBookOpen,
faClose,
} from '@fortawesome/free-solid-svg-icons';
import { Logo } from '../Logo/Logo';
import { Header, HeaderWraper, NavLinks } from './PageHeader.styled';
import { Header, HeaderWraper, NavLinks, SideBtn } from './PageHeader.styled';
import { useDispatch } from 'react-redux';
import { setFilter } from '../../redux/filter/filterSlice';
import { useMediaQuery } from 'react-responsive';

export const PageHeader = () => {
export const PageHeader = ({ currentState, handleToggleSideBar }) => {
const dispatch = useDispatch();
const isMobile = useMediaQuery({ query: '(max-width: 1279px)' });
return (
<Header>
<HeaderWraper>
Expand Down Expand Up @@ -43,6 +51,15 @@ export const PageHeader = () => {
}}
/>
</NavLinks>
{isMobile && (
<SideBtn onClick={handleToggleSideBar}>
{currentState ? (
<FontAwesomeIcon icon={faClose} />
) : (
<FontAwesomeIcon icon={faBookOpen} />
)}
</SideBtn>
)}
</nav>
</HeaderWraper>
</Header>
Expand Down
20 changes: 19 additions & 1 deletion src/components/PageHeader/PageHeader.styled.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ export const NavLinks = styled(NavLink)`
&:focus {
background-color: #3470ff;
color: white;
border-color: #3470ff;
}
}
`;

export const SideBtn = styled.button`
transition: all 250ms linear;
display: inline-flex;
width: 44px;
height: 44px;
align-items: center;
justify-content: center;
border-radius: 50%;
border: 1px solid rgba(195, 212, 233, 0.4);
color: rgba(18, 20, 23, 0.5);
fill: rgba(18, 20, 23, 0.5);
background-color: white;
&:hover,
&:focus {
background-color: #3470ff;
color: white;
}
`;
18 changes: 15 additions & 3 deletions src/components/SharedLayout/SharedLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Outlet, useLocation } from 'react-router-dom';
import { Suspense, useEffect } from 'react';
import { PageHeader } from '../PageHeader/PageHeader';
import { PageFooter } from '../PageFooter/PageFooter';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { fetchCars } from '../../redux/cars/carsOperations';
import { SideBar } from '../SideBar/SideBar';
import { ScrollToTop } from '../ScrollToTop/ScrollToTop';
import { Main } from './SharedLayout.styled';
import { toggleSideBar } from '../../redux/sidebar/sidebarSlice';
import { selectOpenSideBar } from '../../redux/sidebar/sidebarSelectors';

export const SharedLayout = () => {
const { pathname } = useLocation();
Expand All @@ -15,11 +17,21 @@ export const SharedLayout = () => {
useEffect(() => {
dispatch(fetchCars());
}, [dispatch]);

const currentState = useSelector(selectOpenSideBar);

const handleToggleSideBar = () => {
dispatch(toggleSideBar());
};

return (
<>
<PageHeader />
<PageHeader
currentState={currentState}
handleToggleSideBar={handleToggleSideBar}
/>
<Main $isFavouritePage={isFavouritePage}>
{isFavouritePage && <SideBar />}
{isFavouritePage && <SideBar currentState={currentState} />}
<Suspense fallback={<div>Loading...</div>}>
<Outlet />
</Suspense>
Expand Down
15 changes: 13 additions & 2 deletions src/components/SideBar/SideBar.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { useEffect } from 'react';
import { SideBarWrapper } from './SideBar.styled';
import { SideBarSlider } from './SideBarForm/SIdeBarSlider';
import { SideBarForm } from './SideBarForm/SideBarForm';
export const SideBar = () => {

export const SideBar = ({ currentState }) => {
useEffect(() => {
// Applying on mount
if (currentState) document.body.style.overflow = 'hidden';
// Applying on unmount
return () => {
document.body.style.overflow = 'visible';
};
}, [currentState]);

return (
<SideBarWrapper>
<SideBarWrapper $isOpen={currentState}>
<SideBarForm />
<SideBarSlider />
</SideBarWrapper>
Expand Down
13 changes: 13 additions & 0 deletions src/components/SideBar/SideBar.styled.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ export const SideBarWrapper = styled.div`
background-color: white;
min-height: 100%;
padding: 32px;
@media screen and (max-width: 1280px) {
position: fixed;
z-index: 999;
height: 100%;
transform: ${props =>
props.$isOpen ? 'transform: translateX(0%)' : 'translateX(-100%)'};
transition: transform 250ms cubic-bezier(0.4, 0, 0.2, 1);
}
body & {
overflow: ${props => (props.$isOpen ? 'hidden' : 'auto')};
overflow: hidden;
}
`;

export const CheckboxForm = styled.form`
Expand Down
9 changes: 6 additions & 3 deletions src/pages/NotFound/NotFound.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import cars from '../../assets/pngegg.png';

export default function NotFound() {
return (
<>
<h1>NotFound</h1>
</>
<div>
<h1>404 Page not found</h1>
<img src={cars} alt="Page not found" />
</div>
);
}
1 change: 1 addition & 0 deletions src/redux/sidebar/sidebarSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectOpenSideBar = state => state.sideBar.currentState;
18 changes: 18 additions & 0 deletions src/redux/sidebar/sidebarSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
currentState: true,
};

const sideBarSlice = createSlice({
name: 'sideBar',
initialState,
reducers: {
toggleSideBar: state => {
state.currentState = !state.currentState;
},
},
});

export const { toggleSideBar } = sideBarSlice.actions;
export const sideBarReducer = sideBarSlice.reducer;
2 changes: 2 additions & 0 deletions src/redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import storage from 'redux-persist/lib/storage';
import { carsReducer } from './cars/carsSlice';
import { filterReducer } from './filter/filterSlice';
import { likesReducer } from './likes/likesSlice';
import { sideBarReducer } from './sidebar/sidebarSlice';

const middleware = [
...getDefaultMiddleware({
Expand All @@ -35,6 +36,7 @@ export const store = configureStore({
cars: carsReducer,
filter: filterReducer,
likes: persistReducer(likesPersistConfig, likesReducer),
sideBar: sideBarReducer,
},
middleware,
});
Expand Down
1 change: 1 addition & 0 deletions src/styles/globalStyles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ManropeSemiBold from '../fonts/Manrope-SemiBold.ttf';

const GlobalStyle = createGlobalStyle`
@font-face {
font-family: 'ManropeRegular';
src: local('ManropeRegular'),
Expand Down

0 comments on commit 0c148f0

Please sign in to comment.