这是createStyles
我的 Typescript 代码中的一个语句:
const useStyles = makeStyles((theme: Theme) =>
createStyles({
menuOpen: {
zIndex: 1,
width: 200,
height: '100%',
position: 'fixed',
top: 48,
transition: 'left .1s',
marginRight: theme.spacing(2),
left: 0,
background: '#3f51b5',
'& div:first-element': {
marginTop: 100
}
},
menuClose: {
zIndex: 1,
width: 200,
height: '100%',
position: 'fixed',
top: 48,
transition: 'left .1s',
marginRight: theme.spacing(2),
left: -200,
background: '#3f51b5',
'& div:first-element': {
marginTop: 100
}
},
}),
);
我在这些之间切换以打开或关闭菜单(使用useState
变量)。如您所见,这些样式具有共同的属性。我如何才能制作出menu
这两种风格可以继承的风格?
编辑:
所以看起来过渡的东西不起作用,我想知道那是不是因为 React 重新加载了那些 DOM 元素?我尝试在menuClose
和menuOpen
样式中添加过渡,以防万一它需要它们,但菜单就像它没有过渡属性一样。
在@doglozano的回答之后,我的createStyles
函数中有以下内容:
// I've even tried putting the transition in a div wrapper
// but I think that element is getting overwritten also.
toolBar: {
'& div': {
transition: 'left .1s'
}
},
menu: {
zIndex: 1,
width: 200,
height: '100%',
position: 'fixed',
top: 48,
transition: 'left .1s',
marginRight: theme.spacing(2),
left: -200,
background: '#3f51b5',
'& div:first-element': {
marginTop: 100
}
},
menuOpen: {
left: 0,
transition: 'left .1s',
},
menuClose: {
left: -200,
transition: 'left .1s',
},
当我的菜单关闭时:
.makeStyles-menuClose-7 {
left: -200px;
transition: left .1s;
}
.makeStyles-menu-5 {
top: 48px;
left: -200px;
width: 200px;
height: 100%;
z-index: 1;
position: fixed;
background: #3f51b5;
transition: left .1s;
margin-right: 16px;
}
.makeStyles-toolBar-4 {
transition: left .1s;
}
当我的菜单打开时:
.makeStyles-menuOpen-6 {
left: 0;
transition: left .1s;
}
.makeStyles-menu-5 {
top: 48px;
left: -200px;
width: 200px;
height: 100%;
z-index: 1;
position: fixed;
background: #3f51b5;
transition: left .1s;
margin-right: 16px;
}
.makeStyles-toolBar-4 {
transition: left .1s;
}
编辑 2: 这是我的Sandbox 链接。
有任何想法吗?