Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions crates/mdbook-html/front-end/js/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,21 @@
}
}

let previousTheme = default_theme;

Check failure on line 390 in crates/mdbook-html/front-end/js/book.js

View workflow job for this annotation

GitHub Actions / GUI tests

'previousTheme' is assigned a value but never used. Allowed unused vars must match /^_/u

function clear_theme_classes() {
themeIds.forEach(function(id) {
html.classList.remove(id.replace(/^mdbook-theme-/, ''));
});
html.classList.remove(default_theme);
if (typeof default_light_theme !== 'undefined') {
html.classList.remove(default_light_theme);
}
if (typeof default_dark_theme !== 'undefined') {
html.classList.remove(default_dark_theme);
}
}

function set_theme(theme, store = true) {
let ace_theme;

Expand Down Expand Up @@ -427,12 +441,23 @@
}
}

html.classList.remove(previousTheme);
clear_theme_classes();
html.classList.add(theme);
previousTheme = theme;
updateThemeSelected();
}

window.addEventListener('storage', function(e) {
if (e.key !== 'mdbook-theme') {
return;
}
if (e.newValue === null) {
set_theme(get_theme(), false);
} else {
set_theme(e.newValue, false);
}
});

const query = window.matchMedia('(prefers-color-scheme: dark)');
query.onchange = function() {
set_theme(get_theme(), false);
Expand Down Expand Up @@ -551,14 +576,16 @@
}
});
sidebarToggleButton.addEventListener('click', () => {
sidebarCheckbox.checked = !sidebarCheckbox.checked;
/* To allow the sidebar expansion animation, we first need to put back the display. */
if (!sidebarCheckbox.checked) {
if (sidebarCheckbox.checked) {
sidebar.style.display = '';
// Workaround for Safari skipping the animation when changing
// `display` and a transform in the same event loop. This forces a
// reflow after updating the display.
sidebar.offsetHeight;
}
sidebarCheckbox.dispatchEvent(new Event('change', { bubbles: true }));
});

function showSidebar() {
Expand Down
Loading