import React from 'react'
import {Link} from "react-router-dom"
const Navbar = () => {
return (
<div className="navbar-dark bg-dark shadow sticky-top">
<div className="container">
<nav className="navbar navbar-expand-lg">
<div className="container-fluid">
<Link to="/" className="navbar-brand"><span className="effect">I <span className="rotation">Design</span> <span className="flash">Views</span></span>
</Link>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav m-auto p-3 mb-2 mb-lg-0">
<li className="nav-item">
<Link to="/" className="nav-link active">Inicio</Link>
</li>
<li className="nav-item">
<Link to="/about" className="nav-link active">Bio</Link>
</li>
<li className="nav-item">
<Link to="/servicios" className="nav-link active">Servicios</Link>
</li>
<li className="nav-item">
<Link to="/contacto" className="nav-link active">Contacto</Link>
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
)
}
export default Navbar
The previous versions of the bootstrap menu came by default to close the menu when clicking, now that I update I don't really know how to do it with react js, if anyone can help me I would appreciate it
You might have missed out adding the bootstrap's js resource. Try adding the below in your index.html.
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
I have used your code as it is, by just adding the bootstrap js resource it's working as expected. Below is the example,
const Navbar = () => {
return (
<div className="navbar-dark bg-dark shadow sticky-top">
<div className="container">
<nav className="navbar navbar-expand-lg">
<div className="container-fluid">
<a href="/" className="navbar-brand"><span className="effect">I <span className="rotation">Design</span> <span className="flash">Views</span></span>
</a>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav m-auto p-3 mb-2 mb-lg-0">
<li className="nav-item">
Inicio
</li>
<li className="nav-item">
Bio
</li>
<li className="nav-item">
Servicios
</li>
<li className="nav-item">
Contacto
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
)
}
ReactDOM.render(<Navbar />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<div id="react"></div>
Note: Just for the demo purpose I have replaced Links with anchors.
Related
I have a react component called NavBar. It gets a variable from another component (App.js) which specifies if the page is in dark mode or not. The variable is called "pageMode". I want to change the class of the navBar from navbar-light to navbar-dark depending of the value of the "pageMode" var.
Here´s the code
import React from 'react';
class NavBar extends React.Component{
render(){
if(this.props.pageMode === "dark"){
//Here I want to do something to render the component using navbar-dark class
}
else{
//Render using navbar-light class
}
return <nav className="navbar navbar-expand-lg [navbar-light] bg-light">
<a className="navbar-brand NavBar-title-fix" href="#">Inicio</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Proyectos</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Sobre mi</a>
</li>
</ul>
</div>
</nav>
}
}
export default NavBar;
In the render return state the class between [] is the one that i want to be changed depending of the variable value.
Thanks for reading!
import React from 'react';
class NavBar extends React.Component{
render(){
const themeClass = {
dark: '[navbar-dark]',
light: '[navbar-light]'
}[this.props.pageMode]
return <nav className={`navbar navbar-expand-lg ${themeClass} bg-light`}>
<a className="navbar-brand NavBar-title-fix" href="#">Inicio</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Proyectos</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Sobre mi</a>
</li>
</ul>
</div>
</nav>
}
}
I have made a Navbar which is made using Bootstrap, it's not full width, Here is the image
I have tried adding nav-fill w-100 class but it didn't worked, Here is my Navbar.js code
import React from 'react'
import PropTypes from 'prop-types'
import { Link } from 'react-router-dom';
export default function Navbar(props) {
return (
<nav className={`navbar navbar-expand-lg navbar-${props.mode} bg-${props.mode} `}>
<div className="container-fluid ">
<Link className="navbar-brand" to="/">{props.title}</Link>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0 ">
<li className="nav-item">
<Link className="nav-link" aria-current="page" to="/">Home</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/about">{props.aboutText}</Link>
</li>
</ul>
<div className={`form-check form-switch text-${props.mode==='light'?'dark':'light'}`}>
<input className="form-check-input" onClick={props.toggleMode} type="checkbox" id="flexSwitchCheckDefault"/>
<label className="form-check-label" htmlFor="flexSwitchCheckDefault">Enable DarkMode</label>
</div>
</div>
</div>
</nav>
)
}
Navbar.propTypes = {
title: PropTypes.string.isRequired,
aboutText: PropTypes.string.isRequired
}
Navbar.defaultProps = {
title: 'Set title here',
aboutText: 'About'
};
You can use vw instead of %. When you're using width it will take 100% of the parents width. However when you want to use the width of the view you can just
.navbar{
width: 100vw!important;
}
I have a menu on website. Below is the HTML code.
<header class="navbar navbar-sticky navbar-expand-lg navbar-dark">
<div class="container position-relative">
<a class="navbar-brand" href="./index.html">
<img class="navbar-brand-regular" src="logo.png"/>
<img class="navbar-brand-sticky" src="logo.png"/>
</a>
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-inner">
<!-- Mobile Menu Toggler -->
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav>
<ul class="navbar-nav" id="navbar-nav">
<li class="nav-item">
<a class="nav-link scroll" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link scroll" href="#about">About</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
On Mobile menu, when i click on a menu item, the menu did not closed. Below is the script code.
Did I miss anything?
// MAIN MENU TOGGLER ICON (MOBILE SITE ONLY)
$('[data-toggle="navbarToggler"]').click(function () {
$('.navbar').toggleClass('active');
$('body').toggleClass('canvas-open');
});
// MAIN MENU TOGGLER ICON
$('.navbar-toggler').click(function () {
$('.navbar-toggler-icon').toggleClass('active');
});
Bootstrap will handle all toggle/collapse by itself you do not need to write extra JS. Toggle button must not duplicate as you did to avoid messing up the navbar functioning.
Below is the answer. I optimized your params in few places. If my answer works for you, please tick it as final answer and upvote it so other people with same problem will get help too. Cheers
<nav class="navbar navbar-dark navbar-expand-sm fixed-top">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#Navbar">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mr-auto" href="./index.html"> <img src="#" height="#" width="#"> </a>
<div class="collapse navbar-collapse" id="Navbar">
<ul class="navbar-nav mr-auto">
<li class="navbar-item active"><a class="nav-link" href="./index.html"> Home</a></li>
<li class="navbar-item"><a class="nav-link" href="./aboutus.html">About</a></li>
</ul>
</div>
</div>
</nav>
I am trying to do responsive navbar by using bootstrap in reactjs.I have update the show in state so as i do like show or hide the element but the toggle button is not trigger.If i use the script link in bootstrap its working but i don't want to use the scripting links and want to update manual.so help me out.
Here is my code:
import React, { Component } from "react";
export default class NavBar extends Component {
constructor(props) {
super(props);
this.state = {
show: true,
};
}
showToggle = () => {
this.setState({
show: false,
});
};
render() {
return (
<div className="container-fluid">
<div className="row">
<div className="col-lg-12 col-md-12 col-sm-12">
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
<div className="navbar-brand">
<img
alt="logo"
src="./images/or.png"
width="100px"
height="30"
className="d-inline-block align-top img-fluid"
/>
{/* <span className="nav-brand">Ortho Life Hospital</span> */}
</div>
<button
className="navbar-toggler"
type="button"
// data-toggle="collapse"
// data-target="#navbarSupportedContent"
// aria-controls="navbarSupportedContent"
// aria-expanded="false"
// aria-label="Toggle navigation"
onClick={this.showToggle}
>
<span className="navbar-toggler-icon"></span>
</button>
{this.state.show ? (
<div
className="collapse navbar-collapse"
id="navbarSupportedContent"
>
<ul className="navbar-nav ml-auto">
<li className="nav-item ml-5">
<a className="nav-link" href="/">
Home
</a>
</li>
<li className="nav-item ml-5">
<div className="dropdown">
<a
className="nav-link"
href="/about"
id="dropdownMenuButton"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
About Us
</a>
<div
className="dropdown-menu"
aria-labelledby="dropdownMenuButton"
>
<a className="dropdown-item" href="/about/#history">
History
</a>
<a className="dropdown-item" href="/about/#vision">
Vision and Mission
</a>
<a className="dropdown-item" href="/about/#whyortho">
Why Ortho?
</a>
</div>
</div>
</li>
<li className="nav-item ml-5">
<div className="dropdown">
<a
className="nav-link"
href="/department"
id="dropDownDepartment"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Department
</a>
<div
className="dropdown-menu"
aria-labelledby="dropDownDepartment"
>
<a
className="dropdown-item"
href="/department/#medical"
>
Medical
</a>
<a
className="dropdown-item"
href="/department/#allied"
>
Allied
</a>
</div>
</div>
</li>
<li className="nav-item ml-5">
<a className="nav-link" href="/media">
Media
</a>
</li>
<li className="nav-item ml-5">
<a className="nav-link" href="/contactus">
Contact Us
</a>
</li>
</ul>
</div>
) : null}
</nav>
</div>
</div>
</div>
);
}
}
I´m trying to build a Boostrap 4 NavBar that will use ReactJS <Link> component from react-router instead of the original <li> class.
Currently I´m having troubles building the component correctly, as at the end the ReactJS component has not the expected behaviour.
Let´s start from the original Boostrap 4 NavBar example:
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
This is being migrated to a ReactJS component as the following:
import React from 'react';
import Link from 'react-router-dom/Link';
function Menu() {
return (
<div>
<nav className="navbar navbar-toggleable-md navbar-light bg-faded">
<button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<a className="navbar-brand" href="#">Navbar</a>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<span className="nav-item active">
<Link className="nav-link" to="/">Home <span className="sr-only">(current)</span></Link>
</span>
<span className="nav-item">
<Link className="nav-link" to="/counter">Counter</Link>
</span>
<span className="nav-item">
<Link className="nav-link disabled" to="/about">About</Link>
</span>
</ul>
<form className="form-inline my-2 my-lg-0">
<input className="form-control mr-sm-2" type="text" placeholder="Search" />
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</div>
);
}
export default Menu;
Altough it runs, many of the formatting is lost and some features like "nav-link disabled" are not working (you can click on About and it goes to the desired page).
I´m sure I´m doing the conversion wrong, so help is appreciated here to fix the component code so that to reflect correctly the original behaviour without React.
Maybe you got it resolved. Here's a working example with Bootstrap 4:
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
const AppPage = () => <h1>React Router 4 & Bootstrap 4</h1>
const HomePage = () => <h1>Welcome Home !</h1>
const AboutPage = () => <h1>About Us</h1>
const OneMorePage = () => <h1>One More Page !</h1>
const PrimaryLayout = () => (
<div>
<nav className="navbar navbar-expand-md navbar-dark bg-dark">
<Link className="navbar-brand" to="/">App</Link>
<button type="button" className="navbar-toggler" data-toggle="collapse" data-target="#navbarNav">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav mr-auto">
<li className="nav-item"><Link className="nav-link" to="/home">Home</Link></li>
<li className="nav-item"><Link className="nav-link" to="/about">About</Link></li>
<li className="nav-item"><Link className="nav-link" to="/one">More</Link></li>
</ul>
</div>
</nav>
<div className="container-fluid">
<Switch>
<Route path="/" exact component={AppPage} />
<Route path="/home" component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/one" component={OneMorePage} />
</Switch>
</div>
</div>
)
const App = () => (
<BrowserRouter>
<PrimaryLayout />
</BrowserRouter>
)
render(<App />, document.getElementById('root'))
For this particular use case, there's an easy solution.
Simply add this to your css:
.nav-link.disabled { pointer-events: none }.
That said, I'd strongly recommend to use a library of bootstrap-based react components on the long term. I personally like https://reactstrap.github.io/.
The problem is Bootstrap has a Javascript component as well which will not work in React as it clashes with React's Javascript. The CSS portion works which is what you are seeing.
You should use something like React-Bootstrap- https://react-bootstrap.github.io/. These are all the Bootstrap components written as React components so you can use them in the standard React manner by importing and using them in your JSX.