Skip to content

Commit

Permalink
Set Mermaid theme based on light/dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin committed Apr 30, 2024
1 parent b984336 commit 1264f54
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions layouts/partials/scripts/mermaid.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@

var settings = norm(mermaid.mermaidAPI.defaultConfig, params);
settings.startOnLoad = true;
const isDark = $('html[data-bs-theme="dark"]').length;
settings.theme = isDark ? 'dark' : 'base';
mermaid.initialize(settings);

// Handle light/dark mode theme changes
const lightDarkModeThemeChangeHandler = function (mutationsList, observer) {
for (const mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-bs-theme') {
// Mermaid doesn't currently support reinitialization, see
// https://github.com/mermaid-js/mermaid/issues/1945. Until then,
// just reload the page.
location.reload();
}
}
};

const observer = new MutationObserver(lightDarkModeThemeChangeHandler);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-bs-theme']
});
// observer.disconnect();

})(jQuery);
</script>

0 comments on commit 1264f54

Please sign in to comment.