unocss.config.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // 高
  110. [/^h-([\.\d]+)$/, ([_, num]) => ({ height: `${num}rpx` })],
  111. // 带透明度的可传入颜色计算rgba值加背景
  112. // 行高
  113. [/^lh-([\.\d]+)$/, ([_, num]) => ({ 'line-height': `${num}` })],
  114. // 黑色透明度
  115. [
  116. /^bg-black-([\.\d]+)$/,
  117. ([_, num]) => ({
  118. 'background-color': `rgba(0, 0, 0, ${num})`,
  119. }),
  120. ],
  121. // 透明度
  122. [
  123. /^opacity-([\.\d]+)$/,
  124. ([_, num]) => ({
  125. opacity: `${num}`,
  126. }),
  127. ],
  128. // z-index
  129. [
  130. /^z-index-([\.\d]+)$/,
  131. ([_, num]) => ({
  132. 'z-index': `${num}`,
  133. }),
  134. ],
  135. // 百分比高度
  136. [/^h-([\.\d]+)%$/, ([_, num]) => ({ height: `${num}%` })],
  137. [
  138. /^wrapper-([\.\d]+)-([\.\d]+)$/,
  139. ([_, maxWidth, minWidth]) => ({
  140. 'max-width': `${maxWidth}rpx`,
  141. 'min-width': `${minWidth}rpx`,
  142. margin: '0 auto',
  143. padding: '0 20px',
  144. }),
  145. ],
  146. // 最大宽
  147. [/^max-w-([\.\d]+)$/, ([_, num]) => ({ 'max-width': `${num}rpx` })],
  148. // 最小宽
  149. [/^min-w-([\.\d]+)$/, ([_, num]) => ({ 'min-width': `${num}rpx` })],
  150. // 最大高
  151. [/^max-h-([\.\d]+)$/, ([_, num]) => ({ 'max-height': `${num}rpx` })],
  152. // 最小高
  153. [/^min-h-([\.\d]+)$/, ([_, num]) => ({ 'min-height': `${num}rpx` })],
  154. // 栅格 30份
  155. [/^hcol-([\.\d]+)$/, ([_, num]) => ({ width: `${(+num / 30) * 100}%` })],
  156. // border-radius-8
  157. [
  158. /^border-radius-([\.\d]+)$/,
  159. ([_, num]) => ({
  160. 'border-radius': `${num}rpx`,
  161. }),
  162. ],
  163. // border-w-4
  164. [
  165. /^border-w-([\.\d]+)$/,
  166. ([_, num]) => ({
  167. 'border-width': `${num}rpx`,
  168. }),
  169. ],
  170. // 旋转度数
  171. [
  172. /^rotate-([\-\.\d]+)$/,
  173. ([_, num]) => ({
  174. transform: `rotate(${num}deg)`,
  175. }),
  176. ],
  177. // gap-20
  178. [
  179. /^gap-([\.\d]+)$/,
  180. ([_, num]) => ({
  181. gap: `${num}rpx`,
  182. }),
  183. ],
  184. ],
  185. theme: {
  186. colors: {
  187. /** 主题色,用法如: text-primary */
  188. primary: 'var(--wot-color-theme,#0957DE)',
  189. },
  190. fontSize: {
  191. /** 提供更小号的字体,用法如:text-2xs */
  192. '2xs': ['20rpx', '28rpx'],
  193. '3xs': ['18rpx', '26rpx'],
  194. },
  195. },
  196. });