gpt4 book ai didi

reactjs - 使用 Styled Components 设置 Material-UI Drawer 组件的样式

转载 作者:行者123 更新时间:2023-12-01 22:00:14 25 4
gpt4 key购买 nike

我正在尝试将下面的抽屉组件的样式移植到样式组件中。

    <Drawer
variant="permanent"
classes={{
paper: classes.drawerPaper
}}
>

,其中 paper 的样式如下:

const styles = theme => ({
drawerPaper: {
position: "relative",
width: 240px
}
});

我不知道如何通过样式组件自定义 paper 属性。以下样式组件语法不起作用:

export const StyledDrawer = styled(Drawer)`
position: "relative";
width: 240px;
`;

source-code对于此组件,表明这是作为 PaperProps 作为 props 传递的,但我仍然找不到覆盖它的方法。

最佳答案

我最近在 https://codesandbox.io/s/material-demo-k9l9h 中给出了一个例子

希望对您有所帮助:

import React, { useState } from "react";
import Drawer from "@material-ui/core/Drawer";
import styled from "styled-components";

const drawerWidth = 240;

const styles = theme => ({
drawer: {
position: "absolute",
overflowX: "hidden",
zIndex: theme.zIndex.drawer + 2,
[theme.breakpoints.up("sm")]: {
position: "relative",
width: drawerWidth,
flexShrink: 0,
zIndex: theme.zIndex.drawer
},
whiteSpace: "nowrap"
},
drawerOpen: {
width: drawerWidth,
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen
})
},
drawerClose: {
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen
}),
overflowX: "hidden",
width: 0,
[theme.breakpoints.up("sm")]: {
width: theme.spacing.unit * 9 + 1
}
}
});

const StyledDrawer = styled(Drawer)`
${({ theme, open }) => {
const classes = styles(theme);
return {
...classes.drawer,
...(open ? classes.drawerOpen : classes.drawerClose)
};
}}

.MuiDrawer-paper {
${({ theme, open }) => {
const classes = styles(theme);
return open ? classes.drawerOpen : classes.drawerClose;
}}

&::-webkit-scrollbar {
width: 2px;
}
&:hover {
&::-webkit-scrollbar-thumb {
display: none;
}
}
&::-webkit-scrollbar-thumb {
display: none;
}
&::-webkit-scrollbar-track {
display: none;
}
}
`;

const PersistentDrawerLeft = () => {
const [isOpen, setIsOpen] = useState(false);

const handleDrawerOpen = () => {
setIsOpen(true);
};

const handleDrawerClose = () => {
setIsOpen(false);
};

return (
<div>
<StyledDrawer variant="permanent" open={isOpen}>
<span>sidebar</span>
<button onClick={handleDrawerClose}>close</button>
</StyledDrawer>
<span>Content</span>
<button onClick={handleDrawerOpen}>open</button>
</div>
);
};

export default PersistentDrawerLeft;

关于reactjs - 使用 Styled Components 设置 Material-UI Drawer 组件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49656531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com