unocss.config.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { defineConfig } from '@unocss/vite'
  2. import presetUno from '@unocss/preset-uno'
  3. import transformerDirectives from '@unocss/transformer-directives'
  4. const directions = ['m', 'ml', 'mr', 'mt', 'mb', 'p', 'pl', 'pr', 'pt', 'pb']
  5. const properties = [
  6. 'margin',
  7. 'margin-left',
  8. 'margin-right',
  9. 'margin-top',
  10. 'margin-bottom',
  11. 'padding',
  12. 'padding-left',
  13. 'padding-right',
  14. 'padding-top',
  15. 'padding-bottom',
  16. ]
  17. const pmRules = directions.map((direction, index) => {
  18. const regex = new RegExp(`^${direction}-(\\d+)$`)
  19. return [regex, ([, d]) => ({ [properties[index]]: `${parseInt(d)}rpx` })]
  20. })
  21. export default defineConfig({
  22. presets: [presetUno({ dark: 'class' })],
  23. transformers: [transformerDirectives()],
  24. shortcuts: {
  25. 'wh-full': 'w-full h-full',
  26. 'flex-center': 'flex justify-center items-center',
  27. 'flex-between-center': 'flex flex-justify-between items-center',
  28. 'flex-between-baseline': 'flex flex-justify-between items-baseline',
  29. 'flex-col-center': 'flex-center flex-col',
  30. 'flex-x-center': 'flex justify-center',
  31. 'flex-y-center': 'flex items-center',
  32. 'i-flex-center': 'inline-flex justify-center items-center',
  33. 'i-flex-x-center': 'inline-flex justify-center',
  34. 'i-flex-y-center': 'inline-flex items-center',
  35. 'flex-col': 'flex flex-col',
  36. 'flex-col-stretch': 'flex-col items-stretch',
  37. 'i-flex-col': 'inline-flex flex-col',
  38. 'i-flex-col-stretch': 'i-flex-col items-stretch',
  39. 'flex-1-hidden': 'flex-1 overflow-hidden',
  40. 'absolute-lt': 'absolute left-0 top-0',
  41. 'absolute-lb': 'absolute left-0 bottom-0',
  42. 'absolute-rt': 'absolute right-0 top-0',
  43. 'absolute-rb': 'absolute right-0 bottom-0',
  44. 'absolute-tl': 'absolute-lt',
  45. 'absolute-tr': 'absolute-rt',
  46. 'absolute-bl': 'absolute-lb',
  47. 'absolute-br': 'absolute-rb',
  48. 'absolute-center': 'absolute-lt flex-center wh-full',
  49. 'fixed-lt': 'fixed left-0 top-0',
  50. 'fixed-lb': 'fixed left-0 bottom-0',
  51. 'fixed-rt': 'fixed right-0 top-0',
  52. 'fixed-rb': 'fixed right-0 bottom-0',
  53. 'fixed-tl': 'fixed-lt',
  54. 'fixed-tr': 'fixed-rt',
  55. 'fixed-bl': 'fixed-lb',
  56. 'fixed-br': 'fixed-rb',
  57. 'fixed-center': 'fixed-lt flex-center wh-full',
  58. 'nowrap-hidden': 'whitespace-nowrap overflow-hidden',
  59. 'ellipsis-text': 'nowrap-hidden text-ellipsis',
  60. 'transition-base': 'transition-all duration-300 ease-in-out',
  61. 'border-d': 'b b-solid b-#ddd',
  62. pointer: 'cursor-pointer',
  63. },
  64. rules: [
  65. ...pmRules,
  66. [/^mx-(\d+)$/, ([, d]) => ({ margin: `0 ${parseInt(d)}px` })],
  67. [/^my-(\d+)$/, ([, d]) => ({ margin: `${parseInt(d)}px 0` })],
  68. [/^px-(\d+)$/, ([, d]) => ({ padding: `0 ${parseInt(d)}px` })],
  69. [/^py-(\d+)$/, ([, d]) => ({ padding: `${parseInt(d)}px 0` })],
  70. [/^fs-(\d+)$/, ([, d]) => ({ 'font-size': `${parseInt(d)}px` })],
  71. ],
  72. shortcuts: [
  73. {
  74. /*** flex布局 ****/
  75. 'flex-center': 'flex items-center justify-center',
  76. 'flex-between-center': 'flex items-center justify-between',
  77. 'flex-around-center': 'flex items-center justify-around',
  78. 'flex-col': 'flex flex-col',
  79. 'flex-col-center': 'flex-center flex-col',
  80. 'flex-x-center': 'flex justify-center',
  81. 'flex-y-center': 'flex items-center',
  82. /***** grid布局 *****/
  83. 'grid-column-2': 'grid grid-cols-2 grid-rows-none',
  84. 'grid-column-3': 'grid grid-cols-3 grid-rows-none',
  85. 'grid-column-4': 'grid grid-cols-4 grid-rows-none',
  86. 'grid-column-5': 'grid grid-cols-5 grid-rows-none',
  87. /**** 固定定位 ****/
  88. 'abs-lt': 'absolute left-0 top-0',
  89. 'abs-lb': 'absolute left-0 bottom-0',
  90. 'abs-rt': 'absolute right-0 top-0',
  91. 'abs-rb': 'absolute right-0 bottom-0',
  92. 'abs-center': 'absolute top-1/2 left-1/2 transform -translate-y-1/2 -translate-x-1/2', //水平垂直都居中的定位
  93. 'abs-x-center': 'absolute left-1/2 transform -translate-x-1/2', //水平居中定位
  94. 'abs-y-center': 'absolute top-1/2 transform -translate-y-1/2', //垂直居中定位
  95. /*** 固定定位 ****/
  96. 'fixed-lt': 'fixed left-0 top-0',
  97. 'fixed-lb': 'fixed left-0 bottom-0',
  98. 'fixed-rt': 'fixed right-0 top-0',
  99. 'fixed-rb': 'fixed right-0 bottom-0',
  100. 'transition-base': 'transition-all duration-300 ease-in-out',
  101. /**** 边框 ****/
  102. 'base-border': 'b b-solid b-light',
  103. 'base-border-b': 'b-b b-b-solid b-b-light',
  104. /****** 渐变色 ******/
  105. 'primary-gradient': 'bg-gradient-to-br from-gradient-start to-gradient-end',
  106. /**** 基础背景图 *****/
  107. 'base-bg-img': 'bg-cover bg-no-repeat',
  108. },
  109. ],
  110. theme: {
  111. colors: {
  112. primary: '#1890FF',
  113. light: '#ffffff',
  114. dark: '#18181c',
  115. },
  116. },
  117. })