Skip to content

Commit

Permalink
feat: theme color switch
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Feb 3, 2021
1 parent 85729f0 commit 3d1681e
Show file tree
Hide file tree
Showing 27 changed files with 468 additions and 252 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### ✨ Features

- `ApiSelect`新增 `numberToString`属性,用于将 value 为`number`的值全部转化为`string`
- 新增主题色切换

### ⚡ Performance Improvements

Expand Down
25 changes: 0 additions & 25 deletions build/config/lessModifyVars.ts

This file was deleted.

106 changes: 106 additions & 0 deletions build/config/themeConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { generate } from '@ant-design/colors';
export const primaryColor = '#0084f4';

export const themeMode = 'light';

export type ThemeMode = 'dark' | 'light';

type Fn = (...arg: any) => any;

export interface GenerateColorsParams {
mixLighten: Fn;
mixDarken: Fn;
tinycolor: any;
color?: string;
}

export function generateAntColors(color: string, mode: ThemeMode) {
return generate(color, {
theme: mode == 'dark' ? 'dark' : 'default',
});
}

export function getThemeColors(color?: string, theme?: ThemeMode) {
const tc = color || primaryColor;
const tm = theme || themeMode;
const colors = generateAntColors(tc, tm);
const primary = colors[5];
const modeColors = generateAntColors(primary, tm === 'dark' ? 'light' : 'dark');

return [...colors, ...modeColors];
}

export function generateColors({
color = primaryColor,
mixLighten,
mixDarken,
tinycolor,
}: GenerateColorsParams) {
const lightens = new Array(19).fill(0).map((t, i) => {
return mixLighten(color, i / 5);
});

const darkens = new Array(19).fill(0).map((t, i) => {
return mixDarken(color, i / 5);
});

const alphaColors = new Array(19).fill(0).map((t, i) => {
return tinycolor(color)
.setAlpha(i / 20)
.toRgbString();
});

const tinycolorLightens = new Array(19)
.fill(0)
.map((t, i) => {
return tinycolor(color)
.lighten(i * 5)
.toHexString();
})
.filter((item) => item !== '#ffffff');

const tinycolorDarkens = new Array(19)
.fill(0)
.map((t, i) => {
return tinycolor(color)
.darken(i * 5)
.toHexString();
})
.filter((item) => item !== '#000000');
return [...lightens, ...darkens, ...alphaColors, ...tinycolorDarkens, ...tinycolorLightens];
}

/**
* less global variable
*/
export function generateModifyVars() {
const palettes = generateAntColors(primaryColor, themeMode);
const primary = palettes[5];

const primaryColorObj: Record<string, string> = {};

for (let index = 0; index < 10; index++) {
primaryColorObj[`primary-${index}`] = palettes[index];
}

return {
'primary-color': primary,
...primaryColorObj,
'info-color': primary,
'alert-info-bg-color': palettes[0],
'alert-info-border-color': palettes[2],
'processing-color': primary,
'success-color': '#55D187', // Success color
'error-color': '#ED6F6F', // False color
'warning-color': '#EFBD47', // Warning color
'disabled-color': 'rgba(0, 0, 0, 0.25)', // Failure color
'heading-color': 'rgba(0, 0, 0, 0.85)', // Title color
'text-color': 'rgba(0, 0, 0, 0.85)', // Main text color
'text-color-secondary ': 'rgba(0, 0, 0, 0.45)', // Subtext color
'font-size-base': '14px', // Main font size
'box-shadow-base': '0 2px 8px rgba(0, 0, 0, 0.15)', // Floating shadow
'border-color-base': '#d9d9d9', // Border color,
'border-radius-base': '2px', // Component/float fillet
'link-color': primary, // Link color
};
}
8 changes: 6 additions & 2 deletions build/vite/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { configHtmlPlugin } from './html';
import { configPwaConfig } from './pwa';
import { configMockPlugin } from './mock';
import { configGzipPlugin } from './gzip';
import { configStyleImportConfig } from './styleImport';
import { configStyleImportPlugin } from './styleImport';
import { configVisualizerConfig } from './visualizer';
import { configThemePlugin } from './theme';

// gen vite plugins
export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
Expand All @@ -29,13 +30,16 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
vitePlugins.push(PurgeIcons());

// vite-plugin-style-import
vitePlugins.push(configStyleImportConfig());
vitePlugins.push(configStyleImportPlugin());

// rollup-plugin-gzip
vitePlugins.push(configGzipPlugin(isBuild));

// rollup-plugin-visualizer
vitePlugins.push(configVisualizerConfig());

//vite-plugin-theme
vitePlugins.push(configThemePlugin());

return vitePlugins;
}
2 changes: 1 addition & 1 deletion build/vite/plugin/styleImport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styleImport from 'vite-plugin-style-import';

export function configStyleImportConfig() {
export function configStyleImportPlugin() {
const pwaPlugin = styleImport({
libs: [
{
Expand Down
15 changes: 15 additions & 0 deletions build/vite/plugin/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { viteThemePlugin, mixLighten, mixDarken, tinycolor } from 'vite-plugin-theme';
import { getThemeColors, generateColors } from '../../config/themeConfig';

export function configThemePlugin() {
const colors = generateColors({
mixDarken,
mixLighten,
tinycolor,
});

const plugin = viteThemePlugin({
colorVariables: [...getThemeColors(), ...colors],
});
return plugin;
}
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
},
"dependencies": {
"@iconify/iconify": "^2.0.0-rc.6",
"@vueuse/core": "^4.0.11",
"@vueuse/core": "^4.0.12",
"ant-design-vue": "2.0.0-rc.9",
"apexcharts": "^3.23.1",
"apexcharts": "^3.24.0",
"axios": "^0.21.1",
"crypto-es": "^1.2.6",
"echarts": "^4.9.0",
Expand All @@ -33,20 +33,20 @@
"path-to-regexp": "^6.2.0",
"qrcode": "^1.4.4",
"sortablejs": "^1.13.0",
"vditor": "^3.7.7",
"vditor": "^3.8.0",
"vue": "^3.0.5",
"vue-i18n": "9.0.0-rc.2",
"vue-router": "^4.0.3",
"vue-types": "^3.0.1",
"vuex": "^4.0.0-rc.2",
"vue-types": "^3.0.2",
"vuex": "^4.0.0",
"vuex-module-decorators": "^1.0.1",
"xlsx": "^0.16.9",
"zxcvbn": "^4.4.2"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@iconify/json": "^1.1.294",
"@iconify/json": "^1.1.296",
"@ls-lint/ls-lint": "^1.9.2",
"@purge-icons/generated": "^0.6.0",
"@types/echarts": "^4.9.3",
Expand All @@ -61,10 +61,10 @@
"@types/sortablejs": "^1.10.6",
"@types/yargs": "^16.0.0",
"@types/zxcvbn": "^4.4.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"@vitejs/plugin-legacy": "^1.2.2",
"@vitejs/plugin-vue": "^1.1.3",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"@vitejs/plugin-legacy": "^1.2.3",
"@vitejs/plugin-vue": "^1.1.4",
"@vitejs/plugin-vue-jsx": "^1.0.2",
"@vue/compiler-sfc": "^3.0.5",
"@vuedx/typecheck": "^0.6.3",
Expand All @@ -74,14 +74,14 @@
"conventional-changelog-cli": "^2.1.1",
"cross-env": "^7.0.3",
"dotenv": "^8.2.0",
"eslint": "^7.18.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^7.5.0",
"esno": "^0.4.0",
"esno": "^0.4.3",
"fs-extra": "^9.1.0",
"husky": "^4.3.8",
"less": "^4.1.0",
"less": "^4.1.1",
"lint-staged": "^10.5.3",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
Expand All @@ -93,12 +93,13 @@
"stylelint-order": "^4.1.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3",
"vite": "2.0.0-beta.59",
"vite": "2.0.0-beta.62",
"vite-plugin-html": "^2.0.0-rc.3",
"vite-plugin-mock": "^2.0.0-rc.2",
"vite-plugin-purge-icons": "^0.6.0",
"vite-plugin-pwa": "^0.4.1",
"vite-plugin-pwa": "^0.4.2",
"vite-plugin-style-import": "^0.5.5",
"vite-plugin-theme": "0.3.2",
"vue-eslint-parser": "^7.4.1",
"yargs": "^16.2.0"
},
Expand All @@ -118,6 +119,6 @@
}
},
"engines": {
"node": ">=10.16.1"
"node": "^12 || ^14"
}
}
2 changes: 1 addition & 1 deletion src/design/ant/btn.less
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

&:hover,
&:focus {
color: @white;
color: @white !important;
background-color: @button-primary-hover-color;
}

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/setting/useRootSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const getShowFooter = computed(() => unref(getRootSetting).showFooter);

const getShowBreadCrumb = computed(() => unref(getRootSetting).showBreadCrumb);

const getThemeColor = computed(() => unref(getRootSetting).themeColor);

const getShowBreadCrumbIcon = computed(() => unref(getRootSetting).showBreadCrumbIcon);

const getFullContent = computed(() => unref(getRootSetting).fullContent);
Expand Down Expand Up @@ -74,5 +76,6 @@ export function useRootSetting() {
getShowFooter,
getContentMode,
getLockTime,
getThemeColor,
};
}
19 changes: 18 additions & 1 deletion src/layouts/default/setting/SettingDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import {
mixSidebarTriggerOptions,
} from './enum';

import { HEADER_PRESET_BG_COLOR_LIST, SIDE_BAR_BG_COLOR_LIST } from '/@/settings/designSetting';
import {
HEADER_PRESET_BG_COLOR_LIST,
SIDE_BAR_BG_COLOR_LIST,
APP_PRESET_COLOR_LIST,
} from '/@/settings/designSetting';

const { t } = useI18n();

Expand All @@ -48,6 +52,7 @@ export default defineComponent({
getColorWeak,
getGrayMode,
getLockTime,
getThemeColor,
} = useRootSetting();

const {
Expand Down Expand Up @@ -129,6 +134,16 @@ export default defineComponent({
);
}

function renderMainTheme() {
return (
<ThemePicker
colorList={APP_PRESET_COLOR_LIST}
def={unref(getThemeColor)}
event={HandlerEnum.CHANGE_THEME_COLOR}
/>
);
}

/**
* @description:
*/
Expand Down Expand Up @@ -391,6 +406,8 @@ export default defineComponent({
>
<Divider>{() => t('layout.setting.navMode')}</Divider>
{renderSidebar()}
<Divider>{() => t('layout.setting.sysTheme')}</Divider>
{renderMainTheme()}
<Divider>{() => t('layout.setting.headerTheme')}</Divider>
{renderHeaderTheme()}
<Divider>{() => t('layout.setting.sidebarTheme')}</Divider>
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/default/setting/components/SettingFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import { useMessage } from '/@/hooks/web/useMessage';
import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { updateColorWeak, updateGrayMode } from '/@/logics/theme';
import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
export default defineComponent({
name: 'SettingFooter',
Expand Down
1 change: 1 addition & 0 deletions src/layouts/default/setting/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { t } = useI18n();

export enum HandlerEnum {
CHANGE_LAYOUT,
CHANGE_THEME_COLOR,
// menu
MENU_HAS_DRAG,
MENU_ACCORDION,
Expand Down
Loading

0 comments on commit 3d1681e

Please sign in to comment.