fix: localStorage

This commit is contained in:
ycjcl868 2019-12-23 17:41:55 +08:00
parent 7acab87a03
commit 60eca31754
2 changed files with 11 additions and 4 deletions

View File

@ -34,7 +34,6 @@
&,
.code-box-demo {
background-color: @component-background;
border-bottom: 1px solid @border-color-base;
}
.markdown {
code {

View File

@ -53,6 +53,8 @@ enquireScreen(b => {
isMobile = b;
});
const SITE_THEME_STORE_KEY = 'site-theme';
export default class Layout extends React.Component {
static contextTypes = {
router: PropTypes.object.isRequired,
@ -72,7 +74,10 @@ export default class Layout extends React.Component {
this.state = {
appLocale,
isMobile,
theme: 'default',
theme:
typeof localStorage !== 'undefined'
? localStorage.getItem(SITE_THEME_STORE_KEY) || 'default'
: 'default',
setTheme: this.setTheme,
};
}
@ -83,6 +88,7 @@ export default class Layout extends React.Component {
}
componentDidMount() {
const { theme } = this.state;
const { router } = this.context;
router.listen(loc => {
if (typeof window.ga !== 'undefined') {
@ -95,6 +101,8 @@ export default class Layout extends React.Component {
}
});
this.setTheme(theme);
const nprogressHiddenStyle = document.getElementById('nprogress-style');
if (nprogressHiddenStyle) {
this.timer = setTimeout(() => {
@ -122,7 +130,7 @@ export default class Layout extends React.Component {
if (dom) {
dom.remove();
}
localStorage.removeItem('site-theme');
localStorage.removeItem(SITE_THEME_STORE_KEY);
} else {
const style = document.createElement('link');
style.type = 'text/css';
@ -130,7 +138,7 @@ export default class Layout extends React.Component {
style.id = 'theme-style';
style.href = '/dark.css';
localStorage.setItem('site-theme', 'dark');
localStorage.setItem(SITE_THEME_STORE_KEY, 'dark');
document.body.append(style);
}
document.body.setAttribute('data-theme', theme);