【问题标题】:Trying to get Material-UI clipped drawer to work in flex layout试图让 Material-UI 剪辑抽屉在 flex 布局中工作
【发布时间】:2018-05-09 23:02:54
【问题描述】:

我从裁剪的抽屉示例代码开始,并尝试围绕它进行构建。在样本中插入组件时(即,将 '{'You think water moving fast? You should see ice.'} 替换为其他内容),内容受抽屉高度的限制。尝试在示例之外插入内容时,所有内容都从抽屉下方开始。

预期行为:能够将内容放置在抽屉周围的任何位置。根据抽屉菜单选择,我有不同的组件隐藏/变得可见

我最初从永久抽屉示例开始,一切正常,除了我需要将抽屉放在应用栏下方。

【问题讨论】:

    标签: material-ui drawer


    【解决方案1】:

    布局由一个包含Drawer 和主要内容区域的弹性容器组成。内容区域 (.appContent) 扩展以填充抽屉右侧(或左侧)的空间。您的所有内容都应放置在此元素内。

    更新:修复了适用于 IE 11 的样式

    基本结构:

    <div className={classes.root}>
      <AppBar position="fixed" className={classes.appBar} />
      <Drawer 
        variant="permanent"
        className={classes.drawer}
        classes={{ paper: classes.drawerPaper }} 
      />
      <main className={classes.appContent}>
        {/* Page content goes here */}
      </main>
    </div>
    

    样式

    const styles = theme => ({
      // The main flex container for the app's layout. Its min-height
      // is set to `100vh` so it always fill the height of the screen.
      root: {
        display: "flex",
        minHeight: "100vh",
        zIndex: 1,
        position: "relative",
        overflow: "hidden",
      },
      appBar: {
        zIndex: theme.zIndex.drawer + 1
      },
      // Styles for the root `div` element in the `Drawer` component.
      drawer: {
        width: theme.layout.drawerWidth
      },
      // Styles for the `Paper` component rendered by `Drawer`.
      drawerPaper: {
        width: "inherit",
        paddingTop: 64  // equal to AppBar height (on desktop)
      },
      // Styles for the content area. It fills the available space
      // in the flex container to the right (or left) of the drawer.
      appContent: theme.mixins.gutters({
        // https://github.com/philipwalton/flexbugs#flexbug-17
        flex: '1 1 100%', // Updated to fix IE 11 issue
        maxWidth: "100%",
        paddingTop: 80,   // equal to AppBar height + 16px
        margin: '0 auto',
        // Set the max content width for large screens
        [theme.breakpoints.up('lg')]: {
          maxWidth: theme.breakpoints.values.lg,
        },
      })
    

    实时示例(codesandbox)

    Permanent Drawer - clipped below appbar

    Permanent Drawer - full height

    【讨论】:

    • Luke,感谢您对这个解释和代码示例的超越。现在我看到它在行动,它很有意义
    • 嘿 Luke 非常感谢代码示例,它很有帮助。但是,我有一个固定的抽屉组件,我的 appContent 仍然没有填充可用空间,并且在左右两边都留下了空白。我使用了与您的第一个示例完全相同的代码,所以我不确定我做错了什么。 :(
    • @Annjawn,在 CodeSandbox 示例中,.appContentmax-width1200pxmargin: 0 auto。所以在大屏幕上,内容区域将被限制在1200px 并在抽屉右侧(或左侧)的空间中居中。 .root 元素总是填满视口的整个宽度。如果你从.appContent 中删除max-width,它应该会扩展为flex 容器的整个宽度(在抽屉旁边)。让我知道这是否有帮助
    • 嘿@LukePeavey,感谢您的回复。真正的问题是我的配置错误,因为我没有正确使用MuiThemeProvider。一旦我解决了这个问题,问题就消失了。
    • @Quill 感谢您的提醒。我将示例更新为最新的反应版本。他们现在正在工作
    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2016-10-09
    • 2020-12-11
    相关资源
    最近更新 更多