add handler for showing/hiding cart

This commit is contained in:
oscar 2023-03-18 12:54:41 +13:00
parent 3896c1be0d
commit 4130321108
5 changed files with 27 additions and 10 deletions

View File

@ -1,14 +1,26 @@
import React from "react";
import React, { useState } from "react";
import Header from "./components/Layout/Header";
import Meals from "./components/Meals/Meals";
import Cart from "./components/Cart/Cart";
const App = () => {
const [cartIsShown, setCartIsShown] = useState(false);
const showCartHandler = () => {
setCartIsShown(true);
};
const hideCartHandler = () => {
setCartIsShown(false);
};
return (
<>
<Cart />
<Header />
{cartIsShown && (
<Cart onShowCart={showCartHandler} onHideCart={hideCartHandler} />
)}
<Header onShowCart={showCartHandler} />
<main>
<Meals />
</main>

View File

@ -11,14 +11,16 @@ const Cart = (props) => {
);
return (
<Modal>
<Modal onShowCart={props.onShowCart} onHideCart={props.onHideCart}>
{cartItems}
<div className={classes.total}>
<span>Total Amount</span>
<span>35.63</span>
</div>
<div className={classes.actions}>
<button className={classes["button--alt"]}>Close</button>
<button className={classes["button--alt"]} onClick={props.onHideCart}>
Close
</button>
<button className={classes.button}>Order</button>
</div>
</Modal>

View File

@ -4,12 +4,12 @@ import mealsImage from "../../assets/meals.jpg";
import classes from "./Header.module.css";
import HeaderCartButton from "./HeaderCartButton";
const Header = () => {
const Header = (props) => {
return (
<>
<header className={classes.header}>
<h1>ReactMeals</h1>
<HeaderCartButton />
<HeaderCartButton onShowCart={props.onShowCart} />
</header>
<div className={classes["main-image"]}>
<img src={mealsImage} alt="A table full of delicious food!" />

View File

@ -3,7 +3,7 @@ import classes from "./HeaderCartButton.module.css";
const HeaderCartButton = (props) => {
return (
<button className={classes.button}>
<button className={classes.button} onClick={props.onShowCart}>
<span className={classes.icon}>
<CartIcon />
</span>

View File

@ -2,7 +2,7 @@ import classes from "./Modal.module.css";
import ReactDOM from "react-dom";
const Backdrop = (props) => {
return <div className={classes.backdrop}></div>;
return <div className={classes.backdrop} onClick={props.onHideCart}></div>;
};
const ModalOverlay = (props) => {
@ -18,7 +18,10 @@ const portalElement = document.getElementById("root-overlays");
const Modal = (props) => {
return (
<>
{ReactDOM.createPortal(<Backdrop />, portalElement)}
{ReactDOM.createPortal(
<Backdrop onHideCart={props.onHideCart} />,
portalElement
)}
{ReactDOM.createPortal(
<ModalOverlay>{props.children}</ModalOverlay>,
portalElement