Skip to content

Commit 971cb27

Browse files
authored
Merge pull request #23 from sisp/feat/custom-schemes
Add support for custom light/dark schemes
2 parents 7fe7be2 + af21c74 commit 971cb27

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

docs/options.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1313
vega_renderer: svg
1414
use_data_path: True
1515
data_path: ""
16+
integrations:
17+
mkdocs_material:
18+
themes_light:
19+
- default
20+
themes_dark:
21+
- slate
1622
```
1723
1824
- `vega_width` (default is `container`). When not specified explicitly in the JSON schema, the `width` to use (see [vegalite customizing size](https://vega.github.io/vega-lite/docs/size.html)). When set to `container` width will be 100%.
1925
- `vega_theme` (default is `default`). Specify one of the available [vegalite themes](https://vega.github.io/vega-themes/).
20-
- `vega_theme_dark` (default is `dark`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme with a dark mode, automatically render charts using this theme. Dark mode toggle is also supported. Specify one of the available [vegalite themes](https://vega.github.io/vega-themes/).
26+
- `vega_theme_dark` (default is `dark`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme with a dark mode or the user's preferred color scheme in the browser or OS is "dark", automatically render charts using this theme. Dark mode toggle is also supported. Specify one of the available [vegalite themes](https://vega.github.io/vega-themes/).
2127
- `vega_renderer` (default is `svg`). Specify one of the available [vegalite renderers](https://vega.github.io/vega-themes/).
2228
- `use_data_path` (default is `True`). When `True`, any relative urls used in the JSON schema are relative to the `data_path`. When `False`, relative urls should be relative to the URL of the page.
2329
- `data_path` (default is `""`). When `use_data_path` is enabled, the base path relative to the `docs/` folder.
30+
- `integrations.mkdocs_material.themes_light` (default is `[default]`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme, specify the light [color schemes](https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#color-scheme).
31+
- `integrations.mkdocs_material.themes_dark` (default is `[slate]`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme, specify the dark [color schemes](https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#color-scheme).
2432

mkdocs_charts_plugin/js/mkdocs-charts-plugin.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ function updateURL(url) {
122122
return url;
123123
}
124124

125+
const bodyelement = document.querySelector('body');
126+
127+
function getTheme() {
128+
// Get theme according to mkdocs-material's color scheme
129+
const materialColorScheme = bodyelement.getAttribute('data-md-color-scheme');
130+
if (materialColorScheme) {
131+
return mkdocs_chart_plugin['integrations']['mkdocs_material']['themes_dark'].includes(materialColorScheme)
132+
? mkdocs_chart_plugin['vega_theme_dark']
133+
: mkdocs_chart_plugin['vega_theme'];
134+
}
135+
// Get theme according to user's preferred color scheme on the browser or OS
136+
if (window.matchMedia) {
137+
return window.matchMedia('(prefers-color-scheme: dark)').matches
138+
? mkdocs_chart_plugin['vega_theme_dark']
139+
: mkdocs_chart_plugin['vega_theme'];
140+
}
141+
// Fall back to light theme
142+
return mkdocs_chart_plugin['vega_theme'];
143+
}
144+
125145
var vegalite_charts = [];
126146

127147
function embedChart(block, schema) {
@@ -178,14 +198,10 @@ function embedChart(block, schema) {
178198
// in a different theme
179199
vegalite_charts.push({'block' : block, 'schema': schema});
180200

181-
// mkdocs-material has a dark mode
182-
// detect which one is being used
183-
var theme = (document.querySelector('body').getAttribute('data-md-color-scheme') == 'slate') ? mkdocs_chart_plugin['vega_theme_dark'] : mkdocs_chart_plugin['vega_theme'];
184-
185201
// Render the chart
186202
vegaEmbed(block, schema, {
187203
actions: false,
188-
"theme": theme,
204+
"theme": getTheme(),
189205
"renderer": mkdocs_chart_plugin['vega_renderer']
190206
});
191207
}
@@ -216,26 +232,27 @@ const chartplugin = className => {
216232

217233
}
218234
}
219-
235+
236+
function updateCharts() {
237+
const theme = getTheme();
238+
for (let i = 0; i < vegalite_charts.length; i++) {
239+
vegaEmbed(vegalite_charts[i].block, vegalite_charts[i].schema, {
240+
actions: false,
241+
theme,
242+
"renderer": mkdocs_chart_plugin['vega_renderer']
243+
});
244+
}
245+
}
220246

221247
// mkdocs-material has a dark mode including a toggle
222248
// We should watch when dark mode changes and update charts accordingly
223249

224-
var bodyelement = document.querySelector('body');
225250
var observer = new MutationObserver(function(mutations) {
226251
mutations.forEach(function(mutation) {
227252
if (mutation.type === "attributes") {
228253

229254
if (mutation.attributeName == "data-md-color-scheme") {
230-
231-
var theme = (bodyelement.getAttribute('data-md-color-scheme') == 'slate') ? mkdocs_chart_plugin['vega_theme_dark'] : mkdocs_chart_plugin['vega_theme'];
232-
for (let i = 0; i < vegalite_charts.length; i++) {
233-
vegaEmbed(vegalite_charts[i].block, vegalite_charts[i].schema, {
234-
actions: false,
235-
"theme": theme,
236-
"renderer": mkdocs_chart_plugin['vega_renderer']
237-
});
238-
}
255+
updateCharts();
239256
}
240257

241258
}
@@ -245,6 +262,12 @@ observer.observe(bodyelement, {
245262
attributes: true //configure it to listen to attribute changes
246263
});
247264

265+
// Watch for user's preferred color scheme changes and update charts accordingly
266+
if (window.matchMedia) {
267+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
268+
updateCharts();
269+
});
270+
}
248271

249272
// Load when DOM ready
250273
if (typeof document$ !== "undefined") {

mkdocs_charts_plugin/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from mkdocs.config import config_options
3+
from mkdocs.config import base, config_options
44
from mkdocs.exceptions import PluginError
55
from mkdocs.plugins import BasePlugin
66
from mkdocs.utils import copy_file
@@ -22,6 +22,15 @@ def check_library(libnames, dependency):
2222
)
2323

2424

25+
class MkdocsMaterialOptions(base.Config):
26+
themes_light = config_options.ListOfItems(config_options.Type(str), default=["default"])
27+
themes_dark = config_options.ListOfItems(config_options.Type(str), default=["slate"])
28+
29+
30+
class IntegrationsOptions(base.Config):
31+
mkdocs_material = config_options.SubConfig(MkdocsMaterialOptions)
32+
33+
2534
class ChartsPlugin(BasePlugin):
2635
config_scheme = (
2736
("data_path", config_options.Type(str, default="")),
@@ -31,6 +40,7 @@ class ChartsPlugin(BasePlugin):
3140
("vega_renderer", config_options.Type(str, default="svg")),
3241
("vega_width", config_options.Type(str, default="container")),
3342
("fallback_width", config_options.Type(str, default="800")),
43+
("integrations", config_options.SubConfig(IntegrationsOptions)),
3444
)
3545

3646
def on_config(self, config, **kwargs):

0 commit comments

Comments
 (0)