如何设置抽屉组件上的 zIndex

Va_*_*a_M 7 javascript z-index reactjs material-ui

我正在努力实现clipped under the app bar临时抽屉的风格。但我似乎无法在临时抽屉上设置 z 索引。

modalMaterial-ui 中的临时抽屉具有组件的 z 索引,1300正如我在此处提出的问题中提到的https://github.com/mui-org/material-ui/issues/22562

因此,如果我使用文档中给出的将应用程序栏 zIndex 设置为 的方法theme.zIndex.modal + 1,我可以获得“在应用程序栏下剪辑”效果。但这也意味着我的应用栏将高于我的所有模式。这不是我想要的。

因此,我想将临时抽屉的 z 索引设置为 ,1250并将应用程序栏的 z 索引设置为 ,1251以获得所需的效果而不产生任何副作用。

我正在尝试使用钩子设置 zIndex,makeStyles如您在沙箱中看到的以及下面的代码片段:

const useStyles = makeStyles((theme) => ({
  appBar: {
    zIndex: 1251
  },
  drawer: {
    width: drawerWidth,
    flexShrink: 0,
    zIndex: 1250
  },
  drawerPaper: {
    width: drawerWidth
  }
}));
Run Code Online (Sandbox Code Playgroud)
<AppBar position="fixed" className={classes.appBar}>
   .
   .
   .
</AppBar>
      
Run Code Online (Sandbox Code Playgroud)
<Drawer
  className={classes.drawer}
  open={true}
  classes={{
    paper: classes.drawerPaper
  }}
>
   .
   .
   .
</Drawer>
Run Code Online (Sandbox Code Playgroud)

codesandbox:https://codesandbox.io/s/material-demo-forked-rq1fj?file =/demo.js

Nea*_*arl 4

如果您不想使用,important!可以zIndex使用 Material-UI 主题 API 或内联样式进行覆盖。

const theme = createMuiTheme({
  zIndex: {
    appBar: 1251,
    modal: 1250,
  }
});

...

<ThemeProvider theme={theme}>
  <Demo />
</ThemeProvider>,
Run Code Online (Sandbox Code Playgroud)

这种方法的缺点是样式适用于所有模态,因此您的实际模态现在低于 ,这AppBar可能不是您想要的。

第二种方法是通过在组件的 style props 中传递样式对象来内联 css 样式

<AppBar
  className={classes.appBar}
  style={{ zIndex: 1251 }}
>
</AppBar>
<Drawer
  className={classes.drawer}
  style={{ zIndex: 1250 }}
>
Run Code Online (Sandbox Code Playgroud)

现场演示

编辑材质-UI - 覆盖 zIndex