16

Using Material UI, how can I construct a Drawer with expandable menu items like the one on the material-ui.com site?

So I want something like this: enter image description here

Each menu item (in bold) can expand to reveal sub-menu items.

How can this be achieved with Material UI?

etayluz
  • 15,920
  • 23
  • 106
  • 151
  • This may help. source code of material-ui site app drawer. https://github.com/mui-org/material-ui/blob/master/docs/src/modules/components/AppDrawer.js – REDDY PRASAD Jun 28 '18 at 14:36
  • I don't see AppDrawerNavItem as one of the API's on the material UI website. Can you please provide a working example on codesandbox.io or otherwise? – etayluz Jun 28 '18 at 14:49
  • `AppDrawerNavItem` is custom component. You can find it in same directory that `AppDrawer` exists. Sorry no live example :( https://github.com/mui-org/material-ui/blob/master/docs/src/modules/components/AppDrawerNavItem.js – REDDY PRASAD Jun 28 '18 at 14:52
  • can you please provide a working example? – etayluz Jun 28 '18 at 14:58
  • 4
    Sorry i was misleading you . its nothing to do with `AppDrawer` . its `Lists` docs: https://material-ui.com/demos/lists/ . live version: https://codesandbox.io/s/6xo0nlxn8k . – REDDY PRASAD Jun 28 '18 at 15:12

1 Answers1

18

you need a function open and close collapse

const [openCollapse, setOpenCollapse] = React.useState(false);    
 
function handleOpenSettings(){
    setOpenCollapse(!openCollapse);
}

return(
        <Drawer>
            <ListItem button onClick={handleOpenSettings}>
              <ListItemIcon>
                <Settings />
              </ListItemIcon>
              <ListItemText primary="Settings" />
              {openCollapse ? <ExpandLess /> : <ExpandMore />}
            </ListItem>
            <Collapse in={openCollapse} timeout="auto" unmountOnExit>
            <List component="div" disablePadding>
              <ListItem button className={classes.nested}>
                <ListItemIcon>
                  <Settings />
                </ListItemIcon>
                <ListItemText inset primary="Starred" />
              </ListItem>
            </List>
          </Collapse>
        </Drawer>
)

DEMO https://material-ui.com/components/lists/#simple-list

General Grievance
  • 4,555
  • 31
  • 31
  • 45