-
Notifications
You must be signed in to change notification settings - Fork 104
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
Detect Dark Mode Settings #978
Conversation
frontend/src/store/index.js
Outdated
state.darkMode = value | ||
localStorage.setItem('global/dark-mode', value) | ||
Vue.vuetify.framework.theme.dark = value | ||
if (value !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view this is an antipattern. A mutation should only mutate the store state and not run mediaqueries. The new value must come from outside the mutation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should start using store plugins for this kind of problems.
export default function createMediaPlugin (window) {
return store => {
const colorScheme = window.localStorage.getItem('global/color-scheme')
store.commit('SET_COLOR_SCHEME', colorScheme)
const mq = window.matchMedia('(prefers-color-scheme: dark)')
switch (store.state.coloScheme) {
case 'dark':
store.commit('SET_DARK_THEME', true)
break
case 'light':
store.commit('SET_DARK_THEME', false)
break
default:
store.commit('SET_DARK_THEME', mq.matches)
break
}
mq.addEventListener(e => {
if (!['dark', 'light'].includes(store.state.coloScheme)) {
store.commit('SET_DARK_THEME', e.matches)
}
})
}
}
frontend/src/app.js
Outdated
@@ -21,8 +21,13 @@ const App = Vue.extend({ | |||
} | |||
}) | |||
|
|||
const darkMode = this.$localStorage.getItem('global/dark-mode') === 'true' | |||
const darkMode = this.$localStorage.getItem('global/setting-dark-mode') || 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why global/seeting-dark-mode
and not global/dark-mode
or global/settings/dark-mode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would propose to have a localStorage porperty 'global/color-scheme' or 'global/settings/color-scheme' with possible values dark
, light
and any other value means use system settings
return 0 | ||
case 'dark': | ||
return 1 | ||
default: | ||
return 2 | ||
} | ||
}, | ||
set (value) { | ||
this.setDarkMode(value) | ||
switch (value) { | ||
case 0: | ||
this.setColorScheme('light') | ||
break | ||
case 1: | ||
this.setColorScheme('dark') | ||
break | ||
default: | ||
this.setColorScheme('auto') | ||
break | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use constants instead of magic/index numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Co-authored-by: Peter Sutter <peter.sutter@sap.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
What this PR does / why we need it:
With this PR the Dashboard can detect the system settings for dark mode
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Release note: