How can I highlight the active selection on a react navigation bar? - reactjs

New to react and trying to figure out how to highlight the active selection on the navbar. In addition to this, I would also like to highlight the "Home" bar as this is the default route once you load the page. I've seen some other people use NavLink instead of Link as well: is this something I should consider? Thanks in advance.
This is App.js
function App() {
return (
<>
<div id="root">
<Router>
<Navbar />
<Routes>
<Route path='/' element={<Home/>} />
<Route path='/projects' element={<Projects/>}/>
<Route path='/about-me' element={<AboutMe/>}/>
<Route path='/contact' element={<Contact/>}/>
</Routes>
</Router>
</div>
</>
);
}
export default App;
This is my navigation bar component:
const Navbar= () =>{
return (
<div>
<ul>
<li>
<a>
<Link className="navbar-click" to="/">Home</Link>
</a>
</li>
<li>
<a>
<Link className="navbar-click" to="/projects">Projects</Link>
</a>
</li>
<li>
<a>
<Link className="navbar-click" to="/about-me">About Me</Link>
</a>
</li>
<li>
<a>
<Link className="navbar-click" to="/contact">Contact</Link>
</a>
</li>
</ul>
</div>
);
}
export default Navbar;
And this is my CSS:
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 220px;
background-color: #2B2D42;
height: 100vh;
position: fixed;
overflow: auto;
}
.navbar-click {
margin: 0;
padding: 25px 0px;
}
li {
text-align: center;
}
li a {
display: block;
color: #fff;
text-decoration: none;
font-size: 20px;
}
li a:hover {
background-color: #202130;
color: white;
}
Link.active {
color: #202130;
}

You can use the hook from react-router-dom, useLocation.
const location = useLocation();
Then you can see what route you are on, even in the navigation bar, and there you can work with that.

Related

I’m building a portfolio but the sidebar component is not showing on any of the pages?

The sidebar is not showing on the home page of my portfolio or on any other pages like it’s supposed to be. Im wondering if it’s the CSS or something deeper. I assume the relevant code to this issue is of course the Sidebar component, App.js, and maybe Layout component and the scss file for the sidebar. Does anyone have any advice on how to narrow down as to which few files are likely the problem as it feels like it could be any of the components or their scss is not working together. Anyone know how to fix the sidebar not showing?
Sidebare index.js --->
import './index.scss'
import LogoS from '../../assets/images/logo-s.png'
import LogoSubtitle from '../../assets/images/logo_sub.png'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import {
faLinkedin,
faGithub,
faYoutube,
faSkype,
} from '#fortawesome/free-brands-svg-icons'
import { faHome, faUser, faEnvelope, faSuitcase } from '#fortawesome/free-solid-svg-icons'
import { Link, NavLink } from 'react-router-dom'
const Sidebar = () => {
return (
<div className="nav-bar">
<Link className="logo" to="/">
<img src={LogoS} alt="Logo" />
<img className="sub-logo" src={LogoSubtitle} alt="slobodan" />
</Link>
<nav>
<NavLink exact="true" activeclassname="active" to="/">
<FontAwesomeIcon icon={faHome} color="#4d4d4e" />
</NavLink>
<NavLink activeclassname="active" className="about-link" to="/about">
<FontAwesomeIcon icon={faUser} color="#4d4d4e" />
</NavLink>
<NavLink activeclassname="active" className="portfolio-link" to="/portfolio">
<FontAwesomeIcon icon={faSuitcase} color="#4d4d4e" />
</NavLink>
<NavLink
activeclassname="active"
className="contact-link"
to="/contact"
>
<FontAwesomeIcon icon={faEnvelope} color="#4d4d4e" />
</NavLink>
</nav>
<ul>
<li>
<a
href="https://www.linkedin.com/in/slobodan-gaji%C4%87-006bb8b8/"
target="_blank"
rel="noreferrer"
>
<FontAwesomeIcon icon={faLinkedin} color="#4d4d4e" />
</a>
</li>
<li>
<a
href="https://github.com/bobangajicsm"
target="_blank"
rel="noreferrer"
>
<FontAwesomeIcon icon={faGithub} color="#4d4d4e" />
</a>
</li>
<li>
<a
href="https://www.youtube.com/channel/UCBu5ulO4d-d47lAVybpRTkw"
rel="noreferrer"
target="_blank"
>
<FontAwesomeIcon icon={faYoutube} color="#4d4d4e" />
</a>
</li>
<li>
<a href="skype:live:bobangajicsm" rel="noreferrer" target="_blank">
<FontAwesomeIcon icon={faSkype} color="#4d4d4e" />
</a>
</li>
</ul>
</div>
)
}
export default Sidebar
Sidebare scss -->
.nav-bar {
background: #181818;
width: 60px;
height: 100%;
position: absolute;
top: 0;
z-index: 3;
min-height: 500px;
.logo {
display: block;
padding: 8px 0;
img {
display: block;
margin: 8px auto;
width: 24px;
height: auto;
&.sub-logo {
width: 50px;
}
}
}
nav {
display: block;
text-align: center;
position: absolute;
height: 210px;
top: 50%;
margin-top: -120px;
width: 100%;
a {
font-size: 22px;
color: #4d4d4e;
display: block;
line-height: 51px;
height: 51px;
position: relative;
text-decoration: none;
i {
transition: all 0.3s ease-out;
}
&:hover {
color: #ffd700;
svg {
opacity: 0;
}
&:after {
opacity: 1;
}
}
&:after {
content: '';
font-size: 9px;
letter-spacing: 2px;
position: absolute;
bottom: 0;
display: block;
width: 100%;
text-align: center;
opacity: 0;
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
&:first-child {
&:after {
content: 'HOME';
}
}
}
a.about-link {
&:after {
content: 'ABOUT';
}
}
a.contact-link {
&:after {
content: 'CONTACT';
}
}
a.portfolio-link {
&:after {
content: 'PORTFOLIO';
}
}
a.active {
svg {
color: #ffd700;
}
}
}
ul {
position: absolute;
bottom: 20px;
width: 100%;
display: block;
padding: 0;
list-style: none;
text-align: center;
margin: 0;
li {
a {
padding: 7px 0;
display: block;
font-size: 15px;
line-height: 16px;
color: #4d4d4e;
&:hover {
color: #ffd700;
}
}
}
}
}
App.js --->
import { Route, Routes } from 'react-router-dom'
import Home from './components/Home'
import About from './components/About'
import Contact from './components/Contact'
import Layout from './components/Layout'
import Portfolio from './components/Portfolio'
import Dashboard from './components/Dashboard'
import './App.scss'
function App() {
return (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="/contact" element={<Contact />} />
<Route path="/portfolio" element={<Portfolio />} />
<Route path="/dashboard" element={<Dashboard />} />
</Route>
</Routes>
</>
)
}
export default App
Layout index.js -->
import { Outlet } from 'react-router-dom'
import Sidebar from '../Sidebar/'
import './index.scss'
const Layout = () => {
return (
<div className="App">
<Sidebar />
<div className="page">
<span className="tags top-tags"><body></span>
<Outlet />
<span className="tags bottom-tags">
</body>
<br />
<span className="bottom-tag-html"></html></span>
</span>
</div>
</div>
)
}
export default Layout
Thank you in advance any help is greatly appreciated!!

CSS modules are "undefined" on navigation bar that uses react-router-dom

I'm making a react app that had a successful navigation bar built on react-router. I used CSS modules to compartmentalize the styling which helped. I messed around with React-PDF and after having removed it from my code, my navigation bar now no longer displays properly. It only displays one link and on dev tools, the CSS for the navigation bar and components within it are "undefined". I'd like to know how to revert this back to normal.
Below is my code.
Nav.js
import React from 'react';
import { BrowserRouter as NavLink } from 'react-router-dom';
import navstyles from './navstyles.module.css';
function Nav() {
return (
<div>
<nav className={`${navstyles.navbar}`}>
<h2 className={`${navstyles.navboxhome}`}>
<NavLink
exact to="/"
activeStyle={{
fontWeight: "bold",
color: "blue"
}}
>
Home
</NavLink>
</h2>
<h2 className={`${navstyles.navboxstories}`}>
<NavLink
exact to="/stories"
activeStyle={{
fontWeight: "bold",
color: "blue"
}}
>
Stories
</NavLink>
</h2>
<h2 className={`${navstyles.navboxcontact}`}>
<NavLink
exact to="/contact"
activeStyle={{
fontWeight: "bold",
color: "blue"
}}
>
Contact
</NavLink>
</h2>
</nav>
</div>
);
};
export default Nav;
navstyles.module.css
.navbar {
display: grid;
width: 100%;
height: 10vh;
grid-template-columns: repeat(3, 1fr);
}
[class^="navbox"] {
background-color: greenyellow;
border-radius: 1px;
border-color: black;
border: 2px;
border-style: solid;
display: grid;
/* To place the letter at the center */
place-items: center;
}
.navboxhome {
background-color: skyblue;
display: grid;
place-items: center;
}
.navboxstories {
background-color: skyblue;
display: grid;
place-items: center;
}
.navboxcontact {
background-color: skyblue;
display: grid;
place-items: center;
}
App.js
import './App.css';
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Nav from './Components/Nav';
import Footer from './Components/Footer';
import Stories from './Pages/Stories';
import Contact from './Pages/Contact';
function App() {
return (
<Router>
<div className="container">
<div className="navigation">
<Nav />
</div>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/stories" exact component={Stories} />
<Route path="/contact" exact component={Contact} />
</Switch>
<div className="bottom">
<Footer />
</div>
</div>
</Router>
);
}
const Home = () => (
<div className="subcontainer">
<div className="box-2">B</div>
<div className="box-3">C</div>
<div className="box-4">D</div>
<div className="box-5">E</div>
<div className="box-6">F</div>
</div>
);
export default App;
In nav.js, replace the
import { BrowserRouter as NavLink } from 'react-router-dom';
with
import { NavLink } from 'react-router-dom';
Because BrowserRouter is not used to create a link, instead it is used to wrap your application so that you can use navigation component in react-routed-dom (eg. NavLink or Link).
https://reactrouter.com/web/api/BrowserRouter

footer page end distance

I added a footer to my page. When it is a page length it covers the content.
There should be a distance between the end of the page and the footer.
It's ok when the page is 100 percent, but it happens when the page gets longer
But I want a distance between.This is scss.
.footer {
display: flex;
background-color: #222831;
min-height: 5vh;
justify-content: space-between;
padding: 10px;
align-items: center;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
This is my layout
import React from "react";
import Footer from "./footer";
import Header from "./header";
const Layout = ({ children }) => {
return (
<div className="layout">
<Header />
<div className="children">{children}</div>
<Footer></Footer>
</div>
);
};
export default Layout;
This is my footer
import {
AiFillLinkedin,
AiFillGithub,
AiFillInstagram,
AiOutlineMail,
} from "react-icons/ai";
export default function Footer() {
return (
<div className="footer">
<div className="logo">
<span className="bracket">{"<"}</span>
<p>Handcrafted by</p>
<span className="bracket">{"{"}</span>
<span className="name">{"ömer"}</span>
<span className="bracket">{"}"}</span>
<span className="bracket">{"/>"}</span>
</div>
<div className="icons">
<a href="https://www.linkedin.com/in/omersahin1/">
<AiFillLinkedin className="icon" size={20} />
</a>
<a href="https://github.com/1sahinomer1">
<AiFillGithub className="icon" size={20} />
</a>
<a href="https://github.com/shnomr">
<AiFillInstagram className="icon" size={20} />
</a>
<a href="mailto:1sahinomer1#gmail.com">
<AiOutlineMail className="icon" size={20} />
</a>
</div>
</div>
);
}
Thank you.

React bootstrap custom nav link active style is not working

Actually I want to make an active style of tab menu as hover style.
Even I tried with activeClassName="selected" not working. Can anyone help the issue which I am trying?
CSS which I am using
.my-navbar{
color: #037979;
font-size: 16px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
line-height: 1.6rem;
text-decoration: none;
margin: 10px 10px 10px 10px;
}
.my-navbar:visited {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 16px;
}
.my-navbar:hover {
text-decoration: none;
font-family: Verdana, Geneva, Tahoma, sans-serif;
color: #c10944;
font-size: 16px;
background-color: #61dafb;
}
.active {
font-family: Verdana, Geneva, Tahoma, sans-serif;
color: #c10944;
font-size: 16px;
background-color: #61dafb;
}
react-bootstrap, react-router-dom these are the import using
<Router>
{/*header Menu Bar*/}
<div className="mycontainer">
<Navbar bg="transparent" expand="lg">
<Navbar.Brand>
<div>
<img src={logo} alt="logo" width="200" />
{/* <img src={process.env.PUBLIC_URL + "./images/logo.png"} alt="mypic" width="200" /> */}
</div>
</Navbar.Brand>
<Navbar.Toggle className="border-0" aria-controls="navbar-toggle" />
<Navbar.Collapse id="navbar-toggle" style={{marginTop: 30}}>
<Nav className="ml-auto" bsStyle="tabs" >
<Link activeClassName="selected" className="my-navbar" to="/">Home</Link>
<Link activeClassName="selected" className="my-navbar" to="/about">About</Link>
<Link className="my-navbar" to="/services">Services</Link>
<Link className="my-navbar" to="/contact">Contact</Link>
<Link className="my-navbar" to="/galleries">Galleries</Link>
</Nav>
</Navbar.Collapse>
</Navbar>
</div>
{/*Slider Bar*/}
<Slidingmain />
<div style={{ paddingTop: 50 }}>
<Route defaultActiveKey="/home" path="/" exact render={() => <HomePage title={this.state.home.title} subTitle={this.state.home.subTitle} text={this.state.home.text} />} className="container-full"/>
<Route path="/about" render={() => <AboutPage title={this.state.about.title} subTitle={this.state.about.subTitle} text={this.state.about.text} />} />
<Route path="/services" render={() => <ServicesPage title={this.state.services.title} />} />
<Route path="/contact" render={() => <ContactPage title={this.state.contact.title} />} />
<Route path="/galleries" render={() => <GalleriesPage title={this.state.galleries.title} />} />
<Footer />
</div>
</Router >
Please use <NavLink activeClassName="active"> instead of <Link activeClassName="selected">.
import { NavLink } from 'react-router';
For more information, visit https://reacttraining.com/react-router/web/api/NavLink
If using react-bootstrap Nav.Link you can use
defaultActiveKey like this:
<Nav defaultActiveKey="#home" className="mr-auto">
<Nav.Link href="#home">Home</Nav.Link>

Next js font awesome and google fonts

Hoping to get some help on an issue I have been having in Next js. I am using Fontawesome-react package for icon imports which is working fine but when I attempt to load in a google font style sheet into the head of my main component, it changes the fonts but makes the font awesome icons disappear. I have tried a couple of different solutions such as loading the font locally and utilizing _document and _app components but none fix the issue I am having. Everything ends up changing the font but still gets rid of all the font awesome icons from every part of my app. I will copy in the Nav component I have and the Layout component that holds everything.
Thank you for any help!
import React from 'react'
import Nav from './nav/Nav'
import Head from 'next/head'
import Footer from './Footer'
type Props = {
title?: string
}
const Layout: React.FC<Props> = ({ children, title = 'Macros' }) => {
return (
<div>
<Head>
<title>{title}</title>
<link
href="https://fonts.googleapis.com/css?family=Darker+Grotesque&display=swap"
rel="stylesheet"
/>
</Head>
<Nav />
<div id="pageContent">{children}</div>
<Footer />
<style jsx global>{`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h1,
h2,
h3,
h4,
h5 {
font-weight: lighter;
font-family: 'Darker Grotesque', sans-serif;
}
#pageContent {
padding: 8rem 1rem;
}
`}</style>
</div>
)
}
export default Layout
import React, { useState } from 'react'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faBars, faSearch, faPlus } from '#fortawesome/free-solid-svg-icons'
import Link from 'next/link'
const NavBar: React.FC = () => {
const [navBarStatus, isOpen] = useState<Boolean>(false)
const toggleMenu = () => {
isOpen(navBarStatus ? false : true)
}
return (
<nav id="navContainer">
<div id="mobileNav">
<ul id="navBarTop">
<li>
<button type="button" className="navIcon" onClick={toggleMenu}>
<FontAwesomeIcon icon={faBars} />
</button>
</li>
<li>
<Link href="/">
<a>
<h3>Macro</h3>
</a>
</Link>
</li>
<li>
<button type="button" className="navIcon">
<FontAwesomeIcon icon={faSearch} />
</button>
</li>
</ul>
<ul id="navbarBottom">
<li>Top</li>
<li>New</li>
<li>Protein</li>
<li>Carbs</li>
<li>Fats</li>
</ul>
</div>
<div id="appDrawer">
<ul id="topIcons">
<li>
<button type="button" className="navIcon" onClick={toggleMenu}>
<FontAwesomeIcon icon={faBars} />
</button>
</li>
<li>
<button type="button" className="navIcon">
<FontAwesomeIcon icon={faPlus} />
</button>
</li>
</ul>
<nav>
<ul id="menuList">
<Link href="/">
<a>
<p>Home</p>
</a>
</Link>
<Link href="/">
<a>Favorites</a>
</Link>
<Link href="/">
<a>Notifications</a>
</Link>
<Link href="/">
<a>Login/Sign up</a>
</Link>
</ul>
</nav>
</div>
<style jsx>{`
#navContainer {
background-color: #504761;
color: white;
position: fixed;
width: 100%;
}
#navContainer a {
text-decoration: none;
color: white;
}
#mobileNav {
display: ${navBarStatus ? 'none' : ''};
}
#navBarTop {
color: white;
display: flex;
justify-content: space-between;
list-style: none;
padding: 1rem 1rem 0.5rem 1rem;
font-size: 1.5rem;
}
.navIcon {
color: white;
border: none;
background-color: #504761;
font-size: 1.5rem;
}
#navbarBottom {
display: flex;
list-style: none;
justify-content: space-evenly;
padding: 1rem 0;
}
#appDrawer {
display: ${navBarStatus ? 'block' : 'none'};
background-color: #504761;
height: 100vh;
}
#topIcons {
display: flex;
justify-content: space-between;
list-style: none;
padding: 1rem;
font-size: 1.5rem;
}
#menuList {
padding: 2rem 0 2rem 1rem;
display: flex;
flex-direction: column;
}
#menuList a {
font-size: 2rem;
margin: 1rem 0;
}
`}</style>
</nav>
)
}
export default NavBar

Resources