Dark Mode and Customized Colour Theme
Dark Mode and Customized Colour Theme
Global.scss
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
@tailwind base;
@tailwind components;
@tailwind utilities;
.dark {
--color-primary: #111827;
--color-secondary: red;
--color-primary-10: #FFFFFF;
--color-primary-50: #F9FAFB;
--color-primary-100: #F3F4F6;
--color-primary-200: #E5E7EB;
--color-primary-300: #D1D5DB;
--color-primary-400: #9CA3AF;
--color-primary-500: #6B7280;
--color-primary-600: #4B5563;
--color-primary-700: #374151;
--color-primary-800: #1F2937;
--color-primary-900: #000000;
--color-primary-btn: #fff;
}
.light {
--color-primary: #F9FAFB;
--color-secondary: blue;
--color-primary-10: #000000;
--color-primary-50: #111827;
--color-primary-100: #1F2937;
--color-primary-200: #374151;
--color-primary-300: #4B5563;
--color-primary-400: #6B7280;
--color-primary-500: #9CA3AF;
--color-primary-600: #D1D5DB;
--color-primary-700: #E5E7EB;
--color-primary-800: #F3F4F6;
--color-primary-900: #FFFFFF;
--color-primary-btn: #000;
}
.orange {
--color-primary-btn: hsl(25, 100%, 50%);
}
.red {
--color-primary-btn: red;
}
Initialising the colour styling for particular modes which we have dark and light themes.
Initialising the colour styling for orange and red this modes will change with in the dark or
light mode to change the theme colours.
This modes will apply to html tag and applying the style throughout the applications
where it is initialized.
Ex: <h1 class=”text-primary-10”>Hello</h1>
text- is tailwind predefined class to change styling for text with in div or tag.
primary-10 is custom class we are going initialize in div or tag.
Tailwind.config.js
/** @type {import('tailwindcss').Config} */
const flowbitePlugin = require('flowbite/plugin');
module.exports = {
darkMode: 'class',
content: [
"./src/**/*.{html,ts}",
"./node_modules/flowbite/**/*.js"
],
theme: {
extend: {
colors: {
primary: "var(--color-primary)",
"primary-btn": "var(--color-primary-btn)",
secondary: "var(--color-secondary)",
"primary-10": "var(--color-primary-10)",
"primary-50": "var(--color-primary-50)",
"primary-100": "var(--color-primary-100)",
"primary-200": "var(--color-primary-200)",
"primary-300": "var(--color-primary-300)",
"primary-400": "var(--color-primary-400)",
"primary-500": "var(--color-primary-500)",
"primary-600": "var(--color-primary-600)",
"primary-700": "var(--color-primary-700)",
"primary-800": "var(--color-primary-800)",
"primary-900": "var(--color-primary-900)",
}
}
},
plugins: [
flowbitePlugin
],
}
Create the dark/light mode toggle functionality with using moon/sun icons with
(click)="toggleMode('dark')" and (click)="toggleMode('light')"
Sending the mode to toggleMode function.
Initialised the classes from tailwind.config.js like bg-primary text-secondary
to add the classes base on the theme mode.
Ex: If theme mode is in dark bg-primary will be --color-primary: #111827;
And text-secondary will be --color-secondary: red;
If theme mode is in light bg-primary will be --color-primary: #F9FAFB;
And text-secondary will be --color-secondary: blue;
Create the orange and red custom colours toggle functionality with using buttons with
(click)="toggleMode(theme, 'orange')" and (click)="toggleMode(theme, 'red')".
Initialised the classes from tailwind.config.js like bg-primary-btn
to add the class on the custom button.
Ex: If custom mode is in orange bg-primary-btn will be --color-primary-btn: hsl(25,
100%, 50%);
If custom mode is in red bg-primary-btn will be --color-primary: red;
Header.component.ts
export class HeaderComponent implements OnInit {
theme = '';
primaryColor = '';
}
ngOnInit() {
this.theme =
localStorage.getItem('color-theme') ||
(!('color-theme' in localStorage) &&
window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light');
this.primaryColor = localStorage.getItem('primary-color') || '';
this.toggleMode(this.theme, this.primaryColor);
Initialised the empty theme= ‘ ‘ ; and primaryColor=’ ‘; to hold the value from
html.component.html file.
theme having the dark/light value and primaryColor having the orange or red value.
First checking the theme mode of the system/browser using this code:
this.theme =
localStorage.getItem('color-theme') ||
(!('color-theme' in localStorage) &&
window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light');
check if custom colour is there in localStore otherwise take the empty value.
Send the this.theme (dark/light value) and this.primaryColor(orange/red) value to
toggleMode function.
Now adding functionality for add or remove the themes/custom classes to html tag using
this code:
toggleMode(theme: string, primaryColor: string = '') {
if (primaryColor)
document.documentElement.classList.add(primaryColor);
if (
(!primaryColor || this.primaryColor != primaryColor) &&
this.primaryColor
)
document.documentElement.classList.remove(this.primaryColor);
document.documentElement.classList.remove(this.theme);
document.documentElement.classList.add(theme);
localStorage.setItem('color-theme', theme);
localStorage.setItem('primary-color', primaryColor);
this.theme = theme;
this.primaryColor = primaryColor;
}
Preview: