diff --git a/changelog/unreleased/enhancement-move-theme-switcher b/changelog/unreleased/enhancement-move-theme-switcher index c0100fd4daf..ba626381d6d 100644 --- a/changelog/unreleased/enhancement-move-theme-switcher +++ b/changelog/unreleased/enhancement-move-theme-switcher @@ -3,4 +3,5 @@ Enhancement: Move ThemeSwitcher into Account Settings We've moved the ThemeSwitcher to the account settings page. https://github.com/owncloud/web/pull/10334 +https://github.com/owncloud/web/pull/10342 https://github.com/owncloud/web/issues/10181 diff --git a/packages/web-pkg/src/composables/piniaStores/theme.ts b/packages/web-pkg/src/composables/piniaStores/theme.ts index ed4539cfd4b..26704eaaf55 100644 --- a/packages/web-pkg/src/composables/piniaStores/theme.ts +++ b/packages/web-pkg/src/composables/piniaStores/theme.ts @@ -1,6 +1,6 @@ import merge from 'deepmerge' import { defineStore } from 'pinia' -import { ref, unref } from 'vue' +import { computed, ref, unref } from 'vue' import { useLocalStorage, usePreferredDark } from '@vueuse/core' import { z } from 'zod' import { applyCustomProp } from 'design-system/src/' @@ -82,7 +82,6 @@ export type WebThemeConfigType = z.infer const themeStorageKey = 'oc_currentThemeName' export const useThemeStore = defineStore('theme', () => { - const currentThemeName = useLocalStorage(themeStorageKey, null) // null as default to make fallback possible const currentLocalStorageThemeName = useLocalStorage(themeStorageKey, null) const isDark = usePreferredDark() @@ -93,20 +92,34 @@ export const useThemeStore = defineStore('theme', () => { const initializeThemes = (themeConfig: WebThemeConfigType) => { availableThemes.value = themeConfig.themes.map((theme) => merge(themeConfig.defaults, theme)) + setThemeFromStorageOrSystem() + } - if (unref(currentThemeName) === null) { - currentThemeName.value = unref(availableThemes).find((t) => t.isDark === unref(isDark)).name - } - + const setThemeFromStorageOrSystem = () => { + const firstLightTheme = unref(availableThemes).find((theme) => !theme.isDark) + const firstDarkTheme = unref(availableThemes).find((theme) => theme.isDark) setAndApplyTheme( - unref(availableThemes).find((t) => t.name === unref(currentThemeName)) || - availableThemes.value[0] + unref(availableThemes).find((t) => t.name === unref(currentLocalStorageThemeName)) || + (unref(isDark) ? firstDarkTheme : firstLightTheme) || + unref(availableThemes)[0], + false ) } - const setAndApplyTheme = (theme: WebThemeType) => { + const setAutoSystemTheme = () => { + currentLocalStorageThemeName.value = null + setThemeFromStorageOrSystem() + } + + const isCurrentThemeAutoSystem = computed(() => { + return currentLocalStorageThemeName.value === null + }) + + const setAndApplyTheme = (theme: WebThemeType, updateStorage = true) => { currentTheme.value = theme - currentLocalStorageThemeName.value = unref(currentTheme).name + if (updateStorage) { + currentLocalStorageThemeName.value = unref(currentTheme).name + } const customizableDesignTokens = [ { name: 'breakpoints', prefix: 'breakpoint' }, @@ -132,6 +145,8 @@ export const useThemeStore = defineStore('theme', () => { availableThemes, currentTheme, initializeThemes, - setAndApplyTheme + setAndApplyTheme, + setAutoSystemTheme, + isCurrentThemeAutoSystem } }) diff --git a/packages/web-runtime/src/components/ThemeSwitcher.vue b/packages/web-runtime/src/components/ThemeSwitcher.vue new file mode 100644 index 00000000000..d717d34c899 --- /dev/null +++ b/packages/web-runtime/src/components/ThemeSwitcher.vue @@ -0,0 +1,56 @@ + + diff --git a/packages/web-runtime/src/components/Topbar/ThemeSwitcher.vue b/packages/web-runtime/src/components/Topbar/ThemeSwitcher.vue deleted file mode 100644 index e0e9a686eb4..00000000000 --- a/packages/web-runtime/src/components/Topbar/ThemeSwitcher.vue +++ /dev/null @@ -1,43 +0,0 @@ - - diff --git a/packages/web-runtime/src/pages/account.vue b/packages/web-runtime/src/pages/account.vue index f656e8853f4..32d315009de 100644 --- a/packages/web-runtime/src/pages/account.vue +++ b/packages/web-runtime/src/pages/account.vue @@ -107,7 +107,7 @@
- +
@@ -163,7 +163,7 @@ import { AppLoadingSpinner } from '@ownclouders/web-pkg' import { SSEAdapter } from '@ownclouders/web-client/src/sse' import { supportedLanguages } from '../defaults/languages' import { User } from '@ownclouders/web-client/src/generated' -import ThemeSwitcher from 'web-runtime/src/components/Topbar/ThemeSwitcher.vue' +import ThemeSwitcher from 'web-runtime/src/components/ThemeSwitcher.vue' export default defineComponent({ name: 'AccountPage', @@ -406,10 +406,6 @@ export default defineComponent({ } } - const onUpdateTheme = () => { - showMessage({ title: $gettext('Preference saved.') }) - } - onMounted(async () => { await loadAccountBundleTask.perform() await loadValuesListTask.perform() @@ -459,8 +455,7 @@ export default defineComponent({ loadAccountBundleTask, loadGraphUserTask, loadValuesListTask, - showEditPasswordModal, - onUpdateTheme + showEditPasswordModal } }, computed: { diff --git a/packages/web-runtime/tests/unit/components/Topbar/ThemeSwitcher.spec.ts b/packages/web-runtime/tests/unit/components/Topbar/ThemeSwitcher.spec.ts index 3eea186b0dd..6d84d47dec0 100644 --- a/packages/web-runtime/tests/unit/components/Topbar/ThemeSwitcher.spec.ts +++ b/packages/web-runtime/tests/unit/components/Topbar/ThemeSwitcher.spec.ts @@ -1,6 +1,6 @@ import { WebThemeType, useThemeStore } from '@ownclouders/web-pkg' import { mock } from 'jest-mock-extended' -import ThemeSwitcher from 'web-runtime/src/components/Topbar/ThemeSwitcher.vue' +import ThemeSwitcher from 'web-runtime/src/components/ThemeSwitcher.vue' import defaultTheme from 'web-runtime/themes/owncloud/theme.json' import { defaultPlugins, defaultStubs, mount } from 'web-test-helpers' diff --git a/packages/web-runtime/tests/unit/components/Topbar/__snapshots__/ThemeSwitcher.spec.ts.snap b/packages/web-runtime/tests/unit/components/Topbar/__snapshots__/ThemeSwitcher.spec.ts.snap index aa12a9e0b8d..262f9ef90c6 100644 --- a/packages/web-runtime/tests/unit/components/Topbar/__snapshots__/ThemeSwitcher.spec.ts.snap +++ b/packages/web-runtime/tests/unit/components/Topbar/__snapshots__/ThemeSwitcher.spec.ts.snap @@ -2,12 +2,12 @@ exports[`ThemeSwitcher component restores dark theme if dark theme is saved in localstorage 1`] = `
- +
`; exports[`ThemeSwitcher component restores light theme if light theme is saved in localstorage 1`] = `
- +
`;