React bootstrap custom nav link active style is not working - reactjs

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>

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!!

using react-router-dom Link with styled components renders nothing

I am using styled-components for the first time and facing some issue while using Link form react-router-dom v6.
Nothing is rendered with the current code but when I remove the Link tag code works fine. Please help.
import styled from "styled-components";
import { Link } from "react-router-dom";
const Home = () => {
return (
<Link to="/">
<HomeButton>Place Order</HomeButton>
</Link>
);
};
const HomeButton = styled.button`
color: black;
font-weight: bold;
font-family: "Space Grotesk", sans-serif;
font-size: 1.2rem;
background-color: white;
height: 45px;
border-radius: 5px;
width: 86%;
margin-left: 7%;
margin-right: 7%;
margin-top: 20px;
-webkit-box-shadow: -1px 0px 2px 2px rgba(99, 99, 99, 1);
`;
export default Home;
react router dom v6 requires your navabar component and <Route path= element={}/> if you need to use link.
<Router >
<Navbar/>
<Routes>
<Route path="/" element={<div><h1>Home Page</h1></div>}/>
<Route path="register" element={<RegisterPage />} />
<Route path="login" element={<LoginPage />} />
</Routes>
</Router>

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

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.

How do I use NavHashLink to smooth scroll through my pages?

I want to have all my pages on one page that i could scroll through and when i press the button on the navbar, it scrolls to that section.
In my App JS I have the following normal setup
<Switch>
<Router>
<Sidebar />
<Route path='/Home' component={Home} />
<Route path='/Education' component={Education} />
<Route path='/Skills' component={Skills} />
<Route path='/Experience' component={Experience} />
<Route path='/Projects' component={Projects} />
</Switch>
</Router>
I am using as my navigation, inside sidebar we have
import React, { useState } from "react";
import { NavHashLink } from "react-router-hash-link";
const Container = styled.div`
position: fixed;
z-index: 100;
top: 50%;
transform: translateY(-50%);
.active {
border-right: 4px solid var(--white);
border-radius: 0 30px 30px 0;
img {
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(93deg)
brightness(103%) contrast(103%);
}
}
`;
const SlickBar = styled.ul`
color: var(--white);
list-style: none;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--black);
top: 10rem;
left: 0;
width: ${(props) => (props.clicked ? "14rem" : "3.5rem")};
transition: all 0.5s ease;
border-radius: 0 30px 30px 0;
`;
const Item = styled(NavHashLink)`
text-decoration: none;
color: var(--white);
width: 100%;
padding: 1rem 0;
cursor: pointer;
display: flex;
padding-left: 1rem;
&:hover {
border-right: 4px solid var(--white);
border-radius: 0 30px 30px 0;
img {
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(93deg)
brightness(103%) contrast(103%);
}
}
img {
width: 1.2rem;
height: auto;
filter: invert(92%) sepia(4%) saturate(1033%) hue-rotate(169deg)
brightness(78%) contrast(85%);
}
`;
const Text = styled.span`
width: ${(props) => (props.clicked ? "100%" : "0")};
overflow: hidden;
margin-left: ${(props) => (props.clicked ? "1.5rem" : "0")};
transition: all 0.3s ease;
`;
const Sidebar = () => {
const [click, setClick] = useState(false);
return (
<Container>
<SlickBar
onMouseEnter={() => setClick(true)}
onMouseLeave={() => setClick(false)}
clicked={click}>
<Item
onClick={() => setClick(false)}
exact
activeClassName="active"
to="/Home"
>
<img src={Home} alt="Home" />
<Text clicked={click}>DashBoard</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Education"
>
<img src={Education} alt="Education" />
<Text clicked={click}>Education</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Skills"
>
<img src={Skills} alt="Skills" />
<Text clicked={click}>Skills</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Experience"
>
<img src={Experience} alt="Experience" />
<Text clicked={click}>Experience</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Projects"
>
<img src={Projects} alt="Projects" />
<Text clicked={click}>Projects</Text>
</Item>
</SlickBar>
</Container>
);
};
After watching many tutorial ands trying different things nothing seemed to work.

react-bootstrap flex/ justify-content is not working

I'm doing instagram clonecoding with react, react-bootstrap, scss
Navbar was created, but it is not centered with flex box.
How can I solved this problem?
why flex center is not working?
And how do you typically customize in react-bootstrap?
Header.js
<Navbar className="navbarContainer align-items-center" fixed="top">
<div className="nav">
<Navbar.Brand href="/">
<img src={insta_logo} alt="instagram_logo" />
</Navbar.Brand>
<Form>
<FormControl
type="text"
placeholder="Search"
className="searchBar"
/>
</Form>
<Nav
navbar="false"
className="flex-row navRight"
>
<Nav.Link href="/">
<Home fontSize="24px" />
</Nav.Link>
<Nav.Link href="/message">
<Send fontSize="22px" />
</Nav.Link>
<Nav.Link href="/explore">
<Compass fontSize="24px" />
</Nav.Link>
<Nav.Link href="/favorite">
<Heart fontSize="24px" />
</Nav.Link>
<NavDropdown
title={
<Image
src={face}
roundedCircle
className="profileImg"
></Image>
}
drop="left"
>
<NavDropdown.Item href="#action/3.1" className="dropdownItem">
<Profile className="marginRight" />
<span>Profile</span>
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.2" className="dropdownItem">
<Bookmark className="marginRight" />
<span>Bookmark</span>
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3" className="dropdownItem">
<Setting className="marginRight" />
<span>Setting</span>
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.4" className="dropdownItem">
<Switch className="marginRight" />
<span>Switch Account</span>
</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.4">Log Out</NavDropdown.Item>
</NavDropdown>
</Nav>
</div>
</Navbar>
header.scss
#mixin flex($justify: null, $align: null) {
display: flex;
justify-content: $justify;
align-items: $align;
}
.navbarContainer {
#include flex(center);
border-bottom: 1px solid #dbdbdb;
z-index: 200;
background-color: #fff;
.nav {
width: 940px;
display: flex;
justify-content: space-between;
align-items: center;
}
.searchBar {
height: 30px;
width: 220px;
background-color: #fafafa;
}
.navRight {
width: 225px;
display: flex;
justify-content: space-between;
}
.profileImg {
width: 25px;
height: 25px;
}
.dropdownItem {
#include flex(null, center);
.marginRight {
margin-right: 8px;
}
}
}
screen
enter image description here
I used a translator because I am not good at English. Please understand if the sentence is not good.
thank you
I found bootstrap class
Bootstrap can make layout using class.
https://getbootstrap.com/docs/4.0/utilities/flex/#enable-flex-behaviors
I modified the code
<Navbar
className="d-flex justify-content-center navbarContainer"
fixed="top"
>

Resources