Angular: Bootstrap 4 dark & light mode switch

半世苍凉 提交于 2019-12-18 06:44:52

问题


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!


回答1:


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




回答2:


I wasn't able to solve this using Angular CLI and Bootstrap 4 scss. I tried to configure the project to compile 2 lazy bundles, which works, but I could not have 2 scss files with dark.scss and light.scss each containing the same variables with different values, 1 resulting in the dark bundle, one resulting in then light bundle. What I ended up with is one scss source containing variables and styles for both dark and light, compiles into 2 bundles woth dark.scss and light.scss containing css overrides for each specific style. Thanks anyways guys for the suggestions!



来源:https://stackoverflow.com/questions/53077314/angular-bootstrap-4-dark-light-mode-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!