1.0.7 • Published 9 months ago

antd-dynamic-theme-plugin v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

npm version webpack version node version typescript version less version code style: prettier

Antd Dynamic Theme Plugin

This plugin allows the project to support theme switching between dark and light modes

Getting Started

Make sure the project has antd, less, less-loader and webpack installed before starting.

To begin, you'll need to install antd-dynamic-theme-plugin.

npm install antd-dynamic-theme-plugin --save-dev

or

yarn add -D antd-dynamic-theme-plugin

Config

Then add the plugin to your webpack config. For example:

webpack.config.js

import AntdDynamicThemePlugin from 'antd-dynamic-theme-plugin';

export default {
  plugins: [
    new AntdDynamicThemePlugin({
      themeDir: 'src/theme',
      darkFileName: 'dark.less',
      lightFileName: 'light.less',
    }),
  ],
};

You need to create src/theme folder, then create dark.less and light.less, corresponding to light and dark themes respectively.For example:

dark.less

// antd color
@primary-color: #4e5969;
@background-color: #101222;

// more color
@my-color: blue;

light.less

@primary-color: green;

@my-color: red;

Usage

Now, you can use variables directly in less files without importing dark.less or light.less, For example:

.root {
  color: @my-color;
}

In js, you can get the corresponding variable through the global variable, For example:

const isDark = true;

function getColor(color) {
  const { dark, light } = window.THEMEVARS;
  return isDark ? dark[color] : light[color];
}

getColor('primary-color'); // return #4e5969

you can change theme, like this:

// dark;
window.changeGlobalTheme(true);

//light;
window.changeGlobalTheme(false);

Options

NameTypeDefaultDescription
'root'stringprocess.cwd()project root directory
'themeClassPre'stringantd-themeglobal class name pre
'themeDir'stringsrc/themeThe directory where the theme files are located
'darkFileName'stringdark.lessdark theme filename
'lightFileName'stringlight.lesslight theme filename
'initTheme'dark or lightdarkdefault theme