how import JavaScript in React? - reactjs
I Have a navbar that in Mobile is hamburger Menu, for this nav i have some js code that have some event on html element( add class on tag ) and i dont know how can i import js code into React for my menu .if is there a way to import js code , i will be so happy to learn that .
Header Component code :
import React from "react";
import logo from "../images/logo.png";
function Header(props) {
return (
<header
id="header"
className="fixed-top d-flex align-items-center header-transparent"
dir="rtl"
>
<div className="container d-flex align-items-center justify-content-between">
<div className="logo d-flex">
<img src={logo} alt="عکس" className="imglogo px-2 " />
{/* <!-- <h1><span>ایپاکو</span></h1> --> */}
</div>
<a className=" btn btn-danger text-center text-white fs-5" href="#">
ورود | ثبت نام
</a>
<nav id="navbar" className="navbar d-flex justify-content-between">
<ul>
<li>
<a
className="nav-link scrollto active border-success fs-4"
href="#hero"
>
خانه
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#about">
درباره ما{" "}
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#">
ازمون سنجش{" "}
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#">
فرم ارز یابی
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#">
محصولات
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#">
فروشگاه
</a>
</li>
<li>
<a className="nav-link scrollto fs-5" href="#">
مقالات
</a>
</li>
</ul>
<i className="bi bi-list mobile-nav-toggle"></i>
</nav>
{/* <!-- .navbar --> */}
</div>
</header>
// End Header
);
}
export default Header;
App js code :
import React, { Component } from 'react';
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js";
import "./App.css";
import Header from "./component/Header";
import "./maincode"; // My JS CODE
export default class App extends Component {
render() {
return (
<>
<Header />
</>
)
}
}
App css code :
/*--------------------------------------------------------------
# Header
--------------------------------------------------------------*/
#header {
height: 80px;
-webkit-transition: all 0.5s;
transition: all 0.5s;
z-index: 997;
transition: all 0.5s;
/* background: -webkit-gradient(linear, left top, right top, from(rgba(0, 170, 80, 0.75)), to(rgba(0, 120, 73, 0.75)));*/
background: linear-gradient(to right, rgba(0, 170, 80, 0.75), rgba(0, 120, 73, 0.75));
}
#header.header-scrolled {
/* background: -webkit-gradient(linear, left top, right top, from(rgba(2, 113, 54, 0.75)), to(rgba(1, 59, 37, 0.75)));*/
background: linear-gradient(to right, rgba(2, 113, 54, 0.75), rgba(1, 59, 37, 0.75));
height: 60px;
}
#header .logo h1 {
font-size: 28px;
margin: 0;
padding: 0;
line-height: 1;
font-weight: 700;
}
#header .logo h1 a, #header .logo h1 a:hover {
color: #fff;
text-decoration: none;
}
#header .logo img {
padding: 0;
margin: 0;
max-height: 40px;
}
/*--------------------------------------------------------------
# Navigation Menu
--------------------------------------------------------------*/
/**
* Desktop Navigation
*/
.navbar {
padding: 0;
}
.navbar ul {
margin: 0;
padding: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
list-style: none;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.navbar li {
position: relative;
}
.navbar a, .navbar a:focus {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
padding: 10px 0 10px 15px;
font-size: 10px;
font-weight: 500;
color: rgba(255, 255, 255, 0.7);
white-space: nowrap;
-webkit-transition: 0.3s;
transition: 0.3s;
}
.navbar a i, .navbar a:focus i {
font-size: 12px;
line-height: 0;
margin-left: 5px;
}
.navbar > ul > li > a:before {
content: "";
position: absolute;
width: 0;
height: 2px;
bottom: 3px;
left: 30px;
background-color: #1acc8d;
visibility: hidden;
width: 0px;
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
.navbar a:hover:before, .navbar li:hover > a:before, .navbar .active:before {
visibility: visible;
width: 25px;
}
.navbar a:hover, .navbar .active, .navbar .active:focus, .navbar li:hover > a {
color: #fff;
}
/**
* Mobile Navigation
*/
.mobile-nav-toggle {
color: #fff;
font-size: 28px;
cursor: pointer;
display: none;
line-height: 0;
-webkit-transition: 0.5s;
transition: 0.5s;
}
#media (max-width: 991px) {
.mobile-nav-toggle {
display: block;
}
.navbar ul {
display: none;
}
}
.navbar-mobile {
position: fixed;
overflow: hidden;
top: 0;
right: 0;
left: 0;
bottom: 0;
/* background: -webkit-gradient(linear, left top, right top, from(rgba(0, 170, 80, 0.75)), to(rgba(0, 120, 73, 0.75)));*/
background: linear-gradient(to right, rgba(0, 170, 80, 0.75), rgba(0, 120, 73, 0.75));
-webkit-transition: 0.3s;
transition: 0.3s;
z-index: 999;
}
.navbar-mobile .mobile-nav-toggle {
position: absolute;
top: 15px;
right: 15px;
}
.navbar-mobile ul {
display: block;
position: absolute;
top: 55px;
right: 15px;
bottom: 15px;
left: 15px;
padding: 10px 0;
border-radius: 8px;
background-color: #fff;
overflow-y: auto;
-webkit-transition: 0.3s;
transition: 0.3s;
width: 10em;
}
.navbar-mobile > ul > li > a:before {
right: 20px;
}
.navbar-mobile a, .navbar-mobile a:focus {
padding: 10px 20px;
font-size: 15px;
color: #04723d;
}
.navbar-mobile a:hover, .navbar-mobile .active, .navbar-mobile li:hover > a {
color: #64ffc1;
}
.navbar-mobile .getstarted, .navbar-mobile .getstarted:focus {
margin: 15px;
}
.navbar-mobile .dropdown ul, .navbar-mobile .dropdown .dropdown ul {
position: static;
display: none;
margin: 10px 20px;
padding: 10px 0;
z-index: 99;
opacity: 1;
visibility: visible;
background: #fff;
/* -webkit-box-shadow: 0px 0px 30px rgba(127, 137, 161, 0.25); */
box-shadow: 0px 0px 30px rgba(127, 137, 161, 0.25);
}
.navbar-mobile .dropdown ul li, .navbar-mobile .dropdown .dropdown ul li {
min-width: 200px;
}
.navbar-mobile .dropdown ul a, .navbar-mobile .dropdown .dropdown ul a {
padding: 10px 20px;
}
.navbar-mobile .dropdown ul a i, .navbar-mobile .dropdown .dropdown ul a i {
font-size: 12px;
}
.navbar-mobile .dropdown ul a:hover, .navbar-mobile .dropdown ul .active:hover, .navbar-mobile .dropdown ul li:hover > a, .navbar-mobile .dropdown .dropdown ul a:hover, .navbar-mobile .dropdown .dropdown ul .active:hover, .navbar-mobile .dropdown .dropdown ul li:hover > a {
color: #1acc8d;
}
.navbar-mobile .dropdown > .dropdown-active, .navbar-mobile .dropdown .dropdown > .dropdown-active {
display: block;
}
Maincode js (My js code for NAV):
/**
* Easy selector helper function
*/
const select = (el, all = false) => {
el = el.trim()
if (all) {
return [...document.querySelectorAll(el)]
} else {
return document.querySelector(el)
}
}
/**
* Easy event listener function
*/
const on = (type, el, listener, all = false) => {
let selectEl = select(el, all)
if (selectEl) {
if (all) {
selectEl.forEach(e => e.addEventListener(type, listener))
} else {
selectEl.addEventListener(type, listener)
}
}
}
/**
* Easy on scroll event listener
*/
const onscroll = (el, listener) => {
el.addEventListener('scroll', listener)
}
/**
* Navbar links active state on scroll
*/
let navbarlinks = select('#navbar .scrollto', true)
const navbarlinksActive = () => {
let position = window.scrollY + 200
navbarlinks.forEach(navbarlink => {
if (!navbarlink.hash) return
let section = select(navbarlink.hash)
if (!section) return
if (position >= section.offsetTop && position <= (section.offsetTop + section.offsetHeight)) {
navbarlink.classList.add('active')
} else {
navbarlink.classList.remove('active')
}
})
}
window.addEventListener('load', navbarlinksActive)
onscroll(document, navbarlinksActive)
/**
* Scrolls to an element with header offset
*/
const scrollto = (el) => {
let header = select('#header')
let offset = header.offsetHeight
if (!header.classList.contains('header-scrolled')) {
offset -= 20
}
let elementPos = select(el).offsetTop
window.scrollTo({
top: elementPos - offset,
behavior: 'smooth'
})
}
/**
* Toggle .header-scrolled class to #header when page is scrolled
*/
let selectHeader = select('#header')
if (selectHeader) {
const headerScrolled = () => {
if (window.scrollY > 100) {
selectHeader.classList.add('header-scrolled')
} else {
selectHeader.classList.remove('header-scrolled')
}
}
window.addEventListener('load', headerScrolled)
onscroll(document, headerScrolled)
}
/**
* Back to top button
*/
let backtotop = select('.back-to-top')
if (backtotop) {
const toggleBacktotop = () => {
if (window.scrollY > 100) {
backtotop.classList.add('active')
} else {
backtotop.classList.remove('active')
}
}
window.addEventListener('load', toggleBacktotop)
onscroll(document, toggleBacktotop)
}
/**
* Mobile nav toggle
*/
on('click', '.mobile-nav-toggle', function(e) {
select('#navbar').classList.toggle('navbar-mobile')
this.classList.toggle('bi-list')
this.classList.toggle('bi-x')
})
/**
* Mobile nav dropdowns activate
*/
on('click', '.navbar .dropdown > a', function(e) {
if (select('#navbar').classList.contains('navbar-mobile')) {
e.preventDefault()
this.nextElementSibling.classList.toggle('dropdown-active')
}
}, true)
/**
* Scrool with ofset on links with a class name .scrollto
*/
on('click', '.scrollto', function(e) {
if (select(this.hash)) {
e.preventDefault()
let navbar = select('#navbar')
if (navbar.classList.contains('navbar-mobile')) {
navbar.classList.remove('navbar-mobile')
let navbarToggle = select('.mobile-nav-toggle')
navbarToggle.classList.toggle('bi-list')
navbarToggle.classList.toggle('bi-x')
}
scrollto(this.hash)
}
}, true)
/**
* Scroll with ofset on page load with hash links in the url
*/
window.addEventListener('load', () => {
if (window.location.hash) {
if (select(window.location.hash)) {
scrollto(window.location.hash)
}
}
});
Related
how to change font awesome icon color
Please help me I am new at web development, how do I change the font awesome icon color? In my Navbar.js import { Component } from "react"; import "../Css/Elements/Navbar.css"; class Navbar extends Component { state = { clicked: false }; handleClick = () => { this.setState({ clicked: !this.state.clicked }); }; render() { return ( <> <nav> <a id="navlogo" href="/">Blank</a> <div> <ul id="navbar" className={this.state.clicked ? "#navbar active" : "#navbar"} > <li> <a href="/"> Home </a> </li> <li> Shop </li> <li> Blog </li> <li> About </li> <li> Contact </li> </ul> </div> <div id="mobile" onClick={this.handleClick}> <i id="bar" className={this.state.clicked ? "fas fa-times" : "fas fa-bars"} ></i> </div> </nav> </> ); } } export default Navbar; Css (i put this here just in case) /* Nav */ nav { font-family: sans-serif; display: flex; position: fixed; align-items: center; justify-content: space-between; background: #F1F5F9; width: 100%; padding: 20px 23px; z-index: 100; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.06); } a { text-decoration: none; } #navlogo { font-weight: 700; font-size: 25px; color: #1D3053; text-transform: uppercase; transition: all 0.2s ease; } #navlogo:hover { color: #319afc; } #navbar { display: flex; align-items: center; justify-content: center; } #navbar li { list-style: none; padding: 0 20px; position: relative; } #navbar li a { text-decoration: none; font-size: 15px; font-weight: 700 !important; color: #1D3053; text-transform: uppercase; transition: 0.2s ease-in-out; } #navbar li a:hover { color: #319afc; } #navbar li a:hover::after { content: ""; width: 30%; height: 2px; background: #319afc; position: absolute; bottom: -4px; left: 20px; } #mobile { display: none; } #mobile i { color: #ffff; align-items: center; } #media screen and (max-width: 769px) { #navbar { display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-start; position: fixed; top: 69px; right: -100%; width: 270px; height: 100%; background: #ffff; box-shadow: 0 40px 60px rgba(0, 0, 0, 0.1); padding: 40px 0 0 10px; z-index: 100; transition: 0.3s ease-in-out; } #navbar.active { right: 0px; } #navbar li { margin-bottom: 25px; z-index: 100; } #mobile { display: block; } #mobile i { font-size: 24px; cursor: pointer; } } how do I change the "fa-bars" and the "fa-times" icon color my website theme is white and the icon is white too, so its hard to see the icon Sorry for the bad English :)
You can add style={{color: "<color-name>"}} like this: <i id="bar" className={this.state.clicked ? "fas fa-times" : "fas fa-bars"} style={{color: "red"}}> </i>
The color of icon is controlled either by itself. #mobile i { color: red; align-items: center; } I believe fontawesome also allow you to define color from its parent, because by default it also has this line built-in depending on the UI library you are using. i { color: inherit; }
Pushing Sign Up & Login buttons to bottom of sidebar
I am trying to recreate this design: Click to view However, the below is currently occurring. Click to view I am trying to push the SignUp and Login buttons to the bottom of the sidebar to match the first image. Am I missing a CSS class that would allow me to do so, or would this be a JS configuration? Hope I have not overlooked something basic that would allow me to achieve this state. Sidebar.js import React from "react"; import "../App.css"; import { SidebarData } from './SidebarData' import Logo from './Logo.svg' import { Login } from './Login' import { SignUp } from './SignUp' function Sidebar() { return ( <div className="Sidebar"> <div className="Header"> <div><img src = {Logo} alt='Logo’ className='Logo’ /></div> </div> <div class="line-break" /> <ul className="SidebarList"> {SidebarData.map((val, key) => { return ( <li key={key} className="row" id={window.location.pathname == val.link ? "active" : ""} onClick={() => { window.location.pathname = val.link; }} > <div id="icon">{val.icon}</div> <div id="title">{val.title}</div> </li> ); })} </ul> <div class="line-break" /> <div className="footer"> <ul className= "SidebarList"> {Login.map((val, key) => { return ( <li key={key} className="Login" id={window.location.pathname == val.link ? "active" : ""} onClick={() => { window.location.pathname = val.link; }} > <div id="title">{val.title}</div> </li> )} )} </ul> <ul className= "SidebarList"> {SignUp.map((val, key) => { return ( <li key={key} className="SignUp" id={window.location.pathname == val.link ? "active" : ""} onClick={() => { window.location.pathname = val.link; }} > <div id="title">{val.title}</div> </li> )} )} </ul> </div> </div> Here is CSS .App { width: 100vw; height: 100vh; } body { margin: 0; padding: 0; } .Sidebar { height: 100%; width: 300px; background-color: white; border-right: 1px solid #F0F4FB; padding-left: 15px; box-sizing: border-box; } .SidebarList { height: auto; width: 100%; padding-left: 15px; font-size: 18px; border: 2px #FD954E; box-sizing: border-box } .SidebarList .row { width: 100%; height: 50px; background-color: white; list-style-type: none; margin: 0%; padding-bottom: 15px; display: flex; color: #A7ACB6; justify-content: center; align-items: center; font-family: Arial, Helvetica, sans-serif; } .SidebarList .row:hover { cursor: pointer; background-color: #E7E7E7 ; } .SidebarList #active { background-color: white; color: #FD954E } .SidebarList .Login { background-color: white; color: #FD954E; width: 90%; height: 5vh; right: 1596px; top: 958px; border: 1px solid #FD954E; box-sizing: border-box; border-radius: 19.5px; text-align: center; font-family: Arial, Helvetica, sans-serif } .SidebarList .SignUp { width: 90%; height: 5vh; right: 1596px; top: 1011px; background: #FD954E; border-radius: 19.5px; border: none; text-align: center; font-family: Arial, Helvetica, sans-serif; } .row #icon { flex: 30%; display: grid; place-items: center; transform: scale(1.2) } .row #title { flex: 70%; } .Logo { padding-left: 25px; padding-top: 25px; padding-bottom: 30px; width: 55%; } .line-break { height: 1px; width: 100%; background-color: #F0F4FB; } ul.nav { list-style-type: none; padding: 20px 0 0 40px; list-style-type: none; } ul.nav li { margin-bottom: 14px; list-style-type: none; } ul.nav li:last-child { margin-bottom: 0; list-style-type: none; } .footer { display: flex; flex-direction: column; align-items: center; padding: 20px 0; } .footer button { padding: 6px 0; width: 80%; margin-bottom: 14px; } .footer button:last-child { margin-bottom: 0; } .header .Logo { height: 40px; width: 200px; background-color: grey; margin: 20px; }
Without seeing the source for the entire page and not just a couple of the buttons, it is impossible to say for sure, but my guess would be the following is causing your problem: .Sidebar { height: 100%; ... } Setting height: 100% will only tell the sidebar to take up 100% of the space it needs for its content (or 100% of the height of its bounding container, depending on the display properties of both). I would recommend changing it to this to solve your problem: .Sidebar { min-height: 100vh; ... }
How to convert from TSX to JSX in this Codesandbox
I am learning React and JavaScript and now I have this CodeSandbox but I can't convert it to JavaScript React I have tried this for the Card Component: import React, { useState } from "react"; const Card = props => { const [expanded, setExpanded] = useState(false); const RenderCard = (className) => { return ( <div className={`card ${className}${expanded ? " expanded" : ""}`} onClick={() => { setExpanded(!expanded); }} > <div className="image"> <img alt="digger" src={props.image} /> </div> <div className="info"> <div className="bg1" style={{ backgroundImage: "url('" + props.image + "')" }} /> <div className="bg2" style={{ backgroundImage: "url('" + props.image + "')" }} /> <div className="bg3" style={{ backgroundImage: "url('" + props.image + "')" }} /> <h1>JCB OES System</h1> <h2>Report Number #1</h2> <h3>Load lifter work area</h3> <div className="button">Open</div> </div> </div> ); }; return ( <div className={`card-container${expanded ? " expanded" : ""}`}> {expanded && <div className="background" />} {<RenderCard className="card1"/>} {<RenderCard className="card2"/>} </div> ); }; export default Card; And I show the Card like this. It's very simple, it's just that when I click a card it does not pop up like the CodeSandbox does. import Card from "./Card"; import "./styles.scss"; const CreateContent = () => { return ( <div className="app"> <link rel="stylesheet" href="https://use.typekit.net/jli8mqj.css" /> <Card image="https://www.thetimes.co.uk/imageserver/image/methode%2Fsundaytimes%2Fprodmigration%2Fweb%2Fbin%2Fbeae8dfb-0a41-4f8e-b076-ffd68417287b.jpg?crop=1024%2C683%2C0%2C0&resize=685" /> <Card image="https://www.virginexperiencedays.co.uk/content/img/product/large/PJCBRA__01.jpg" /> <Card image="https://www.toyfarmers.co.uk/images/britains-jcb-3cx-backhoe-loader.jpg" /> <Card image="https://seatylive.blob.core.windows.net/eventimages/2019-Aug-20-Tue_18-37-31-969.png" /> </div> ); }; export default CreateContent; The styles is the same, with no changes: I understand that the expanded background when set to expanded=true using the hook that the scss style for background should be in effect but all I get is a black screen. .app { font-family: sans-serif; text-align: center; padding: 4em 0; } * { box-sizing: border-box; padding: 0; margin: 0; border: 0; } .card-container { font-family: niveau-grotesk, sans-serif !important; position: relative; z-index: 0; &.expanded { z-index: 3; #keyframes fade-in { 0% { opacity: 0; } 100% { opacity: 1; } } .background { animation: fade-in .3s ease-out; z-index: 2; position: fixed; background: black; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; } } .card { margin: 0 auto; width: 25em; cursor: pointer; border-radius: .5em; box-shadow: 0 50px 100px -20px rgba(50,50,93,.25), 0 30px 60px -30px rgba(0,0,0,.3), 0 -18px 60px -10px rgba(0,0,0,.03); margin-bottom: 4em; &.card2 { transition: .2s ease-out; position: absolute; top: 0; width: 25em; left: 50%; z-index: 4; transform: translateX(-50%); &.expanded { width: 40em; } } .image { img { display: block; border-top-left-radius: .5em; border-top-right-radius: .5em; width: 100%; } } $button-color: rgb(87, 87, 228); &:hover { .info .button { color: white; background: $button-color; } } .info { background: white; position: relative; border-bottom-left-radius: .5em; border-bottom-right-radius: .5em; overflow: hidden; padding: .9em 1.2em; text-align: left; $darkness: 0; &:after { content: ''; position: absolute; top: -20%; left: -20%; right: -20%; bottom: -20%; height: 140%; width: 140%; // filter: blur(8px); // -webkit-filter: blur(8px); background: linear-gradient(to right, rgba(0, 0, 0, 0.4) 30%, rgba(0, 0, 0, 0) 100%); } .bg1, .bg2 { position: absolute; top: -20%; left: -20%; right: -20%; bottom: -20%; height: 140%; width: 140%; z-index: 0; background-position: center; background-repeat: no-repeat; background-size: 100%; transform: rotate(180deg); filter: blur(16px); } .bg2 { background-position: bottom; transform: rotate(0deg); opacity:0.4; } .bg3 { background-position: top; transform: rotate(180deg); opacity:0.4; } .button { transition: .2s ease-out; text-transform: uppercase; line-height: 1.2em; position: absolute; top: 50%; transform: translateY(-50%); right: 1.8em; z-index: 1; background: white; border-radius: 1em; padding: .4em 1.1em; font-weight: 600; color: $button-color; } h1, h2, h3 { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; max-width: 13em; color: white; position: relative; z-index: 1; text-shadow: 1px 1px 1px rgba(0,0,0,0.8); } h1 { line-height: .8em; letter-spacing: .03em; font-size: 1.1em; font-weight: 900; padding: .1em 0; } h2 { letter-spacing: .02em; font-size: 1.0em; font-weight: 400; padding: .1em 0; opacity: 0.6; } h3 { letter-spacing: .02em; font-size: .9em; font-weight: 400; padding: .1em 0; } } } } What am I missing?
To swap your Code Sandbox from TypeScript to JavaScript you should just need to: Remove all typescript specific syntax from your ".ts" and ".tsx" files (interfaces, typings, etc) Change all the file extensions to their JavaScript equivalent i.e. ".ts" -> ".js" and ".tsx" -> ".jsx". Update the "main" property of the "package.json" file to point to the renamed entry point. i.e. "src/index.jsx". I created a quick CodeSandbox with this already done.
Mobile menu effect expands to desktop view
Question: How can I have the navigation of the mobile menu not affect desktop? When I click on the hamburger button to hide the menu and resize the browser to desktop the navigation disappears. I want to know what I did wrong to better understand my mistake. GIF Video Full Code Below: class Navigation extends React.Component { constructor(props) { super(props); this.state = { show: false } this.toggleShow = this.toggleShow.bind(this) this.hide = this.hide.bind(this) } toggleShow() { this.setState({ show: !this.state.show }); } hide(e) { if (e && e.relatedTarget) { e.relatedTarget.click(); } } render() { return ( <Router> <div className="FlexContainer NavbarContainer"> <div className="mobilecontainer LeftNav"> <h2 className="BrandName LeftNav mobileboxmenu inline FarRight">Kommonplaces</h2> <div className="hamburger inlinev" onClick={this.toggleShow} onBlur={this.hide}> <img alt="menubtn" src={hamburger}></img> </div> </div> { this.state.show && ( <ul className="NavBar"> <Dropdown/> <li className="RightNav"><Link to="/">Host Your Space</Link></li> <li className="RightNav"><Link to="/">About Us</Link></li> <li className="RightNav"><Link to="/">Contact Us</Link></li> <li className="RightNav"><Link to="/">Sign Up</Link></li> <li className="RightNav"><Link to="/">Login</Link></li> </ul> ) } </div> </Router> ); } } export default Navigation; SCSS Code: .NavbarContainer { border-bottom: 1px #E7E7E7 solid; .mobilecontainer { margin: 2.1em; .hamburger { display: inline; img { width: 35px; cursor: pointer; float: right; } } } .NavBar { // display: none; .RightNav { text-align: center; padding: 27px 0; border-right: none; border-left: none; border-bottom: 1px #E7E7E7 solid; font-size: large; a { color: #0E0E0E; } } } .Dropdown { float: none; overflow: hidden; .Dropdown-Content { position: relative; background-color: white; z-index: 2; min-width: 217px; a { float: none; display: block; text-align: center; border: 1px #E7E7E7 solid; padding: 15px 22px; } } } .dropdown-content { a { &:hover { background-color: #ddd } } } .dropdown { &:hover { .dropdown-content { display: block; } } } .LeftNav { flex-grow: 8.2; font-weight: bold; font-size: large; display: flex; align-items: center; } #media (min-width: 55em) { display: flex; flex-direction: row; align-items: center; top: 0; left: 0; z-index: 9999; width: 100%; height: 60px; background-color: #ffffff; padding: 3em; .LeftNav { flex-grow: 0.2; font-weight: bold; font-size: large; } .mobilecontainer { margin: 0; .hamburger { img { display: none } } } .NavBar { display: flex; .RightNav { padding-left: 15px; padding-right: 15px; text-align: initial; border-bottom: none; font-size: large; } .FarRight { flex-grow: 1; } } .Dropdown { float: left; overflow: hidden; .Dropdown-Content { position: absolute; top: 96px; background-color: white; z-index: 2; min-width: 217px; a { text-align: left; padding: 15px 22px; } } } } } This is SCSS code that I'm using for this project.
this.state.show is responsible just for toggle mobile menu.when show ===false react ignore rendering navBar neither in mobile or desktop.you cant use this way for your goal.My suggestion is to consider using react-bootstrap Navbar or reactstrap Navbar I hope this will help you.
Responsive Navigation bar Multimenu
I have a horizontal navigation bar that collapses into a dropdown menu for mobile devices. My problem is the sub menu for the 'Portfolio' item doesn't redirect to the link in mobile device, but it does when the view is full width. It works when no link is assigned but as soon as a link is added the mobile device view becomes unclickable. Any help is greatly appreciated. Thanks <style> * { margin: 0; padding: 0; } body, html { font-family: segoe ui; font-size: 100%; height: 100%; } /* **************************************************** CONTAINER ******************************************************/ .container { background: #FFF; min-height: 100%; overflow: auto; } /* ENLACES */ a { color: #fff; cursor: pointer; display: block; padding: 1rem 1.5rem; text-decoration: none; transition: background-color .16s ease-in; } a:hover { background-color: #CD5C5C; } /* ************************************************** MENU NORMAL ****************************************************/ .navbar-menu { background-color: #F08080; margin: 2% auto; max-width: 1000px; width: calc(100%); } .navbar-menu .menu { display: block; text-align: center; } .navbar-menu .menu li { display: inline-block; } .navbar-menu .menu li:hover > .submenu { display: block; -webkit-animation-name: showSubMenu; -webkit-animation-duration: .4s; } .navbar-menu .menu li ul { background-color: #f08080; display: none; position: absolute; } .navbar-menu .menu li ul li { display: block; } .navbar-menu .menu li ul li a:active { -webkit-animation-name: hideSubMenu; -webkit-animation-duration: .4s; } /****************************************************** MINI MENU ******************************************************/ /* Mini menu */ .navbar-mini-menu { background-color: #f08080; display: none; } .navbar-mini-menu .menu-select { color: #fff; padding: 1rem 1.5rem; } .navbar-mini-menu .menu-select .btn-select { background: url("icon.png") no-repeat; cursor: pointer; position: absolute; height: 30px; width: 30px; right: 10px; top: 10px; } .navbar-mini-menu .mini-menu-options { display: block; } .navbar-mini-menu .mini-menu-options li { display: block; } .navbar-mini-menu .mini-menu-options li .submenu { display: none; } .navbar-mini-menu .mini-menu-options li:focus { outline: 0; } .navbar-mini-menu .mini-menu-options li:focus > .submenu { display: block; -webkit-animation-name: showSubMenu; -webkit-animation-duration: .4s; } .navbar-mini-menu .mini-menu-options li a { display: block; padding: 1rem 1.5rem; } #-webkit-keyframes showSubMenu { 0% { transform: scale(0,0); } 100% { transform: scale(1,1); } } #-webkit-keyframes hideSubMenu { 0% { transform: scale(1,1); } 100% { transform: scale(0,0); } } /***************************************************** MEDIAQUERIES *****************************************************/ #media screen and (max-width: 750px) { .navbar-menu { margin: 0; } .navbar-menu .menu { display: none; } .navbar-mini-menu { display: block; } .navbar-mini-menu .mini-menu-options { display: none; } } </style> <main class="container"> <header> <nav class="navbar-menu"> <!-- Menu normal --> <ul class="menu"> <li><a>Home</a> </li> <li><a>Portfolio</a> <ul class="submenu"> <li>Weddings</li> <li><a>Other Work</a></li> </ul> </li> <li><a>Rates</a></li> <li><a>Shop</a></li> <li><a>Contact</a></li> </ul> <!-- Mini Menu --> <nav class="navbar-mini-menu"> <div class="menu-select"> <span class="menu-actual"> Menu </span> <span class="btn-select"> </span> </div> <ul class="mini-menu-options"> <li><a>Home</a></li> <li tabIndex="0"><a>Portfolio</a> <ul class="submenu"> <li>Weddings</li> <li><a>Other Work</a></li> </ul> </li> <li tabIndex="0"><a>Rates</a></li> <li tabIndex="0"><a>Shop</a></li> <li tabIndex="0"><a>Contact</a></li> </ul> </nav> </nav> </header> </main> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $(document).on("ready",function() { // 0 = hide, 1 = visible var menuState = 0; //if($(".mini-menu-options").is(":hidden")) { /* Add a Click event listener to btn-select */ $(".btn-select").on("click",function() { if(menuState === 0) { $(".mini-menu-options").slideDown("slow"); menuState = 1; } else { $(".mini-menu-options").slideUp("slow"); menuState = 0; } }); //} $(".mini-menu-options li").on("click", function() { var numHijos = $(this).children().length; if(numHijos < 2) { // hide the menu $(".mini-menu-options").hide("fast"); var texto = $(this).text(); $(".menu-select .menu-actual").text(texto); } menuState = 0; }); }); </script>