Skip to content

Commit

Permalink
Fix Dark Mode Toggle (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
versile2 authored Jan 21, 2025
1 parent 73b5b80 commit cb820bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/TryMudBlazor.Client/Pages/Repl.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ private async void UpdateTheme()
await LayoutService.ToggleDarkMode();
string theme = LayoutService.IsDarkMode ? "vs-dark" : "default";
this.JsRuntime.InvokeVoid(Try.Editor.SetTheme, theme);
// LayoutService calls StateHasChanged, we need the updated <style> tags for updateIframeTheme to work
await Task.Yield();
await JsRuntime.InvokeVoidAsync("updateIframeTheme");
}
}
}
24 changes: 18 additions & 6 deletions src/TryMudBlazor.Client/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,25 @@
<script src="_content/MudBlazor/MudBlazor.min.js?v=#{CACHE_TOKEN}#"></script>
<script src="editor/main.js?v=#{CACHE_TOKEN}#"></script>
<script type="text/javascript">
window.addEventListener('storage', (e) => {
if (e.key == "userPreferences") {
const mudblazorTheme = parent.document.getElementsByTagName('style')[4];
const iFrameTheme = document.getElementsByTagName('style')[3];
iFrameTheme.innerHTML = mudblazorTheme.innerHTML;
function updateIframeTheme() {
const mudblazorTheme = parent.document.querySelector('.mud-theme-provider') || parent.document.getElementsByTagName('style')[3];

if (mudblazorTheme) {
// Find the iframe
const userPageIframe = parent.document.getElementById('user-page-window');

if (userPageIframe && userPageIframe.contentDocument) {
const iframeDoc = userPageIframe.contentDocument;
const iFrameTheme = iframeDoc.querySelector('.mud-theme-provider') || iframeDoc.getElementsByTagName('style')[2];

if (iFrameTheme) {
iFrameTheme.innerHTML = mudblazorTheme.innerHTML;
}
}
} else {
console.error('Could not find the parent theme element.');
}
});
}
</script>
</body>

Expand Down

0 comments on commit cb820bc

Please sign in to comment.