hugo-theme-gruvbox icon indicating copy to clipboard operation
hugo-theme-gruvbox copied to clipboard

setting defaultTheme has no effect

Open mikefarmer01 opened this issue 2 years ago • 2 comments

Setting params.defaultTheme in ./config/_default/config.toml has no effect. No matter whether set to "dark" or "light", the theme is always dark.

On the other hand, setting params.themeColor and params.themeContrast have the desired effects.

mikefarmer01 avatar Sep 02 '22 13:09 mikefarmer01

Theme selection is a bit convoluted, I probably should add some explanations somewhere.

When the site initially loads, it will set theme attribute of the html properly:

https://github.com/schnerring/hugo-theme-gruvbox/blob/dbf1af452041c500e276dd6589963b081769c849/layouts/_default/baseof.html#L2-L5

Inside dark-mode.js this behavior is overriden by the following logic:

https://github.com/schnerring/hugo-theme-gruvbox/blob/dbf1af452041c500e276dd6589963b081769c849/assets/js/dark-mode.js#L1-L31

  1. Check whether a preferred theme is stored inside local storage. This will always have a value after the page loaded for the first time (Initial Load). So if you debugged the page and set defaultTheme after, you'll have to delete cookies/site data.
  2. Also, if the browser supports it, the OS theme-preference is checked which overrides defaultTheme. If you want to test this, disable JavaScript.
  3. If neither (1) and (2) (Undefined) are true, the defaultTheme will be displayed.

schnerring avatar Sep 02 '22 20:09 schnerring

Understood.
So in my case, it's because the brower's default theme.

Thanks for the detailed explanation. Where should it be documented?

mikefarmer01 avatar Sep 08 '22 20:09 mikefarmer01

The README or the code/config file.

schnerring avatar Oct 03 '22 19:10 schnerring