How to theme mermaid diagrams?

You can save the source with another attribute via JavaScript, such as data-src, when theme changing, then re-render the diagram from data-src, this approach would be friendly for UE.

mermaid-theming

It depends on how you implement the theming, the following references may help.