General source code refactoring
This commit is contained in:
32
src/content/theme.js
Normal file
32
src/content/theme.js
Normal file
@@ -0,0 +1,32 @@
|
||||
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 };
|
||||
}
|
||||
Reference in New Issue
Block a user