Ways websites implement dark mode

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Interesting article. I recommend adding tags like JavaScript and CSS for better reach. Total 5 tags are allowed - so you probably need to remove some.
sure, I am new to the platform. Still making my way around it. Thanks for the advice.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

A lot of websites these days are providing users with option of Dark Mode. This is getting increasingly popular. But it could sometimes be a challenge for the devs at least for the existing applications which were not designed considering dark mode in mind.
So following are the ways the devs are implementing dark mode in their websites -
many UI frameworks support dark mode or themes natively. Ex- Vuetify
// src/plugins/vuetify.js
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
Vue.use(Vuetify)
export default new Vuetify({
theme: {
dark: true,
},
})
To toggle -
// make a switch
<v-switch
v-model="$vuetify.theme.dark"
hide-details
inset
label="Theme Dark"
></v-switch>
Damn simple! Vuetify internally makes use of many variables - primary, secondary, success, background etc. Which is customisable as well. Similarly, many other frameworks have native support for Dark mode and themes. So, if you are starting with a new project - choose a framework which supports dark mode.
Using css variables are another way people implement dark mode, but this should only be done if the above two methods are not an option. This will require changing the css code in multiple places.
Start with defining css vars for both light and dark modes
:root {
--primary-color: #302AE6;
--secondary-color: #536390;
--font-color: #424242;
--bg-color: #fff;
--heading-color: #292922;
}
[data-theme="dark"] {
--primary-color: #9A97F3;
--secondary-color: #818cab;
--font-color: #e1e1ff;
--bg-color: #161625;
--heading-color: #818cab;
}
Make use of the css vars in the css style
body {
background-color: var(--bg-color);
color: var(--font-color);
}
h1 {
color: var(--secondary-color);
}
a {
color: var(--primary-color);
}
Add js code to change the data-theme
function setDarkTheme(){
document.documentElement.setAttribute('data-theme', 'dark');
}
function setLightTheme(){
document.documentElement.setAttribute('data-theme', 'light');
}
This is considered a bad way to implement the dark mode, but still exists as an option for people who have simple websites.
// css style
body {
filter: invert(100%);
}
// undo inversion on images
img {
filter: invert(100%);
}
This works well in text only websites which make use of a simple color scheme.
In this case we will need to define two classes for each element we want to change the mode for -
p.light {
background: #fff;
color: #000;
}
p.dark{
background: #000;
color: #fff;
}
We will then need to put the class in the respective html tags
<p class="light">Some text</p>
Next we will need to add js code to replace the classes (light->dark or dark -> light)
function setDarkMode() {
var x = [...document.getElementsByClassName("light")];
x.forEach(function (el,index){
el.classList.add("dark");
el.classList.remove("light");
})
}
function setLightMode() {
var x = [...document.getElementsByClassName("dark")];
x.forEach(function (el,index){
el.classList.add("light");
el.classList.remove("dark");
})
}