materializecss / materialize

Materialize, a web framework based on Material Design
https://materializeweb.com/
MIT License
891 stars 104 forks source link

[Bug]: Changing Theme Switch is broken #498

Open JonathanWoollett-Light opened 2 weeks ago

JonathanWoollett-Light commented 2 weeks ago

Before submitting...

Context

I recently attempted to add a theme switch to my webpage (https://github.com/JonathanWoollett-Light/cv/commit/1e9323509877f1644fdba3fa83cc6bc0bf39282d). I copied the code from https://materializeweb.com/themes.html and the element from the page.

It functions like the theme switch on https://materializeweb.com/themes.html except that the page theme doesn't change.

Current Behavior

The icon changes and the correct case occurs but the theme isn't changed.

Expected behavior

The page theme should be flipped between light and dark.

Possible Solutions or Causes

No response

Steps to reproduce

Open https://github.com/JonathanWoollett-Light/cv/blob/1e9323509877f1644fdba3fa83cc6bc0bf39282d/index.html in a webbrowser and click the theme switch icon.

It looks like:

Screenshot from 2024-08-31 13-29-44

Your Environment

OlivierMadant commented 2 weeks ago

Hello Jonathan,

If I try to run in the console from your website (https://jonathanwoollett-light.github.io/cv/) document.documentElement.setAttribute('theme', 'dark'); then the theme is switching in dark mode.

If I run document.documentElement.removeAttribute('theme'); then the theme is switching back in light mode.

So the theme change seems to work properly...

I hope this comment helps you.

Olivier

JonathanWoollett-Light commented 1 week ago

It seems to not work when my device theme is darkmode but does work when in lightmode.

I think the example assumes the device theme is lightmode so that when document.documentElement.removeAttribute('theme'); is run it switches to darkmode.

But when the device theme is darkmode when you run document.documentElement.removeAttribute('theme'); the site will remain dark.

A fix would be changing document.documentElement.removeAttribute('theme'); to document.documentElement.setAttribute('theme','light');, possibly in the future it could be extended with a 3rd state that passthroughs the theme.