Comments (11)
Following @tylfin worked for me, with some modifications for the material theme setup:
docs/extra/refresh_on_toggle_dark_light.js
var paletteSwitcher1 = document.getElementById("__palette_1");
var paletteSwitcher2 = document.getElementById("__palette_2");
paletteSwitcher1.addEventListener("change", function () {
location.reload();
});
paletteSwitcher2.addEventListener("change", function () {
location.reload();
});
mkdocs.yml
extra_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'
from mkdocs-mermaid2-plugin.
I'm working on something similar, I aligned dark/light mode to a radio button like:
- mermaid2:
arguments:
theme: |
^(!document.getElementById("[radio button ID]").checked) ? 'dark' : 'light'
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):
// mkdocs.yml
plugins:
- mermaid2:
arguments:
theme: |
^(!document.getElementById("[radio button ID]").checked) ? 'dark' : 'light'
extra_javascript:
- https://unpkg.com/mermaid/dist/mermaid.min.js
- js/custom.js
// custom.js
var radioButton = document.getElementById("[radio button ID]");
radioButton.addEventListener("change", function() {
location.reload()
})
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.
from mkdocs-mermaid2-plugin.
@elgalu I am impressed. This is quite elegant.
Is it a solution that would work in general?
from mkdocs-mermaid2-plugin.
Thank you for your contribution! This is very appreciated.
from mkdocs-mermaid2-plugin.
That's an interesting question... Which theme are you using: Material?
What are the triggers on it (what happens when you push that button)?
from mkdocs-mermaid2-plugin.
@tylfin 👏 This looks like wizardry to me (I more or less understand what this is doing).
But that was the whole idea behind the ^
operator: it is used to declare javascript code directly in the config.yaml
file.
Using the ternary operator to decide between dark
and light
is actually quite clever.
from mkdocs-mermaid2-plugin.
Does something remain to do on this issue?
from mkdocs-mermaid2-plugin.
Thanks @fralau ! This should work when using the Material theme as it relies on id="__palette_{{ loop.index }}"
see material/partials/javascripts/palette.html , material/partials/header.html and local storage key
mkdocs.yml
theme:
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
from mkdocs-mermaid2-plugin.
Following @tylfin worked for me, with some modifications for the material theme setup:
This does not work when the page is read from file://
- see #46
from mkdocs-mermaid2-plugin.
@elgalu, this doesn't work since mkdocs-material
> 7.3.6. Could you help me to achieve the same effect for 8.1.0?
from mkdocs-mermaid2-plugin.
see #48
from mkdocs-mermaid2-plugin.
Related Issues (20)
- Unhandled exception when checking if URL exists HOT 5
- Add Zoom and Pan to Diagrams? (ER Diagram very tiny) HOT 5
- ZenUML: Using "Custom loader" leads to endless loops HOT 6
- Add support for mermaid gitgraph (make plugin compatible with mermaid lib > 10) HOT 2
- sequenceDiagram brokens HOT 5
- Superfences custom fence function does not seem to work HOT 14
- Cannot get theme to work on 10.2.4 HOT 3
- extra_javascript in mkdocs 1.5.0 is list of objects and no longer string HOT 5
- mkdocs build command prints deprecation warnings HOT 8
- Arguments in the config file do not work
- Tag the source HOT 3
- Include local `mermaid` library for offline support HOT 18
- javascript parameter seems not to work with default configuration HOT 5
- Mermaid2 diagrams with annotations shows half the (+) button and nothing happens when you click it HOT 9
- Markdown inside of node text is not rendering HOT 2
- Local Mermaid library not found HOT 11
- Handle module style inclusion of local Mermaid v10 library HOT 10
- Rendering Mermaid diagram with Pan and Zoom ability on mkdocs pages HOT 4
- Warnings in webdoc, because of relative links HOT 4
- [doc/readthedoc.io] link error in index HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mkdocs-mermaid2-plugin.