Material-UI themeで他要素の規定パラメータを取得
React
でmaterial ui
を使っていて、ある要素を他要素より手前に持ってきたかった。
const styles = (theme: Theme): StyleRules => createStyles({
mainAppBar: {
flexGrow: 1,
},
selectYearForm: { // これを手前に持ってきたい
top: theme.spacing(3),
right: theme.spacing(5),
position: "fixed",
width: 80,
},
}
これだけだとselectYearForm
が背後に隠れる。

勿論、それぞれにzIndex
(z-index
)を指定すれば手前に持ってこれる
const styles = (theme: Theme): StyleRules => createStyles({
mainAppBar: {
flexGrow: 1,
zIndex: 100
},
selectYearForm: { // これを手前に持ってきたい
top: theme.spacing(3),
right: theme.spacing(5),
position: "fixed",
width: 80,
zIndex: 110
},
}
ただ、既存のmainAppBar
のzIndex
に影響を与えずにselectYearForm
を手前に持ってきたかった。
以下のように書いて解決。
const styles = (theme: Theme): StyleRules => createStyles({
mainAppBar: {
flexGrow: 1,
},
selectYearForm: { // これを手前に持ってきたい
top: theme.spacing(3),
right: theme.spacing(5),
position: "fixed",
width: 80,
zIndex: theme.zIndex.appBar + 1
},
}

なお、Material UIでコンポーネントのデフォルトz-index
は以下のようになっている。
const zIndex = {
mobileStepper: 1000,
speedDial: 1050,
appBar: 1100,
drawer: 1200,
modal: 1300,
snackbar: 1400,
tooltip: 1500,
};
https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/styles/zIndex.js