Skip to content

Instantly share code, notes, and snippets.

@AshikNesin
Created July 17, 2018 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshikNesin/9fbeab0e27800b91cd31a98c4e6c44d1 to your computer and use it in GitHub Desktop.
Save AshikNesin/9fbeab0e27800b91cd31a98c4e6c44d1 to your computer and use it in GitHub Desktop.
React Sidebar
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import classNames from "classnames";
class Sidebar extends React.Component {
constructor(props) {
super(props);
this.state = {
showMenu: false
};
}
componentDidMount = () => {
document.addEventListener("click", this.handleClickOutside, true);
};
componentWillUnmount = () => {
document.removeEventListener("click", this.handleClickOutside, true);
};
toggleMenu = () => {
this.setState({ showMenu: !this.state.showMenu });
};
handleClickOutside = event => {
const domNode = ReactDOM.findDOMNode(this);
if (!domNode || !domNode.contains(event.target)) {
this.setState({
showMenu: false
});
}
};
render() {
const showMenu = this.state.showMenu;
const sidebarClass = classNames({
sidebar: true,
"sidebar-menu-expanded": showMenu,
"sidebar-menu-collapsed": !showMenu
});
const elementsClass = classNames({
"expanded-element": true,
"is-hidden": !showMenu
});
return (
<nav className={sidebarClass}>
<img
className="menuIcon"
src={"https://png.icons8.com/menu/ffffff"}
onClick={this.toggleMenu}
/>
<ul>
<li>
<a className="expandable" href="#" title="Setting">
<img src={"https://png.icons8.com/setting/ffffff"} alt="" />
<span className={elementsClass}>Setting</span>
</a>
</li>
</ul>
</nav>
);
}
}
export default Sidebar;
.menuIcon {
margin-top: 70px;
cursor: pointer;
}
nav.sidebar-menu-collapsed {
width: 70px;
}
nav.sidebar-menu-expanded {
width: 211px;
z-index: 1;
}
nav.sidebar {
position: fixed;
top: 0px;
left: 0px;
height: 100%;
background: none repeat scroll 0 0 #000;
color: white;
padding: 20px 10px;
}
nav.sidebar a#justify-icon {
outline: 0;
color: white;
font-size: 24px;
font-style: normal;
}
nav.sidebar ul {
margin: 0;
padding: 0;
}
nav.sidebar ul li {
margin: 0;
padding: 0;
margin-top: 20px;
list-style-type: none;
}
nav.sidebar ul li a.expandable {
outline: 0;
color: white;
text-decoration: none;
font-size: 20px;
}
nav.sidebar ul li a.expandable:hover {
color: #bbb;
}
nav.sidebar ul li a.expandable span.expanded-element {
color: #fff;
margin-left: 12px;
font-size: 16px;
position: relative;
bottom: 2px;
}
nav.sidebar ul li.active {
background: none repeat scroll 0 0 black;
border-radius: 4px;
text-align: center;
margin-left: -4px;
padding: 4px;
}
nav.sidebar ul li.active a.expandable {
color: white !important;
}
nav.sidebar ul li.active a.expandable:hover {
color: white !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment