Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 461
methis Avatar asked Oct 05 '16 05:10

methis


People also ask

Does twitter use material UI?

The official Twitter app is receiving a Material Design makeover. The update is a server-side switch, so keep an eye out for the new UI. A member of XDA Developers has uploaded a video showcasing a new build of the official Twitter app, complete with a Material Design makeover.


3 Answers

This can be accomplished with the new version of Material-UI.

Check this demos out.

The behavior is described in the Material specification under the "Persistent" heading.

The mentioned version of Material-UI is v1.0.0-beta.38. So far I noticed it very stable.

like image 89
Daniel Duarte Avatar answered Oct 09 '22 15:10

Daniel Duarte


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

https://material.io/archive/guidelines/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.

like image 24
Jeff McCloud Avatar answered Oct 09 '22 16:10

Jeff McCloud


I'm not sure if Material Design has changed since this was posted but I think OP is describing "Persistent" Drawer which is defined in the spec. Look under "Behavior". However, material-ui doesn't provide a persistent drawer.

Here's a decent work around using the current material-ui drawer by pushing the content to the right the same width as the drawer.

How to get Material-UI Drawer to 'squeeze' other content when open

like image 2
Byron Avatar answered Oct 09 '22 15:10

Byron