Skip to content

Commit

Permalink
feat(projects): add recommend color switch. closed #388
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Apr 25, 2024
1 parent 3499997 commit a1920fc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/layouts/modules/theme-drawer/modules/theme-color.vue
Expand Up @@ -36,6 +36,27 @@ const swatches: string[] = [
<template>
<NDivider>{{ $t('theme.themeColor.title') }}</NDivider>
<div class="flex-col-stretch gap-12px">
<NTooltip placement="top-start">
<template #trigger>
<SettingItem key="recommend-color" :label="$t('theme.recommendColor')">
<NSwitch v-model:value="themeStore.recommendColor" />
</SettingItem>
</template>
<p>
<span class="pr-12px">{{ $t('theme.recommendColorDesc') }}</span>
<br />
<NButton
text
tag="a"
href="https://uicolors.app/create"
target="_blank"
rel="noopener noreferrer"
class="text-gray"
>
https://uicolors.app/create
</NButton>
</p>
</NTooltip>
<SettingItem v-for="(_, key) in themeStore.themeColors" :key="key" :label="$t(`theme.themeColor.${key}`)">
<template v-if="key === 'info'" #suffix>
<NCheckbox v-model:checked="themeStore.isInfoFollowPrimary">
Expand Down
3 changes: 2 additions & 1 deletion src/locales/langs/en-us.ts
Expand Up @@ -65,7 +65,8 @@ const local: App.I18n.Schema = {
'vertical-mix': 'Vertical Mix Menu Mode',
'horizontal-mix': 'Horizontal Mix menu Mode'
},
recommendColor: 'Apply Recommended Algorithm Color',
recommendColor: 'Apply Recommended Color Algorithm',
recommendColorDesc: 'The recommended color algorithm refers to',
themeColor: {
title: 'Theme Color',
primary: 'Primary',
Expand Down
1 change: 1 addition & 0 deletions src/locales/langs/zh-cn.ts
Expand Up @@ -66,6 +66,7 @@ const local: App.I18n.Schema = {
'horizontal-mix': '顶部菜单混合模式'
},
recommendColor: '应用推荐算法的颜色',
recommendColorDesc: '推荐颜色的算法参照',
themeColor: {
title: '主题颜色',
primary: '主色',
Expand Down
4 changes: 2 additions & 2 deletions src/store/modules/theme/index.ts
Expand Up @@ -45,7 +45,7 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {
});

/** Naive theme */
const naiveTheme = computed(() => getNaiveTheme(themeColors.value));
const naiveTheme = computed(() => getNaiveTheme(themeColors.value, settings.value.recommendColor));

/**
* Settings json
Expand Down Expand Up @@ -125,7 +125,7 @@ export const useThemeStore = defineStore(SetupStoreId.Theme, () => {

/** Setup theme vars to html */
function setupThemeVarsToHtml() {
const { themeTokens, darkThemeTokens } = createThemeToken(themeColors.value);
const { themeTokens, darkThemeTokens } = createThemeToken(themeColors.value, settings.value.recommendColor);
addThemeVarsToHtml(themeTokens, darkThemeTokens);
}

Expand Down
12 changes: 8 additions & 4 deletions src/store/modules/theme/shared.ts
Expand Up @@ -33,9 +33,10 @@ export function initThemeSettings() {
* Create theme token
*
* @param colors Theme colors
* @param [recommended=false] Use recommended color. Default is `false`
*/
export function createThemeToken(colors: App.Theme.ThemeColor) {
const paletteColors = createThemePaletteColors(colors);
export function createThemeToken(colors: App.Theme.ThemeColor, recommended = false) {
const paletteColors = createThemePaletteColors(colors, recommended);

const themeTokens: App.Theme.ThemeToken = {
colors: {
Expand Down Expand Up @@ -75,6 +76,7 @@ export function createThemeToken(colors: App.Theme.ThemeColor) {
* Create theme palette colors
*
* @param colors Theme colors
* @param [recommended=false] Use recommended color. Default is `false`
*/
function createThemePaletteColors(colors: App.Theme.ThemeColor, recommended = false) {
const colorKeys = Object.keys(colors) as App.Theme.ThemeColorKey[];
Expand Down Expand Up @@ -205,6 +207,7 @@ interface NaiveColorAction {
* Get naive theme colors
*
* @param colors Theme colors
* @param [recommended=false] Use recommended color. Default is `false`
*/
function getNaiveThemeColors(colors: App.Theme.ThemeColor, recommended = false) {
const colorActions: NaiveColorAction[] = [
Expand Down Expand Up @@ -234,13 +237,14 @@ function getNaiveThemeColors(colors: App.Theme.ThemeColor, recommended = false)
* Get naive theme
*
* @param colors Theme colors
* @param [recommended=false] Use recommended color. Default is `false`
*/
export function getNaiveTheme(colors: App.Theme.ThemeColor) {
export function getNaiveTheme(colors: App.Theme.ThemeColor, recommended = false) {
const { primary: colorLoading } = colors;

const theme: GlobalThemeOverrides = {
common: {
...getNaiveThemeColors(colors),
...getNaiveThemeColors(colors, recommended),
borderRadius: '6px'
},
LoadingBar: {
Expand Down
1 change: 1 addition & 0 deletions src/typings/app.d.ts
Expand Up @@ -305,6 +305,7 @@ declare namespace App {
grayscale: string;
layoutMode: { title: string } & Record<UnionKey.ThemeLayoutMode, string>;
recommendColor: string;
recommendColorDesc: string;
themeColor: {
title: string;
followPrimary: string;
Expand Down

0 comments on commit a1920fc

Please sign in to comment.