unocss.config.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { presetUni } from '@uni-helper/unocss-preset-uni';
  2. import { defineConfig, presetIcons, presetAttributify, transformerDirectives, transformerVariantGroup } from 'unocss';
  3. export default defineConfig({
  4. presets: [
  5. presetUni(),
  6. presetIcons({
  7. scale: 1.2,
  8. warn: true,
  9. extraProperties: {
  10. display: 'inline-block',
  11. 'vertical-align': 'middle',
  12. },
  13. }),
  14. // 支持css class属性化, 可能会与ui的组件属性冲突
  15. presetAttributify({
  16. ignoreAttributes: [
  17. 'color',
  18. // ...
  19. ],
  20. }),
  21. ],
  22. transformers: [
  23. // 启用指令功能:主要用于支持 @apply、@screen 和 theme() 等 CSS 指令
  24. transformerDirectives(),
  25. // 启用 () 分组功能
  26. // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
  27. transformerVariantGroup(),
  28. ],
  29. shortcuts: [
  30. {
  31. center: 'flex justify-center items-center',
  32. 'p-rtv': 'position: relative',
  33. 'text-indent-2': 'text-indent: 2em',
  34. 'w-s-no': 'white-space: nowrap',
  35. },
  36. ],
  37. rules: [
  38. [/^mg-([\.\d]+)$/, ([_, num]) => ({ margin: `${num}rpx` })],
  39. [/^pd-([\.\d]+)$/, ([_, num]) => ({ padding: `${num}rpx` })],
  40. [/^bg-([\w#-]+)$/, ([_, color]) => ({ 'background-color': color })],
  41. [/^c-([\w#-]+)$/, ([_, color]) => ({ color: color })],
  42. // bc-#fafafa
  43. [/^bc-([\w#-]+)$/, ([_, color]) => ({ 'border-color': color })],
  44. // 下边距
  45. [/^mb-([\.\d]+)$/, ([_, num]) => ({ 'margin-bottom': `${num}rpx` })],
  46. // 上边距
  47. [/^mt-([\.\d]+)$/, ([_, num]) => ({ 'margin-top': `${num}rpx` })],
  48. // 左边距
  49. [/^ml-([\.\d]+)$/, ([_, num]) => ({ 'margin-left': `${num}rpx` })],
  50. // 右边距
  51. [/^mr-([\.\d]+)$/, ([_, num]) => ({ 'margin-right': `${num}rpx` })],
  52. // 上padding
  53. [/^pt-([\.\d]+)$/, ([_, num]) => ({ 'padding-top': `${num}rpx` })],
  54. // 下padding
  55. [/^pb-([\.\d]+)$/, ([_, num]) => ({ 'padding-bottom': `${num}rpx` })],
  56. // pd3-40-0-20
  57. // padding
  58. [
  59. /^pd3-([\.\d]+)-([\.\d]+)-([\.\d]+)$/,
  60. ([_, top, right, bottom]) => ({
  61. padding: `${top}rpx ${right}rpx ${bottom}rpx`,
  62. }),
  63. ],
  64. // pd2-40-20
  65. [
  66. /^pd2-([\.\d]+)-([\.\d]+)$/,
  67. ([_, top, right]) => ({
  68. padding: `${top}rpx ${right}rpx`,
  69. }),
  70. ],
  71. // pd4-40-20-10-5
  72. [
  73. /^pd4-([\.\d]+)-([\.\d]+)-([\.\d]+)-([\.\d]+)$/,
  74. ([_, top, right, bottom, left]) => ({
  75. padding: `${top}rpx ${right}rpx ${bottom}rpx ${left}rpx`,
  76. }),
  77. ],
  78. // mg2-40-20
  79. [
  80. /^mg2-([\.\d]+)-([\.\d]+)$/,
  81. ([_, top, right]) => ({
  82. margin: `${top}rpx ${right}rpx`,
  83. }),
  84. ],
  85. // mg3-40-20-10
  86. [
  87. /^mg3-([\.\d]+)-([\.\d]+)-([\.\d]+)$/,
  88. ([_, top, right, bottom]) => ({
  89. margin: `${top}rpx ${right}rpx ${bottom}rpx`,
  90. }),
  91. ],
  92. // mg4-40-20-10-5
  93. [
  94. /^mg4-([\.\d]+)-([\.\d]+)-([\.\d]+)-([\.\d]+)$/,
  95. ([_, top, right, bottom, left]) => ({
  96. margin: `${top}rpx ${right}rpx ${bottom}rpx ${left}rpx`,
  97. }),
  98. ],
  99. // 左padding
  100. [/^pl-([\.\d]+)$/, ([_, num]) => ({ 'padding-left': `${num}rpx` })],
  101. // 右padding
  102. [/^pr-([\.\d]+)$/, ([_, num]) => ({ 'padding-right': `${num}rpx` })],
  103. // 字体大小
  104. [/^f-s-([\.\d]+)$/, ([_, num]) => ({ 'font-size': `${num}rpx` })],
  105. // 字体加粗
  106. [/^f-w-([\.\d]+)$/, ([_, num]) => ({ 'font-weight': `${num > 100 ? num : num * 100}` })],
  107. // 宽
  108. [/^w-([\.\d]+)$/, ([_, num]) => ({ width: `${num}rpx` })],
  109. [/^w-([\.\d]+)%$/, ([_, num]) => ({ width: `${num}%` })],
  110. // 高
  111. [/^h-([\.\d]+)$/, ([_, num]) => ({ height: `${num}rpx` })],
  112. [/^h-([\.\d]+)%$/, ([_, num]) => ({ height: `${num}%` })],
  113. // 带透明度的可传入颜色计算rgba值加背景
  114. // 行高
  115. [/^lh-([\.\d]+)$/, ([_, num]) => ({ 'line-height': `${num}` })],
  116. // 黑色透明度
  117. [
  118. /^bg-black-([\.\d]+)$/,
  119. ([_, num]) => ({
  120. 'background-color': `rgba(0, 0, 0, ${num})`,
  121. }),
  122. ],
  123. // 透明度
  124. [
  125. /^opacity-([\.\d]+)$/,
  126. ([_, num]) => ({
  127. opacity: `${num}`,
  128. }),
  129. ],
  130. // z-index
  131. [
  132. /^z-index-([\.\d]+)$/,
  133. ([_, num]) => ({
  134. 'z-index': `${num}`,
  135. }),
  136. ],
  137. // 百分比高度
  138. [/^h-([\.\d]+)%$/, ([_, num]) => ({ height: `${num}%` })],
  139. [
  140. /^wrapper-([\.\d]+)-([\.\d]+)$/,
  141. ([_, maxWidth, minWidth]) => ({
  142. 'max-width': `${maxWidth}rpx`,
  143. 'min-width': `${minWidth}rpx`,
  144. margin: '0 auto',
  145. padding: '0 20px',
  146. }),
  147. ],
  148. // 最大宽
  149. [/^max-w-([\.\d]+)$/, ([_, num]) => ({ 'max-width': `${num}rpx` })],
  150. // 最小宽
  151. [/^min-w-([\.\d]+)$/, ([_, num]) => ({ 'min-width': `${num}rpx` })],
  152. // 最大高
  153. [/^max-h-([\.\d]+)$/, ([_, num]) => ({ 'max-height': `${num}rpx` })],
  154. // 最小高
  155. [/^min-h-([\.\d]+)$/, ([_, num]) => ({ 'min-height': `${num}rpx` })],
  156. // 栅格 30份
  157. [/^hcol-([\.\d]+)$/, ([_, num]) => ({ width: `${(+num / 30) * 100}%` })],
  158. // border-radius-8
  159. [
  160. /^border-radius-([\.\d]+)$/,
  161. ([_, num]) => ({
  162. 'border-radius': `${num}rpx`,
  163. }),
  164. ],
  165. // border-w-4
  166. [
  167. /^border-w-([\.\d]+)$/,
  168. ([_, num]) => ({
  169. 'border-width': `${num}rpx`,
  170. }),
  171. ],
  172. // 旋转度数
  173. [
  174. /^rotate-([\-\.\d]+)$/,
  175. ([_, num]) => ({
  176. transform: `rotate(${num}deg)`,
  177. }),
  178. ],
  179. // gap-20
  180. [
  181. /^gap-([\.\d]+)$/,
  182. ([_, num]) => ({
  183. gap: `${num}rpx`,
  184. }),
  185. ],
  186. ],
  187. theme: {
  188. colors: {
  189. /** 主题色,用法如: text-primary */
  190. primary: 'var(--wot-color-theme,#0957DE)',
  191. },
  192. fontSize: {
  193. /** 提供更小号的字体,用法如:text-2xs */
  194. '2xs': ['20rpx', '28rpx'],
  195. '3xs': ['18rpx', '26rpx'],
  196. },
  197. },
  198. });