You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My website has a button to manually switch between dark and light mode.
The button click event implements the switch by adding or removing the "dark" class to the top level HTML tree.
Now instead of dark:{class} classes being applied based on prefers-color-scheme, they will be applied whenever the dark class is present earlier in the HTML tree.
How you add the dark class to the html element is up to you, but a common approach is to use a bit of JavaScript that reads a preference from somewhere (like localStorage) and updates the DOM accordingly.
<!-- Dark mode not enabled --><html><body><!-- Will be white --></body></html><!-- Dark mode enabled --><htmlclass="dark"><body><!-- Will be black --></body></html>
By referring to Tailwind CSS, we can learn some tips.
For dark mode detection, the package should leave the decision to the developer. Package only detecting the dark class of the outer DOM.
Packages should not detect @media (prefers-color-scheme: dark). Since it is read-only, projects cannot change the appearance of components by toggling this value.
What developers do in their projects
Automatic switching: listen to @media (prefers-color-scheme: dark) and switch the dark class.
Manual switching: dark mode switch button.
User preferences: local storage, browser session storage, server storage, etc.
Package only do
The package only needs to add the dark mode CSS to the dark class, no other behavior is required.
Hey man. The idea of this component is to mimic or be aligned with the browser UI. This concerns the language and dark mode. Because user experience start from this custom dialog and continues in browser UI, and it's important to keep it consistend.
OK, thanks for the clarification. This plugin is designed to complement the browser's capabilities, not to extend functionality to project. I understand now.
My website has a button to manually switch between dark and light mode.
The button click event implements the switch by adding or removing the "dark" class to the top level HTML tree.
Why use Dark Mode by adding a "dark" class?
Tailwind CSS recommends doing this. Dark mode in Tailwind CSS:
By referring to Tailwind CSS, we can learn some tips.
For dark mode detection, the package should leave the decision to the developer. Package only detecting the
dark
class of the outer DOM.Packages should not detect
@media (prefers-color-scheme: dark)
. Since it is read-only, projects cannot change the appearance of components by toggling this value.What developers do in their projects
@media (prefers-color-scheme: dark)
and switch thedark
class.Package only do
The package only needs to add the dark mode CSS to the
dark
class, no other behavior is required.More
This issue also resolves #16 .
The text was updated successfully, but these errors were encountered: