Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projects): support grayscale. fixed #385 #395

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/layouts/modules/theme-drawer/modules/dark-mode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function handleSegmentChange(value: string | number) {
themeStore.setThemeScheme(value as UnionKey.ThemeScheme);
}

function handleGrayscaleChange(value: boolean) {
themeStore.setGrayscale(value);
}

const showSiderInverted = computed(() => !themeStore.darkMode && themeStore.layout.mode.includes('vertical'));
</script>

Expand All @@ -46,6 +50,9 @@ const showSiderInverted = computed(() => !themeStore.darkMode && themeStore.layo
<NSwitch v-model:value="themeStore.sider.inverted" />
</SettingItem>
</Transition>
<SettingItem :label="$t('theme.grayscale')">
<NSwitch :value="themeStore.grayscale" @update:value="handleGrayscaleChange" />
</SettingItem>
</div>
</template>

Expand Down
1 change: 1 addition & 0 deletions src/locales/langs/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const local: App.I18n.Schema = {
dark: 'Dark',
auto: 'Follow System'
},
grayscale: 'Grayscale',
layoutMode: {
title: 'Layout Mode',
vertical: 'Vertical Menu Mode',
Expand Down
1 change: 1 addition & 0 deletions src/locales/langs/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const local: App.I18n.Schema = {
dark: '暗黑模式',
auto: '跟随系统'
},
grayscale: '灰度模式',
layoutMode: {
title: '布局模式',
vertical: '左侧菜单模式',
Expand Down
31 changes: 29 additions & 2 deletions src/store/modules/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { useEventListener, usePreferredColorScheme } from '@vueuse/core';
import { getColorPalette } from '@sa/color-palette';
import { SetupStoreId } from '@/enum';
import { localStg } from '@/utils/storage';
import { addThemeVarsToHtml, createThemeToken, getNaiveTheme, initThemeSettings, toggleCssDarkMode } from './shared';
import {
addThemeVarsToHtml,
createThemeToken,
getNaiveTheme,
initThemeSettings,
toggleCssDarkMode,
toggleGrayscaleMode
} from './shared';

/** Theme store */
export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
Expand All @@ -23,6 +30,9 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
return settings.value.themeScheme === 'dark';
});

/** grayscale mode */
const grayscaleMode = computed(() => settings.value.grayscale);

/** Theme colors */
const themeColors = computed(() => {
const { themeColor, otherColor, isInfoFollowPrimary } = settings.value;
Expand Down Expand Up @@ -60,6 +70,15 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
settings.value.themeScheme = themeScheme;
}

/**
* Set grayscale value
*
* @param isGrayscale
*/
function setGrayscale(isGrayscale: boolean) {
settings.value.grayscale = isGrayscale;
}

/** Toggle theme scheme */
function toggleThemeScheme() {
const themeSchemes: UnionKey.ThemeScheme[] = ['light', 'dark', 'auto'];
Expand Down Expand Up @@ -130,12 +149,19 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
{ immediate: true }
);

watch(
grayscaleMode,
val => {
toggleGrayscaleMode(val);
},
{ immediate: true }
);

// themeColors change, update css vars and storage theme color
watch(
themeColors,
val => {
setupThemeVarsToHtml();

localStg.set('themeColor', val.primary);
},
{ immediate: true }
Expand All @@ -153,6 +179,7 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
themeColors,
naiveTheme,
settingsJson,
setGrayscale,
resetStore,
setThemeScheme,
toggleThemeScheme,
Expand Down
28 changes: 20 additions & 8 deletions src/store/modules/theme/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getColorByPaletteNumber, getColorPalette } from '@sa/color-palette';
import { addColorAlpha, getRgbOfColor } from '@sa/utils';
import { overrideThemeSettings, themeSettings } from '@/theme/settings';
import { themeVars } from '@/theme/vars';
import { toggleHtmlClass } from '@/utils/common';
import { localStg } from '@/utils/storage';

const DARK_CLASS = 'dark';
Expand Down Expand Up @@ -167,18 +168,29 @@ export function addThemeVarsToHtml(tokens: App.Theme.BaseToken, darkTokens: App.
* @param darkMode Is dark mode
*/
export function toggleCssDarkMode(darkMode = false) {
function addDarkClass() {
document.documentElement.classList.add(DARK_CLASS);
}
const { add, remove } = toggleHtmlClass(DARK_CLASS);

function removeDarkClass() {
document.documentElement.classList.remove(DARK_CLASS);
if (darkMode) {
add();
} else {
remove();
}
}

if (darkMode) {
addDarkClass();
/**
* Toggle grayscale mode
*
* @param grayscaleMode Is grayscale mode
*/
export function toggleGrayscaleMode(grayscaleMode = false) {
const GRAYSCALE_CLASS = 'grayscale';

const { add, remove } = toggleHtmlClass(GRAYSCALE_CLASS);

if (grayscaleMode) {
add();
} else {
removeDarkClass();
remove();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/styles/css/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ body,
html {
overflow-x: hidden;
}

html.grayscale {
filter: grayscale(100%);
}
1 change: 1 addition & 0 deletions src/theme/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Default theme settings */
export const themeSettings: App.Theme.ThemeSetting = {
themeScheme: 'light',
grayscale: false,
themeColor: '#646cff',
otherColor: {
info: '#2080f0',
Expand Down
3 changes: 3 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ declare namespace App {
themeScheme: UnionKey.ThemeScheme;
/** Theme color */
themeColor: string;
/** grayscale mode */
grayscale: boolean;
/** Other color */
otherColor: OtherColor;
/** Whether info color is followed by the primary color */
Expand Down Expand Up @@ -298,6 +300,7 @@ declare namespace App {
};
theme: {
themeSchema: { title: string } & Record<UnionKey.ThemeScheme, string>;
grayscale: string;
layoutMode: { title: string } & Record<UnionKey.ThemeLayoutMode, string>;
themeColor: {
title: string;
Expand Down
20 changes: 20 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ export function translateOptions(options: CommonType.Option<string>[]) {
label: $t(option.label as App.I18n.I18nKey)
}));
}

/**
* Toggle html class
*
* @param className
*/
export function toggleHtmlClass(className: string) {
function add() {
document.documentElement.classList.add(className);
}

function remove() {
document.documentElement.classList.remove(className);
}

return {
add,
remove
};
}
Loading