Skip to content

feat: Added support for automatic dark/light mode based on user os settings #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/components/top-nav/settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import Head from './head.jsx';
var THEMES = [
// value, display_name
'light', 'Light',
'dark', 'Dark'
'dark', 'Dark',
'auto', 'Auto'
];

var MODES = [
Expand Down Expand Up @@ -84,7 +85,8 @@ class Settings extends React.Component {

this.state = {
// Boolean indicating whether the settings menu is open or closed:
'open': false
'open': false,
'selectedTheme': props.theme
};
}

Expand Down Expand Up @@ -127,7 +129,14 @@ class Settings extends React.Component {
* @param {Object} event - event object
*/
_onThemeChange = ( event ) => {
this.props.onThemeChange( event.target.value );
const selectedTheme = event.target.value;
let effectiveTheme = selectedTheme;
if(selectedTheme === 'auto'){
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
effectiveTheme = prefersDark ? 'dark' : 'light';
}
this.setState({ selectedTheme });
this.props.onThemeChange(effectiveTheme);
}

/**
Expand Down Expand Up @@ -246,7 +255,7 @@ class Settings extends React.Component {
className="settings-select"
onChange={ this._onThemeChange }
>
{ this._renderOptions( THEMES, this.props.theme ) }
{ this._renderOptions( THEMES, this.state.selectedTheme ) }
</select>
<div className="settings-select-custom">
<ChevronDownIcon className="settings-select-custom-icon"/>
Expand Down