Skip to content

Commit

Permalink
added theme context
Browse files Browse the repository at this point in the history
  • Loading branch information
g-marcin committed Jun 24, 2023
1 parent 78c1b26 commit 3fba236
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 56 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"tabWidth": 2,
"endOfLine": "auto",
"semi": true,
"printWidth": 120
"printWidth": 130
}
6 changes: 5 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { RouterProvider } from 'react-router-dom';
import { ThemeContextProvider } from './contexts';
import { AppRouter } from './router';

import 'bootstrap/dist/css/bootstrap.css';

function App() {
return (
<>
<RouterProvider router={AppRouter} />
<ThemeContextProvider>
<RouterProvider router={AppRouter} />
</ThemeContextProvider>
</>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/CustomPagination/CustomPagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const CustomPagination: FC<customPaginationProps> = ({ page, setPageHandl
);
return (
<div className={styles.paginationWrapper}>
<Pagination size="sm" className={styles["pagination-custom"]} >
<Pagination size="sm" className={styles['pagination-custom']}>
{items}
</Pagination>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './layout';
export * from "./CustomPagination";
export * from './Loader';
export * from './layout';
60 changes: 34 additions & 26 deletions src/components/layout/CustomOffcanvas/CustomOffcanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import { FC } from "react";
import { Offcanvas } from "react-bootstrap";
import { Moon, Sun } from "react-feather";
import ReactFocusLock from "react-focus-lock";
import styles from "./customOffcanvas.module.css";
import { FC, useContext } from 'react';
import { Offcanvas } from 'react-bootstrap';
import { Moon, Sun } from 'react-feather';
import ReactFocusLock from 'react-focus-lock';
import { ThemeContext } from '../../../contexts';
import styles from './customOffcanvas.module.css';

type OffcanvasProps = {
name:string
show:boolean
handleShow:()=>void
handleClose:()=>void

}
type CustomOffcanvasProps = {
name: string;
show: boolean;
handleShow: () => void;
handleClose: () => void;
};

export const CustomOffcanvas: FC<CustomOffcanvasProps> = ({ show, handleShow, handleClose, ...props }) => {
const { isDark, setIsDark } = useContext(ThemeContext);

export const CustomOffcanvas:FC<OffcanvasProps> = ({ show, handleShow, handleClose, ...props }) => {


return (
<>
<Offcanvas show={show} onHide={handleClose} {...props} placement={"end"}>
<ReactFocusLock>
return (
<>
<ReactFocusLock>
<Offcanvas show={show} onHide={handleClose} className={styles.offcanvas} {...props} placement={'end'}>
<Offcanvas.Header closeButton>
<Offcanvas.Title>Menu:</Offcanvas.Title>
</Offcanvas.Header>
<Offcanvas.Body>

<button className={styles.button} ><Moon size={36}/></button>
<button className={styles.button}><Sun size={36}/></button>
<div className={styles["menu-item"]}>
Toggle page theme:
{isDark ? (
<button className={styles.button}>
<Sun size={36} onClick={() => setIsDark(isDark)} />
</button>
) : (
<button className={styles.button} onClick={() => setIsDark(isDark)}>
<Moon size={36} />
</button>
)}
</div>
</Offcanvas.Body>
</Offcanvas>
</ReactFocusLock>
</Offcanvas>
</>
);
}
</>
);
};
11 changes: 11 additions & 0 deletions src/components/layout/CustomOffcanvas/customOffcanvas.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@
background: none;
text-decoration: none;
}

.menu-item {
display: flex;
align-items: center;
gap: 25px;
font-size: 18px;
}

.offcanvas {
width: 200px;
}
2 changes: 1 addition & 1 deletion src/components/layout/CustomOffcanvas/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./CustomOffcanvas";
export * from './CustomOffcanvas';
4 changes: 2 additions & 2 deletions src/components/layout/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import styles from './header.module.css';

export const Header: FC = () => {
const [show, setShow] = useState(false);

const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
return (
Expand All @@ -20,8 +20,8 @@ export const Header: FC = () => {
<button className={styles.menuButton} onClick={handleShow}>
<Menu className={styles.menu} color="#ffffff" />
</button>
<CustomOffcanvas name={"hamburger-menu"} show={show} handleClose={handleClose} handleShow={handleShow} />
</header>
<CustomOffcanvas name={'hamburger-menu'} show={show} handleClose={handleClose} handleShow={handleShow} />
</>
);
};
43 changes: 43 additions & 0 deletions src/contexts/ThemeContext/ThemeContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FC, PropsWithChildren, createContext, useEffect, useState } from 'react';

type ThemeContextType = {
isDark: boolean;
setIsDark: (isDark: boolean) => void;
};

export const ThemeContext = createContext<ThemeContextType>({
isDark: true,
setIsDark: (isDark: boolean) => {
return isDark;
},
});

export const ThemeContextProvider: FC<PropsWithChildren> = ({ children }) => {
const [isDark, setIsDark] = useState(true);

useEffect(() => {
const theme = window.localStorage.getItem('theme');
if (theme) {
setIsDark(!(theme === 'light'));
}
}, []);

useEffect(() => {
document.body.setAttribute('data-bs-theme', `${isDark ? 'dark' : 'light'}`);
}, [isDark]);

const isDarkHandler = (isDark: boolean) => {
setIsDark(!isDark);
};

return (
<ThemeContext.Provider
value={{
isDark: isDark,
setIsDark: isDarkHandler,
}}
>
{children}
</ThemeContext.Provider>
);
};
1 change: 1 addition & 0 deletions src/contexts/ThemeContext/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ThemeContext';
1 change: 1 addition & 0 deletions src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ThemeContext';
2 changes: 1 addition & 1 deletion src/hooks/useBeerDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { httpClient } from '../common';
import { BeerType, beerDataDTO } from '../types';
import { beerDetailsMapper } from './beerDetailsMapper';

export const useBeerDetails = (id = '1') => {
export const useBeerDetails = (id="1") => {
const [beerDetails, setBeerDetails] = useState<BeerType>();
const [isLoading, setIsLoading] = useState(false);

Expand Down
11 changes: 4 additions & 7 deletions src/modules/BeerBrowser/BeerBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { AxiosResponse } from 'axios';
import { FC, useEffect, useState } from 'react';
import { Container } from 'react-bootstrap';
import { httpClient } from '../../common';
import { Loader } from '../../components';
import { CustomPagination } from '../../components/CustomPagination';
import { CustomPagination, Loader } from '../../components';
import { BeerType, beerDataDTO } from '../../types';
import { BeerCard } from './BeerCard';
import styles from './beerBrowser.module.css';
import { beerDataMapper } from './beerDataMapper';

const BeerBrowser: FC = () => {
const BeerBrowser: FC = () => {
const [isLoading, setIsLoading] = useState(false);
const [page, setPage] = useState(1);
const setPageHandler = (page: number) => {
Expand Down Expand Up @@ -38,9 +37,7 @@ import { beerDataMapper } from './beerDataMapper';
<div>
<Container className={styles.beerCardsContainer}>
{beerData?.map((beer) => {
return (
<BeerCard name={beer.name} tagline={beer.tagline} image={beer.imageUrl} id={beer.id} key={beer.id} />
);
return <BeerCard name={beer.name} tagline={beer.tagline} image={beer.imageUrl} id={beer.id} key={beer.id} />;
})}
</Container>
<CustomPagination page={page} setPageHandler={setPageHandler} />
Expand All @@ -50,4 +47,4 @@ import { beerDataMapper } from './beerDataMapper';
);
};

export default BeerBrowser
export default BeerBrowser;
7 changes: 1 addition & 6 deletions src/modules/BeerBrowser/BeerCard/BeerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ type BeerCardProps = {
id: number;
};

export const BeerCard: FC<BeerCardProps> = ({
image = '',
name = '',
tagline = '',
id,
}) => {
export const BeerCard: FC<BeerCardProps> = ({ image, name, tagline, id }) => {
const navigate = useNavigate();
const beerCardHandler = (id: number) => {
navigate(`/details/${id}`);
Expand Down
1 change: 0 additions & 1 deletion src/modules/BeerBrowser/index.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export * from './BeerBrowser';
13 changes: 8 additions & 5 deletions src/modules/BeerDetails/BeerDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IngredientsInfo } from './IngredientsInfo';
import styles from './beerDetails.module.css';

const BeerDetails: FC = () => {
const { id } = useParams();
const { id} = useParams();
const { beerDetails, isLoading } = useBeerDetails(id);

if (!beerDetails) {
Expand All @@ -17,17 +17,20 @@ const BeerDetails: FC = () => {

return (
<>
{isLoading ? (
<Loader />
{isLoading ? (
<Loader />
) : (
<div className={`${styles.wrapper} `}>
<div className={styles["flex-column"]}>
<BeerInfo beerDetails={beerDetails} />

</div>
<Card.Img variant="top" src={beerDetails?.imageUrl} className={styles.detailsImage} />
<IngredientsInfo beerDetails={beerDetails} />
</div>
)}
</>
</>
);
};

export default BeerDetails
export default BeerDetails;
6 changes: 6 additions & 0 deletions src/modules/BeerDetails/beerDetails.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ h3 {
font-size: 16px;
font-weight: bold;
}

.flex-column {
display: flex;
flex-direction: column;
justify-content: space-between;
}
5 changes: 2 additions & 3 deletions src/router/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Suspense, lazy } from 'react';
import { createBrowserRouter } from 'react-router-dom';
import { Layout, Loader } from '../components';
import { ErrorPage } from './ErrorPage';
const BeerBrowser = lazy(()=> import ("../modules/BeerBrowser/BeerBrowser"))
const BeerDetails = lazy(()=> import ("../modules/BeerDetails/BeerDetails"))

const BeerBrowser = lazy(() => import('../modules/BeerBrowser/BeerBrowser'));
const BeerDetails = lazy(() => import('../modules/BeerDetails/BeerDetails'));

export const AppRouter = createBrowserRouter([
{
Expand Down

0 comments on commit 3fba236

Please sign in to comment.