An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
Dark Mode - Unmistified!
Disclaimer!
This is going to be a long post. I will try and assimilate all the resources and information I have on a proper dark mode implementation on your website.
CSS Filter Hack!
I have explained this setup in my previous post. And I should repeat, it is only a hack and not a proper flawless dark theme implementation.
JS Implementation
So now we know, how to make an awesome dark mode using CSS. For the above-mentioned implementation, the javascript idea is very straightforward.
const html = document.getElementsByTagName('html')[0];const toggleTheme = (theme) => { html.dataset.theme = theme;}
And now all you have to do is call the toggleTheme()
method from UI with dark
and light
values respectively.
Native Dark/ Light mode ?
Am I telling you, that you can detect system preference for light/ dark mode from CSS or js?
Say hello to the sassy new Media Queries!
<script> // If `prefers-color-scheme` is not supported, fall back to light mode. // In this case, light.css will be downloaded with `highest` priority. if (window.matchMedia('(prefers-color-scheme: dark)').media === 'not all') { document.documentElement.style.display = 'none'; document.head.insertAdjacentHTML( 'beforeend', '<link rel="stylesheet" href="https://dev.to/light.css" onload="document.documentElement.style.display = \'\'">' ); }</script><!-- Conditionally either load the light or the dark stylesheet. The matching file will be downloaded with `highest`, the non-matching file with `lowest` priority. If the browser doesn't support `prefers-color-scheme`, the media query is unknown and the files are downloaded with `lowest` priority (but above I already force `highest` priority for my default light experience).--><link rel="stylesheet" href="/dark.css" media="(prefers-color-scheme: dark)"><link rel="stylesheet" href="/light.css" media="(prefers-color-scheme: light)"><!-- The main stylesheet --><link rel="stylesheet" href="/style.css">
What the above code does is, it checks for any preference set by the user regarding light mode/ dark mode. And according to this preference, the browser downloads appropriate CSS files.
One thing to note is even though the system preference would be for the dark mode, the browser does download the other CSS as well but with the lowest priority.
Now, what if we want to do all of it in javascript?
- dynamically check what is the preferred color scheme
- override theme in the application if the user wants to
- remember that decision by the user using
localStorage
Enter Javascript magic!
Self-advertisement Warning Rolling In
This is a library I built using vanilla js which does exactly what we have been discussing. And reducing your workload to just two lines
Include the js file
<script src="https://cdn.jsdelivr.net/gh/akhilarjun/tinylibs@latest/themejs/theme.min.js" onload="setupThemeIcon()"></script>
And an image with id theme-selector
and onclick function as switchTheme(this)
<img src="./sun.svg" data-light-src="./sun.svg" data-dark-src="./moon.svg" alt="light theme" id="theme-selector" onclick="switchTheme(this)">
data-light-src
and data-dark-src
if provided will be used to swap between icons while changing the theme attribute of Html
tag.
Mic Drop Time
The library will even auto-detect the system preference change and switch themes between dark and light.
And we will try doing this using chrome dev tools.
That's it for today guys. Cheers! Its Coffee time.
My days are fueled with coffees and only coffees. So I know, you know what we all should do
References
Original Link: https://dev.to/akhilarjun/dark-mode-unmistified-1ji6
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To