Git Product home page Git Product logo

Comments (5)

wldrocha avatar wldrocha commented on August 14, 2024 4

Cree el componente del menu lateral, adicional lo importe en el componente Header, cree otro estado llamado toggleMobileMenu, a su vez en la imagen del menu en mobile le a帽ad铆 un evento click y lo a帽ad铆 encima de los otros 2 elementos modales, adicionalmente coloque en los estilos una posici贸n absoluta, left de 0 para que flotar谩 del lado izquierdo y le a帽ad铆 un fondo blanco

from react-practico.

christian-olivers avatar christian-olivers commented on August 14, 2024 2

mi soluci贸n quedo de la siguiente manera en header.jsx:
...
const Header = () => {
...
const [menuToggle, setMenuToggle] = useState(false);

 const menuLeftToggle = () => {
    setMenuToggle(!menuToggle);
    console.log("entra")
}
return (
    <nav>
        <img src={menu} alt="menu" className="menu" onClick={menuLeftToggle}/>
         ...
         <ul className={'menu-left' + (menuToggle ? '--active':'')}>
               ...
               <div className={menuToggle ? 'settings-user' : ''}>
                          <div className='line'></div>
                          <a href="/">My orders</a>
                          <a href="/">My account</a>
                          <div className='user'>
                              <a className='user-email'>[email protected]</a>
                              <a className='singOut'>Sign out</a>
                          </div>
                      </div>
         </ul>

y modificamos los estilos y sevea wuai

@media (max-width: 640px) {
.menu {
display: block;
position:relative;
}
.navbar-left ul {
display: none;
}
.navbar-left .menu-left--active{
display: block;
height: 100vh;
width: 60%;
position: absolute;
margin-top: 60px;
background-color: var(--white);
padding-top: 20px;
float: left;
margin-left: -45%;
animation: swipeLeft .40s;
font-weight: bold;
overflow: scroll;
}
.navbar-left ul li {
padding: 20px 30px;
color: var(--black);
}
.navbar-left ul li a {
color: var(--black);
}
.line {
border: 1px solid var(--very-light-pink);
width: 80%;
margin-left: 14% ;
opacity: 25%;
}
.settings-user {
width: 100%;
height: 80px;
display: flex;
flex-direction: column;
}
.settings-user a {
padding: 20px 40px;
text-decoration: none;
color: var(--black);
}
.user {
display: flex;
flex-direction: column;
margin-top: 20px;
}
.user a {
padding: 8px 40px;
}
.user .user-email {
font-weight: 500;
}
.user .singOut {
color: var(--hospital-green);
}
.navbar-email {
display: none;
}
@Keyframes swipeLeft {
0% {
width: 0;
}
100% {
width: 60%;
}
}
}

from react-practico.

lvargascol avatar lvargascol commented on August 14, 2024

Agregue el componente MobileMenu.jsx y sus respectivos estilos MobileMenu.scss.

En vez de agregar un nuevo estado en Header.jsx para el Menu Mobile, utilice el mismo del Menu Desktop, pero alternando el display mediante Media Query.

Se agrega lo siguiente en Header.jsx:

<img src={menuIcon} alt="menu" className="menu-icon" onClick={handleToggle}/>

{toggle && <MobileMenu/>}

Se agrega lo siguiente en Menu.scss:

@media (max-width: 640px) { .menu { display: none; } }

from react-practico.

Misael-GC avatar Misael-GC commented on August 14, 2024

Comparto el componente y sus estilos
MenuMobile.jsx
`
import React from 'react';
import '../styles/MenuMobile.scss';

function MenuMobile() {
return (

  <ul>
    <li>
      <a href="/">CATEGORIES</a>
    </li>
    <li>
      <a href="/">All</a>
    </li>
    <li>
      <a href="/">Clothes</a>
    </li>
    <li>
      <a href="/">Electronics</a>
    </li>
    <li>
      <a href="/">Furnitures</a>
    </li>
    <li>
      <a href="/">Toys</a>
    </li>
    <li>
      <a href="/">Others</a>
    </li>
  </ul>

  <ul>
    <li>
      <a href="/orders">My orders</a>
    </li>
    <li>
      <a href="/account">My account</a>
    </li>
  </ul>

  <ul>
    <li>
      <a href="/" className="email">
        [email protected]
      </a>
    </li>
    <li>
      <a href="/" className="sign-out">
        Sign out
      </a>
    </li>
  </ul>
  
</div>

)
}

export default MenuMobile;
`

MenuMobile.scss
`
@import 'vars';

.mobile-menu {
background: var(--white);
position: absolute; /* add styles clase 19*/
top: 45px; /* add styles clase 19*/
left: 0;
width: 100%;
padding: 12px 24px 24px 24px;
/* transition: 400ms; */
display: none;
z-index: 3;
}

.mobile-menu a{
text-decoration: none;
color: var(--black);
font-weight: bold;
}

.mobile-menu ul:not(.email-1){
padding: 0;
margin: 24px 0 0;
list-style: none;
}

.mobile-menu ul:nth-child(1){
border-bottom: 1px solid var(--very-light-pink);
}

.mobile-menu ul li{
margin-bottom: 22px;
}

.mobile-menu .email{
font-size: var(--sm);
font-weight: 300;
}

.mobile-menu .sign-out{
font-size: var(--sm);
color: var(--hospital-green);
}

.email-1, #exp, #exp-1{
margin-bottom: 0;
}

.email-1{
padding: 0;
margin: 70px 0 0;
list-style: none;
}

@media (max-width: 640px) {
.mobile-menu {
display: inline-block;
}

}
`

from react-practico.

Gustavolando avatar Gustavolando commented on August 14, 2024

Reto superado:

Mi c贸digo:

MenuMobile.jsx:

import React from 'react'
import '@styles/MenuMobile.scss'
import close from '@icons/icon_close.png'

function MenuMobile( { onCerrar } ) {
  return (
    <div className="mobile-menu">
      <img src={close} alt="close menu" className="close" onClick={onCerrar} />
      <div>
        <ul>
          <li><a href="/">CATEGORIES</a></li>
          <li><a href="/">All</a></li>
          <li><a href="/">Cloth</a></li>
          <li><a href="/">Electronics</a></li>
          <li><a href="/">Furniture</a></li>
          <li><a href="/">Toys</a></li>
          <li><a href="/">Others</a></li>
        </ul>
        <ul>
          <li><a href="/">My orders</a></li>
          <li><a href="/">My Account</a></li>
        </ul>
      </div>
      <ul>
        <li><a href="/" className="email">[email protected]</a></li>
        <li><a href="/" className="sign-out">Sign out</a></li>
      </ul>
    </div>
  )
}

export default MenuMobile

MenuMobile.scss:

.mobile-menu-container {
  position: fixed;
  left: 0;
}
.mobile-menu {
  position: fixed;
  top: 60px;
  bottom: 0;
  left: 0;
  height: calc(100% - 60px);
  box-sizing: border-box;
  width: 100%;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: var(--white);
  opacity: 0.95;
}
.mobile-menu a {
  text-decoration: none;
  color: var(--black);
  font-weight: bold;
}
.mobile-menu div {
  margin: 0;
  padding: 0;
  width: 100%;
}
.mobile-menu ul {
  padding: 0;
  margin: 24px 0 0;
  list-style: none;
}
.mobile-menu ul:nth-child(1) {
  border-bottom: 1px solid var(--very-light-pink);
}
.mobile-menu ul:last-child {
  margin-bottom: 28px;
}
.mobile-menu div ul li {
  margin-bottom: 32px;
}
.mobile-menu .email {
  font-size: var(--sm);
  font-weight: 300;
  margin-bottom: 4px;
  display: inline-block;
}
.mobile-menu .sign-out {
  color: var(--hospital-green);
  font-size: var(--sm);
}
.mobile-menu .close {
  position: absolute;
  top: 8px;
  right: 0px;
  width: 16px;
  object-fit: cover;
  padding: 16px;
  border-radius: 50%;
  cursor: pointer;
}

Y los cambios en el Header.jsx:

import React, { useState, useContext, useRef } from 'react'
import '@styles/Header.scss'
import Menu from '@components/Menu'
import MyOrder from '@containers/MyOrder'
import MenuMobile from '@containers/MenuMobile'
import menu from '@icons/icon_menu.svg'
import logo from '@logos/logo_yard_sale.svg'
import AppContext from '@context/AppContext'
import shoppingCart from '@icons/icon_shopping_cart.svg'

const Header = () => {
  const [toggle, setToggle] = useState(false)
  const [toggleOrders, setToggleOrders] = useState(false)
  const [toggleMenuMobile, setToggleMenuMobile] = useState(false)
  const { state } = useContext(AppContext)

  const handleToggle = () => {
    setToggle(!toggle)
    // setToggleOrders(false)
  }

  const handleToggleOrders = () => {
    setToggleOrders(!toggleOrders)
    // setToggle(false)
  }

  const handleToggleMenuMobile = () => {
    setToggleMenuMobile(!toggleMenuMobile)
    // setToggleMenuMobile(false)
  }

  const userMenu = useRef(null)
  const menuMobile = useRef(null)
  const asideMyOrder = useRef(null)
  const closeOpenMenus = (e)=>{
    if (userMenu.current && toggle && !userMenu.current.contains(e.target)) {
      setToggle(false)
    }
    const navbarEmail = document.querySelector(".navbar-email")
    if (asideMyOrder.current && toggleOrders && !asideMyOrder.current.contains(e.target) && e.target != navbarEmail) {
      setToggleOrders(false)
    }
    if (menuMobile.current && toggleMenuMobile && !menuMobile.current.contains(e.target)) {
      setToggleMenuMobile(false)
    }
  }
  document.addEventListener('mousedown',closeOpenMenus)
  
  return (
    <nav>
      <img src={menu} alt="menu" className="menu" onClick={handleToggleMenuMobile} />
      <div className="navbar-left">
        <img src={logo} alt="logo" className="nav-logo" />
        <ul>
          <li>
            <a href="/">All</a>
          </li>
          <li>
            <a href="/">Cloth</a>
          </li>
          <li>
            <a href="/">Electronics</a>
          </li>
          <li>
            <a href="/">Furniture</a>
          </li>
          <li>
            <a href="/">Toys</a>
          </li>
          <li>
            <a href="/">Others</a>
          </li>
        </ul>
      </div>
      <div className="navbar-right">
        <ul>
          <li className="navbar-email" onClick={handleToggle}>
            [email protected]
          </li>
          <li 
            className="navbar-shopping-cart" 
            onClick={handleToggleOrders}
          >
            <img src={shoppingCart} alt="shopping cart" />
            {state.cart.length > 0 ? <div>{state.cart.length}</div> : null}
          </li>
        </ul>
      </div>
      <div className="menu-container" ref={userMenu}>
        {toggle && <Menu />}
      </div>
      <div className="my-order-container" ref={asideMyOrder}>
        {toggleOrders && <MyOrder onCerrar = {() => setToggleOrders(false)} />}
      </div>
      <div className="mobile-menu-container" ref={menuMobile}>
        {toggleMenuMobile && <MenuMobile onCerrar = {() => setToggleMenuMobile(false)} />}
      </div>
    </nav>
  )
}

export default Header

Para m谩s detalles los invito a revisar el c贸digo:
https://github.com/Gustavolando/react-shop

Nota: En el reto anterior expliqu茅 porqu茅 coment茅 los setToogle en los m茅todos handle

from react-practico.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.