Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Bootstrap 4 dark & light mode switch

What is the best way to implement a dark mode and light mode throughout my bootstrap 4 (scss) angular application? Angular cli compiles the scss files, so the old way of including a separate css file for each theme does not appeal to me. How?? Set a body class and use .dark / .light throughout my scss files? Use mixins? Use bootstrap color themes?

Any ideas are welcome!

like image 993
Mcanic Avatar asked Oct 31 '18 06:10

Mcanic


1 Answers

Don't know if this was the best way, but this is what i did.

I used [ngClass]="setPrimary() on the top most component

this setPrimary function checked the time of the day (https://api.sunrise-sunset.org) and depending upon the hour of night or sunrise/sunset or day it would set the class on this top most component

inside the scss file: i developed 3 different color sets for day, night, sunrise/sunset

on page load, default value was day, but as soon as setPrimary() would place the appropriate class, the rest of the styling will follow it

like image 194
Akber Iqbal Avatar answered Sep 29 '22 07:09

Akber Iqbal