React Scroll Active class is not working? - reactjs

Im using react-scroll and I want to get a border to my Navbar sections when they are in the sections using a className of active. But it works just for the first section.
when I inspect I just see the first li get the active class.
I tried to get them manual class by using react-scroll function activeClass=active and then set a CSS file for the class but it doesnt work either
see the img
see the img
import { Link as LinkR} from 'react-router-dom'
import { Link as LinkS} from 'react-scroll'
import styled from 'styled-components'
export const Nav = styled.nav`
background: ${({scrollNav}) => (scrollNav ? '#000' : 'transparent ')} ;
height: 80px;
margin-top: -80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
#media screen and (max-width: 960px) {
transition: 0.8s all ease
}
`
export const NavbarContainer = styled.div`
display: flex;
justify-content: space-between;
height: 80px;
z-index: 1;
width: 100%;
padding: 0 24px;
max-width: 1100px;
align-items: center;
`
export const NavLogo = styled(LinkR)`
color: #fff;
justify-self: flex-start;
cursor: pointer;
font-size: 1.5rem;
align-items: center;
margin-left: 24px;
font-weight: bold;
text-decoration: none;
`
export const MobileIcon = styled.div `
display: none;
#media screen and (max-width: 768px) {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
color: #fff;
}
`
export const NavMenu = styled.ul `
display: flex;
align-items: center;
list-style: none;
text-align: center;
margin-right: -22px;
#media screen and (max-width: 768px){
display: none;
}
`
export const NavItem = styled.li `
height: 80px;
`
export const NavLinks = styled(LinkS) `
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 1rem;
height: 100%;
cursor: pointer;
&.active {
border-bottom: 3px solid #01bf74;
}
`
export const NavBtn = styled.nav `
display: flex;
align-items: center;
#media screen and (max-width: 768px){
display: none;
}
`
export const NavBtnLink = styled(LinkR) `
border-radius: 50px;
background: #01bf71;
white-space: nowrap;
padding: 10px 22px;
color: #010606;
font-size: 16px;
outline: none;
border: none;
cursor: pointer;
transition: all 0.2s ease-in-out;
text-decoration: none;
&:hover {
transition: all 0.2s ease-in-out;
background: #fff;
color: #010606;
}
`
import React, { useEffect, useState } from 'react'
import { MobileIcon, Nav, NavbarContainer, NavBtn, NavBtnLink, NavItem, NavLinks, NavLogo, NavMenu } from './NavbarEelemnts'
import { FaBars } from 'react-icons/fa'
import { animateScroll as scroll } from 'react-scroll'
const Navbar = ({ togle }) => {
const [scrollNav, setScrollNav] = useState(false)
const ChangeNav = () => {
if (window.scrollY >= 80) {
setScrollNav(true)
} else {
setScrollNav(false)
}
}
useEffect(() => {
window.addEventListener('scroll', ChangeNav)
}, [])
const togleHome = () => {
scroll.scrollToTop()
}
return (
<>
<Nav scrollNav={scrollNav}>
<NavbarContainer>
<NavLogo to='/' onClick={togleHome}>Dolla</NavLogo>
<MobileIcon onClick={togle} >
<FaBars />
</MobileIcon>
<NavMenu>
<NavItem>
<NavLinks
to="about"
spy={true}
smooth={true}
offset={-80}
duration={500}
exact='true'
>
About
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="discover" spy={true} smooth={true} offset={-80} duration={500} exact='true' >
Discover
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="services" spy={true} smooth={true} offset={-80} duration={500} exact='true' >
Services
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="signup" spy={true} smooth={true} offset={-80} duration={500} exact='true'>
Sign Up
</NavLinks>
</NavItem>
</NavMenu>
<NavBtn>
<NavBtnLink to='/signin'>Sign In</NavBtnLink>
</NavBtn>
</NavbarContainer>
</Nav>
</>
)
}
export default Navbar

Related

Why are my link tags delayed before activating in react

I set up a list of React Link tags in my website and when one gets clicked on, it should immediately automatically scroll down to the section the user clicks on. That works, however, for whatever reason there's about a 1-2 second delay before this action actually occurs after a click. I've been trying to figure out what is causing this but I can't figure out what the problem is.
Here is the NavBar component:
import React from "react";
import { Link } from 'react-scroll';
import { FaBars, FaTimes } from 'react-icons/fa';
import { useState, useEffect } from "react";
import './Navbar.css';
const Navbar = () => {
const [click, setClick] = useState(false);
const handleClick = () => setClick(!click);
const closeMenu = () => setClick(false);
const [fix, setFix] = useState(window.scrollY >= 950);
useEffect(() => {
const setFixed = () => {
setFix(window.scrollY >= 950);
};
window.addEventListener("scroll", setFixed);
return () => {
window.removeEventListener("scroll", setFixed);
};
}, []);
return (
<div className={fix ? 'header active' : 'header'}>
<nav className={fix ? 'navbar fixed' : 'navbar'}>
<div className='hamburger' onClick={handleClick}>
{click ? (<FaTimes size={30} style={{ color: '#ffffff' }} />)
: (<FaBars size={30} style={{ color: '#ffffff' }} />)}
</div>
<ul className={click ? "nav-menu active" : "nav-menu"}>
<li className='nav-item'>
<Link to="header" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Home</Link>
</li>
<li className='nav-item'>
<Link to="aboutMe" spy={true} smooth={true} offset={-100} onClick={closeMenu}>About</Link>
</li>
<li className='nav-item'>
<Link to="portfolio" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Projects</Link>
</li>
<li className='nav-item'>
<Link to="resume" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Resume</Link>
</li>
</ul>
</nav>
</div>
)
}
export default Navbar;
Here is the css for the NavBar component:
.header {
position: fixed;
height: 90px;
width: 100%;
top: 0;
left: 0;
z-index: 1;
transition: .3s ease-in;
overflow: hidden;
background-color: transparent;
}
.header.active {
background-color: rgba(0,0,0,.9);
}
.header .navbar {
display: flex;
justify-content: center;
align-items: center;
margin: auto;
height: 100%;
padding: 0 1rem;
}
.header .nav-menu a {
color: #ffffff;
}
.header .nav-menu {
display: flex;
}
.header .nav-item {
padding: 1rem;
font-weight: 500;
}
.header .nav-item a:hover {
padding-bottom: 4px;
color: #d4af37;
cursor: pointer;
}
.nav-menu a.active {
color: #d4af37;
text-decoration: none;
}
.hamburger {
display: none;
}
.nav-item {
list-style: none;
}
#homeLink {
text-decoration: none;
}
#media screen and (max-width:940px) {
.header {
max-width: 100%;
background-color: rgba(0,0,0,.9);
}
.header .navbar {
max-width: 100%;
justify-content: space-between;
}
.hamburger {
display: block;
}
.nav-menu {
position: fixed;
left: -100%;
top: 90px;
flex-direction: column;
background-color: rgba(0,0,0,.9);
width: 100%;
height: 90vh;
z-index: 999;
text-align: center;
transition: .3s;
}
.nav-menu.active {
left: 0;
}
.nav-item {
margin: 1.5rem 0;
}
.header .navbar img {
width: 150px;
}
}
Played around with it a bit more. I found that the smooth={true} prop in the Link tags was what slowed it down. Not entirely sure why that was the case, but removing it made my navbar work as intended.

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;
}

React Component not loading in localhost

i am currently following a tutorial to build a react website, however, i am unable to load NavBar (code is below) on my localhost. the web pack is being complied successfully and i am able to load a normal header, just not NavBar. i have attached the codes below. appreciate all the helps and inputs. tia
video that i am following: https://www.youtube.com/watch?v=Nl54MJDR2p8&t=375s
App.js
import './App.css';
import Navbar from './components/NavBar';
import { BrowserRouter as Router } from 'react-router-dom';
function App() {
return (
<Router>
<Navbar />
</Router>
);
};
export default App;
index.js for NavBar
import React from 'react';
import { FaBars } from 'react-icons/fa';
import { Nav, NavbarContainer, NavLogo, MobileIcon, NavMenu, NavItem, NavLinks } from './NavBarElements';
const Navbar = () => {
return (
<>
<Nav>
<NavbarContainer>
<NavLogo to='/'>Blockchain Voting System</NavLogo>
<MobileIcon>
<FaBars />
</MobileIcon>
<NavMenu>
<NavItem>
<NavLinks to="about">About</NavLinks>
</NavItem>
</NavMenu>
</NavbarContainer>
</Nav>
</>
);
};
export default Navbar;
NavBarElements.js
import styled from 'styled-components'
import { Link as LinkR } from 'react-router-dom'
import { Link as LinkS } from 'react-scroll'
export const Nav = styled.nav`
background: #000;
height: 80px;
// margin-top: -80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
#media screen and (max-width: 960px){
transition: 0.8s all ease;
}
`;
export const NavbarContainer = styled.div`
display: flex;
justify-content: space-between;
height: 80px;
z-index: 1;
width: 100%;
padding: 0 24px;
max-width: 1100px;
`;
export const NavLogo = styled(LinkR)`
color: red;
justify-self: flex-start;
cursor: pointer;
font-size: 1.5rem;
display: flex;
align-items: center;
margin-left: 24px;
font-weight: bold;
text-decoration: none;
`;
export const MobileIcon = styled.div`
display: none;
#media screen and (max-width: 768px) {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
`;
export const NavMenu = styled.ul`
display: flex;
align-items: center;
list-style: none;
text-align: center;
margin-right: -22px;
#media screen and (max-width: 768px) {
display: none;
}
`;
export const NavItem = styled.li`
height: 80px;
`;
export const NavLinks = styled(LinkS)`
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 1 rem;
height: 100%;
cursor: pointer;
&.active {
border-bottom: 3px solid #01bf71;
}
`;

Hamburger menu logic not working properly in React

I have set up the logic for Hamburger menu in Navbar.jsx but when I press the hamburger button it just transitions to X button and doesn't display links menu. How do I display the menu and what would be your approach to animating the transition between hamburger and X icon. Here is my code:
import React from 'react'
import './Navbar.css'
import { useState } from 'react'
import { FaSearch } from 'react-icons/fa'
import { NavLink } from 'react-router-dom'
import { GiHamburgerMenu } from 'react-icons/gi'
import { AiOutlineClose } from 'react-icons/ai'
function Navbar() {
const [toggleMenu, setToggleMenu] = useState(false)
const [toggleSearch, setSearchWindow] = useState(false)
return (
<div className='mka__navbar'>
<div className='mka__navbar-links'>
<div className='mka__navbar-links_container'>
<p><NavLink to='/'>HOME</NavLink></p>
<p><NavLink to='/fahrzeugangebote'>FAHRZEUGANGEBOTE</NavLink></p>
<p><NavLink to='/finanzierung'>FINANZIERUNG</NavLink></p>
<p><NavLink to='/fahrzeugankauf'>FAHRZEUGANKAUF</NavLink></p>
<p><NavLink to='/galerie'>GALERIE</NavLink></p>
<p><NavLink to='/kontakt'>KONTAKT</NavLink></p>
<div className='mka__search_column'>
<p><i><FaSearch onClick={() => setSearchWindow(!toggleSearch)} /></i></p>
{
toggleSearch ?
<div className='mka__search-div'>
< input type="text" value="Suche..."/>
</div>
: null
}
</div>
</div>
{
toggleMenu
? <AiOutlineClose color="#fff" className='mka___hamburger-menu' size={27} onClick={() => setToggleMenu(false)} />
: <GiHamburgerMenu size={27} className='mka___hamburger-menu' color="#fff" onClick={() => setToggleMenu(true)} />
}
{
<div className='mka__navbar-menu-margins'>
<div className='mka__navbar-menu_container'>
<div className='mka__navbar-menu_container-links'>
<p><NavLink to='/'>HOME</NavLink></p>
<p><NavLink to='/fahrzeugangebote'>FAHRZEUGANGEBOTE</NavLink></p>
<p><NavLink to='/finanzierung'>FINANZIERUNG</NavLink></p>
<p><NavLink to='/fahrzeugankauf'>FAHRZEUGANKAUF</NavLink></p>
<p><NavLink to='/galerie'>GALERIE</NavLink></p>
<p><NavLink to='/kontakt'>KONTAKT</NavLink></p>
</div>
</div>
</div> && toggleMenu
}
</div>
</div>
);
}
export default Navbar;
And here is my CSS code for the same component:
/* Navbar links */
.mka__navbar {
display: flex;
padding: 2rem;
}
.mka__navbar-links {
flex: 1;
display: flex;
justify-content: end;
align-items: center;
}
.mka__navbar-links_container {
display: flex;
flex-direction: row;
}
.mka__navbar-links_container p {
color: #fff;
font-size: 14px;
line-height: 66px;
text-transform: uppercase;
font-weight: 400;
font-family: var(--font-family);
transition: padding .25s linear;
padding: 0 0.5rem;
cursor: pointer;
}
.mka__navbar-links_container p > a:link {
color: #fff;
}
.mka__navbar-links_container p > a:hover {
transition: 0.5s;
color: #db2d2e;
}
.mka__navbar-links_container p > i:hover {
transition: 0.5s;
color: #db2d2e;
}
.mka__navbar-links_container p > a.active {
color: #db2d2e;
}
/* Hamburger menu */
.mka__navbar-menu_container-links {
color: #fff;
font-size: 14px;
line-height: 66px;
text-transform: uppercase;
font-weight: 400;
font-family: var(--font-family);
padding: 0 1.5rem;
cursor: pointer;
width: 100%;
}
/* Search */
.mka__search-div {
background: #0e0e0ed1;
display: block;
padding: 15px;
width: 327px;
z-index: 888;
border-radius: 0;
border-top: 5px solid #db2d2e;
position: absolute;
margin-left: auto;
margin-right: auto;
left: 26rem;
right: 0;
top: 9rem;
}
input {
border: 1px solid #fff;
background: #0f1215;
color: gray;
padding: 12px;
width: 290px;
}
.mka__search_column {
display: flex;
flex-direction: column;
}
/* Media Queries */
#media (min-width: 992px){
.mka__navbar-links {
justify-content: center;
}
.mka__navbar-menu_container-links {
display: none;
}
.mka___hamburger-menu {
display: none;
}
}
#media (max-width: 992px){
.mka__navbar-links_container {
display: none;
}
.mka__navbar-menu_container {
background-color: #0f1215;
width: 100%;
margin-top: 0.5rem;
border-radius: 5px;
}
.mka__navbar-menu-margins {
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
width: 100%;
margin-top: 40px;
}
}
#media (min-width: 1200px){
.mka__navbar-links_container p {
padding: 0 1rem;
}
}
#media (max-width: 1200px){
.mka__search-div {
left: 21rem;
}
}
I created a hamburger menu which you can toggle it with X icon. and also implemented the drawer transition. the link is here https://codesandbox.io/s/great-hugle-oy5s8b?file=/src/App.js
hope it can help you.

I am not able to redirect this page to about page, trying from last 2 hours?

import React from 'react'
import {FaBars} from 'react-icons/fa';
import {Nav, NavbarContainer, NavLinks, NavMenu, NavLogo, MobileIcon, NavItem, NavBtn, NavBtnLink} from './NavbarElements';
{/* Here is NavBar section*/}
const NavBar = ({toggle}) => {
return (
<>
<Nav>
<NavbarContainer>
<NavLogo to="/">Random</NavLogo>
<MobileIcon onClick={toggle}>
<FaBars />
</MobileIcon>
<NavMenu>
<NavItem>
{/*This is for logo*/}
<NavLinks to="generate" onClick={toggle}>Generate Password</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="about" onClick={toggle}>About us</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="strength" onClick={toggle}>check strength</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="signup" onClick={toggle}>Sign Up</NavLinks>
</NavItem>
</NavMenu>
<NavBtn><NavBtnLink to="/profile">View Profile</NavBtnLink></NavBtn>
</NavbarContainer>
</Nav>
</>
);
}
export default NavBar
Here I have created different links in header section but when i am trying to redirect it, its not happening.
I read 20 articles in the last two hours but I am not able to redirect the about page.I am a beginner please help me in react.
import styled from 'styled-components'
import {Link} from 'react-router-dom';
import {Link as LinkS} from 'react-scroll';
export const Nav = styled.nav`
background: black;
height: 80px;
margin-top: -80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
#media screen and (max-width: 960px){
transition: 0.8s all ease;
}
`
export const NavbarContainer = styled.div`
display: flex;
justify-content: space-between;
height: 80px;
z-index: 1;
width: 100%;
padding: 0 24px;
max-width: 1100px;
`
export const NavLogo = styled(Link)`
color: #F90919;
justify-self: flex-start;
cursor: pointer;
font-size: 1.5rem;
display: flex;
align-items: center;
margin-left: 24px;
font-weight: bold;
text-decoration: none;
&.active{
border-bottom: 3px solid #F90919;
}
`;
export const NavMenu = styled.ul`
display: flex;
align-items: center;
list-style: none;
text-align: center;
margin-right: -22px;
#media screen and (max-width: 768px){
display: none;
}
`
export const NavItem = styled.li`
height: 80px;
`
export const NavLinks = styled(LinkS)`
color: white;
display: flex;
align-items: center;
padding: 0 1rem;
height: 100%;
cursor: pointer;
text-decoration: none;
`;
export const NavBtn = styled.nav`
display: flex;
align-items: center;
#media screen and (max-width: 760px){
display: none;
}
`
export const NavBtnLink = styled(Link)`
border-radius: 50px;
background: #F90919;
white-space: nowrap;
padding: 10px 22px;
color: #010606;
font-size: 16px;
outline: none;
border: none;
cursor: pointer;
transition: all 0.2s ease-in-out;
text-decoration: none;
&:hover{
transition: all 0.2s ease-in-out;
background: #fff;
color: #010606;
}
`
I have edited Nav, Naccontainer, NavItem, NavLogo, NavMenu, and NavLinks, Kindly check.

Resources