-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: override the color variables in tailwind.css with antd's color …
…variables
- Loading branch information
1 parent
365d4c2
commit 036acea
Showing
24 changed files
with
385 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// CSS 变量前缀 | ||
export const prefix = "oo"; | ||
// 基础色 | ||
export const colors = [ | ||
"blue", | ||
"purple", | ||
"cyan", | ||
"green", | ||
"magenta", | ||
"pink", | ||
"red", | ||
"orange", | ||
"yellow", | ||
"volcano", | ||
"geekblue", | ||
"gold", | ||
"lime", | ||
]; | ||
// 品牌色 | ||
export const brandColors = [ | ||
"colorPrimary", | ||
"colorPrimaryBg", | ||
"colorPrimaryBgHover", | ||
"colorPrimaryBorder", | ||
"colorPrimaryBorderHover", | ||
"colorPrimaryHover", | ||
"colorPrimaryActive", | ||
"colorPrimaryTextHover", | ||
"colorPrimaryText", | ||
"colorPrimaryTextActive", | ||
]; | ||
// 成功色 | ||
export const successColors = [ | ||
"colorSuccess", | ||
"colorSuccessBg", | ||
"colorSuccessBgHover", | ||
"colorSuccessBorder", | ||
"colorSuccessBorderHover", | ||
"colorSuccessHover", | ||
"colorSuccessActive", | ||
"colorSuccessTextHover", | ||
"colorSuccessText", | ||
"colorSuccessTextActive", | ||
]; | ||
// 警告色 | ||
export const warningColors = [ | ||
"colorWarning", | ||
"colorWarningBg", | ||
"colorWarningBgHover", | ||
"colorWarningBorder", | ||
"colorWarningBorderHover", | ||
"colorWarningHover", | ||
"colorWarningActive", | ||
"colorWarningTextHover", | ||
"colorWarningText", | ||
"colorWarningTextActive", | ||
]; | ||
// 错误色 | ||
export const errorColors = [ | ||
"colorError", | ||
"colorErrorBg", | ||
"colorErrorBgHover", | ||
"colorErrorBorder", | ||
"colorErrorBorderHover", | ||
"colorErrorHover", | ||
"colorErrorActive", | ||
"colorErrorTextHover", | ||
"colorErrorText", | ||
"colorErrorTextActive", | ||
]; | ||
// 信息色 | ||
export const infoColors = [ | ||
"colorInfo", | ||
"colorInfoBg", | ||
"colorInfoBgHover", | ||
"colorInfoBorder", | ||
"colorInfoBorderHover", | ||
"colorInfoHover", | ||
"colorInfoActive", | ||
"colorInfoTextHover", | ||
"colorInfoText", | ||
"colorInfoTextActive", | ||
]; | ||
// 功能性色 | ||
export const functionalColors = [ | ||
...successColors, | ||
...warningColors, | ||
...errorColors, | ||
...infoColors, | ||
]; | ||
// 中性色 | ||
export const neutralColors = [ | ||
"colorText", | ||
"colorTextSecondary", | ||
"colorTextTertiary", | ||
"colorTextQuaternary", | ||
// 组件容器背景色 | ||
"colorBgContainer", | ||
"colorBgElevated", | ||
// 布局背景色 | ||
"colorBgLayout", | ||
|
||
"colorBgSpotlight", | ||
"colorBgMask", | ||
// 边框色 | ||
"colorBorder", | ||
"colorBorderSecondary", | ||
// 填充色 | ||
"colorFill", | ||
"colorFillSecondary", | ||
"colorFillTertiary", | ||
"colorFillQuaternary", | ||
]; | ||
export const productLevelColorSystem = [ | ||
...brandColors, | ||
...functionalColors, | ||
]; | ||
export const colorPaletteNumbers = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950]; | ||
export const colorVariantsCount = 10; | ||
|
||
// ['blue', 'blue-1', 'blue-2', ……, 'blue-10', 'purple',……] | ||
export const colorPalettes = colors.flatMap(color => | ||
[color, ...Array.from({ length: colorVariantsCount }, (_, i) => `${color}-${i + 1}`)], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { GlobalToken } from "antd"; | ||
import { colorPalettes, neutralColors, prefix, productLevelColorSystem } from "./constants"; | ||
|
||
/** | ||
* 16 进制颜色值转 RGB 颜色值,因为 16 进制的颜色值在 tailwind 中不支持透明度,比如无法使用 bg-blue-500/20 | ||
* @see https://tailwindcss.com/docs/customizing-colors#using-css-variables | ||
*/ | ||
function hexToRGB(hex: string) { | ||
// 移除可能存在的 # 号 | ||
hex = hex.replace("#", ""); | ||
|
||
// 获取 R、G、B 的值 | ||
const r = Number.parseInt(hex.substring(0, 2), 16); | ||
const g = Number.parseInt(hex.substring(2, 4), 16); | ||
const b = Number.parseInt(hex.substring(4, 6), 16); | ||
|
||
return `${r} ${g} ${b}`; | ||
} | ||
|
||
// 判断是否是 RGB 颜色值 | ||
function isRGBColor(color: string) { | ||
return color.trim().startsWith("rgb"); | ||
} | ||
|
||
export function getCSSVariablesByTokens(tokens: GlobalToken) { | ||
return Object.entries(tokens) | ||
.reduce((acc, [key, value]): string => { | ||
// 功能色系,不包含中性色系 | ||
if (productLevelColorSystem.includes(key)) { | ||
const rgb = hexToRGB(value); | ||
return `${acc}--${prefix}-${key}:${rgb};`; | ||
} | ||
|
||
// 中性色系 | ||
if (neutralColors.includes(key)) { | ||
// 如果颜色值是 rgb 格式,则直接使用 | ||
const rgb = isRGBColor(value) ? value : `rgb(${hexToRGB(value)})`; | ||
return `${acc}--${prefix}-${key}:${rgb};`; | ||
} | ||
// 色板 | ||
return colorPalettes.includes(key) ? `${acc}--${prefix}-${key}:${hexToRGB(value)};` : acc; | ||
}, ""); | ||
} | ||
|
||
/** Setup antd theme tokens to html */ | ||
export function setupAntdThemeTokensToHtml(antdTokens: GlobalToken) { | ||
const cssVariablesString = getCSSVariablesByTokens(antdTokens); | ||
|
||
const styleId = "antd-theme-tokens"; | ||
const styleSheet = document.querySelector(`#${styleId}`) || document.createElement("style"); | ||
styleSheet.id = styleId; | ||
styleSheet.textContent = `:root { ${cssVariablesString} }`; | ||
document.head.appendChild(styleSheet); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
036acea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
更加友好的主题展示:
使用 antd 的颜色系统和断点系统覆盖 tailwind.css 的样式系统,从而保证系统风格的统一性。
截图来自 https://kitchen.alipay.com/ 插件。
036acea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
覆盖 tailwind.css 的颜色值之后,新的可获得的颜色如下,使用的时候取下面对象的 key 即可,比如
bg-blue-50
,还可以和透明度结合使用,例如bg-blue-50/20
:036acea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中性色板
d4a7931