mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2026-06-21 23:38:39 +00:00
7496dd41fa
- Migrate theme persistence from `sessionStorage` to `localStorage` - Rename theme HTML attribute to `data-bs-theme` for Bootstrap compatibility - Trim and compile CSS according to the chosen theme mode
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
/**
|
|
* Sets up the mode toggle dropdown, allowing users to switch between light, dark, and system themes.
|
|
*
|
|
* Dependencies:
|
|
* - Theme (${JS_ROOT}/theme.js)
|
|
*/
|
|
|
|
import 'bootstrap/js/src/dropdown.js';
|
|
|
|
const ACTIVE_CLASS = 'active';
|
|
const dropdown = document.querySelector('#mode-toggle + .dropdown-menu');
|
|
const activeMode = Theme.isSystemTheme
|
|
? Theme.Mode.SYSTEM
|
|
: Theme.resolvedTheme;
|
|
|
|
export function modeWatcher() {
|
|
if (!Theme.isToggleable) {
|
|
return;
|
|
}
|
|
|
|
dropdown.querySelectorAll('.dropdown-item').forEach((option) => {
|
|
const mode = option.dataset.themeMode;
|
|
if (mode === activeMode) {
|
|
option.classList.add(ACTIVE_CLASS);
|
|
return;
|
|
}
|
|
});
|
|
|
|
dropdown.addEventListener('click', (event) => {
|
|
const current = event.target.closest('.dropdown-item');
|
|
|
|
if (!current) {
|
|
return;
|
|
}
|
|
|
|
const lastActive = dropdown.querySelector(`.${ACTIVE_CLASS}`);
|
|
|
|
if (lastActive === current) {
|
|
return;
|
|
}
|
|
|
|
lastActive.classList.remove(ACTIVE_CLASS);
|
|
current.classList.add(ACTIVE_CLASS);
|
|
Theme.update(current.dataset.themeMode);
|
|
});
|
|
}
|