gpt4 book ai didi

javascript - 设置 Material UI Snackbar 的背景颜色

转载 作者:行者123 更新时间:2023-12-01 00:36:42 24 4
gpt4 key购买 nike

当我设置 className 来更改 Snackbar 的背景时,它不会覆盖它。相反,当页面呈现时,它会暂时显示我想要的背景颜色,然后立即被覆盖。

我查看了其他一些 Stackoverflow 答案,但仍然无法使其正常工作。

// imports....
import Snackbar from '@material-ui/core/Snackbar';
import createClientsStyle from "../../../PageComponents/Landing/CreateClients/style";

function CreateClients(props) {

//....code

const { classes } = props;

return (

//............code

<div>

<Snackbar

className={classes.snackbarStyle} //<--- here

anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}

open={true}
autoHideDuration={6000}

ContentProps={{
'aria-describedby': 'message-id',

}}

message={<span id="message-id"><div>Hi there! Some message.</div></span>}

/>
</div>

);
}

CreateClients.propTypes = {
classes: PropTypes.object.isRequired
}

const styles = (theme)=>(createClientsStyle(theme));

export default withStyles(styles)(CreateClients)

样式表

const createClientsStyle = function(theme){
return {
root: {
flexGrow: 1,
position:"relative",
top:"175px"

},

logoContainer:{
position:"relative",
margin:"0 auto",
top:"120px"
},
container: {
marginTop:"0px",
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
},
clientItem:{
fontSize:"2em",
display:"inline-block",
textAlign:"center",
width:"100%",
color:"grey",
'&:hover': {
background: "#8a0eff3b",
transition: "0.4s"
},
},
clientItemSelected: {
background: "#8a0eff3b",
fontSize:"2em",
display:"inline-block",
textAlign:"center",
color:"grey",
'&:hover': {
background: "#8a0eff3b",
transition: "0.4s"
},
},

textField:{
width:"25em",
},

listItem:{
fontSize:'35px',//Insert your required size

},
clientButton:{
backgroundColor:"purple"
},

tinyTextClickMe:{
position:"relative",
fontSize:"0.5em",
right:"10%"
},
snackbarStyle:{
backgroundColor:"orange"
}
}
}

export default createClientsStyle

最佳答案

Snackbar component处理打开/关闭状态、转换和定位,但 SnackbarSnackbar 外观的控制(例如背景颜色、版式、填充)委托(delegate)给 SnackbarContent component .

如果你看Customized snackbars demo ,您将看到它通过在 SnackbarContent 元素而不是在 Snackbar 元素上指定 className 来更改背景颜色。您还可以通过在 ContentProps 中指定 className 来实现相同的效果。

下面的示例演示了指定内容的类名的两种方法。

import React from "react";
import ReactDOM from "react-dom";

import Snackbar from "@material-ui/core/Snackbar";
import SnackbarContent from "@material-ui/core/SnackbarContent";
import { withStyles } from "@material-ui/core/styles";
const styles = {
snackbarStyleViaContentProps: {
backgroundColor: "orange"
},
snackbarStyleViaNestedContent: {
backgroundColor: "lightgreen",
color: "black"
}
};
function App({ classes }) {
return (
<div>
<Snackbar
anchorOrigin={{
vertical: "top",
horizontal: "right"
}}
open={true}
ContentProps={{
"aria-describedby": "message-id",
className: classes.snackbarStyleViaContentProps
}}
message={
<span id="message-id">
<div>Hi there! Some message.</div>
</span>
}
/>
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "right"
}}
open={true}
>
<SnackbarContent
aria-describedby="message-id2"
className={classes.snackbarStyleViaNestedContent}
message={
<span id="message-id2">
<div>Hi there! Message 2.</div>
</span>
}
/>
</Snackbar>
</div>
);
}

const StyledApp = withStyles(styles)(App);

const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);

Edit Snackbar background color

关于javascript - 设置 Material UI Snackbar 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58074039/

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