uv-icon.vue 7.7 KB

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