Provide a way to dynamically change the color of ant design components.
Note: Support hex8.
@import "~antd/dist/antd.less";
@import "~react-antd-cssvars/dist/style.less";
You can redifine the default color value
// in your less file
@import "~antd/dist/antd.less";
@import "~react-antd-cssvars/dist/style.less";
@default-antd-primary-color: #fff;
@default-antd-primary-color-hover: #333333;
@default-debug-background: #333333;
root {
--debug-background : @default-debug-background;
}
// to use as a variable in your less files
@debug-background: var(--debug-background);
// in your typescript file
import { Theme, TThemeColorTypes } from "react-antd-cssvars"
const theme = new Theme<TThemeColorTypes>()
You can initialize the theme with the first parameter of the contructor.
note: it will trigger the compute function
import { Theme, TThemeColorTypes, TTheme } from "react-antd-cssvars"
const initTheme: TTheme<TThemeColorTypes> = {
"menu-background": "#000000",
"submenu-background": "#333333",
"menu-text-color": "#fff",
}
const theme = new Theme<TThemeColorTypes>(initTheme)
You can compute color from others color change by using the second parameter.
import { Theme, TThemeColorTypes, TTheme } from "react-antd-cssvars"
const computed = (key: TThemeColorTypes, value: string, luminance: number) => {
if (key === "primary-color") {
theme.set("table-head-text-color", value)
const background = Theme.tint(value, 90)
theme.set("table-head-background", background)
theme.set("background-selected", background)
theme.set("table-head-background-hover", Theme.tint(value, 75))
theme.set("table-head-background-selected", Theme.tint(value, 15))
theme.set("table-head-background-selected-hover", value)
theme.set("box-shadow-color", value, false, 20)
}
if (theme.has(`${key}-hover` as TThemeColorTypes)) {
theme.set(`${key}-hover` as TThemeColorTypes, Theme.tint(value, 17.5), false)
}
if (theme.has(`${key}-background` as TThemeColorTypes)) {
theme.set(`${key}-background` as TThemeColorTypes, Theme.tint(value, 75), false)
}
if (key === "menu-background") {
if (luminance < 0.015) {
theme.set("submenu-background", Theme.tint(value, 20), false)
} else {
theme.set("submenu-background", Theme.shade(value, 20), false)
}
if (Theme.isdark(value)) {
theme.set("menu-text-color", theme.get("text-color-inv"), false, 0.65)
} else {
theme.set("menu-text-color", theme.get("text-color"), false, 0.65)
}
}
}
const theme = new Theme<TThemeColorTypes>(null, computed)
If you want to manage more cssvars. (Do not forget to define and initialize it in your less/css file)
import { Theme, TThemeColorTypes, TTheme, DThemeColorTypes, ThemeColorKeys } from "react-antd-cssvars"
type CustomTThemeColorTypes = TThemeColorTypes | "debug"
const customDThemeColorTypes: CustomTThemeColorTypes[] = [...ThemeColorKeys]
customDThemeColorTypes.push("debug")
// optional if you have define it in your less/css file
const initTheme: TTheme<CustomTThemeColorTypes> = {
"debug": "#333333",
}
const theme = new Theme<CustomTThemeColorTypes>(initTheme, null, customDThemeColorTypes)
Generated using TypeDoc