methis methis - 4 months ago 54
React JSX Question

Making Sidebar with material-ui

i'm learning material-ui by making the right navigation menu like this one: http://demo.geekslabs.com/materialize/v3.1/ or least like this: http://www.material-ui.com/#/components/app-bar

I'm using Drawer to make my sidebar, the problem is when the sidebar is toggled, it hides the content on the right. I want when my sidebar is toggled, it took place and push the content to the right and both sidebar and content got their own scrollbar. Here is my current code:

Sidebar.js:

import React from 'react';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import Drawer from 'material-ui/Drawer';
import MenuItem from 'material-ui/MenuItem';
import RaisedButton from 'material-ui/RaisedButton';
import AppBar from 'material-ui/AppBar';

export default class Sidebar extends React.Component {

constructor(props) {
super(props);
this.state = {open: false};
}

handleToggle = () => this.setState({open: !this.state.open});

handleClose = () => this.setState({open: false});

render() {
return (
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
<div>
<RaisedButton
label="Open Drawer"
onTouchTap={this.handleToggle}
/>
<Drawer
containerStyle={{height: 'calc(100% - 64px)', top: 64}}
docked={true}
width={200}
open={this.state.open}
onRequestChange={(open) => this.setState({open})
}
>
<AppBar title="AppBar" />
<MenuItem onTouchTap={this.handleClose}>Menu Item</MenuItem>
<MenuItem onTouchTap={this.handleClose}>Menu Item 2</MenuItem>
</Drawer>
</div>
</MuiThemeProvider>
);
}
}


My Layout.js:

import React, { PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Layout.css';
import Header from '../Header';
import Feedback from '../Feedback';
import Footer from '../Footer';
import Sidebar from '../Sidebar';

import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import AppBar from 'material-ui/AppBar';
import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin();

function Layout({ children }) {
return (
<div>
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
<AppBar title="My web" />
</MuiThemeProvider>

<Sidebar/>
{React.Children.only(children)}
<Feedback />
<Footer />
</div>
);
}

Layout.propTypes = {
children: PropTypes.element.isRequired,
};

export default withStyles(s)(Layout);

Answer Source

This is by design. "Material Design", that is :)

https://material.google.com/patterns/navigation-drawer.html

What you're asking for would no longer be a drawer, as described by the Material Design spec. Material-ui attempts to follow that spec faithfully.

You will probably have to create your own component, because I don't think you'll be able to sufficiently manipulate the markup and inline CSS rendered by the Drawer component to accomplish what you're looking for.