Skip to content

Commit

Permalink
fix: propergate dark mode to mermaid diagrams (#1627)
Browse files Browse the repository at this point in the history
This commit adds the dark mode theme for mermaid diagrams that match the selected theme.
  • Loading branch information
iverly authored Jan 15, 2024
1 parent af8466d commit c013a05
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/theme/Mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// `}/>

import React, { useEffect, useState } from "react"
import { useColorMode } from "@docusaurus/theme-common"
import mermaid from "mermaid"
import styles from "./mermaid.module.css"
import cn from "classnames"
Expand Down Expand Up @@ -43,11 +44,19 @@ const Mermaid = ({ chart }) => {
const [svg, setSvg] = useState(undefined)
const [id] = useState(`mermaid-${Math.random().toString(36).substr(2, -1)}`)
const toggle = () => setZoomed(!zoomed)
const { colorMode } = useColorMode()

useEffect(() => {
mermaid.render(id, chart, (svg) => {
setSvg(svg)
})
// https://mermaid.js.org/config/theming.html#diagram-specific-themes
mermaid.render(
id,
`%%{init: {'theme':'${
colorMode === "light" ? "neutral" : "dark"
}'}}%%\n${chart}`,
(svg) => {
setSvg(svg)
},
)
}, [])

return (
Expand Down

0 comments on commit c013a05

Please sign in to comment.