hipstersmoothie / storybook-dark-mode

A storybook addon that lets your users toggle between dark and light mode.
MIT License
439 stars 56 forks source link

Fix: `current` param not being respected by the dark OS color mode #207

Closed beltranrengifo closed 1 year ago

beltranrengifo commented 1 year ago

Issue #206

Motivation

As described in the issue, the current param seems not to be respected. When loading the Storybook with OS color schema as dark, a default initialMode is always created with the dark value, so the re-evaluation of the current param value never happens again. I wrote my initial thoughts here in the issue.

Solution proposed

Respect the param

For this purpose, I added a new property in the store to explicitly control if the user has clicked in the theme handler. If so, we respect the user decision. If not, we respect the current param, and at the end we look at the preferred color schema.

Why a new property? I think relying on the initialMode is not safe because it is set from multiple side effects. Having a unique property ensure we know if the user has set a preference.

Not reacting to OS color schema changes

I decided to add a check to NOT react to color schema changes in the OS because of this two cases:

How to test

beltranrengifo commented 1 year ago

hi @hipstersmoothie ! hope you could take a look at this soon let me know if I can help 😃

hipstersmoothie commented 1 year ago

:rocket: PR was released in v2.0.5 :rocket: