Implementing darkmode in your webapp will be sugar for your nightowl readers. It implements a high-contrast color scheme that's soothing for the ey...
For further actions, you may consider blocking this person and/or reporting abuse
Hi there!
When I move pages, the button always has the toggle animation (even though the correct theme is active) when I enable dark mode.
So if I go from one page to the other, the toggle will initially move again while keeping the theme intact.
Any ideas? I went over your code and it looks the exact same for the script part.
Are you using the component within a router-view? It's possible the animation gets triggered whenever the component re-renders.
You could try and place the component outside of
<vue-router />
.Alternatively, you could try to abstract the CSS positioning into a separate class & dynamically apply it to the template:
If you can, please share your source code and I'll have a closer look :-)
So what I found out is that the transition css element should only be triggered on a click and now it triggers on every load.
The second solution didn't do it for me either.
My component is not being triggered within the vue router, but i found a different solution which is a little bit hacky:
.switch-toggle-checked {
transform: translateX(calc(var(--element-size) * 0.6)) !important;
transition: none;
}
This only shows the transition animation if you're in light mode.
Thank you a lot for this tutorial! It helped me a lot
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-scheme
is 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.scss
file: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).