From 60eca317545c5708cfa3bd4b0b1108c11693d344 Mon Sep 17 00:00:00 2001 From: ycjcl868 <45808948@qq.com> Date: Mon, 23 Dec 2019 17:41:55 +0800 Subject: [PATCH] fix: localStorage --- site/theme/static/demo.less | 1 - site/theme/template/Layout/index.jsx | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/site/theme/static/demo.less b/site/theme/static/demo.less index 4fd474ed00..4bfeb23ea6 100644 --- a/site/theme/static/demo.less +++ b/site/theme/static/demo.less @@ -34,7 +34,6 @@ &, .code-box-demo { background-color: @component-background; - border-bottom: 1px solid @border-color-base; } .markdown { code { diff --git a/site/theme/template/Layout/index.jsx b/site/theme/template/Layout/index.jsx index 3feda5e17b..bee092a2b8 100644 --- a/site/theme/template/Layout/index.jsx +++ b/site/theme/template/Layout/index.jsx @@ -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);