How do I flex the contents of my navbar when I toggle the sidebar on/off in ReactJS? In my current website my sidebar overlaps the navbar so when I toggle it on, more than half of the title gets covered (sample1, sample2)
This is my Navbar.js
import React, { useState } from 'react'
import {Navbar, Container} from 'react-bootstrap'
import { Link } from 'react-router-dom';
import { useAuth } from "../contexts/AuthContext"
import './styles/styles.css';
import * as FaIcons from 'react-icons/fa';
import * as AiIcons from 'react-icons/ai';
import { IconContext } from 'react-icons';
import { SidebarItem } from './SidebarItem';
export default function Navubaru({component: Component, ...rest}) {
const { currentUser } = useAuth()
const [sidebar, setSidebar] = useState(false);
const showSidebar = () => setSidebar(!sidebar);
return (
<div>
<Navbar bg="myColor" variant="dark">
<IconContext.Provider value={{ color: '#fff' }}>
<Link to='#' className='menu-bars'>
<FaIcons.FaBars onClick={showSidebar} />
</Link>
<nav className={sidebar ? 'nav-menu active' : 'nav-menu'}>
<ul className='nav-menu-items' onClick={showSidebar}>
<li className='navbar-toggle'>
<Link to='#' className='menu-bars'>
<AiIcons.AiOutlineClose />
</Link>
</li>
{SidebarItem.map((item, index) => {
return (
<li key={index} className={item.cName}>
<Link to={item.path}>
{item.icon}
<span>{item.title}</span>
</Link>
</li>
);
})}
</ul>
</nav>
</IconContext.Provider>
<Container>
<Navbar.Brand href="/">Welcome to my Website</Navbar.Brand>
<Navbar.Toggle />
<Navbar.Collapse className="justify-content-end">
<Navbar.Text>
Signed in as: {currentUser && currentUser.email}
</Navbar.Text>
</Navbar.Collapse>
</Container>
</Navbar>
</div>
)
}
I'd also like to flex/minimize the size of the main page when I toggle the sidebar if there's not much difference in the methods of doing it. I'd appreciate any help and thoughts given. Thank you
You can set padding-left to the navbar's wrapper when sidebar is open and remove it when the sidebar close.
Here is an example navbar.
I created a web application in Angular in which i have components, the login component and register component. Each time i add the element routerLink to the <a...> in order for me to navigate from one component to another, it doesn't work. It turns the text or nav item into a read-only text.
The funny thing is that if i change it from "routerLink" to "href", the text/nav item becomes active. I really don't understand why this is happening. I have followed every suggestion made in this forum similar to this but none actually work when i tried them. I also followed suggestions written on the Angular.io website. It is still the same. i also imported RouterModule into my app.module.ts. It hasn't worked as we speak. I am using Angular/CLI 12.2.0, Bootstrap 5, and Typescript 4. Below is my code
<!-- Navbar-->
<nav
class="
navbar navbar-transparent navbar-color-on-scroll
fixed-top
navbar-expand-lg
"
color-on-scroll="100"
id="sectionsNav"
>
<div class="container-fluid">
<div class="navbar-translate">
<a routerLink="#" class="navbar-brand">
<img
src="../../../assets/img/comgreen.png"
width="50"
height="50"
alt=""
class="d-inline-block"
/>
<strong>Comgreen</strong></a
>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbar"
aria-controls="navbar"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="sr-only">Toggle navigation</span>
<span class="navbar-toggler-icon"></span>
<span class="navbar-toggler-icon"></span>
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a
routerLink="/login"
routerLinkActive="active"
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
target=""
data-original-title="Log in with your credentials"
>
<i class="material-icons">fingerprint</i>Login
</a>
</li>
<li class="nav-item">
<a
routerLink="/user-register"
routerLinkActive="active"
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
target=""
data-original-title="Don't have an account? Register"
>
<i class="fa fa-user-plus"></i>Register
</a>
</li>
<li class="nav-item">
<a
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
target="_blank"
data-original-title="Follow us on Twitter"
rel="nofollow"
>
<i class="fa fa-twitter"></i>
</a>
</li>
<li class="nav-item">
<a
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
routerLink="/hero"
target="_blank"
data-original-title="Like us on Facebook"
rel="nofollow"
>
<i class="fa fa-facebook-square"></i>
</a>
</li>
<li class="nav-item">
<a
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
routerLink="/hero"
target="_blank"
data-original-title="Follow us on Instagram"
rel="nofollow"
>
<i class="fa fa-instagram"></i>
</a>
</li>
</ul>
</div>
</div>
</nav>
<!--End of Navbar-->
My app.module.ts file:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { HttpClientModule } from '#angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {RouterModule } from '#angular/router';
import { FooterComponent } from './shared/footer/footer.component';
import { MatFormFieldModule } from '#angular/material/form-field';
import { MatInputModule } from '#angular/material/input';
import { MatButtonModule } from '#angular/material/button';
import { MatCheckboxModule } from '#angular/material/checkbox';
import { MatMenuModule } from '#angular/material/menu';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { SidebarComponent } from './sidebar/sidebar.component';
import { NavbarComponent } from './shared/navbar/navbar.component';
import { MsgiconbtnComponent } from './shared/msgiconbtn/msgiconbtn.component';
import { SettingsService } from './services/settings.service';
import { HomeComponent } from './dashboard/home/home.component';
import { CropComponent } from './dashboard/crop/crop.component';
import { FarmersComponent } from './dashboard/farmers/farmers.component';
import { InsuranceComponent } from './dashboard/insurance/insurance.component';
import { ImagecardComponent } from './shared/imagecard/imagecard.component';
import { AddNewCropDialogModule } from './shared/dialog/add-new-crop-dialog/add-new-crop-dialog.module';
import { AddNewFarmerDialogModule } from './shared/dialog/add-new-farmer-dialog/add-new-farmer-dialog.module';
import { AngularFirestoreModule } from '#angular/fire/firestore';
import { AngularFireAuthModule } from '#angular/fire/auth';
import { AngularFireModule } from '#angular/fire';
import { environment } from 'src/environments/environment';
import { AngularFireStorageModule } from '#angular/fire/storage';
import { MorecropinfoModule } from './shared/dialog/morecropinfo/morecropinfo.module';
import { UpdateCropInfoModule } from './shared/dialog/update-crop-info/update-crop-info.module';
import { SplashComponent } from './pages/splash/splash.component';
import { HeaderComponent } from './shared/header/header.component';
import { ReactiveFormsModule } from '#angular/forms';
import { userRegisterComponent } from './pages/userregister/userRegister.component';
import { VerifyEmailComponent } from './pages/verify-email/verify-email.component';
import { ForgotPasswordComponent } from './pages/forgot-password/forgot-password.component';
import { LoginComponent } from './pages/login/login.component';
import { RegisterUserComponent } from './pages/register-user/register-user.component';
#NgModule({
declarations: [
AppComponent,
HeaderComponent,
FooterComponent,
SidebarComponent,
NavbarComponent,
MsgiconbtnComponent,
HomeComponent,
CropComponent,
FarmersComponent,
InsuranceComponent,
ImagecardComponent,
SplashComponent,
userRegisterComponent,
LoginComponent,
VerifyEmailComponent,
ForgotPasswordComponent,
userRegisterComponent,
RegisterUserComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
MatFormFieldModule,
RouterModule,
MatInputModule,
MatButtonModule,
MatCheckboxModule,
MatMenuModule,
HttpClientModule,
MorecropinfoModule,
AddNewCropDialogModule,
AddNewFarmerDialogModule,
UpdateCropInfoModule,
BrowserAnimationsModule,
AngularFirestoreModule,
AngularFireAuthModule,
AngularFireStorageModule,
AngularFireModule.initializeApp(environment.firebaseConfig)
],
providers: [SettingsService],
bootstrap: [AppComponent]
})
export class AppModule { }
Where am i going wrong?
I don't know if it would make a difference but in my project I moved the routerLinkActive = "active" to the html li tag.
Maybe it does also work for you...
<li class="nav-item"
routerLinkActive="active">
<a
routerLink="/login"
class="nav-link"
rel="tooltip"
title=""
data-placement="bottom"
target=""
data-original-title="Log in with your credentials"
>
<i class="material-icons">fingerprint</i>Login
</a>
</li>
Try with square brackets, just like it was array of path elements:
<a [routerLink]="['/user-register']">Link</a>
Then if you want deeper it would look like this:
// const userId = '12345';
<a [routerLink]="['/user', 'profile', userId]">Link</a>
This will redirect you to localhost:4200/user/profile/12345
And if we are talking about style of link, like underline/color change on hover, just use some css:
a {
color: black;
cursor: pointer;
}
a:hover {
color: red;
text-decoration: underline;
} // ...etc whatever you need
I want the highlight effect when I'm on the current page. For example, if I'm on the About Page, I woudl like About to be highlighted in the navbar.
How would I do that?
Relevant Code
NavBar.js
function NavBar() {
const [open, setOpen] = useState(false);
NavBar.handleClickOutside = () => setOpen(false);
return(
<nav>
<div className='navbar-container'>
<a href='/' className='logo'>Daniel Zhang</a>
<div className='toggle-button' onClick={() => setOpen(!open)}>
<div className='bar' />
<div className='bar' />
<div className='bar' />
</div>
</div>
<div id='nav-links' className={open ? 'nav-open' : 'nav-collasped'}>
<a href='/'>Home</a>
<a href='/about'>About</a>
{/* <a href='/blog'>Blog</a> */}
<a href='/contact'>Contact</a>
</div>
</nav>
);
}
const clickOutsideConfig = {
handleClickOutside: () => NavBar.handleClickOutside,
};
export default onClickOutside(NavBar, clickOutsideConfig);
You could use the NavLink component from react router dom. Import like:
import { NavLink } from 'react-router-dom';
And instead of <a href='/about'>About</a> you can write:
<NavLink exact to='/about' activeClassName="highlighted">About</Navlink>
And so on for the other links present within the <div id='nav-links'.
Of course you can add the highlighted styles based on the .highlighted
More info, see https://reactrouter.com/web/api/NavLink
Here I'm Doing Angular2 Routing When i Load my Links in nva-bar Under same Html Page is also Load under please Help me How can Separate These two
<nav>
<li> <a [routerLink]="['Employee/Add']" class="btn btn-primary btn-sm">Employee</a></li>
<li> <a [routerLink]="['HrModule/Add']" class="btn btn-primary btn-sm">HrModule</a></li>
</nav>
<div>
<router-outlet>
</router-outlet>
</div>
AppComponent.ts
import { Component } from "#angular/core"
#Component({
selector:"customer-ui",
templateUrl:"../../templates/Master.html"
})
export class AppComponent {
}
I'm trying to make my menu load according to the click on the "Cadastros" action but I believe the problem is the "workspaceSelected" property that is not visible to all components.
I believe the correct location for this case would be the property "workspaceSelected" being in the component sidenav, but I do not know how to handle this type of situation.
I would like to click on the action "Cadastros" and she make the switch to load the correct html component.
principal component class:
export class PrincipalComponent implements OnInit {
workspaceSelecionada: string;
constructor() { }
ngOnInit() {
}
}
Principal component:
<header>
<app-navbar></app-navbar>
<app-sidenav></app-sidenav>
</header>
<div class="container" [ngSwitch]="workspaceSelecionada">
<app-cadastros *ngSwitchCase="'cadastros'"></app-cadastros>
<app-movimentacoes *ngSwitchCase="'movimentacoes'"></app-movimentacoes>
<app-administracao *ngSwitchCase="'administracao'"></app-administracao>
<app-relatorios *ngSwitchCase="'relatorios'"></app-relatorios>
<app-configuracoes *ngSwitchCase="'configuracoes'"></app-configuracoes>
<app-dashboard *ngSwitchDefault></app-dashboard>
</div>
app-sidenav:
<!--SideNav-->
<ul id="slide-out" class="side-nav grey darken-2">
<li>
<div class="userView">
<div class="background">
<img src="images/office.jpg">
</div>
<img class="circle" src="images/yuna.jpg">
<span class="white-text name">John Doe</span>
<span class="white-text email">jdandturk#gmail.com</span>
</div>
</li>
<li>
<a (click)="workspaceSelecionada = 'cadastros'" class="btn-large waves-effect waves-light indigo darken-3">CADASTROS</a>
</li>
<li>
<a class="btn-large waves-effect waves-light purple darken-3">MOVIMENTAÇÕES</a>
</li>
<li>
<a class="btn-large waves-effect waves-light orange darken-3">ADMINISTRAÇÃO</a>
</li>
<li>
<a class="btn-large waves-effect waves-light yellow darken-3">RELATÓRIOS</a>
</li>
<li>
<a class="btn-large waves-effect waves-light light-green darken-3">CONFIGURAÇÕES</a>
</li>
</ul>
<a data-activates="slide-out" class="button-collapse"><i class="material-icons">menu</i></a>
As suggested above in the comment, define a routing module for the application:
import { NgModule } from '#angular/core';
import { Routes, RouterModule } from '#angular/router';
import {CadastrosComponent} from './...component';
....
const appRoutes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'cadastros', component: CadastrosComponent}
];
#NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
Then import the routing module in your app module:
imports: [AppRoutingModule]
Then instead of switch case statement in your principal component, define the router outlet as below:
<router-outlet></router-outlet>
Then in the anchor tag in your side nav component template, define the router link as below:
<a routerLink="/cadastros">CADASTROS</a>
Hope this helps! Do let me know if you face any challenges implementing it.