【问题标题】:Drawer covers AppBar in material-ui抽屉覆盖 Material-ui 中的 AppBar
【发布时间】:2018-12-06 13:35:59
【问题描述】:

我希望我的抽屉组件在 AppBar 组件下打开,而不是覆盖它。但是对于这个新版本的@Material-Ui/core,这一点从来没有被考虑过。

知道我该怎么做吗?

目前,它以涵盖 AppBar 的方式打开。这不是我想要的,我希望抽屉在 appBar 组件下打开,就像任何普通应用一样。

这是我的代码:

const styles = theme => ({


root: {
    flexGrow: 1,
  },
  flex: {
    flex: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
  list: {
      width: 250,
  },

});


class ClippedDrawer extends React.Component {
  constructor(props){
    super(props);
    this.state={
     open: false,     
    }
  }

  toggleDrawer(){
    this.setState({
      open: !this.state.open,
    });
  };

  render(){
    const { classes } = this.props;
    return(
        <div className={classes.root}>
          <AppBar position="relative" className={classes.appBar}>
            <Toolbar>
              <IconButton className={classes.menuButton} onClick={()=>this.toggleDrawer()} color="inherit" aria-label="Menu">
                <MenuIcon />
              </IconButton>
              <Typography variant="title" color="inherit" className={classes.flex}>
                Title
              </Typography>
              <Button color="inherit">Login</Button>
            </Toolbar>
          </AppBar>
          <Drawer className={classes.drawer} open={this.state.open} onClose={()=>this.toggleDrawer()}>
          <div
            tabIndex={0}
            role="button"
            onClick={()=>this.toggleDrawer()}
            onKeyDown={()=>this.toggleDrawer()}
          >
            <div className={classes.list}>
              <List>ola</List>
              <Divider />
              <List>xau</List>
            </div>
          </div>
        </Drawer>
        </div>
      );
    }
  }


ClippedDrawer.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(ClippedDrawer);

【问题讨论】:

  • 我有相反的问题:我希望 AppBar 为 Drawer 腾出空间。
  • 到目前为止所有给定解决方案的问题是,抽屉低于 AppBar,但现在缺少部分抽屉
  • 我喜欢文档中的示例没有得到解释并且实际上不起作用。

标签: reactjs material-ui


【解决方案1】:

将 AppBar 的位置设置为相对:

appBar: {
    ...
    position: 'relative',
    zIndex: theme.zIndex.drawer + 1,
},

如果它不起作用,那么您可能还需要将 zIndex 显式设置为 1400。

【讨论】:

  • "... zIndex 明确到 1400",它对我有用
  • 如果你在你的中渲染一个,你将看不到菜单
【解决方案2】:

您必须在样式中为 appBar 类设置 CSS z-index 属性

    [theme.breakpoints.up("sm")]: {
      width: "100%"
    },
    zIndex: theme.zIndex.drawer + 1
  }

AppBar 组件的默认类中的这个 appBar 类 在您的情况下,appBar 可能有一个 marginLeft ,而 theme.breakpoints.up("sm") 宽度可能是 calc(100% - (drawerWidth)) 用宽度 100% 替换它

希望对你有帮助

【讨论】:

    【解决方案3】:

    对我来说,将位置道具添加到 AppBar 解决了这个问题。 例如。

    <AppBar position="fixed" className={classes.appBar}>
    </AppBar>
    

    【讨论】:

    【解决方案4】:

    对于正在寻找解决这个非常烦人的问题(标题覆盖抽屉的一部分)的其他人,这是我的解决方案(与 zIndex 一起)用于带有非固定标题的抽屉:

    在标题下方放置一个带有 useRef() 的空 div,然后在单击按钮时 ref.current.getBoundingClientRect().top 将其传递给您的抽屉样式用于 paddingTop。

    这会在单击按钮时从屏幕顶部获取 div 的像素距离,因此抽屉将始终具有正确的填充。

    【讨论】:

      【解决方案5】:

      我在使用 Material-UI 的“下一个”(v5) 版本时也遇到了这个问题。我查看了the example of a drawer clipped under the app bar in the next version's documentation,示例和我自己的代码之间几乎没有区别。为我解决这个问题的是在我的应用程序组件周围使用StyledEngineProvider。我一使用它,我的抽屉立即塞进了应用栏下方。

      import * as React from 'react';
      import ReactDOM from 'react-dom';
      import StyledEngineProvider from '@material-ui/core/StyledEngineProvider';
      import App from './App';
      
      ReactDOM.render(
        <StyledEngineProvider injectFirst>
          <App />
        </StyledEngineProvider>,
        document.querySelector("#root")
      );
      

      【讨论】:

        【解决方案6】:

        在你的 Drawer 组件类中,在纸上:{ background: "transparent" } 然后无论你在抽屉中的内容是什么,用 Material-UI 包装它,设置它的样式以便它可以向下移动 #像素作为您的 appBar 的高度。然后相应地对其进行样式设置。 :)

        【讨论】:

          【解决方案7】:

          您可以在样式中将 App bar 的位置设置为绝对位置,并在 Drawer 中放置一个边距以使其处于正确的位置。

          但是查看文档,那里有很多示例https://material-ui.com/demos/drawers/

          【讨论】:

          • 我已经检查了文档,但这不是我想要的。我想要的是在 IconMenu 中单击,appBar 下出现一个抽屉,然后在 drower 外部单击它又消失了。
          猜你喜欢
          • 2018-06-02
          • 2021-12-19
          • 2020-06-19
          • 2020-07-07
          • 1970-01-01
          • 2018-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多