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

id and url rather than ID and URL

`id` and `url` rather than `ID` and `URL`
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 () => { const fetchThemes = async () => {

Convert to regular function rather than arrow function. (Forgot to comment on this before)

Convert to regular function rather than arrow function. (Forgot to comment on this before)
for (const theme of defaultThemes) { for (const theme of defaultThemes) {
const themeConfig = await fetch(`${baseURL}themes/${theme}.json`) const themeConfig = await fetch(`${baseURL}themes/${theme}.json`)

Use $fetch instead of fetch, which among other things automatically parses the body.

Use `$fetch` instead of `fetch`, which among other things automatically parses the body.
const themeConfigJson = await themeConfig.json() as Theme const themeConfigJson = await themeConfig.json() as Theme
themeConfigJson.ID = theme
themes.push(themeConfigJson) themes.push(themeConfigJson)
} }
console.log(themes)
} }
await fetchThemes() await fetchThemes()
@ -67,6 +93,7 @@ await fetchThemes()
display: inline-block; display: inline-block;
text-align: center; text-align: center;
align-content: center; align-content: center;
cursor: pointer;
} }
.theme-title { .theme-title {

View file

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

View file

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

View file

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