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


Tags: css,reactjs,material-ui

Problem :

If anyone is familiar with css and Material UI any help with this would be much appreciated. Basically I'm trying to implement a Material UI Drawer component which when opened doesn't just slide out over the top of the page content, but rather the page content conforms around the drawer i.e squeezes or expands. I'm using bootstrap rows and containers in my project, but I have no clue how I'd use them to achieve this. Here's how my component is laid out:

<div>
    <AppBar
    onLeftIconButtonTouchTap={this.handleToggle}
      title="Imaginary Company"
    />
    <Row>
      <QuotesList />
    </Row>
    <Drawer
    containerStyle={{ top: 64, width:150 }}
    open={this.state.open}
    >
      <MenuItem>Menu Item</MenuItem>
      <MenuItem>Menu Item 2</MenuItem>
    </Drawer>
  </div>

Note the 'top' property is just because I want the top title area of the App Bar to remain visible with the drawer open or close. As always any help would be amazing! Cheers

UPDATE: I've found a kind of hacky solution that's working for me. Using the awesome 'styled-components' library (which allows styles to change based on dynamic props you pass in) I was able to create a wrapper component which accepts a 'collapsed' prop, with an animation sliding to the right equal to the drawer's width (150). If anyone stumbles upon this and they're interested here's my code in styled- components (there's a few copied and pasted bootstrap container styles in there as well) :

const CollapsibleContainer = styled.div`
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
$:after {
content: "";
display: table;
clear: both;
}
position: absolute;
right: 0;
left: ${props => props.collapsed ? '150px' : '0'}!important;
transition: 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
`; 

Maybe this isn't the perfect solution but it looks fine. Hope this helps someone maybe :)



Solution :

Similar to your "hacky" solution perhaps, but here's a jsFiddle: https://jsfiddle.net/88uq8751/3/

Same prinicipal. You nudge your content rightward the same distance as the drawer's width, and using the same transition/animation... This could be accomplished in a variety of ways using some form or combo of width, marginLeft, absolute position, maybe even transform:translate, but I used marginLeft below:

const { Drawer, RaisedButton, MuiThemeProvider, getMuiTheme } = MaterialUI;

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = { drawerOpen: false };
  }

  render() {
    const contentStyle = {  transition: 'margin-left 450ms cubic-bezier(0.23, 1, 0.32, 1)' };

    if (this.state.drawerOpen) {
      contentStyle.marginLeft = 256;
    }

    return (
      <div>
        <Drawer open={this.state.drawerOpen}>
          <div style={{ textAlign: 'right' }}>
            <RaisedButton onClick={() => this.setState({ drawerOpen: false }) }>X</RaisedButton>
          </div>
          {this.props.children}
        </Drawer>
        <div style={contentStyle}>
          <RaisedButton onClick={() => this.setState({ drawerOpen: true }) }>Open</RaisedButton>
          <p>
            Lorem ipsum dolor sit amet, amet epicuri vel ea, noster causae oporteat has ut, ad est periculis concludaturque.Mundi docendi volutpat ut sea, an sea suas epicurei.Sea numquam definitionem ne.Te postea aliquip invidunt quo.Id prima commune complectitur pri, sed at vero posse contentiones, sea cu fugit etiam iusto.Ei mei prima appareat, est brute luptatum iudicabit id.Alii homero imperdiet usu id, dico meis alienum per ad, dolorem mentitum philosophia quo id.
          </p><p>
            Nec in dolor ancillae contentiones, at harum graecis his.An delectus scripserit pro.Ei dicta liber vis, sed no quidam legimus fabellas, no expetendis vituperata mei.Vim et ferri nominavi constituto.Wisi tamquam intellegebat duo in.Omittam adolescens abhorreant no sea.Cibo iusto verear ut mea, per at viris nominavi referrentur.
          </p><p>
            Sea officiis moderatius te.Graeci causae malorum ius in, cu has offendit tractatos interpretaris.Ea porro liberavisse mei, no mei propriae salutandi intellegat, te nibh singulis vim.Vim mucius feugait blandit ea.At est mollis vivendo.Eu has choro doctus verterem.Utroque suscipiantur ne eum, vix in alia intellegat.
          </p><p>
            Lorem ipsum dolor sit amet, amet epicuri vel ea, noster causae oporteat has ut, ad est periculis concludaturque.Mundi docendi volutpat ut sea, an sea suas epicurei.Sea numquam definitionem ne.Te postea aliquip invidunt quo.Id prima commune complectitur pri, sed at vero posse contentiones, sea cu fugit etiam iusto.Ei mei prima appareat, est brute luptatum iudicabit id.Alii homero imperdiet usu id, dico meis alienum per ad, dolorem mentitum philosophia quo id.
          </p><p>
            Nec in dolor ancillae contentiones, at harum graecis his.An delectus scripserit pro.Ei dicta liber vis, sed no quidam legimus fabellas, no expetendis vituperata mei.Vim et ferri nominavi constituto.Wisi tamquam intellegebat duo in.Omittam adolescens abhorreant no sea.Cibo iusto verear ut mea, per at viris nominavi referrentur.
          </p><p>
            Sea officiis moderatius te.Graeci causae malorum ius in, cu has offendit tractatos interpretaris.Ea porro liberavisse mei, no mei propriae salutandi intellegat, te nibh singulis vim.Vim mucius feugait blandit ea.At est mollis vivendo.Eu has choro doctus verterem.Utroque suscipiantur ne eum, vix in alia intellegat.
          </p><p>
            Lorem ipsum dolor sit amet, amet epicuri vel ea, noster causae oporteat has ut, ad est periculis concludaturque.Mundi docendi volutpat ut sea, an sea suas epicurei.Sea numquam definitionem ne.Te postea aliquip invidunt quo.Id prima commune complectitur pri, sed at vero posse contentiones, sea cu fugit etiam iusto.Ei mei prima appareat, est brute luptatum iudicabit id.Alii homero imperdiet usu id, dico meis alienum per ad, dolorem mentitum philosophia quo id.
          </p><p>
            Nec in dolor ancillae contentiones, at harum graecis his.An delectus scripserit pro.Ei dicta liber vis, sed no quidam legimus fabellas, no expetendis vituperata mei.Vim et ferri nominavi constituto.Wisi tamquam intellegebat duo in.Omittam adolescens abhorreant no sea.Cibo iusto verear ut mea, per at viris nominavi referrentur.
          </p><p>
            Sea officiis moderatius te.Graeci causae malorum ius in, cu has offendit tractatos interpretaris.Ea porro liberavisse mei, no mei propriae salutandi intellegat, te nibh singulis vim.Vim mucius feugait blandit ea.At est mollis vivendo.Eu has choro doctus verterem.Utroque suscipiantur ne eum, vix in alia intellegat.
          </p>        
        </div>
    </div>
    );
  }

}

const App = () => (
  <MuiThemeProvider muiTheme={getMuiTheme() }>
    <Example />
  </MuiThemeProvider>
);

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

    CSS Howto..

    How to position an image of different size using css?

    how to give css for following data

    How to remove the margin top from a div with display table-cell?

    How to layer a
    element ontop of a other elements

    How can I create facebook style vertical border line (layout)? :)

    Show popout bubble above when no space available below

    How can I pipe the output from gulp-w3c-css to the console

    How to nest multiple css attributes inside another attribute

    How to change buttons to pictures in jquery dialog

    How do I get spotlight/shadow position to remain exactly same relative horizontal center position regardless of zoom?

    How to get rid of hidden space HTML/CSS [closed]

    Awkward gap between menu items. How to fix?

    How do I use Stylish to change the font in Google Tasks?

    how to set a path to the static files which is surver independnet

    how to vertically align this div?

    CSS : How can I add shadow to a label or box

    How to use a custom Style Sheet in WordPress

    how to add a margin on both sides and center the text in a paragraph?

    How to change iframe pages without the parent page changing with external links using CSS

    How to eliminate an annoying blue line in a border area of a hyperlink / h2 class with CSS

    How to align / valign css-rotated span within its parent div

    How do I use this CSS animation in my HTML file?

    how to use static folder in django for css and javascript?

    How to reduce size of a text with police roboto on a small scren

    How do I make elements flow horizontally instead of vertically?

    jQuery slideshow image delay

    How to remove the circle in the background of the impress.js demo

    How prevent Conflict between two $(this) in jquery?

    How to select the first type of child of a particular CSS using CSS selector [duplicate]

    how can i add the on hover effect on hyperlink in jeditorpane using swing in java