uni-icons.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <text :style="{ color: color, 'font-size': iconSize }" class="uni-icons" @click="_onClick">{{unicode}}</text>
  4. <!-- #endif -->
  5. <!-- #ifndef APP-NVUE -->
  6. <text :style="{ color: color, 'font-size': iconSize }" class="uni-icons" :class="['uniui-'+type,customPrefix,customPrefix?type:'']" @click="_onClick"></text>
  7. <!-- #endif -->
  8. </template>
  9. <script>
  10. import icons from './icons.js';
  11. const getVal = (val) => {
  12. const reg = /^[0-9]*$/g
  13. return (typeof val === 'number' || reg.test(val) )? val + 'px' : val;
  14. }
  15. // #ifdef APP-NVUE
  16. var domModule = weex.requireModule('dom');
  17. import iconUrl from './uniicons.ttf'
  18. domModule.addRule('fontFace', {
  19. 'fontFamily': "uniicons",
  20. 'src': "url('"+iconUrl+"')"
  21. });
  22. // #endif
  23. /**
  24. * Icons 图标
  25. * @description 用于展示 icons 图标
  26. * @tutorial https://ext.dcloud.net.cn/plugin?id=28
  27. * @property {Number} size 图标大小
  28. * @property {String} type 图标图案,参考示例
  29. * @property {String} color 图标颜色
  30. * @property {String} customPrefix 自定义图标
  31. * @event {Function} click 点击 Icon 触发事件
  32. */
  33. export default {
  34. name: 'UniIcons',
  35. emits:['click'],
  36. props: {
  37. type: {
  38. type: String,
  39. default: ''
  40. },
  41. color: {
  42. type: String,
  43. default: '#333333'
  44. },
  45. size: {
  46. type: [Number, String],
  47. default: 16
  48. },
  49. customPrefix:{
  50. type: String,
  51. default: ''
  52. }
  53. },
  54. data() {
  55. return {
  56. icons: icons.glyphs
  57. }
  58. },
  59. computed:{
  60. unicode(){
  61. let code = this.icons.find(v=>v.font_class === this.type)
  62. if(code){
  63. return unescape(`%u${code.unicode}`)
  64. }
  65. return ''
  66. },
  67. iconSize(){
  68. return getVal(this.size)
  69. }
  70. },
  71. methods: {
  72. _onClick() {
  73. this.$emit('click')
  74. }
  75. }
  76. }
  77. </script>
  78. <style lang="scss">
  79. /* #ifndef APP-NVUE */
  80. @import './uniicons.css';
  81. @font-face {
  82. font-family: uniicons;
  83. src: url('./uniicons.ttf') format('truetype');
  84. }
  85. /* #endif */
  86. .uni-icons {
  87. font-family: uniicons;
  88. text-decoration: none;
  89. text-align: center;
  90. }
  91. </style>