Skip to content

docs: mermaid diagrams follow the hexdocs light/dark theme - #91

Merged
GenericJam merged 1 commit into
masterfrom
fix/mermaid-follows-hexdocs-theme
Aug 30, 2026
Merged

docs: mermaid diagrams follow the hexdocs light/dark theme#91
GenericJam merged 1 commit into
masterfrom
fix/mermaid-follows-hexdocs-theme

Conversation

@GenericJam

Copy link
Copy Markdown
Owner

Closes#42 (@clsource).

ex_doc marks dark mode by toggling dark on <body> — its stylesheet is written against body.dark. Reading that class rather than prefers-color-scheme covers all three of ex_doc's settings (light, dark, and system), because ex_doc re-toggles the class itself when the OS scheme changes under "system".

Three things the naive version gets wrong

  • Source was destroyed. The old code replaced each <pre> with the rendered SVG, so a theme switch had nothing to re-render from. Source is now captured up front.
  • Async races.mermaid.render is async, so a fast light→dark→light toggle can land results out of order and strand the diagram on the wrong theme. Each pass is stamped; stale results are dropped.
  • <body> class churn. ex_doc also toggles search-focused and scroll-sticky there, so a bare MutationObserver would re-render diagrams on unrelated DOM activity. Only an actual change in dark-ness triggers a re-render.

Relationship to #35

Supersedes it. #35 was closed by its own author ("maybe is better other approach") — it forced the dark theme unconditionally, which breaks light mode. #35 also reported diagrams not rendering at all; that half is already fixed on master — generated markup is <pre><code class="mermaid"> and the existing selector matches it, confirmed in the built docs.

Verification

Done in a real browser against the built docs, not by eye:

StepResult
Initial light renderSVG present, text fill #333, no leftover pre code.mermaid
Toggle body.darkRe-renders as pass 2, fill #ccc
Toggle backRe-renders as pass 3, fill #333
Add search-focused + scroll-stickyNo re-render (id unchanged)

No orphan mermaid error divs after three passes, so no failed renders or leaks. mix format, mix credo --strict clean.

One caveat

This only reaches hexdocs.pm via a published release — doc-only changes without a version bump never ship, per RELEASE.md. Merging this alone won't fix the live docs; it needs to ride a release. I have not bumped the version.

🤖 Generated with Claude Code

Closes#42.
ex_doc marks dark mode by toggling `dark` on <body> (its stylesheet is written
against `body.dark`). Reading that class rather than prefers-color-scheme
covers all three of ex_doc's settings — light, dark and system — because
ex_doc re-toggles the class itself when the OS scheme changes under "system".
Three things the naive version gets wrong, all handled here:
- The old code replaced each <pre> with the rendered SVG, discarding the
diagram source. A theme switch then has nothing to re-render from, so the
source is captured up front.
- mermaid.render is async, so a fast light→dark→light toggle can land results
out of order and leave the diagram on the wrong theme. Each pass is stamped
and stale results are dropped.
- <body>'s class list also churns for search focus and sticky scroll, so a
bare MutationObserver would re-render diagrams on unrelated DOM activity.
Only an actual change in dark-ness triggers a re-render.
Supersedes the approach in #35 (closed by its author, "maybe is better other
approach"), which forced the dark theme unconditionally and so broke light
mode. That PR also reported diagrams not rendering at all; that half is
already fixed on master — the generated markup is <pre><code class="mermaid">
and the existing selector matches it.
Verified in a real browser against the built docs, not just by eye: light
renders (#333 text fill), toggling body.dark re-renders on pass 2 (#ccc),
toggling back re-renders on pass 3 (#333), and adding search-focused /
scroll-sticky does NOT re-render. No orphan mermaid error divs after three
passes, so no failed renders or leaks.
Note this only reaches hexdocs.pm via a published release — doc changes
without a version bump never ship.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@GenericJam
GenericJam merged commit bbfbcd9 into masterAug 30, 2026
4 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mermaid diagrams in hex docs handle light and dark mode

1 participant

@GenericJam