33 lines
991 B
JavaScript
33 lines
991 B
JavaScript
function createContentTheme({
|
|
THEME_STORAGE_KEY,
|
|
createThemeIcon,
|
|
environment
|
|
}) {
|
|
const getLocalStorage = () => environment.localStorage;
|
|
function getSavedTheme() {
|
|
try {
|
|
return getLocalStorage().getItem(THEME_STORAGE_KEY) === "dark" ? "dark" : "light";
|
|
} catch (error) {
|
|
return "light";
|
|
}
|
|
}
|
|
|
|
function applyTheme(modal, button, theme) {
|
|
const isDarkTheme = theme === "dark";
|
|
|
|
modal.setAttribute("data-theme", isDarkTheme ? "dark" : "light");
|
|
button.textContent = "";
|
|
button.setAttribute("aria-label", isDarkTheme ? "Ativar modo claro" : "Ativar modo escuro");
|
|
button.title = isDarkTheme ? "Light mode" : "Dark mode";
|
|
button.append(createThemeIcon(isDarkTheme ? "sun" : "moon"));
|
|
|
|
try {
|
|
getLocalStorage().setItem(THEME_STORAGE_KEY, isDarkTheme ? "dark" : "light");
|
|
} catch (error) {
|
|
// Continue with the selected theme when browser storage is unavailable.
|
|
}
|
|
}
|
|
|
|
return { getSavedTheme, applyTheme };
|
|
}
|