Skip to content

Commit af21c74

Browse files
committed
Add support for dark/light theme based on user's preferred color scheme
1 parent 9219858 commit af21c74

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

docs/options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
2323
2424
- `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%.
2525
- `vega_theme` (default is `default`). 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, 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/).
2727
- `vega_renderer` (default is `svg`). Specify one of the available [vegalite renderers](https://vega.github.io/vega-themes/).
2828
- `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.
2929
- `data_path` (default is `""`). When `use_data_path` is enabled, the base path relative to the `docs/` folder.

mkdocs_charts_plugin/js/mkdocs-charts-plugin.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ function getTheme() {
132132
? mkdocs_chart_plugin['vega_theme_dark']
133133
: mkdocs_chart_plugin['vega_theme'];
134134
}
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+
}
135141
// Fall back to light theme
136142
return mkdocs_chart_plugin['vega_theme'];
137143
}
@@ -227,6 +233,17 @@ const chartplugin = className => {
227233
}
228234
}
229235

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+
}
246+
230247
// mkdocs-material has a dark mode including a toggle
231248
// We should watch when dark mode changes and update charts accordingly
232249

@@ -235,16 +252,7 @@ var observer = new MutationObserver(function(mutations) {
235252
if (mutation.type === "attributes") {
236253

237254
if (mutation.attributeName == "data-md-color-scheme") {
238-
239-
const theme = getTheme();
240-
for (let i = 0; i < vegalite_charts.length; i++) {
241-
vegaEmbed(vegalite_charts[i].block, vegalite_charts[i].schema, {
242-
actions: false,
243-
theme,
244-
"renderer": mkdocs_chart_plugin['vega_renderer']
245-
});
246-
}
247-
255+
updateCharts();
248256
}
249257

250258
}
@@ -254,6 +262,13 @@ observer.observe(bodyelement, {
254262
attributes: true //configure it to listen to attribute changes
255263
});
256264

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+
}
271+
257272
// Load when DOM ready
258273
if (typeof document$ !== "undefined") {
259274
// compatibility with mkdocs-material's instant loading feature

0 commit comments

Comments
 (0)