Skip to content

Commit 164b982

Browse files
feat: dark mode 🕶️ (#32)
* feat: dark mode * feat: add flag for darkmode --------- Signed-off-by: Jan Larwig <[email protected]> Co-authored-by: Yves Chevallier <[email protected]>
1 parent ec5f085 commit 164b982

File tree

9 files changed

+101
-1
lines changed

9 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ plugins:
7575
tooltips: true # control if tooltips will be shown (default: true)
7676
edit: true # control if edit button will be shown in the lightbox view (default: true)
7777
border: 10 # increase or decrease the border / margin around your diagrams (default: 0)
78+
darkmode: true # support darkmode. allows for automatic switching between dark and lightmode based on the theme toggle. (default: false)
7879
```
7980
8081
## Material Integration

examples/docs/configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ plugins:
2828
# Control if edit button will be shown in the lightbox view
2929
# (data-edit)
3030
edit: true
31+
32+
# Control if darkmode is supported
33+
# When activated the color scheme for your diagrams is automatically toggled
34+
# based on the selected theme. Supports classic mkdocs and mkdocs-material.
35+
darkmode: true
3136
```
3237
## HTML Attributes
3338
For each global configuration option you can also use the attribute in the diagram itself. This will override the global configuration. Here is an example:

examples/docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The currently supported features are:
3535
* Support for multi page diagrams by selecting which page to display.
3636
* Compatible with `mkdocs-caption` and `mkdocs-glightbox`.
3737
* Print and edit button.
38+
* Dark Mode 🕶️
3839

3940
## Usage
4041

examples/mkdocs-classic.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
site_name: test_mkdocs_drawio
2+
docs_dir: docs
3+
nav:
4+
- Documentation:
5+
- Getting Started: index.md
6+
- Configuration: configuration.md
7+
- Plumbing: plumbing.md
8+
- Tests:
9+
- Simple Diagram: 'tests/simple-diagram/index.md'
10+
- Error Handling: 'tests/error-handling/index.md'
11+
- Configuration: 'tests/configuration/index.md'
12+
- Code Blocks: 'tests/code-blocks/index.md'
13+
- Relative Paths (a): 'tests/relative-paths/index.md'
14+
- Relative Paths (b): 'tests/relative-paths/example.md'
15+
- Pagging: 'tests/pagging/index.md'
16+
- External URL: 'tests/external-url/index.md'
17+
- SVG Diagram: 'tests/svg/index.md'
18+
19+
theme:
20+
name: mkdocs
21+
user_color_mode_toggle: true
22+
23+
plugins:
24+
- search
25+
- drawio:
26+
toolbar: true
27+
tooltips: true
28+
edit: true
29+
border: 20
30+
darkmode: true
31+
- print-site
32+
33+
34+
markdown_extensions:
35+
- attr_list
36+
- pymdownx.superfences
37+
38+
repo_url: https://github.com/tuunit/mkdocs-drawio

examples/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ plugins:
4848
tooltips: true
4949
edit: true
5050
border: 20
51+
darkmode: true
5152
- print-site
5253

5354
repo_url: https://github.com/tuunit/mkdocs-drawio

mkdocs_drawio/css/drawio-darkmode.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Drawio Viewer uses color-scheme attribute to switch between light and dark mode
2+
Let it synchronize with the Material for MkDocs theme color scheme */
3+
:root {
4+
color-scheme: light;
5+
}
6+
7+
/* Material for MkDocs Theme */
8+
[data-md-color-scheme="slate"] {
9+
color-scheme: dark;
10+
}
11+
12+
/* Mkdocs Theme */
13+
[data-bs-theme="dark"] {
14+
color-scheme: dark;
15+
}

mkdocs_drawio/js/drawio-darkmode.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const colorSchemeToggle = async function () {
2+
document.querySelectorAll("div.mxgraph").forEach(function (div) {
3+
let style = div.getAttribute("style").replace(/color-scheme: light;/gi, "");
4+
div.setAttribute("style", style);
5+
});
6+
};
7+
8+
if (typeof document$ !== "undefined" && typeof document$.subscribe === "function") {
9+
document$.subscribe(({ body }) => {
10+
colorSchemeToggle();
11+
});
12+
} else {
13+
document.addEventListener("DOMContentLoaded", function () {
14+
colorSchemeToggle();
15+
});
16+
}

mkdocs_drawio/plugin.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from html import escape
88
from pathlib import Path
99
from bs4 import BeautifulSoup
10+
from mkdocs.utils import copy_file
1011
from mkdocs.plugins import BasePlugin
1112
from mkdocs.config import base, config_options as c
1213

@@ -27,6 +28,7 @@ class DrawioConfig(base.Config):
2728
tooltips = c.Type(bool, default=True)
2829
border = c.Type(int, default=0)
2930
edit = c.Type(bool, default=True)
31+
darkmode = c.Type(bool, default=False)
3032

3133

3234
class DrawioPlugin(BasePlugin[DrawioConfig]):
@@ -136,3 +138,24 @@ def parse_diagram(data, alt, src="", path=None) -> str:
136138
f"Error: Could not properly parse page name '{alt}' for diagram '{src}' on path '{path}'"
137139
)
138140
return ""
141+
142+
def on_config(self, config: base.Config):
143+
"""Load embedded files"""
144+
self.base = Path(__file__).parent
145+
self.css = []
146+
self.js = []
147+
148+
if self.config.darkmode:
149+
self.css.append("css/drawio-darkmode.css")
150+
self.js.append("js/drawio-darkmode.js")
151+
152+
for path in self.css:
153+
config.extra_css.append(str(Path("/") / path))
154+
for path in self.js:
155+
config.extra_javascript.append(str(Path("/") / path))
156+
157+
def on_post_build(self, config: base.Config):
158+
"""Copy embedded files to the site directory"""
159+
site = Path(config["site_dir"])
160+
for path in self.css + self.js:
161+
copy_file(self.base / path, site / path)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "mkdocs-drawio"
3-
version = "1.10.0"
3+
version = "1.11.0"
44
description = "MkDocs plugin for embedding Drawio files"
55
authors = [
66
"Jan Larwig <[email protected]>",

0 commit comments

Comments
 (0)