I am using React JS to create a responsive UI. I want to create a collapsible sidebar like the following:
So when I click the vertical bar(Graph information) it should expand like the second picture. I have seen some example like in Jsfiddle
Sample code .But here, they have used a static button to control the collapse. Is there any library that I can use? Or any code suggestion?
I am learning React JS. So any help or suggestion will be appreciated.
Let's get started! cd into sidebar-demo and open it up in your go-to text editor. Now inside of our src folder let's go ahead and create a components folder where we'll be keeping our sidebar component. Inside our components folder lets create a Sidebar.
The collapse component is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 . Given how CSS handles animations, you cannot use padding on a .
You can have a button just like in the fiddle, but have it in the sidebar component.
I've updated the fiddle
The beauty of React is separating the state. I think like this:
So Parent
becomes (now passing in the function to the SideBar
)
var Parent = React.createClass({
getInitialState: function(){
return {sidebarOpen: false};
},
handleViewSidebar: function(){
this.setState({sidebarOpen: !this.state.sidebarOpen});
},
render: function() {
return (
<div>
<Header onClick={this.handleViewSidebar} />
<SideBar isOpen={this.state.sidebarOpen} toggleSidebar={this.handleViewSidebar} />
<Content isOpen={this.state.sidebarOpen} />
</div>
);
}
});
and the SideBar
becomes (adding a button that calls that function):
var SideBar = React.createClass({
render: function() {
var sidebarClass = this.props.isOpen ? 'sidebar open' : 'sidebar';
return (
<div className={sidebarClass}>
<div>I slide into view</div>
<div>Me too!</div>
<div>Meee Threeeee!</div>
<button onClick={this.props.toggleSidebar} className="sidebar-toggle">Toggle Sidebar</button>
</div>
);
}
});
You can use the React Offcanvas component to get this working. Here is the link to get install the offcanvas component.
https://github.com/vutran/react-offcanvas
I have implemented the side drawer without using any of these packages like slide-drawer or react-sidebar.
You can check out Side Drawer in my GitHub account GhostWolfRider.
Output Images for reference:
Before Slide:
After Slide:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With