tailwind.ts 1.0 KB

12345678910111213141516171819202122232425262728
  1. /**
  2. * 判断 Tailwind class 字符串中是否包含文本颜色类(如 text-red, text-red-500, text-sky 等)
  3. * @param className 传入的 class 字符串
  4. * @returns 是否包含文本颜色类
  5. */
  6. export function hasTextColor(className: string): boolean {
  7. if (className == "") return false;
  8. const regex =
  9. /\btext-(primary|surface|red|blue|green|yellow|purple|pink|indigo|gray|grey|black|white|orange|amber|lime|emerald|teal|cyan|sky|violet|fuchsia|rose|slate|zinc|neutral|stone)(?:-\d+)?\b/;
  10. return regex.test(className);
  11. }
  12. /**
  13. * 判断 Tailwind class 字符串中是否包含字体大小类
  14. * 支持如 text-xs, text-sm, text-base, text-lg, text-xl, 以及 text-[22px]、text-[22rpx] 等自定义写法
  15. * @param className 传入的 class 字符串
  16. * @returns 是否包含字体大小类
  17. */
  18. export function hasTextSize(className: string): boolean {
  19. if (className == "") return false;
  20. const regex =
  21. /\btext-(xs|sm|md|base|lg|xl|2xl|3xl|4xl|5xl|6xl|7xl|8xl|9xl|\[\d+[a-zA-Z%]*\])\b/;
  22. return regex.test(className);
  23. }