I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
Thank you for your reply. & you got a valid point. I'll add your suggestion here & in the code sandbox. Will keep the getMediaPreference() as a default tho
Why keep it as a default? It really is BAD. My solution is SIMPLE, easy to implement and last but not least: secure and without any issues on all kind of browsers. My 2 pennies.
I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
With default, I meant as much as 'if there's no previous user preference in localStorage, use the result from getUserPreference. Please pick me up though on what's bad about reading out "(prefers-color-scheme: dark)" (besides missing support for IE11). I've seen it in other implementations and never had any issues using it.
I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
Right now I'm working on a multi themes setup. I'll come back to you later when a demo project has been finished. Will take me a couple of days (in my spare time).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Odd that I am the first one to react here. For this article is GREAT!
And above all: All steps work till the very end. Congrats.
That being said... getMediaPreference() is not a good idea for lots of reasons.
Simply replace it with a get from localStorage with f.i.:
getTheme() {this.setTheme(localStorage.getItem("user-theme"))
},
Thank you for your reply. & you got a valid point. I'll add your suggestion here & in the code sandbox. Will keep the
getMediaPreference()as a default thoWhy keep it as a default? It really is BAD. My solution is SIMPLE, easy to implement and last but not least: secure and without any issues on all kind of browsers. My 2 pennies.
With default, I meant as much as 'if there's no previous user preference in localStorage, use the result from
getUserPreference. Please pick me up though on what's bad about reading out"(prefers-color-scheme: dark)"(besides missing support for IE11). I've seen it in other implementations and never had any issues using it.Oké. I was in error. Nothing wrong with
getUserPreference. Thanks and good luck with all your work.:-)BTW: for just toggling between two color themes you can also do it with plain CSS. See developer.mozilla.org/en-US/docs/W...
Mh, how would you do this?
prefers-color-schemeis a read-only attribute in the browser's context.In another project (w/o Vue), I'm using a different approach with SASS though, creating a function & a few mixins to apply themes
In a
styles/state.scssfile:To apply it to the whole HTML - site: in
styles/index.scss:The JS code is quite similar to the one in the article as well
Right now I'm working on a multi themes setup. I'll come back to you later when a demo project has been finished. Will take me a couple of days (in my spare time).