I'm trying to make a sidebar and this is my App.js
function App() {
return (
<BrowserRouter>
<AuthContextProvider>
<NavbarComponent></NavbarComponent>
<Container>
<Routes>
<Route index element={<Store />} />
<Route path="register" element={<Signup></Signup>} />
<Route path="success" element={<Success />} />
<Route path="cancel" element={<Cancel />} />
<Route path="sell" element={<Sell />} />
<Route path="login" element={<Login />} />
</Routes>
</Container>
<Sidebar></Sidebar>
</AuthContextProvider>
</BrowserRouter>
);
}
export default App;
Sidebar.js
export default function Sidebar() {
return (
<div>
<div className="sidebar">Yo whats good</div>
<div className="sidebar">Yo whats good</div>
<div className="sidebar">Yo whats good</div>
<div className="sidebar">Yo whats good</div>
</div>
)
}
CSS for Sidebar:
.sidebar {
background-color: red;
width: 80px;
height: 100px;
}
The first page you see is the Store and so here that is too:
export default function Store() {
const [searchQuery, setSearchQuery] = useState("")
const { limiteds, loadedLimiteds } = UserAuth()
const filteredItems = filterItems(limiteds, searchQuery)
return (
<div className="store" style={{ marginBottom: "8rem" }}>
<Banner text="Test" />
<Search
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
/>
{loadedLimiteds ? <Row xs={1} md={4} className="g-4">
{filteredItems.sort((a, b) => b.rap - a.rap).map((product, i) => (
<Col align="center" key={i}>
<ProductCard product={product}></ProductCard>
</Col>
))}
</Row> : <MoonLoader size="100px" color="white" cssOverride={{ marginLeft: "auto", marginRight: "auto", marginTop: "auto", top: "35px" }} />}
</div >
)
}
Basically no matter what I do with the CSS the height seems to be stuck and not changing, does anyone know how I can fix this? It just seems to be stuck at one value and not possible to change the height to make it taller.
My navigation bar which I created using Bootstrap Offcanvas Example works perfectly fine in my local environment. But after uploading my build to a live website the navigation links lead to a 404 Error saying the page does not exist. However, my footer at the bottom is able to link and load pages properly.
Here is my Navbar:
import React from "react";
import logo from './logo_small.png';
import styled from 'styled-components';
import Button from 'react-bootstrap/Button';
import Container from 'react-bootstrap/Container';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import NavDropdown from 'react-bootstrap/NavDropdown';
import Offcanvas from 'react-bootstrap/Offcanvas';
const Styles = styled.div`
.test{
color: black;
&:hover{
color: #30b849;
}
}
.button{
background-color: #30b849;
color: white;
padding: 5px 10px;
border: 1px solid #30b849;
transition: all 0.3s ease-out;
margin-right: 5%;
&:hover{
background: #000066;
color: white;
transition: all 0.3s ease-out;
}
}
.nav-item.dropdown:hover .dropdown-menu {
display: block;
}
.logo{
margin-left: 10%;
}
`;
function OffcanvasExample() {
return (
<Styles>
{['xl'].map((expand) => (
<Navbar key={expand} bg="white" expand={expand} className="navbar">
<Container fluid>
<Navbar.Brand href="/"><img className='logo' src={logo} alt="Depo App Logo"/></Navbar.Brand>
<Navbar.Toggle aria-controls={`offcanvasNavbar-expand-${expand}`} />
<Navbar.Offcanvas
id={`offcanvasNavbar-expand-${expand}`}
aria-labelledby={`offcanvasNavbarLabel-expand-${expand}`}
placement="end"
>
<Offcanvas.Header closeButton>
<Offcanvas.Title id={`offcanvasNavbarLabel-expand-${expand}`}>
<img src={logo} alt="Depo App Logo"/>
</Offcanvas.Title>
</Offcanvas.Header>
<Offcanvas.Body>
<Nav className="justify-content-lg-end flex-grow-1">
<Nav.Link href="/" className="ps-3 pe-5 fs-5">Home</Nav.Link>
<NavDropdown
href = "/"
className="ps-3 pe-5 fs-5"
title="App"
id={`offcanvasNavbarDropdown-expand-${expand}`}
renderMenuOnMount={true}
>
<NavDropdown.Item href="/our-app">Our App</NavDropdown.Item>
<NavDropdown.Item href="/lawyers">Lawyers</NavDropdown.Item>
<NavDropdown.Item href="/freelancers">Freelancers</NavDropdown.Item>
</NavDropdown>
<NavDropdown
className="ps-3 pe-5 fs-5"
title="Company"
id={`offcanvasNavbarDropdown-expand-${expand}`}
renderMenuOnMount={true}
>
<NavDropdown.Item href="/about-us">About Us</NavDropdown.Item>
<NavDropdown.Item href="/contact-us" >Contact Us</NavDropdown.Item>
<NavDropdown.Item href="/tech-support">Tech Support</NavDropdown.Item>
</NavDropdown>
</Nav>
<Nav.Link href="/sign-in" className="fs-5">Sign In</Nav.Link>
<Button href="/sign-up" className="button fs-5" >Sign Up</Button>
</Offcanvas.Body>
</Navbar.Offcanvas>
</Container>
</Navbar>
))}
</Styles>
);
}
export default OffcanvasExample;
Here is the app.js:
import {BrowserRouter as Router, Switch, Route} from "react-router-dom";
import Nav from './components/Nav';
import './App.css';
import Home from './components/pages/Home/Home'
import SignIn from './components/SignIn/Form'
import SignUp from './components/SignUp/Form'
import Lawyer from "./components/pages/Lawyers/Lawyers";
import Freelancer from "./components/pages/Freelancers/Freelancers";
import About_Us from "./components/pages/About_Us/About_Us";
import Our_App from "./components/pages/Our_App/Our_App";
import Contact_Us from "./components/pages/Contact_Us/Contact_Us";
import Tech_Support from "./components/pages/Tech_Support/Tech_Support";
import Terms_Of_Use from "./components/pages/Terms_Of_Use/Terms_Of_Use";
import Privacy_Policy from "./components/pages/Privacy_Policy/Privacy_Policy";
function App() {
return (
<>
<Router>
<Nav/>
<Switch>
<Route path = '/' exact component={Home}/>
<Route path = '/sign-up' component={SignUp}/>
<Route path = '/sign-in' component={SignIn}/>
<Route path = '/lawyers' component={Lawyer}/>
<Route path = '/freelancers' component={Freelancer}/>
<Route path = '/about-us' component={About_Us}/>
<Route path = '/our-app' component={Our_App}/>
<Route path = '/contact-us' component={Contact_Us}/>
<Route path = '/tech-support' component={Tech_Support}/>
<Route path = '/terms-of-use' component={Terms_Of_Use}/>
<Route path = '/privacy-policy' component={Privacy_Policy}/>
</Switch>
</Router>
</>
);
}
export default App;
Here is a link to the live website: https://thedepoapp.com/
Thanks for the help!
You are using the hrefs on a <NavDropdown.Item> you should be using hrefs only on <Nav.Link> components.
You can alter the dropdowns to these:
<NavDropdown.Item >
<Nav.Link href="/other-route">Other Route</Nav.Link>
</NavDropdown.Item>
See the docs for more details: https://react-bootstrap.github.io/components/navs/#using-dropdowns
My problem is that react-router-dom resets the different states of my app.
For example if a note is entered in the note category, and then I click on 'music' and then go back to the notes, they will have disappeared on refresh.
The same for my header, where I included a button to enlarge the window and another to reduce it. If I am in the 'note' category and I enlarge the window. And I navigate to 'music' or 'inbox', the window size will reset to the original size. And it will not keep the css class that the button injected.
App
class App extends React.Component {
render() {
return (
<>
<div className="window" id="window" >
<div className="window-body" >
<HeaderType />
<div className='window_inner'>
<ColumnLeft />
<ColumnRight />
</div>
</div>
</div>
<div className="addWindowBody" id="openWindow">
<a href='/'>
<div className="addWindow">
<div class="material-symbols-outlined" >
open_in_full
</div>
</div>
</a>
</div>
<div className="addDarkMode"></div>
</>
)
}
}
export default App;
ColumnLeft
class ColumnLeft extends React.Component {
render() {
return (
<>
<div className="column_left">
<a href="/note" className='link'>
<div className="elements">
Prendre des notes
<span className="line"></span>
</div>
</a>
<a href="/inbox" className='link'>
<div className="elements">
Envoyer un message
<span className="line"></span>
</div>
</a>
<a href="/music" className='link'>
<div className="elements">
Ecouter de la musique
<span className="line"></span>
</div>
</a>
</div>
</>
)
}
}
export default ColumnLeft;
ColumnRight
class ColumnRight extends React.Component {
render() {
return (
<>
<div className="column_right">
<BrowserRouter>
<Routes>
<Route path="/" />
<Route path="/note" element={<Note />} />
<Route path="/inbox" element={<Inbox />} />
<Route path="/music" element={<Music />} />
</Routes>
</BrowserRouter>
</div>
</>
)
}
}
export default ColumnRight;
Use the Link component with target path on the to prop instead of raw anchor tags with href attribute to issue internal navigation actions to the other routes your app handles. The raw anchor tag is reloading the page, and thus reloads the app. In other words, the React app is remounting.
Move the BrowserRouter component higher in the ReactTree such that it is providing a routing context to the entire app. This is so the links in ColumnLeft and the routes in ColumnRight access the same routing context for routing and navigation purposes.
Example:
App
import { BrowserRouter, Link } from 'react-router-dom';
class App extends React.Component {
render() {
return (
<BrowserRouter>
<div className="window" id="window">
<div className="window-body">
<HeaderType />
<div className='window_inner'>
<ColumnLeft />
<ColumnRight />
</div>
</div>
</div>
<div className="addWindowBody" id="openWindow">
<Link to='/'>
<div className="addWindow">
<div class="material-symbols-outlined">
open_in_full
</div>
</div>
</Link>
</div>
<div className="addDarkMode" />
</BrowserRouter>
);
}
}
ColumnLeft
import { Link } from 'react-router-dom';
class ColumnLeft extends React.Component {
render() {
return (
<div className="column_left">
<Link to="/note" className='link'>
<div className="elements">
Prendre des notes
<span className="line" />
</div>
</Link>
<Link to="/inbox" className='link'>
<div className="elements">
Envoyer un message
<span className="line" />
</div>
</Link>
<Link to="/music" className='link'>
<div className="elements">
Ecouter de la musique
<span className="line" />
</div>
</Link>
</div>
);
}
}
ColumnRight
class ColumnRight extends React.Component {
render() {
return (
<div className="column_right">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/note" element={<Note />} />
<Route path="/inbox" element={<Inbox />} />
<Route path="/music" element={<Music />} />
</Routes>
</div>
);
}
}
<div className="App">
<div className="navbar">
<Navbar></Navbar>
</div>
<div className="main">
<Layout>
<div className = "routes">
<Switch>
<Route exact path = "/">
<Homepage/>
</Route>
<Route path = "/exchanges">
<Exchanges/>
</Route>
<Route exact path = "/cryptocurrencies">
<Cryptocurrencies/>
</Route>
<Route path = "/crypto/:coinId">
<CryptoDetails/>
</Route>
<Route path = "/news">
<News/>
</Route>
</Switch>
</div>
</Layout>
<div className="footer">
<Typography.Title level = {5} style = {{color:"white"}} align = "center">
The CryptoVerse <br/>
All rights reserved #2021<br/> made by Utkrisht
</Typography.Title>
<Space>
<Link to = "/">Home</Link>
<Link to = "/exchanges">Exchanges</Link>
<Link to = "/news">News</Link>
</Space>
</div>
</div>
</div>
Below is the css for the code
I want the navbar and the main divs to appear just side by side so that the content of the main page can be displayed beside the navbar. But right now, the main div and the navbar divs are overlapping and because of this the content is being hidden by the navbar.
.app{
display: flex;
overflow: hidden;
}
.navbar{
flex: 0.2;
background-color: rgb(0, 21, 41);
}
.main{
flex: 0.8;
width: 100%;
}
.routes{
padding: 20px;
}
CSS is case-sensitive. In your HTML you've got this:
<div className="App">
…
</div>
And your CSS is attempting to style it using this:
.app {
…
}
You'll need to match cases using this:
.App {
…
}
I am trying to create a Bootstrap sidebar like this picture here.
I have looked at all the code on react-bootstrap and Twitter Bootstrap and I am yet to find a how-to code this. Basically, if they are viewing on a desktop, I want the sidebar to be visible, otherwise hidden.
The sidebar should stay still while the content on the page scrolls up and down.
Ok so for people who want to make a sidebar sadly the news is you gotta make it all yourself.
What I have done is the following.
See the example at https://github.com/StartBootstrap/startbootstrap-simple-sidebar
Create sidebar.js somewhere in your app.
import React from "react";
import {Nav} from "react-bootstrap";
import { withRouter } from "react-router";
import '../pages/style/Dashboard.css'
const Side = props => {
return (
<>
<Nav className="col-md-12 d-none d-md-block bg-light sidebar"
activeKey="/home"
onSelect={selectedKey => alert(`selected ${selectedKey}`)}
>
<div className="sidebar-sticky"></div>
<Nav.Item>
<Nav.Link href="/home">Active</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link eventKey="link-1">Link</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link eventKey="link-2">Link</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link eventKey="disabled" disabled>
Disabled
</Nav.Link>
</Nav.Item>
</Nav>
</>
);
};
const Sidebar = withRouter(Side);
export default Sidebar
My Dashboard.css has the following in it.
.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
min-height: 100vh !important;
z-index: 100;
padding: 48px 0 0;
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}
#sidebar-wrapper{
min-height: 100vh !important;
width: 100vw;
margin-left: -1rem;
-webkit-transition: margin .25s ease-out;
-moz-transition: margin .25s ease-out;
-o-transition: margin .25s ease-out;
transition: margin .25s ease-out;
}
#sidebar-wrapper .sidebar-heading {
padding: 0.875rem 1.25rem;
font-size: 1.2rem;
}
#page-content-wrapper {
min-width: 0;
width: 100%;
}
Then final step
In the file you want it to be show in do the following
import React from "react";
import {Container, Row, Col, Card, Form, Button } from "react-bootstrap";
import { withRouter } from "react-router";
import Sidebar from "../moduls/sidebar.js";
import './style/Dashboard.css'
const Dash = props => {
return (
<>
<Container fluid>
<Row>
<Col xs={2} id="sidebar-wrapper">
<Sidebar />
</Col>
<Col xs={10} id="page-content-wrapper">
this is a test
</Col>
</Row>
</Container>
</>
);
};
const Dashboard = withRouter(Dash);
export default Dashboard
As of 2022 there is pure react-boostrap based component called react-boostrap-sidebar-menu . It is the cleanest solution so far and quite customizable.
https://www.npmjs.com/package/react-bootstrap-sidebar-menu
https://github.com/ivp-dev/react-bootstrap-sidebar-menu
import SidebarMenu from 'react-bootstrap-sidebar-menu';
<SidebarMenu>
<SidebarMenu.Header>
<SidebarMenu.Brand>
{/* Your brand icon */}
</SidebarMenu.Brand>
<SidebarMenu.Toggle />
</SidebarMenu.Header>
<SidebarMenu.Body>
<SidebarMenu.Nav>
<SidebarMenu.Nav.Link>
<SidebarMenu.Nav.Icon>
{/* Menu item icon */}
</SidebarMenu.Nav.Icon>
<SidebarMenu.Nav.Title>
{/* Menu item title */}
</SidebarMenu.Nav.Title>
</SidebarMenu.Nav.Link>
<SidebarMenu.Nav/>
<SidebarMenu.Sub>
<SidebarMenu.Sub.Toggle>
<SidebarMenu.Nav.Icon />
<SidebarMenu.Nav.Title>
{/* Submenu title */}
</SidebarMenu.Nav.Title>
</SidebarMenu.Sub.Toggle>
<SidebarMenu.Sub.Collapse>
<SidebarMenu.Nav>
<SidebarMenu.Nav.Link>
<SidebarMenu.Nav.Icon>
{/* Submenu item icon */}
</SidebarMenu.Nav.Icon>
<SidebarMenu.Nav.Title>
{/* Submenu item title */}
</SidebarMenu.Nav.Title>
</SidebarMenu.Nav.Link>
</SidebarMenu.Nav>
</SidebarMenu.Sub.Collapse>
</SidebarMenu.Sub>
<SidebarMenu.Body/>
</SidebarMenu>
One can now use a library, react-bootstrap-drawer, which provides a sidenav / drawer which was taken directly from the react-bootstrap documentation. Oddly, this is not a component provided by the library itself so one must use a third-party library:
import 'react-bootstrap-drawer/lib/style.css';
import React, { useState } from 'react';
import {
Col,
Collapse,
Container,
Row,
} from 'react-bootstrap';
import { Drawer, } from 'react-bootstrap-drawer';
const ApplicationDrawer = (props) => {
const [open, setOpen] = useState(false);
const handleToggle = () => setOpen(!open);
return (
<Drawer { ...props }>
<Drawer.Toggle onClick={ handleToggle } />
<Collapse in={ open }>
<Drawer.Overflow>
<Drawer.ToC>
<Drawer.Header href="/">Application</Drawer.Header>
<Drawer.Nav>
<Drawer.Item href="/settings">Settings</Drawer.Item>
</Drawer.Nav>
</Drawer.ToC>
</Drawer.Overflow>
</Collapse>
</Drawer>
);
};
const Application = (props) => {
return (
<Container fluid>
<Row className="flex-xl-nowrap">
<Col as={ ApplicationDrawer } xs={ 12 } md={ 3 } lg={ 2 } />
<Col xs={ 12 } md={ 9 } lg={ 10 }>{ props.children }</Col>
</Row>
</Container>
);
};
See: https://github.com/SimpleSigner/react-bootstrap-drawer
of course, you have to make the navbar yourself and the examples above are very valid, but cdbreact speeds up the process a lot faster. just add cdbreact using
npm I cdbreact
or
yarn add cdbreact
and then import CDBSidebar, CDBSidebarContent, CDBSidebarHeader, etc. with cdbreact, you don't need to bother about installing bootsrap in your react app.
cdbreact also comes with icons, and a lot more.
import React from 'react'
import {CDBSidebar,
CDBSidebarContent,
CDBSidebarHeader,
CDBSidebarFooter, CDBSidebarMenu,
CDBSidebarMenuItem} from 'cdbreact';
import {NavLink, Link} from 'react-router-dom';
import {} from 'react-router-dom';
const Sidebar=()=>{
return (
<div style={{display:'flex', height:'100%', overflow:'scroll initial'}}>
<CDBSidebar textColer="#fff" backgroundColor="rgb(37, 90, 122)">
<CDBSidebarHeader prefix={<i className="fa fa-bars fa-large"></i>}>
<Link to="/dashboard">Dashboard</Link>
</CDBSidebarHeader>
<CDBSidebarContent className="sidebar-content">
<CDBSidebarMenu>
<NavLink exact to="/dashboard" activeClassName="activeClicked">
<CDBSidebarMenuItem icon="columns">
Transfer
</CDBSidebarMenuItem>
</NavLink>
<NavLink exact to="/dashboard" activeClassName="activeClicked">
<CDBSidebarMenuItem icon="columns">
Transfer
</CDBSidebarMenuItem>
</NavLink>
<NavLink exact to="/dashboard" activeClassName="activeClicked">
<CDBSidebarMenuItem icon="columns">
Transfer
</CDBSidebarMenuItem>
</NavLink>
<NavLink exact to="/dashboard" activeClassName="activeClicked">
<CDBSidebarMenuItem icon="columns">
Transfer
</CDBSidebarMenuItem>
</NavLink>
</CDBSidebarMenu>
</CDBSidebarContent>
<CDBSidebarFooter style={{textAlign:'center'}}>
<div className="sidebar-btn-wrapper" style={{ padding :'20px 5px' }}>
sidebar footer
</div>
</CDBSidebarFooter>
</CDBSidebar>
</div>
)
}
export default Sidebar;
you can also follow the guide here https://dev.to/devwares/how-to-create-a-responsive-sidebar-in-react-using-bootstrap-and-contrast-5gi2 follow this link to see