【发布时间】:2021-03-17 10:25:05
【问题描述】:
我试图让我的管理员能够更改我的项目的品牌和颜色,我目前正在使用 CSS 自定义属性来执行此操作,但是,这似乎并没有节省。当我刷新页面时,它会将颜色刷新回原始状态。有什么办法可以解决这个问题吗?
代码
JavaScript - 管理面板
import React, { Component } from 'react'
import './CompanyBranding.scss'
import firebase from 'firebase'
export default class CompanyBranding extends Component {
state = {
content: [],
textbox: "",
link: "",
primaryColour: "",
secondaryColour: ""
}
componentDidMount() {
firebase.firestore().collection('site').doc('public').collection('footer').doc('content').get().then(doc => {
const content = []
const data = doc.data()
this.setState(data)
content.push(data)
this.setState({content})
this.setState({contentLength: content.length})
})
}
handleChange = (e) => {
this.setState({
[e.target.id]: e.target.value,
})
}
handleSubmit = (e) => {
e.preventDefault();
firebase.firestore().collection('site').doc('public').collection('footer').doc('content').set({
textbox: this.state.textbox,
link: this.state.link
}).then(alert("Updated"),
document.getElementById("companydetails").reset()
)
document.documentElement.style.setProperty(
'--primary-color',
this.state.primaryColour
)
document.documentElement.style.setProperty(
'--secondary-color',
this.state.secondaryColour
)
}
render() {
return (
<div>
<div className="CompanyBranding">
<h1 className = "PageHeading"> Branding</h1>
<form className = "CompanyBrandingForm">
<label htmlFor = "Logo"> Company Logo</label> <br/>
<input type = "file" id = "Logo" name = "Logo" accept = "image/png, image/jpeg"/><br/><br/>
<label htmlFor = "PrimaryColour"> Primary Colour </label> <br/>
<input type = "color" id = "primaryColour" name = "PrimaryColour" onChange = {this.handleChange}/><br/><br/>
<label htmlFor = "SecondaryColour"> Secondary Colour </label> <br/>
<input type = "color" id = "secondaryColour" name = "SecondaryColour" onChange = {this.handleChange}/><br/><br/>
<button onClick = {this.handleSubmit} className = "SubmitButton"> Submit </button>
</form>
</div>
<div className="FooterContent">
<h1 className = "PageHeading"> Footer </h1>
<form className = "CompanyBrandingForm">
<label htmlFor = "textbox"> Text box</label> <br/>
<textarea id = "textbox" value = {this.state.textbox} onChange = {this.handleChange} />
<br/><br/>
<label htmlFor = "link"> GDPR link </label> <br/>
<input type = "text" id = "link" value = {this.state.link} onChange = {this.handleChange}/>
<button onClick = {this.handleSubmit} className = "SubmitButton"> Submit </button>
</form>
</div>
</div>
)
}
}
变量.scss
:root {
--primary-color: #2f78e6;
--secondary-color:#2d4f81;
}
// Primary colour (can be changed by admin)
$base-colour: var(--primary-color);
$secondary-color: var(--secondary-color);
【问题讨论】:
标签: javascript css reactjs sass css-variables