主题

移动端的组件对主题定制的需求会更多,从 cube-ui^1.0.2 开始,通过 webpack 工具可以实现自定义主题样式。

注意: 自定义主题依赖后编译

主题文件

在你的项目的 src/ 目录下创建一个 theme.styl 的文件,内容如下(展示的全是默认样式,你可以选择这针对不同组件做定制):

  1. // 如果你需要使用 cube-ui 自带的颜色值 需要 require 进来
  2. @require "~cube-ui/src/common/stylus/var/color.styl"
  3. // action-sheet
  4. action-sheet-color := color-grey
  5. action-sheet-active-color := color-orange
  6. action-sheet-bgc := color-white
  7. action-sheet-active-bgc := color-light-grey-opacity
  8. action-sheet-title-color := color-dark-grey
  9. action-sheet-space-bgc := color-mask-bg
  10. /// picker style
  11. action-sheet-picker-cancel-color := color-light-grey
  12. action-sheet-picker-cancel-active-color := color-light-grey-s
  13. // bubble
  14. // button
  15. btn-color := color-white
  16. btn-bgc := color-regular-blue
  17. btn-bdc := color-regular-blue
  18. btn-active-bgc := color-blue
  19. btn-active-bdc := color-blue
  20. btn-disabled-color := color-white
  21. btn-disabled-bgc := color-light-grey-s
  22. btn-disabled-bdc := color-light-grey-s
  23. /// primary
  24. btn-primary-color := color-white
  25. btn-primary-bgc := color-orange
  26. btn-primary-bdc := color-orange
  27. btn-primary-active-bgc := color-dark-orange
  28. btn-primary-active-bdc := color-dark-orange
  29. /// light
  30. btn-light-color := color-grey
  31. btn-light-bgc := color-light-grey-sss
  32. btn-light-bdc := color-light-grey-sss
  33. btn-light-active-bgc := color-active-grey
  34. btn-light-active-bdc := color-active-grey
  35. /// outline
  36. btn-outline-color := color-grey
  37. btn-outline-bgc := transparent
  38. btn-outline-bdc := color-grey
  39. btn-outline-active-bgc := color-grey-opacity
  40. btn-outline-active-bdc := color-grey
  41. /// outline-primary
  42. btn-outline-primary-color := color-orange
  43. btn-outline-primary-bgc := transparent
  44. btn-outline-primary-bdc := color-orange
  45. btn-outline-primary-active-bgc := color-orange-opacity
  46. btn-outline-primary-active-bdc := color-dark-orange
  47. // checkbox
  48. checkbox-color := color-grey
  49. checkbox-icon-color := color-light-grey-s
  50. /// checked
  51. checkbox-checked-icon-color := color-orange
  52. checkbox-checked-icon-bgc := color-white
  53. /// disabled
  54. checkbox-disabled-icon-color := color-light-grey-ss
  55. checkbox-disabled-icon-bgc := color-light-grey-ss
  56. // checkbox hollow
  57. checkbox-hollow-checked-icon-color := color-orange
  58. checkbox-hollow-disabled-icon-color := color-light-grey-ss
  59. // checkbox-group
  60. checkbox-group-bgc := color-white
  61. checkbox-group-horizontal-bdc := color-light-grey-s
  62. // radio
  63. radio-group-bgc := color-white
  64. radio-group-horizontal-bdc := color-light-grey-s
  65. radio-color := color-grey
  66. radio-icon-color := color-light-grey-s
  67. /// selected
  68. radio-selected-icon-color := color-white
  69. radio-selected-icon-bgc := color-orange
  70. /// disabled
  71. radio-disabled-icon-bgc := color-light-grey-ss
  72. // radio hollow
  73. radio-hollow-selected-icon-color := color-orange
  74. radio-hollow-disabled-icon-color := color-light-grey-ss
  75. // dialog
  76. dialog-color := color-grey
  77. dialog-bgc := color-white
  78. dialog-icon-color := color-regular-blue
  79. dialog-icon-bgc := color-background
  80. dialog-title-color := color-dark-grey
  81. dialog-close-color := color-light-grey
  82. dialog-btn-color := color-light-grey
  83. dialog-btn-bgc := color-white
  84. dialog-btn-active-bgc := color-light-grey-opacity
  85. dialog-btn-highlight-color := color-orange
  86. dialog-btn-highlight-active-bgc := color-light-orange-opacity
  87. dialog-btn-disabled-color := color-light-grey
  88. dialog-btn-disabled-active-bgc := transparent
  89. dialog-btns-split-color := color-row-line
  90. // index-list
  91. index-list-bgc := color-white
  92. index-list-title-color := color-dark-grey
  93. index-list-anchor-color := color-light-grey
  94. index-list-anchor-bgc := #f7f7f7
  95. index-list-item-color := color-dark-grey
  96. index-list-item-active-bgc := color-light-grey-opacity
  97. index-list-nav-color := color-grey
  98. index-list-nav-active-color := color-orange
  99. // loading
  100. // picker
  101. picker-bgc := color-white
  102. picker-title-color := color-dark-grey
  103. picker-confirm-btn-color := color-orange
  104. picker-confirm-btn-active-color := color-light-orange
  105. picker-cancel-btn-color := color-light-grey
  106. picker-cancel-btn-active-color := color-light-grey-s
  107. picker-item-color := color-dark-grey
  108. // popup
  109. popup-mask-bgc := rgb(37, 38, 45)
  110. popup-mask-opacity := .4
  111. //scroll
  112. // slide
  113. slide-dot-bgc := color-light-grey-s
  114. slide-dot-active-bgc := color-orange
  115. // time-picker
  116. // tip
  117. tip-color := color-white
  118. tip-bgc := color-dark-grey-opacity
  119. // toast
  120. toast-color := color-light-grey-s
  121. toast-bgc := rgba(37, 38, 45, 0.9)
  122. // upload
  123. upload-btn-color := color-grey
  124. upload-btn-bgc := color-white
  125. upload-btn-active-bgc := color-light-grey-opacity
  126. upload-btn-box-shadow := 0 0 6px 2px color-grey-opacity
  127. upload-btn-border-color := #e5e5e5
  128. upload-file-bgc := color-white
  129. upload-file-remove-color := rgba(0, 0, 0, .8)
  130. upload-file-remove-bgc := color-white
  131. upload-file-state-bgc := color-mask-bg
  132. upload-file-success-color := color-orange
  133. upload-file-error-color := #f43530
  134. upload-file-status-bgc := color-white
  135. upload-file-progress-color := color-white
  136. // switch
  137. switch-on-bgc := color-orange
  138. switch-off-bgc := color-white
  139. switch-off-border-color := #e4e4e4
  140. // input
  141. input-color := color-grey
  142. input-bgc := color-white
  143. input-border-color := color-row-line
  144. input-focus-border-color := color-orange
  145. input-placeholder-color := color-light-grey-s
  146. input-clear-icon-color := color-light-grey
  147. //textarea
  148. textarea-color := color-grey
  149. textarea-bgc := color-white
  150. textarea-border-color := color-row-line
  151. textarea-focus-border-color := color-orange
  152. textarea-outline-color := color-orange
  153. textarea-placeholder-color := color-light-grey-s
  154. textarea-indicator-color := color-light-grey-s
  155. // validator
  156. validator-msg-def-color := #e64340
  157. // select
  158. select-color := color-grey
  159. select-bgc := color-white
  160. select-disabled-color := #b8b8b8
  161. select-disabled-bgc := color-light-grey-opacity
  162. select-border-color := color-light-grey-s
  163. select-border-active-color := color-orange
  164. select-icon-color := color-light-grey
  165. select-placeholder-color := color-light-grey
  166. // swipe
  167. swipe-btn-color := color-white
  168. // form
  169. form-color := color-grey
  170. form-bgc := color-white
  171. form-invalid-color := #e64340
  172. form-group-legend-color := color-light-grey
  173. form-group-legend-bgc := color-background
  174. form-label-required-color := #e64340
  175. // drawer
  176. drawer-color := color-dark-grey
  177. drawer-title-bdc := color-light-grey-ss
  178. drawer-title-bgc := color-white
  179. drawer-panel-bgc := color-white
  180. drawer-item-active-bgc := color-light-grey-opacity

配置 webpack

如果你的项目是用 vue-cli 生成的,那么找到 build/utils.js 中的 exports.cssLoaders 函数,改成如下:

  1. exports.cssLoaders = function (options) {
  2. options = options || {}
  3. const cssLoader = {
  4. loader: 'css-loader',
  5. options: {
  6. minimize: process.env.NODE_ENV === 'production',
  7. sourceMap: options.sourceMap
  8. }
  9. }
  10. var postcssLoader = {
  11. loader: 'postcss-loader',
  12. options: {
  13. sourceMap: options.sourceMap
  14. }
  15. }
  16. // generate loader string to be used with extract text plugin
  17. function generateLoaders (loader, loaderOptions) {
  18. const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
  19. if (loader) {
  20. loaders.push({
  21. loader: loader + '-loader',
  22. options: Object.assign({}, loaderOptions, {
  23. sourceMap: options.sourceMap
  24. })
  25. })
  26. }
  27. // Extract CSS when that option is specified
  28. // (which is the case during production build)
  29. if (options.extract) {
  30. return ExtractTextPlugin.extract({
  31. use: loaders,
  32. fallback: 'vue-style-loader'
  33. })
  34. } else {
  35. return ['vue-style-loader'].concat(loaders)
  36. }
  37. }
  38. const stylusOptions = {
  39. 'resolve url': true,
  40. // 这里 新增 import 配置项,指向自定义主题文件
  41. import: [path.resolve(__dirname, '../src/theme')]
  42. }
  43. // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  44. return {
  45. css: generateLoaders(),
  46. postcss: generateLoaders(),
  47. less: generateLoaders('less'),
  48. sass: generateLoaders('sass', { indentedSyntax: true }),
  49. scss: generateLoaders('sass'),
  50. stylus: generateLoaders('stylus',stylusOptions),
  51. styl: generateLoaders('stylus',stylusOptions)
  52. }
  53. }

结果

这样修改自定义的主题文件内容,最终编译打包后的样式就是你需要的;而且这样也不存在样式冗余覆盖的问题,因为直接是对组件样式的变量做的修改,而不是采用传统的样式覆盖。

原文:

https://didi.github.io/cube-ui/#/zh-CN/docs/theme