u-icon.vue 7.4 KB

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