u-icon.vue 8.5 KB

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