| 1 |
var debug = 1 ? console.log.bind(console, '[navStore]') : function () {}; |
| 2 |
|
| 3 |
var ColorScheme = { |
| 4 |
System: 1, |
| 5 |
Light: 2, |
| 6 |
Dark: 3, |
| 7 |
}; |
| 8 |
|
| 9 |
const localStorageUserSettingsKey = 'hugoDocsUserSettings'; |
| 10 |
|
| 11 |
export const navStore = (Alpine) => ({ |
| 12 |
init() { |
| 13 |
// There is no $watch available in Alpine stores, |
| 14 |
// but this has the same effect. |
| 15 |
this.userSettings.onColorSchemeChanged = Alpine.effect(() => { |
| 16 |
if (this.userSettings.settings.colorScheme) { |
| 17 |
this.userSettings.isDark = isDark(this.userSettings.settings.colorScheme); |
| 18 |
toggleDarkMode(this.userSettings.isDark); |
| 19 |
} |
| 20 |
}); |
| 21 |
|
| 22 |
// Also react to changes in system settings. |
| 23 |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { |
| 24 |
this.userSettings.setColorScheme(ColorScheme.System); |
| 25 |
}); |
| 26 |
}, |
| 27 |
|
| 28 |
destroy() {}, |
| 29 |
|
| 30 |
scroll: { |
| 31 |
atTop: true, |
| 32 |
}, |
| 33 |
|
| 34 |
mobileMenu: { |
| 35 |
open: false, |
| 36 |
toggle() { |
| 37 |
this.open = !this.open; |
| 38 |
}, |
| 39 |
close() { |
| 40 |
this.open = false; |
| 41 |
}, |
| 42 |
}, |
| 43 |
|
| 44 |
userSettings: { |
| 45 |
// settings gets persisted between page navigations. |
| 46 |
settings: Alpine.$persist({ |
| 47 |
// light, dark or system mode. |
| 48 |
// If not set, we use the OS setting. |
| 49 |
colorScheme: ColorScheme.System, |
| 50 |
// Used to show the most relevant tab in config listings etc. |
| 51 |
configFileType: 'toml', |
| 52 |
}).as(localStorageUserSettingsKey), |
| 53 |
|
| 54 |
isDark: false, |
| 55 |
|
| 56 |
setColorScheme(colorScheme) { |
| 57 |
this.settings.colorScheme = colorScheme; |
| 58 |
this.isDark = isDark(colorScheme); |
| 59 |
}, |
| 60 |
|
| 61 |
toggleColorScheme() { |
| 62 |
let next = this.settings.colorScheme + 1; |
| 63 |
if (next > ColorScheme.Dark) { |
| 64 |
next = ColorScheme.System; |
| 65 |
} |
| 66 |
this.setColorScheme(next); |
| 67 |
}, |
| 68 |
colorScheme() { |
| 69 |
return this.settings.colorScheme ? this.settings.colorScheme : ColorScheme.System; |
| 70 |
}, |
| 71 |
}, |
| 72 |
}); |
| 73 |
|
| 74 |
function isMediaDark() { |
| 75 |
return window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 76 |
} |
| 77 |
|
| 78 |
function isDark(colorScheme) { |
| 79 |
if (!colorScheme || colorScheme == ColorScheme.System) { |
| 80 |
return isMediaDark(); |
| 81 |
} |
| 82 |
|
| 83 |
return colorScheme == ColorScheme.Dark; |
| 84 |
} |
| 85 |
|
| 86 |
export function initColorScheme() { |
| 87 |
// The AlpineJS store has not have been initialized yet, so access the |
| 88 |
// localStorage directly. |
| 89 |
let settingsJSON = localStorage[localStorageUserSettingsKey]; |
| 90 |
if (settingsJSON) { |
| 91 |
let settings = JSON.parse(settingsJSON); |
| 92 |
toggleDarkMode(isDark(settings.colorScheme)); |
| 93 |
return; |
| 94 |
} |
| 95 |
toggleDarkMode(isDark(null)); |
| 96 |
} |
| 97 |
|
| 98 |
const toggleDarkMode = function (dark) { |
| 99 |
if (dark) { |
| 100 |
document.documentElement.classList.add('dark'); |
| 101 |
} else { |
| 102 |
document.documentElement.classList.remove('dark'); |
| 103 |
} |
| 104 |
}; |