feat(projects): Grayscale mode is added

This commit is contained in:
~li 2024-04-23 16:50:26 +08:00
parent 5a5232bdf4
commit 302fef6a39
6 changed files with 46 additions and 13 deletions

View File

@ -46,6 +46,9 @@ const showSiderInverted = computed(() => !themeStore.darkMode && themeStore.layo
<NSwitch v-model:value="themeStore.sider.inverted" /> <NSwitch v-model:value="themeStore.sider.inverted" />
</SettingItem> </SettingItem>
</Transition> </Transition>
<SettingItem label="灰度模式">
<NSwitch v-model:value="themeStore.grayscale" />
</SettingItem>
</div> </div>
</template> </template>

View File

@ -5,7 +5,15 @@ import { useEventListener, usePreferredColorScheme } from '@vueuse/core';
import { getColorPalette } from '@sa/color-palette'; import { getColorPalette } from '@sa/color-palette';
import { SetupStoreId } from '@/enum'; import { SetupStoreId } from '@/enum';
import { localStg } from '@/utils/storage'; import { localStg } from '@/utils/storage';
import { addThemeVarsToHtml, createThemeToken, getNaiveTheme, initThemeSettings, toggleCssDarkMode } from './shared'; import {
DARK_CLASS,
GRAYSCALE_CLASS,
addThemeVarsToHtml,
createThemeToken,
getNaiveTheme,
initThemeSettings,
toggleThemeMode
} from './shared';
/** Theme store */ /** Theme store */
export const useThemeStore = defineStore(SetupStoreId.Theme, () => { export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
@ -23,6 +31,9 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
return settings.value.themeScheme === 'dark'; return settings.value.themeScheme === 'dark';
}); });
/** grayscale mode */
const grayscaleMode = computed(() => settings.value.grayscale);
/** Theme colors */ /** Theme colors */
const themeColors = computed(() => { const themeColors = computed(() => {
const { themeColor, otherColor, isInfoFollowPrimary } = settings.value; const { themeColor, otherColor, isInfoFollowPrimary } = settings.value;
@ -125,7 +136,15 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
watch( watch(
darkMode, darkMode,
val => { val => {
toggleCssDarkMode(val); toggleThemeMode(DARK_CLASS, val);
},
{ immediate: true }
);
watch(
grayscaleMode,
val => {
toggleThemeMode(GRAYSCALE_CLASS, val);
}, },
{ immediate: true } { immediate: true }
); );

View File

@ -5,7 +5,10 @@ import { overrideThemeSettings, themeSettings } from '@/theme/settings';
import { themeVars } from '@/theme/vars'; import { themeVars } from '@/theme/vars';
import { localStg } from '@/utils/storage'; import { localStg } from '@/utils/storage';
const DARK_CLASS = 'dark'; export const DARK_CLASS = 'dark';
export const GRAYSCALE_CLASS = 'grayscale';
type ThemeMode = typeof DARK_CLASS | typeof GRAYSCALE_CLASS;
/** Init theme settings */ /** Init theme settings */
export function initThemeSettings() { export function initThemeSettings() {
@ -162,23 +165,24 @@ export function addThemeVarsToHtml(tokens: App.Theme.BaseToken, darkTokens: App.
} }
/** /**
* Toggle css dark mode * Toggle css dark mode or grayscale mode
* *
* @param darkMode Is dark mode * @param mode DARK_CLASS or GRAYSCALE_CLASS
* @param status
*/ */
export function toggleCssDarkMode(darkMode = false) { export function toggleThemeMode(mode: ThemeMode, status = false) {
function addDarkClass() { function addClass() {
document.documentElement.classList.add(DARK_CLASS); document.documentElement.classList.add(mode);
} }
function removeDarkClass() { function removeClass() {
document.documentElement.classList.remove(DARK_CLASS); document.documentElement.classList.remove(mode);
} }
if (darkMode) { if (status) {
addDarkClass(); addClass();
} else { } else {
removeDarkClass(); removeClass();
} }
} }

View File

@ -11,3 +11,7 @@ body,
html { html {
overflow-x: hidden; overflow-x: hidden;
} }
html.grayscale {
filter: grayscale(100%);
}

View File

@ -1,6 +1,7 @@
/** Default theme settings */ /** Default theme settings */
export const themeSettings: App.Theme.ThemeSetting = { export const themeSettings: App.Theme.ThemeSetting = {
themeScheme: 'light', themeScheme: 'light',
grayscale: false,
themeColor: '#646cff', themeColor: '#646cff',
otherColor: { otherColor: {
info: '#2080f0', info: '#2080f0',

View File

@ -20,6 +20,8 @@ declare namespace App {
themeScheme: UnionKey.ThemeScheme; themeScheme: UnionKey.ThemeScheme;
/** Theme color */ /** Theme color */
themeColor: string; themeColor: string;
/** grayscale mode */
grayscale: boolean;
/** Other color */ /** Other color */
otherColor: OtherColor; otherColor: OtherColor;
/** Whether info color is followed by the primary color */ /** Whether info color is followed by the primary color */