Skip to content

Commit de1d2e2

Browse files
committed
Avoid jump when toggling light/dark mode
1 parent 7d76f06 commit de1d2e2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docs/javascripts/extra.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Prevent page jump when clicking theme toggle
2+
document.addEventListener('DOMContentLoaded', function() {
3+
// Find all palette toggle buttons
4+
const paletteToggles = document.querySelectorAll('.md-header__button[for^="__palette_"]');
5+
6+
paletteToggles.forEach(function(toggle) {
7+
toggle.addEventListener('click', function(e) {
8+
// Prevent the default anchor link behavior that causes page jump
9+
e.preventDefault();
10+
11+
// Get the input element that the label is for
12+
const targetId = toggle.getAttribute('for');
13+
const input = document.getElementById(targetId);
14+
15+
if (input) {
16+
// Toggle the input manually
17+
input.checked = true;
18+
// Trigger change event so Material theme switcher works
19+
input.dispatchEvent(new Event('change', { bubbles: true }));
20+
}
21+
});
22+
});
23+
});
24+

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ markdown_extensions:
7272
extra_css:
7373
- stylesheets/extra.css
7474

75+
# Extra JavaScript
76+
extra_javascript:
77+
- javascripts/extra.js
78+
7579
# Extra configuration
7680
extra:
7781
social:

0 commit comments

Comments
 (0)