u-icon.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]">
  3. <image class="u-icon__img" v-if="isImg" :src="name" :mode="imgMode" :style="[imgStyle]"></image>
  4. <view
  5. v-else
  6. class="u-icon__icon"
  7. :class="customClass"
  8. :style="[iconStyle]"
  9. :hover-class="hoverClass"
  10. @touchstart="touchstart"
  11. >
  12. <text
  13. v-if="showDecimalIcon"
  14. :style="[decimalIconStyle]"
  15. :class="decimalIconClass"
  16. :hover-class="hoverClass"
  17. class="u-icon__decimal"
  18. ></text>
  19. </view>
  20. <!-- 这里进行空字符串判断,如果仅仅是v-if="label",可能会出现传递0的时候,结果也无法显示,微信小程序不传值默认为null,故需要增加null的判断 -->
  21. <text
  22. v-if="label !== '' && label !== null"
  23. class="u-icon__label"
  24. :style="{
  25. color: labelColor,
  26. fontSize: $u.addUnit(labelSize),
  27. marginLeft: labelPos == 'right' ? $u.addUnit(marginLeft) : 0,
  28. marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
  29. marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
  30. marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0
  31. }"
  32. >
  33. {{ label }}
  34. </text>
  35. </view>
  36. </template>
  37. <script>
  38. /**
  39. * icon 图标
  40. * @description 基于字体的图标集,包含了大多数常见场景的图标。
  41. * @tutorial https://www.uviewui.com/components/icon.html
  42. * @property {String} name 图标名称,见示例图标集
  43. * @property {String} color 图标颜色(默认inherit)
  44. * @property {String | Number} size 图标字体大小,单位rpx(默认32)
  45. * @property {String | Number} label-size label字体大小,单位rpx(默认28)
  46. * @property {String} label 图标右侧的label文字(默认28)
  47. * @property {String} label-pos label文字相对于图标的位置,只能right或bottom(默认right)
  48. * @property {String} label-color label字体颜色(默认#606266)
  49. * @property {Object} custom-style icon的样式,对象形式
  50. * @property {String} custom-prefix 自定义字体图标库时,需要写上此值
  51. * @property {String | Number} margin-left label在右侧时与图标的距离,单位rpx(默认6)
  52. * @property {String | Number} margin-top label在下方时与图标的距离,单位rpx(默认6)
  53. * @property {String | Number} margin-bottom label在上方时与图标的距离,单位rpx(默认6)
  54. * @property {String | Number} margin-right label在左侧时与图标的距离,单位rpx(默认6)
  55. * @property {String} label-pos label相对于图标的位置,只能right或bottom(默认right)
  56. * @property {String} index 一个用于区分多个图标的值,点击图标时通过click事件传出
  57. * @property {String} hover-class 图标按下去的样式类,用法同uni的view组件的hover-class参数,详情见官网
  58. * @property {String} width 显示图片小图标时的宽度
  59. * @property {String} height 显示图片小图标时的高度
  60. * @property {String} top 图标在垂直方向上的定位
  61. * @property {String} top 图标在垂直方向上的定位
  62. * @property {String} top 图标在垂直方向上的定位
  63. * @property {Boolean} show-decimal-icon 是否为DecimalIcon
  64. * @property {String} inactive-color 背景颜色,可接受主题色,仅Decimal时有效
  65. * @property {String | Number} percent 显示的百分比,仅Decimal时有效
  66. * @event {Function} click 点击图标时触发
  67. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  68. */
  69. export default {
  70. name: "u-icon",
  71. emits: ["click", "touchstart"],
  72. props: {
  73. // 图标类名
  74. name: {
  75. type: String,
  76. default: ""
  77. },
  78. // 图标颜色,可接受主题色
  79. color: {
  80. type: [String, null],
  81. default: ""
  82. },
  83. // 字体大小,单位rpx
  84. size: {
  85. type: [Number, String],
  86. default: "inherit"
  87. },
  88. // 是否显示粗体
  89. bold: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  94. index: {
  95. type: [Number, String],
  96. default: ""
  97. },
  98. // 触摸图标时的类名
  99. hoverClass: {
  100. type: String,
  101. default: ""
  102. },
  103. // 自定义扩展前缀,方便用户扩展自己的图标库
  104. customPrefix: {
  105. type: String,
  106. default: "uicon"
  107. },
  108. // 图标右边或者下面的文字
  109. label: {
  110. type: [String, Number],
  111. default: ""
  112. },
  113. // label的位置,只能右边或者下边
  114. labelPos: {
  115. type: String,
  116. default: "right"
  117. },
  118. // label的大小
  119. labelSize: {
  120. type: [String, Number],
  121. default: "28"
  122. },
  123. // label的颜色
  124. labelColor: {
  125. type: String,
  126. default: "#606266"
  127. },
  128. // label与图标的距离(横向排列)
  129. marginLeft: {
  130. type: [String, Number],
  131. default: "6"
  132. },
  133. // label与图标的距离(竖向排列)
  134. marginTop: {
  135. type: [String, Number],
  136. default: "6"
  137. },
  138. // label与图标的距离(竖向排列)
  139. marginRight: {
  140. type: [String, Number],
  141. default: "6"
  142. },
  143. // label与图标的距离(竖向排列)
  144. marginBottom: {
  145. type: [String, Number],
  146. default: "6"
  147. },
  148. // 图片的mode
  149. imgMode: {
  150. type: String,
  151. default: "widthFix"
  152. },
  153. // 自定义样式
  154. customStyle: {
  155. type: Object,
  156. default() {
  157. return {};
  158. }
  159. },
  160. // 用于显示图片小图标时,图片的宽度
  161. width: {
  162. type: [String, Number],
  163. default: ""
  164. },
  165. // 用于显示图片小图标时,图片的高度
  166. height: {
  167. type: [String, Number],
  168. default: ""
  169. },
  170. // 用于解决某些情况下,让图标垂直居中的用途
  171. top: {
  172. type: [String, Number],
  173. default: 0
  174. },
  175. // 是否为DecimalIcon
  176. showDecimalIcon: {
  177. type: Boolean,
  178. default: false
  179. },
  180. // 背景颜色,可接受主题色,仅Decimal时有效
  181. inactiveColor: {
  182. type: String,
  183. default: "#ececec"
  184. },
  185. // 显示的百分比,仅Decimal时有效
  186. percent: {
  187. type: [Number, String],
  188. default: "50"
  189. }
  190. },
  191. computed: {
  192. customClass() {
  193. let classes = [];
  194. let { customPrefix, name } = this;
  195. let index = name.indexOf("-icon-");
  196. if (index > -1) {
  197. customPrefix = name.substring(0, index + 5);
  198. classes.push(name);
  199. } else {
  200. classes.push(`${customPrefix}-${name}`);
  201. }
  202. // uView的自定义图标类名为u-iconfont
  203. if (customPrefix === "uicon") {
  204. classes.push("u-iconfont");
  205. } else {
  206. classes.push(customPrefix);
  207. }
  208. // 主题色,通过类配置
  209. if (
  210. this.showDecimalIcon &&
  211. this.inactiveColor &&
  212. this.$u.config.type.includes(this.inactiveColor)
  213. ) {
  214. classes.push("u-icon__icon--" + this.inactiveColor);
  215. } else if (this.color && this.$u.config.type.includes(this.color))
  216. classes.push("u-icon__icon--" + this.color);
  217. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  218. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  219. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  220. classes = classes.join(" ");
  221. //#endif
  222. return classes;
  223. },
  224. iconStyle() {
  225. let style = {};
  226. style = {
  227. fontSize: this.size == "inherit" ? "inherit" : this.$u.addUnit(this.size),
  228. fontWeight: this.bold ? "bold" : "normal",
  229. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  230. top: this.$u.addUnit(this.top)
  231. };
  232. // 非主题色值时,才当作颜色值
  233. if (
  234. this.showDecimalIcon &&
  235. this.inactiveColor &&
  236. !this.$u.config.type.includes(this.inactiveColor)
  237. ) {
  238. style.color = this.inactiveColor;
  239. } else if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color;
  240. return style;
  241. },
  242. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  243. isImg() {
  244. return this.name.indexOf("/") !== -1;
  245. },
  246. imgStyle() {
  247. let style = {};
  248. // 如果设置width和height属性,则优先使用,否则使用size属性
  249. style.width = this.width ? this.$u.addUnit(this.width) : this.$u.addUnit(this.size);
  250. style.height = this.height ? this.$u.addUnit(this.height) : this.$u.addUnit(this.size);
  251. return style;
  252. },
  253. decimalIconStyle() {
  254. let style = {};
  255. style = {
  256. fontSize: this.size == "inherit" ? "inherit" : this.$u.addUnit(this.size),
  257. fontWeight: this.bold ? "bold" : "normal",
  258. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  259. top: this.$u.addUnit(this.top),
  260. width: this.percent + "%"
  261. };
  262. // 非主题色值时,才当作颜色值
  263. if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color;
  264. return style;
  265. },
  266. decimalIconClass() {
  267. let classes = [];
  268. classes.push(this.customPrefix + "-" + this.name);
  269. // uView的自定义图标类名为u-iconfont
  270. if (this.customPrefix == "uicon") {
  271. classes.push("u-iconfont");
  272. } else {
  273. classes.push(this.customPrefix);
  274. }
  275. // 主题色,通过类配置
  276. if (this.color && this.$u.config.type.includes(this.color))
  277. classes.push("u-icon__icon--" + this.color);
  278. else classes.push("u-icon__icon--primary");
  279. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  280. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  281. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  282. classes = classes.join(" ");
  283. //#endif
  284. return classes;
  285. }
  286. },
  287. methods: {
  288. click() {
  289. this.$emit("click", this.index);
  290. },
  291. touchstart() {
  292. this.$emit("touchstart", this.index);
  293. }
  294. }
  295. };
  296. </script>
  297. <style scoped lang="scss">
  298. @import "../../libs/css/style.components.scss";
  299. @import "../../iconfont.css";
  300. .u-icon {
  301. display: inline-flex;
  302. align-items: center;
  303. &--left {
  304. flex-direction: row-reverse;
  305. align-items: center;
  306. }
  307. &--right {
  308. flex-direction: row;
  309. align-items: center;
  310. }
  311. &--top {
  312. flex-direction: column-reverse;
  313. justify-content: center;
  314. }
  315. &--bottom {
  316. flex-direction: column;
  317. justify-content: center;
  318. }
  319. &__icon {
  320. position: relative;
  321. &--primary {
  322. color: $u-type-primary;
  323. }
  324. &--success {
  325. color: $u-type-success;
  326. }
  327. &--error {
  328. color: $u-type-error;
  329. }
  330. &--warning {
  331. color: $u-type-warning;
  332. }
  333. &--info {
  334. color: $u-type-info;
  335. }
  336. }
  337. &__decimal {
  338. position: absolute;
  339. top: 0;
  340. left: 0;
  341. display: inline-block;
  342. overflow: hidden;
  343. }
  344. &__img {
  345. height: auto;
  346. will-change: transform;
  347. }
  348. &__label {
  349. line-height: 1;
  350. }
  351. }
  352. </style>