diff --git a/src/App.js b/src/App.js index 0043513..1752422 100644 --- a/src/App.js +++ b/src/App.js @@ -21,7 +21,7 @@ class App extends Component { search_text: null, flow_render_key: +new Date(), token: localStorage['TOKEN']||null, - darkmode: window.matchMedia('(prefers-color-scheme: dark)').matches, + darkmode: App.is_darkmode(), }; this.show_sidebar_bound=this.show_sidebar.bind(this); this.set_mode_bound=this.set_mode.bind(this); @@ -33,9 +33,9 @@ class App extends Component { componentDidMount() { this.update_color_scheme(); - window.matchMedia('(prefers-color-scheme: dark)').addListener((e)=>{ + window.matchMedia('(prefers-color-scheme: dark)').addListener(()=>{ this.setState({ - darkmode: e.matches, + darkmode: App.is_darkmode(), }); }); } @@ -45,6 +45,14 @@ class App extends Component { this.update_color_scheme(); } + static is_darkmode() { + if(window.config.color_scheme==='dark') return true; + if(window.config.color_scheme==='light') return false; + else { // 'default' + return window.matchMedia('(prefers-color-scheme: dark)').matches; + } + } + update_color_scheme() { if(this.state.darkmode) document.body.classList.add('root-dark-mode'); diff --git a/src/Config.js b/src/Config.js index bc71abe..b494f41 100644 --- a/src/Config.js +++ b/src/Config.js @@ -19,6 +19,7 @@ const DEFAULT_CONFIG={ color_picker: true, easter_egg: true, comment_cache: false, + color_scheme: 'default', }; export function load_config() { @@ -119,6 +120,47 @@ class ConfigBackground extends PureComponent { } } +class ConfigColorScheme extends PureComponent { + constructor(props) { + super(props); + this.state={ + color_scheme: window.config.color_scheme, + }; + } + + save_changes() { + this.props.callback({ + color_scheme: this.state.color_scheme, + }); + } + + on_select(e) { + let value=e.target.value; + this.setState({ + color_scheme: value, + },this.save_changes.bind(this)); + } + + render() { + return ( +
+ 夜间模式: + + #color_scheme +
++ 选择浅色或深色模式,或者与系统颜色模式一致 +
+