-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the UI toggle for changing theme (#4840)
Co-authored-by: zackdotcat <[email protected]> Co-authored-by: zack <[email protected]>
- Loading branch information
1 parent
f286651
commit 3711b60
Showing
10 changed files
with
213 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!-- | ||
Present users with a way to change the app theme between three options: | ||
light, dark and system. | ||
--> | ||
|
||
<script setup lang="ts"> | ||
import { useI18n, useDarkMode } from "#imports" | ||
|
||
import { computed, type ComputedRef } from "vue" | ||
|
||
import { useUiStore } from "~/stores/ui" | ||
|
||
import VIcon from "~/components/VIcon/VIcon.vue" | ||
import VSelectField, { | ||
type Choice, | ||
} from "~/components/VSelectField/VSelectField.vue" | ||
|
||
const i18n = useI18n({ useScope: "global" }) | ||
const uiStore = useUiStore() | ||
|
||
const THEME_ICON_NAME = Object.freeze({ | ||
light: "sun", | ||
dark: "moon", | ||
}) | ||
|
||
const THEME_TEXT = { | ||
light: i18n.t(`theme.choices.light`), | ||
dark: i18n.t(`theme.choices.dark`), | ||
} | ||
|
||
const colorMode = computed({ | ||
get: () => uiStore.colorMode, | ||
set: (value) => { | ||
uiStore.setColorMode(value) | ||
}, | ||
}) | ||
|
||
const darkMode = useDarkMode() | ||
|
||
/** | ||
* The icon always reflects the actual theme applied to the site. | ||
* Therefore, it must be based on the value of `effectiveColorMode`. | ||
*/ | ||
const currentThemeIcon = computed( | ||
() => THEME_ICON_NAME[darkMode.effectiveColorMode.value] | ||
) | ||
|
||
/** | ||
* The choices are computed because the text for the color mode choice | ||
* "system" is dynamic and reflects the user's preferred color scheme at | ||
* the OS-level. | ||
*/ | ||
const choices: ComputedRef<Choice[]> = computed(() => { | ||
const systemText = `${i18n.t(`theme.choices.system`)} (${THEME_TEXT[darkMode.osColorMode.value]})` | ||
return [ | ||
{ key: "light", text: THEME_TEXT.light }, | ||
{ key: "dark", text: THEME_TEXT.dark }, | ||
{ key: "system", text: systemText }, | ||
] | ||
}) | ||
</script> | ||
|
||
<template> | ||
<VSelectField | ||
v-model="colorMode" | ||
field-id="theme" | ||
:choices="choices" | ||
:blank-text="$t('theme.theme')" | ||
:label-text="$t('theme.theme')" | ||
:show-selected="false" | ||
> | ||
<template #start> | ||
<VIcon :name="currentThemeIcon" /> | ||
</template> | ||
</VSelectField> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters