-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dark mode implementation #1092
Comments
This can be useful
Can you explain this. Can you share your implementation for the 1st point if you have any also, the dark mode plugin is here, so you can refer that as well if required. |
I'm talking about as we can fetch the preference without needing to add a toggle. Compare the following:
which switches between dark and light when you have the site open in a supported browser on a supported OS and switch between dark and light mode in the OS. My issue with #1031 is that:
It seems the
I know, the vuepress example above adds themes to the default, like your plugin. I'm suggesting we could skip a beat though, and fix the core here rather than your plugin. |
If you need screencasts for visuals, let me know. I was too lazy to make those without an expressed need. |
Actually this plugin is just temp. I am planning to have a dark mode switcher from the core as docusaurus does. I need to look at your given links more precisely, |
Sounds good. I'd vote for dropping that toggle button though and making it seamlessly follow the OS. |
Preference detection is great! I think having the toggle option is a useful accessibility feature. Just because a person's OS environment is set to dark mode doesn't necessarily mean that he wishes to read documentation in dark mode (and vice versa). 🌞 🌚 👀 |
After having OS preference detection on a personal docsify site for probably more than a year (and a self-made darkmode implementation) this is what I've noticed works pretty well: Tri-state logic:
If no theme is selected (visiting the site for the first time) choose a theme based on the OS preference (and save it). If a preference change is detected while the site is open update the theme. The user can update the selected theme using a toggle / button. The code to implement something like this is pretty short: const determineInitialTheme = () => {
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches
const isLightMode = window.matchMedia("(prefers-color-scheme: light)").matches
const isNotSpecified = window.matchMedia("(prefers-color-scheme: no-preference)").matches
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
if(localStorage.getItem('docsify.theme')) return localStorage.getItem('docsify.theme') // saved theme
else if(!hasNoSupport && isDarkMode) return 'dark' // system dark mode
else if(!hasNoSupport && isLightMode) return 'light' // system light mode
else if(!hasNoSupport && isNotSpecified) return 'light' // system unspecified
else return 'light' // default
}
const toggleTheme = (theme) => { /* ... */ }
window.matchMedia("(prefers-color-scheme: dark)").addListener(e => e.matches && toggleTheme('dark'))
window.matchMedia("(prefers-color-scheme: light)").addListener(e => e.matches && toggleTheme('light'))
toggleTheme(determineInitialTheme()) |
Closing as stale. |
@anikethsaha Thanks for this addition in #1031. Maybe you could review the exact implementation of this functionality and:
please compare the discussion in:
for details.
The text was updated successfully, but these errors were encountered: