Skip to content

Commit

Permalink
Fix theme switch (#3534)
Browse files Browse the repository at this point in the history
* Fix theme switch

* Fix switching theme on refresh

* Add watch option

* Fix app theme
  • Loading branch information
samaradel authored Oct 22, 2024
1 parent 6d524ec commit 1b54bd9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
32 changes: 19 additions & 13 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,29 @@ function setSidebarOnResize() {
window.addEventListener("resize", setSidebarOnResize);
const themeMatcher = window.matchMedia("(prefers-color-scheme: dark)");
// changes theme based on changes in system mode
themeMatcher.addEventListener("change", updateTheme);
const themeMatchers = {
light: window.matchMedia("(prefers-color-scheme: light)"),
dark: window.matchMedia("(prefers-color-scheme: dark)"),
};
themeMatchers.dark.addEventListener("change", updateTheme);
themeMatchers.light.addEventListener("change", updateTheme);
function updateTheme() {
if (themeMatcher.matches) {
theme.global.name.value = AppThemeSelection.dark;
} else {
theme.global.name.value = AppThemeSelection.light;
const themeKey = getThemeKey();
theme.global.name.value = AppThemeSelection[themeKey];
localStorage.setItem(LocalStorageSettingsKey.THEME_KEY, AppThemeSelection[themeKey]);
}
function getThemeKey() {
if (themeMatchers.dark.matches) {
return "dark";
} else if (themeMatchers.light.matches) {
return "light";
}
localStorage.setItem(LocalStorageSettingsKey.THEME_KEY, ThemeSettingsInterface.System);
return "system";
}
// sets theme to system mode on application mount
onMounted(() => {
updateTheme();
});
watch(
() => $route.meta,
meta => {
Expand Down Expand Up @@ -551,7 +558,6 @@ import type { GridClient } from "@threefold/grid_client";
import { DashboardRoutes } from "@/router/routes";
import { AppThemeSelection } from "@/utils/app_theme";
import { ThemeSettingsInterface } from "@/utils/settings";
import AppTheme from "./components/app_theme.vue";
import DeploymentListManager from "./components/deployment_list_manager.vue";
Expand Down
6 changes: 5 additions & 1 deletion packages/playground/src/components/app_theme.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ const theme = useTheme();
const light = ref(false);
watch(light, light => (theme.global.name.value = light ? "light" : "dark"));
watch(theme.global.name, theme => localStorage.setItem(LocalStorageSettingsKey.THEME_KEY, theme));
watch(theme.global.name, theme => {
localStorage.setItem(LocalStorageSettingsKey.THEME_KEY, theme);
light.value = theme === "light";
});
onMounted(() => {
const theme = localStorage.getItem(LocalStorageSettingsKey.THEME_KEY);
light.value = theme === "light";
Expand Down
1 change: 1 addition & 0 deletions packages/playground/src/utils/app_theme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum AppThemeSelection {
dark = "dark",
light = "light",
system = "system",
}
12 changes: 10 additions & 2 deletions packages/playground/src/views/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<v-card class="my-5"
><v-card-title>Theme</v-card-title> <v-card-text>Pick an application theme!</v-card-text>

<v-select class="pa-3" :items="themes" v-model="selectedTheme" />
<v-select class="pa-3 capitalize" :items="themes" v-model="selectedTheme" />

<v-card-actions class="justify-end mb-3 mx-3">
<v-btn :disabled="isCurrentTheme()" @click="UpdateTheme" class="justify-end ml-auto"
Expand Down Expand Up @@ -211,6 +211,15 @@ export default {
}
});
watch(
theme.global.name,
theme => {
selectedTheme.value = currentTheme.value = theme.includes("mode") ? theme : `${theme} mode`;
localStorage.setItem(LocalStorageSettingsKey.THEME_KEY, theme);
},
{ immediate: true },
);
const deploymentTimeoutdefaultMinutes = gridStore?.client.clientOptions.deploymentTimeoutMinutes;
const selectedDeploymentTimeout = ref(0);
const currentDeploymentTimeout = ref(0);
Expand Down Expand Up @@ -238,7 +247,6 @@ export default {
case ThemeInterface.Dark:
currentTheme.value = ThemeInterface.Dark;
theme.global.name.value = AppThemeSelection.dark;
localStorage.se;
break;
case ThemeInterface.Light:
currentTheme.value = ThemeInterface.Light;
Expand Down

0 comments on commit 1b54bd9

Please sign in to comment.