Created
March 25, 2019 23:10
-
-
Save jamesgathu/684d091e2a02c6376966f58b1f17cebf to your computer and use it in GitHub Desktop.
ReactJS add Sidebar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import './App.css'; | |
import SideDrawer from "./components/Sidedrawer/side_drawer"; | |
class App extends Component { | |
state = { | |
sideDrawerOpen: false | |
}; | |
drawerToggleClickHandler = () => { | |
this.setState((prevState) => { | |
return {sideDrawerOpen: !prevState.sideDrawerOpen} | |
}) | |
}; | |
backdropClickHandler = () => { | |
this.setState({sideDrawerOpen: false}); | |
}; | |
render() { | |
let backDrop; | |
if (this.state.sideDrawerOpen) { | |
backDrop = <div className="backdrop" onClick={this.backdropClickHandler}/>; | |
} | |
return ( | |
<div className="App" style={{height: '100%'}}> | |
<header className="toolbar"> | |
<nav className="toolbar__navigation"> | |
<div> | |
<button className="toggle_button" onClick={this.drawerToggleClickHandler}> | |
<img src={require('../../assets/images/menu.svg')} alt="hamburger-icon" | |
className="side-menu-toggle"/> | |
</button> | |
</div> | |
<div className="toolbar__logo"><a href="/">THE LOGO</a></div> | |
<div className="spacer"/> | |
<div className="toolbar_navigation-items"> | |
<ul> | |
<li><a href="/">Users</a></li> | |
<li><a href="/">Products</a></li> | |
</ul> | |
</div> | |
</nav> | |
</header> | |
<SideDrawer show={this.state.sideDrawerOpen}/> | |
{backDrop} | |
<main style={{marginTop: '64px'}}> | |
This is the page content | |
</main> | |
</div> | |
); | |
} | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
const SideDrawer = props => { | |
let classes = 'side-drawer'; | |
if(props.show){ | |
classes = classes + ' open'; | |
} | |
return ( | |
<nav className={classes}> | |
<ul> | |
<li><a href="/">Products</a></li> | |
<li><a href="/">Users</a></li> | |
</ul> | |
</nav> | |
); | |
}; | |
export default SideDrawer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment