u-icon.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view
  3. class="u-icon"
  4. @tap="clickHandler"
  5. :class="['u-icon--' + labelPos]"
  6. >
  7. <image
  8. class="u-icon__img"
  9. v-if="isImg"
  10. :src="name"
  11. :mode="imgMode"
  12. :style="[imgStyle, $u.addStyle(customStyle)]"
  13. ></image>
  14. <text
  15. v-else
  16. class="u-icon__icon"
  17. :class="uClasses"
  18. :style="[iconStyle, $u.addStyle(customStyle)]"
  19. :hover-class="hoverClass"
  20. >{{ icon }}
  21. </text>
  22. <!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示 -->
  23. <text
  24. v-if="label !== ''"
  25. class="u-icon__label"
  26. :style="{
  27. color: labelColor,
  28. fontSize: $u.addUnit(labelSize),
  29. marginLeft: labelPos == 'right' ? $u.addUnit(space) : 0,
  30. marginTop: labelPos == 'bottom' ? $u.addUnit(space) : 0,
  31. marginRight: labelPos == 'left' ? $u.addUnit(space) : 0,
  32. marginBottom: labelPos == 'top' ? $u.addUnit(space) : 0,
  33. }"
  34. >{{ label }}
  35. </text>
  36. </view>
  37. </template>
  38. <script>
  39. // #ifdef APP-NVUE
  40. // nvue通过weex的dom模块引入字体,相关文档地址如下:
  41. // https://weex.apache.org/zh/docs/modules/dom.html#addrule
  42. const fontUrl = 'https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf'
  43. const domModule = weex.requireModule('dom')
  44. domModule.addRule('fontFace', {
  45. 'fontFamily': "uicon-iconfont",
  46. 'src': `url('${fontUrl}')`
  47. })
  48. // #endif
  49. // 引入图标名称,已经对应的unicode
  50. import icons from './icons'
  51. import props from './props.js';
  52. ;
  53. /**
  54. * icon 图标
  55. * @description 基于字体的图标集,包含了大多数常见场景的图标。
  56. * @tutorial https://www.uviewui.com/components/icon.html
  57. * @property {String} name 图标名称,见示例图标集
  58. * @property {String} color 图标颜色,可接受主题色 (默认 color['u-content-color'] )
  59. * @property {String | Number} size 图标字体大小,单位px (默认 '16px' )
  60. * @property {Boolean} bold 是否显示粗体 (默认 false )
  61. * @property {String | Number} index 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  62. * @property {String} hoverClass 图标按下去的样式类,用法同uni的view组件的hoverClass参数,详情见官网
  63. * @property {String} customPrefix 自定义扩展前缀,方便用户扩展自己的图标库 (默认 'uicon' )
  64. * @property {String | Number} label 图标右侧的label文字
  65. * @property {String} labelPos label相对于图标的位置,只能right或bottom (默认 'right' )
  66. * @property {String | Number} labelSize label字体大小,单位px (默认 '15px' )
  67. * @property {String} labelColor 图标右侧的label文字颜色 ( 默认 color['u-content-color'] )
  68. * @property {String | Number} space label与图标的距离,单位px (默认 '3px' )
  69. * @property {String} imgMode 图片的mode
  70. * @property {String | Number} width 显示图片小图标时的宽度
  71. * @property {String | Number} height 显示图片小图标时的高度
  72. * @property {String | Number} top 图标在垂直方向上的定位 用于解决某些情况下,让图标垂直居中的用途 (默认 0 )
  73. * @property {Boolean} stop 是否阻止事件传播 (默认 false )
  74. * @property {Object} customStyle icon的样式,对象形式
  75. * @event {Function} click 点击图标时触发
  76. * @event {Function} touchstart 事件触摸时触发
  77. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  78. */
  79. export default {
  80. name: 'u-icon',
  81. data() {
  82. return {}
  83. },
  84. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  85. computed: {
  86. uClasses() {
  87. let classes = []
  88. classes.push(this.customPrefix + '-' + this.name)
  89. // // uView的自定义图标类名为u-iconfont
  90. // if (this.customPrefix == 'uicon') {
  91. // classes.push('u-iconfont')
  92. // } else {
  93. // classes.push(this.customPrefix)
  94. // }
  95. // 主题色,通过类配置
  96. if (this.color && uni.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color)
  97. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  98. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  99. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  100. classes = classes.join(' ')
  101. //#endif
  102. return classes
  103. },
  104. iconStyle() {
  105. let style = {}
  106. style = {
  107. fontSize: uni.$u.addUnit(this.size),
  108. lineHeight: uni.$u.addUnit(this.size),
  109. fontWeight: this.bold ? 'bold' : 'normal',
  110. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  111. top: uni.$u.addUnit(this.top)
  112. }
  113. // 非主题色值时,才当作颜色值
  114. if (this.color && !uni.$u.config.type.includes(this.color)) style.color = this.color
  115. return style
  116. },
  117. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  118. isImg() {
  119. return this.name.indexOf('/') !== -1
  120. },
  121. imgStyle() {
  122. let style = {}
  123. // 如果设置width和height属性,则优先使用,否则使用size属性
  124. style.width = this.width ? uni.$u.addUnit(this.width) : uni.$u.addUnit(this.size)
  125. style.height = this.height ? uni.$u.addUnit(this.height) : uni.$u.addUnit(this.size)
  126. return style
  127. },
  128. // 通过图标名,查找对应的图标
  129. icon() {
  130. // 如果内置的图标中找不到对应的图标,就直接返回name值,因为用户可能传入的是unicode代码
  131. return icons['uicon-' + this.name] || this.name
  132. }
  133. },
  134. methods: {
  135. clickHandler(e) {
  136. this.$emit('click', this.index)
  137. // 是否阻止事件冒泡
  138. this.stop && this.preventEvent(e)
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. @import "../../libs/css/components.scss";
  145. // 变量定义
  146. $u-icon-primary: $u-primary !default;
  147. $u-icon-success: $u-success !default;
  148. $u-icon-info: $u-info !default;
  149. $u-icon-warning: $u-warning !default;
  150. $u-icon-error: $u-error !default;
  151. $u-icon-label-line-height: 1 !default;
  152. /* #ifndef APP-NVUE */
  153. // 非nvue下加载字体
  154. @font-face {
  155. font-family: 'uicon-iconfont';
  156. src: url('https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf') format('truetype');
  157. }
  158. /* #endif */
  159. .u-icon {
  160. /* #ifndef APP-NVUE */
  161. display: flex;
  162. /* #endif */
  163. align-items: center;
  164. &--left {
  165. flex-direction: row-reverse;
  166. align-items: center;
  167. }
  168. &--right {
  169. flex-direction: row;
  170. align-items: center;
  171. }
  172. &--top {
  173. flex-direction: column-reverse;
  174. justify-content: center;
  175. }
  176. &--bottom {
  177. flex-direction: column;
  178. justify-content: center;
  179. }
  180. &__icon {
  181. font-family: uicon-iconfont;
  182. position: relative;
  183. @include flex;
  184. align-items: center;
  185. &--primary {
  186. color: $u-icon-primary;
  187. }
  188. &--success {
  189. color: $u-icon-success;
  190. }
  191. &--error {
  192. color: $u-icon-error;
  193. }
  194. &--warning {
  195. color: $u-icon-warning;
  196. }
  197. &--info {
  198. color: $u-icon-info;
  199. }
  200. }
  201. &__img {
  202. /* #ifndef APP-NVUE */
  203. height: auto;
  204. will-change: transform;
  205. /* #endif */
  206. }
  207. &__label {
  208. /* #ifndef APP-NVUE */
  209. line-height: $u-icon-label-line-height;
  210. /* #endif */
  211. }
  212. }
  213. </style>