react-sidebar
Version:
A sidebar component for React.
89 lines (75 loc) • 2.34 kB
JavaScript
import React from 'react';
import ReactDOM from 'react-dom';
import Sidebar from '../../src';
import MaterialTitlePanel from './material_title_panel';
import SidebarContent from './sidebar_content';
const styles = {
contentHeaderMenuLink: {
textDecoration: 'none',
color: 'white',
padding: 8,
},
content: {
padding: '16px',
},
};
const App = React.createClass({
getInitialState() {
return {docked: false, open: false};
},
componentWillMount() {
const mql = window.matchMedia(`(min-width: 800px)`);
mql.addListener(this.mediaQueryChanged);
this.setState({mql: mql, docked: mql.matches});
},
componentWillUnmount() {
this.state.mql.removeListener(this.mediaQueryChanged);
},
onSetOpen(open) {
this.setState({open: open});
},
mediaQueryChanged() {
this.setState({docked: this.state.mql.matches});
},
toggleOpen(ev) {
this.setState({open: !this.state.open});
if (ev) {
ev.preventDefault();
}
},
render() {
const sidebar = <SidebarContent />;
const contentHeader = (
<span>
{!this.state.docked &&
<a onClick={this.toggleOpen} href="#" style={styles.contentHeaderMenuLink}>=</a>}
<span> Responsive React Sidebar</span>
</span>);
const sidebarProps = {
sidebar: sidebar,
docked: this.state.docked,
open: this.state.open,
onSetOpen: this.onSetOpen,
};
return (
<Sidebar {...sidebarProps}>
<MaterialTitlePanel title={contentHeader}>
<div style={styles.content}>
<p>
This example will automatically dock the sidebar if the page
width is above 800px (which is currently {'' + this.state.docked}).
</p>
<p>
This functionality should live in the component that renders the sidebar.
This way you're able to modify the sidebar and main content based on the
responsiveness data. For example, the menu button in the header of the
content is now {this.state.docked ? 'hidden' : 'shown'} because the sidebar
is {!this.state.docked && 'not'} visible.
</p>
</div>
</MaterialTitlePanel>
</Sidebar>
);
},
});
ReactDOM.render(<App />, document.getElementById('example'));