Skip to content
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

Closed
jrappen opened this issue Mar 25, 2020 · 8 comments
Closed

dark mode implementation #1092

jrappen opened this issue Mar 25, 2020 · 8 comments

Comments

@jrappen
Copy link
Contributor

jrappen commented Mar 25, 2020

@anikethsaha Thanks for this addition in #1031. Maybe you could review the exact implementation of this functionality and:

  • make the detection automatic, as most (current) browsers support it
  • while removing the toggle button

please compare the discussion in:

for details.

@anikethsaha
Copy link
Member

  • make the detection automatic, as most (current) browsers support it

This can be useful

while removing the toggle button

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.

@anikethsaha anikethsaha added the wait for information something is not clear, waiting for the author of the issue/pr label Apr 1, 2020
@jrappen
Copy link
Contributor Author

jrappen commented Apr 9, 2020

@anikethsaha

I'm talking about prefers-color-scheme and removing this button:

image

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 dark theme in ./src/themes is a dark version of the vue theme. So maybe it would make sense to:

  • merge the dark theme into the vue theme and make sure it displays with the correct color palette for each preferences
  • then extend buble, dolphin and pure with dark palettes of their own
  • and fix the themes page functionality while at it.

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.

@jrappen
Copy link
Contributor Author

jrappen commented Apr 9, 2020

If you need screencasts for visuals, let me know. I was too lazy to make those without an expressed need.

@anikethsaha
Copy link
Member

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,
Also, if you want you can submit a PoC for this about the implementation and we can decide that.

@jrappen
Copy link
Contributor Author

jrappen commented Apr 10, 2020

Sounds good. I'd vote for dropping that toggle button though and making it seamlessly follow the OS.
Not sure I have time enough for the PoC, I'll see what I can do. My JavaScript knowledge is limited.

@anikethsaha anikethsaha removed the wait for information something is not clear, waiting for the author of the issue/pr label Apr 10, 2020
@jsejcksn
Copy link

jsejcksn commented Sep 9, 2020

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). 🌞 🌚 👀

@JaninaWibker
Copy link

JaninaWibker commented Oct 7, 2020

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:

  • no theme selected
  • dark theme selected
  • light theme selected

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 theme is already selected use 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())

@jrappen
Copy link
Contributor Author

jrappen commented May 31, 2022

Closing as stale.

@jrappen jrappen closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants