【问题标题】:How to hide Material-Ui Mini variant Drawer on mobile view如何在移动视图上隐藏 Material-Ui Mini 变体抽屉
【发布时间】:2018-09-19 00:36:57
【问题描述】:

如何在移动视图上隐藏迷你变体抽屉。我不想要移动视图上的侧边栏。它应该隐藏在移动视图中。

我在关注官方文档Original Code

在移动视图中删除

【问题讨论】:

    标签: material-design material-ui


    【解决方案1】:

    改变样式来做到这一点:

    drawerPaperClose: {
            overflowX: 'hidden',
            transition: theme.transitions.create('width', {
            easing: theme.transitions.easing.sharp,
            duration: theme.transitions.duration.leavingScreen,
        }),
        width: theme.spacing.unit * 7,
        [theme.breakpoints.up('sm')]: {
          width: theme.spacing.unit * 9,
        },
        [theme.breakpoints.down('sm')]: {
          width: 0,
          display:'none',
        },
      },
      nested: {
        paddingLeft: theme.spacing.unit * 4,
      },
    

    【讨论】:

      【解决方案2】:

      您需要做的就是更改 drawerClose 对象的样式。

      DOCUMENTATION 中的代码

        drawerClose: {
          transition: theme.transitions.create('width', {
            easing: theme.transitions.easing.sharp,
            duration: theme.transitions.duration.leavingScreen,
          }),
          overflowX: 'hidden',
          width: theme.spacing(7) + 1,
          [theme.breakpoints.up('sm')]: {
            width: theme.spacing(9) + 1,
          },
          [theme.breakpoints.down('sm')]: {
            width: theme.spacing(0) + 1,
          },
        },
      

      以下是新代码,您可以在其中通过添加 .down('sm') 方法来更新断点。这会修改指定 'sm' 断点下方所有屏幕的样式。

      [theme.breakpoints.down('sm')]: {
            width: theme.spacing(0) + 1,
          },
      

      新的更新代码:

        drawerClose: {
          transition: theme.transitions.create('width', {
            easing: theme.transitions.easing.sharp,
            duration: theme.transitions.duration.leavingScreen,
          }),
          overflowX: 'hidden',
          width: theme.spacing(7) + 1,
          [theme.breakpoints.up('sm')]: {
            width: theme.spacing(9) + 1,
          },
          // New Code. 
          [theme.breakpoints.down('sm')]: {
            width: theme.spacing(0) + 1,
          },
        },
      

      【讨论】:

        【解决方案3】:
            const drawerWidth = 240;
        
        const styles = theme => ({
          root: {
            display: 'flex',
          },
          toolbar: {
            paddingRight: 24, // keep right padding when drawer closed
          },
          toolbarIcon: {
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'flex-end',
            padding: '0 8px',
            ...theme.mixins.toolbar,
          },
          appBar: {
            zIndex: theme.zIndex.drawer + 1,
            transition: theme.transitions.create(['width', 'margin'], {
              easing: theme.transitions.easing.sharp,
              duration: theme.transitions.duration.leavingScreen,
            }),
          },
          appBarShift: {
            marginLeft: drawerWidth,
            width: `calc(100% - ${drawerWidth}px)`,
            transition: theme.transitions.create(['width', 'margin'], {
              easing: theme.transitions.easing.sharp,
              duration: theme.transitions.duration.enteringScreen,
            }),
          },
          menuButton: {
            marginLeft: 12,
            marginRight: 36,
          },
          menuButtonHidden: {
            display: 'none',
          },
          title: {
            flexGrow: 1,
          },
          drawerPaper: {
            position: 'relative',
            whiteSpace: 'nowrap',
            width: drawerWidth,
            transition: theme.transitions.create('width', {
              easing: theme.transitions.easing.sharp,
              duration: theme.transitions.duration.enteringScreen,
            }),
            [theme.breakpoints.up('md')]: {
              position: 'relative',
            },
          },
          drawerPaperClose: {
            overflowX: 'hidden',
            transition: theme.transitions.create('width', {
              easing: theme.transitions.easing.sharp,
              duration: theme.transitions.duration.leavingScreen,
            }),
            width: theme.spacing.unit * 7,
            [theme.breakpoints.up('sm')]: {
              width: theme.spacing.unit * 9,
            },
          },
          appBarSpacer: theme.mixins.toolbar,
          content: {
            flexGrow: 1,
            padding: theme.spacing.unit * 3,
            height: '100vh',
            overflow: 'auto',
          },
          chartContainer: {
            marginLeft: -22,
          },
          tableContainer: {
            height: 320,
          },
        });
        
        class Dashboard extends React.Component {
          state = {
            open: false,
            auth: true,
            anchorEl: null,
          };
        
          handleDrawerOpen = () => {
            this.setState({ open: true });
          };
        
          handleDrawerClose = () => {
            this.setState({ open: false });
          };
          handleChange = event => {
            this.setState({ auth: event.target.checked });
          };
        
          handleMenu = event => {
            this.setState({ anchorEl: event.currentTarget });
          };
        
          handleClose = () => {
            this.setState({ anchorEl: null });
          };
        
          render() {
            const { classes, siteTitle, children,theme } = this.props;
            const { auth, anchorEl } = this.state;
            const open = Boolean(anchorEl);
            return (
              <React.Fragment>
                <CssBaseline />
                <div className={classes.root}>
                  <AppBar
                    position="absolute"
                    className={classNames(classes.appBar, this.state.open && classes.appBarShift)}
                    style={{ background: '#051745', boxShadow: 'none' }}
                  >
                    <Toolbar disableGutters={!this.state.open} className={classes.toolbar}>
                      <IconButton
                        color="inherit"
                        aria-label="Open drawer"
                        onClick={this.handleDrawerOpen}
                        className={classNames(
                          classes.menuButton,
                          this.state.open && classes.menuButtonHidden,
                        )}
                      >
                        <MenuIcon />
                      </IconButton>
                      <Typography variant="title" color="inherit" noWrap className={classes.title}>
                        {siteTitle}
                      </Typography>
                      <IconButton color="inherit">
                        <Badge badgeContent={4} color="secondary">
                          <NotificationsIcon />
                        </Badge>
                      </IconButton>
                      <div>
                        <IconButton
                          aria-owns={open ? 'menu-appbar' : null}
                          aria-haspopup="true"
                          onClick={this.handleMenu}
                          color="inherit"
                        >
                          <AccountCircle />
                        </IconButton>
                        <Menu
                          id="menu-appbar"
                          anchorEl={anchorEl}
                          anchorOrigin={{
                            vertical: 'top',
                            horizontal: 'right',
                          }}
                          transformOrigin={{
                            vertical: 'top',
                            horizontal: 'right',
                          }}
                          open={open}
                          onClose={this.handleClose}
                        >
                          <MenuItem onClick={this.handleClose}>Profile</MenuItem>
                          <MenuItem onClick={this.handleClose}>My account</MenuItem>
                        </Menu>
                      </div>
        
        
                    </Toolbar>
                  </AppBar>
        
                  <Hidden mdUp>
                  <Drawer
                    variant="temporary"
        
        
                    onClose={this.handleDrawerClose}
                    classes={{
                      paper: classNames(classes.drawerPaper, !this.state.open && classes.drawerPaperClose),
                    }}
                    ModalProps={{
                      keepMounted: true, // Better open performance on mobile.
                    }}
                    open={this.state.open}
                  >
                    <div className={classes.toolbarIcon}>
                      {siteTitle}
        
        
                      <IconButton onClick={this.handleDrawerClose}>
        
                        <ChevronLeftIcon />
                      </IconButton>
                    </div>
                    <Divider />
                    <List >{mainListItems}</List>
                    <Divider />
                    <List>{secondaryListItems}</List>
        
                  </Drawer>
                </Hidden>
                <Hidden smDown implementation="css">
                  <Drawer
                    variant="permanent"
                    classes={{
                      paper: classNames(classes.drawerPaper, !this.state.open && classes.drawerPaperClose),
                    }}
                    open={this.state.open}
                  >
                    <div className={classes.toolbarIcon}>
                      {siteTitle}
        
        
                      <IconButton onClick={this.handleDrawerClose}>
        
                        <ChevronLeftIcon />
                      </IconButton>
                    </div>
                    <Divider />
                    <List >{mainListItems}</List>
                    <Divider />
                    <List>{secondaryListItems}</List>
                  </Drawer>
                  </Hidden>
                  <main className={classes.content} >
                    <div className={classes.appBarSpacer} />
                    {children}
        
                    <Footer />
                  </main>
        
                </div>
              </React.Fragment>
            );
          }
        }
        
        Dashboard.propTypes = {
          classes: PropTypes.object.isRequired,
          theme: PropTypes.object.isRequired,
        };
        
        export default withStyles(styles)(Dashboard);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-09
          • 2020-07-07
          • 2019-01-05
          • 2021-11-03
          • 2020-10-28
          • 2018-09-28
          • 1970-01-01
          相关资源
          最近更新 更多