Add theme switching!!!! #18

Merged
twig merged 9 commits from settings-appearance into main 2025-07-05 17:29:46 +00:00
4 changed files with 31 additions and 4 deletions
Showing only changes of commit 441dc0c15c - Show all commits

View file

@ -5,7 +5,11 @@
<p class="subtitle">THEMES</p>
<div class="themes">
<div v-for="theme of themes" class="theme-preview-container">
<span class="theme-preview" :title="theme.displayName" :style="{background:`linear-gradient(${theme.previewGradient})`}">
<span class="theme-preview"
:title="theme.displayName"
:style="{background:`linear-gradient(${theme.previewGradient})`}"
@click="changeTheme(theme.ID, theme.themeURL)"
>
<span class="theme-title" :style="{color:`${theme.complementaryColor}`}">
{{ theme.displayName }}
</span>
@ -25,23 +29,45 @@
const runtimeConfig = useRuntimeConfig()
const defaultThemes = runtimeConfig.public.defaultThemes
const baseURL = runtimeConfig.app.baseURL;
let themeLinkElement: HTMLLinkElement | null = null;
const themes: Array<Theme> = []
interface Theme {

id and themeUrl rather than ID and themeURL, and update theme files to match

`id` and `themeUrl` rather than `ID` and `themeURL`, and update theme files to match
ID: string
displayName: string
previewGradient: string
complementaryColor: string
themeURL: string
}
function changeTheme(ID: string, URL: string) {
if (themeLinkElement && themeLinkElement.getAttribute('href') === `${baseURL}themes/${URL}`) {
return;
}
localStorage.setItem("selectedTheme", ID);
// if the theme didn't originally load for some reason, create it
if (!themeLinkElement) {
themeLinkElement = document.createElement('link');
themeLinkElement.rel = 'stylesheet';
document.head.appendChild(themeLinkElement);
}
themeLinkElement.href = `${baseURL}themes/${URL}`;
}
const fetchThemes = async () => {
for (const theme of defaultThemes) {
const themeConfig = await fetch(`${baseURL}themes/${theme}.json`)
const themeConfigJson = await themeConfig.json() as Theme
themeConfigJson.ID = theme
themes.push(themeConfigJson)
}
console.log(themes)
}
await fetchThemes()
@ -67,6 +93,7 @@ await fetchThemes()
display: inline-block;
text-align: center;
align-content: center;
cursor: pointer;
}
.theme-title {

View file

@ -2,5 +2,5 @@
"displayName": "Ash",
"previewGradient": "45deg, #2f2e2d, #46423b",
"complementaryColor": "white",
"themeData": "ash.css"
"themeURL": "ash.css"
}

View file

@ -2,5 +2,5 @@
"displayName": "Dark",
"previewGradient": "45deg, #1f1e1d, #36322b",
"complementaryColor": "white",
"themeData": "dark.css"
"themeURL": "dark.css"
}

View file

@ -2,5 +2,5 @@
"displayName": "Light",
"previewGradient": "45deg, #f0ebe8, #d4d0ca",
"complementaryColor": "black",
"themeData": "light.css"
"themeURL": "light.css"
}