Skip to content

Commit 5affd7d

Browse files
authored
Fix RTD code block and sidebar colors in dark mode (#119)
* Remove code block white background, use default * Sync sidebar dark mode with parent page * Move iframe recieve script
1 parent fe3f856 commit 5affd7d

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

docs/_ext/generate_toc_html.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ def generate_toc_html(app, exception):
324324
</svg>
325325
</symbol>
326326
</svg>
327+
<script src="iframe_theme_receiver.js"></script>
327328
</head>
328-
<body>
329+
<body data-theme="auto">
329330
<div class="content-container">
330331
<div id="tocSearchPanel">
331332
<div id="tocSearchPanelInner">

docs/_static/iframe_theme_receiver.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Listen for theme changes from parent window
2+
window.addEventListener('message', function(event) {{
3+
if (event.data && event.data.type === 'theme-change') {{
4+
document.body.setAttribute('data-theme', event.data.theme);
5+
}}
6+
}});

docs/_static/iframe_theme_sync.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Theme synchronization for iframe
2+
(function() {
3+
'use strict';
4+
5+
// Function to notify iframe about theme changes
6+
function notifyIframeOfThemeChange(theme) {
7+
const iframe = document.querySelector('.custom-nav iframe');
8+
if (iframe && iframe.contentWindow) {
9+
try {
10+
iframe.contentWindow.postMessage({
11+
type: 'theme-change',
12+
theme: theme
13+
}, '*');
14+
} catch (e) {
15+
console.warn('Could not send theme change message to iframe:', e);
16+
}
17+
}
18+
}
19+
20+
// Function to get current theme
21+
function getCurrentTheme() {
22+
return document.body.getAttribute('data-theme') || 'auto';
23+
}
24+
25+
// Monitor theme changes using MutationObserver
26+
function observeThemeChanges() {
27+
const observer = new MutationObserver(function(mutations) {
28+
mutations.forEach(function(mutation) {
29+
if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
30+
const newTheme = getCurrentTheme();
31+
notifyIframeOfThemeChange(newTheme);
32+
}
33+
});
34+
});
35+
36+
observer.observe(document.body, {
37+
attributes: true,
38+
attributeFilter: ['data-theme']
39+
});
40+
}
41+
42+
// Initialize when DOM is ready
43+
document.addEventListener('DOMContentLoaded', function() {
44+
// Start observing theme changes
45+
observeThemeChanges();
46+
47+
// Send initial theme when iframe loads
48+
const iframe = document.querySelector('.custom-nav iframe');
49+
if (iframe) {
50+
iframe.addEventListener('load', function() {
51+
notifyIframeOfThemeChange(getCurrentTheme());
52+
});
53+
}
54+
});
55+
})();

docs/_static/theme_overrides.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ form.sidebar-search-container {
305305
/* Code block styles */
306306
pre, pre code {
307307
color: #000000 !important;
308-
background: #F8F8F8 !important;
309308
}
310309

311310
/* Code block styling */

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def setup(app):
144144
"search.js",
145145
"section_highlight.js",
146146
"custom_body_classes.js",
147+
"iframe_theme_sync.js",
147148
]
148149
html_theme_options = {
149150
"light_css_variables": {

0 commit comments

Comments
 (0)