lucide-colors.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import plugin from "tailwindcss/plugin";
  2. import flattenColorPalette from "tailwindcss/lib/util/flattenColorPalette";
  3. // 颜色类同时携带图标绘制信息,确保父按钮、嵌套颜色和 Tailwind 状态使用同一条继承链。
  4. export default plugin(({ matchUtilities, theme, addBase, addUtilities }) => {
  5. matchUtilities(
  6. {
  7. text: (value) => {
  8. if (
  9. [
  10. "inherit",
  11. "currentColor",
  12. "currentcolor",
  13. "var(--color-inherit)",
  14. "var(--color-current)",
  15. ].includes(value)
  16. ) {
  17. return { "--lucide-stroke": "inherit", "--lucide-filter": "inherit" };
  18. }
  19. // Tailwind 将 /50 等颜色修饰符传成 color-mix;保留它的额外 alpha。
  20. const mixed = value.match(
  21. /^color-mix\(in oklab, (var\(--color-foreground-subt(?:le|lest)\)) ([\d.]+)%, transparent\)$/,
  22. );
  23. const color = mixed?.[1] ?? value;
  24. const tone =
  25. color === "var(--color-foreground-subtle)"
  26. ? "0.6"
  27. : color === "var(--color-foreground-subtlest)"
  28. ? "var(--lucide-subtlest-opacity)"
  29. : null;
  30. const alpha = mixed ? `calc(${tone} * ${Number(mixed[2]) / 100})` : tone;
  31. return {
  32. "--lucide-stroke": tone ? "rgb(from currentColor r g b / 1)" : "currentColor",
  33. "--lucide-filter": tone ? `opacity(${alpha})` : "initial",
  34. };
  35. },
  36. },
  37. {
  38. // 用 token 身份判断层级,不能比较构建时展开的色值:深浅主题会在运行时覆盖它。
  39. values: Object.fromEntries(
  40. Object.keys(flattenColorPalette(theme("colors"))).map((name) => [
  41. name,
  42. `var(--color-${name})`,
  43. ]),
  44. ),
  45. type: "color",
  46. },
  47. );
  48. const relativeColorSupport = "@supports (color: rgb(from red r g b / 1))";
  49. addBase({
  50. [relativeColorSupport]: {
  51. // 描边必须先不透明绘制,再对整个 SVG 合成。filter opacity 与已有 opacity-* 独立相乘。
  52. // 限定 Lucide 默认描边入口,其他 SVG / 自定义描边不参与。
  53. 'svg.lucide[stroke="currentColor"]': {
  54. stroke: "var(--lucide-stroke, currentColor)",
  55. filter: "var(--lucide-filter, none)",
  56. },
  57. 'svg.lucide[stroke="currentColor"][fill="currentColor"]': {
  58. fill: "var(--lucide-stroke, currentColor)",
  59. },
  60. },
  61. });
  62. // 实心状态点和停止按钮也要先用不透明填充;非 Lucide 的 fill-current 沿用原值。
  63. addUtilities({
  64. ".fill-current": {
  65. [relativeColorSupport]: {
  66. '&:is(.lucide):where(svg[stroke="currentColor"])': {
  67. fill: "var(--lucide-stroke, currentColor)",
  68. },
  69. },
  70. },
  71. });
  72. });