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 Header from "./components/Layout/Header";
import Meals from "./components/Meals/Meals"; import Meals from "./components/Meals/Meals";
import Cart from "./components/Cart/Cart"; import Cart from "./components/Cart/Cart";
const App = () => { const App = () => {
const [cartIsShown, setCartIsShown] = useState(false);
const showCartHandler = () => {
setCartIsShown(true);
};
const hideCartHandler = () => {
setCartIsShown(false);
};
return ( return (
<> <>
<Cart /> {cartIsShown && (
<Header /> <Cart onShowCart={showCartHandler} onHideCart={hideCartHandler} />
)}
<Header onShowCart={showCartHandler} />
<main> <main>
<Meals /> <Meals />
</main> </main>

View File

@ -11,14 +11,16 @@ const Cart = (props) => {
); );
return ( return (
<Modal> <Modal onShowCart={props.onShowCart} onHideCart={props.onHideCart}>
{cartItems} {cartItems}
<div className={classes.total}> <div className={classes.total}>
<span>Total Amount</span> <span>Total Amount</span>
<span>35.63</span> <span>35.63</span>
</div> </div>
<div className={classes.actions}> <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> <button className={classes.button}>Order</button>
</div> </div>
</Modal> </Modal>

View File

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

View File

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