-
Notifications
You must be signed in to change notification settings - Fork 27
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
Support switch between light and dark theme #39
Comments
Thank you for your contribution! This is very appreciated. |
That's an interesting question... Which theme are you using: Material? What are the triggers on it (what happens when you push that button)? |
I'm working on something similar, I aligned dark/light mode to a radio button like:
But it requires a refresh to render properly. I was going to look into adding a callback, but the documentation is a little confusing. EDIT: Okay, I don't know if this will work for you, but I got this working by doing (roughly):
That way, the page will refresh and the light/dark mode will automatically match the radio button. It's not perfect, but I don't think folks will mind the forced-refresh too much. |
@tylfin 👏 This looks like wizardry to me (I more or less understand what this is doing). But that was the whole idea behind the Using the ternary operator to decide between |
Does something remain to do on this issue? |
Following @tylfin worked for me, with some modifications for the material theme setup: docs/extra/refresh_on_toggle_dark_light.jsvar paletteSwitcher1 = document.getElementById("__palette_1");
var paletteSwitcher2 = document.getElementById("__palette_2");
paletteSwitcher1.addEventListener("change", function () {
location.reload();
});
paletteSwitcher2.addEventListener("change", function () {
location.reload();
}); mkdocs.ymlextra_javascript:
- extra/refresh_on_toggle_dark_light.js
plugins:
- mermaid2:
arguments:
# Theme auto switch (depending on toggle) doesn't work out of the box
# The user (or a script) will need to refresh the page when switching between dark/light modes
theme: |
^(JSON.parse(window.localStorage.getItem(__prefix('__palette'))).index == 1) ? 'dark' : 'light' |
@elgalu I am impressed. This is quite elegant. Is it a solution that would work in general? |
Thanks @fralau ! This should work when using the Material theme as it relies on mkdocs.ymltheme:
name: material
# https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#color-palette
palette:
- media: "(prefers-color-scheme: light)"
scheme: default
primary: indigo
accent: light-blue
toggle:
icon: material/toggle-switch-off-outline
name: Switch to dark mode
- media: "(prefers-color-scheme: dark)"
scheme: slate
primary: black
accent: deep orange
toggle:
icon: material/toggle-switch
name: Switch to light mode
# https://facelessuser.github.io/pymdown-extensions/extensions/superfences/
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:mermaid2.fence_mermaid |
@elgalu, this doesn't work since |
see #48 |
Could you consider switching the mermaid theme as you choose the light and dark theme by the button on the top bar of material-mkdocs?
Choosing the theme works well depending on the user's environment (I tested both mobile and desktop), however the theme is not changed when the user makes the manual selection. It just follows the default user environment theme.
This means that the light and dark themes work fine according to the documentation, but do not switch when the user selects the button.
So, for example, when the user has the light theme by default but decides to see the mermaid designs in the dark theme, the designs do not appear correctly.
The text was updated successfully, but these errors were encountered: