I know this may seem like a difficult task, and I understand that what I want to achieve might be nearly impossible.
My goal is to incorporate a dark/light mode switch on my website. The challenge lies in the fact that the site consists of static files on S3 and there's no server-side control at my disposal.
The issue I'm facing is how to load the correct theme when a new page is loaded. Since the value is stored in local storage, I need to retrieve it and adjust the value upon page load.
Currently, I've implemented an initial class of hidden
on the body element, which changes to either light
or dark
when DOMContentLoaded
event occurs. Unfortunately, this still results in one paint before the class is updated. Placing the JavaScript in the <head>
tag solved the problem in Safari and Chrome, but not in Firefox.
The issue with the initial paint occurring is that it creates a very abrupt flashing effect, especially when switching to dark mode as the screen swiftly transitions from white to black.
So, my query is whether there's a way to modify the classes of the body
element before the DOMContentLoaded
event triggers?
Thank you