Blame view

src/components/Icon/index.tsx 1.86 KB
陈文彬 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import type { PropType } from 'vue';
import { defineComponent, ref, watch, onMounted, nextTick, unref, computed } from 'vue';
import Iconify from '@purge-icons/generated';
import { isString } from '/@/utils/is';
import './index.less';
export default defineComponent({
  name: 'GIcon',
  props: {
    // icon name
    icon: {
      type: String as PropType<string>,
      required: true,
    },
    // icon color
    color: {
      type: String as PropType<string>,
    },
    // icon size
    size: {
      type: [String, Number] as PropType<string | number>,
      default: 14,
    },
    prefix: {
      type: String as PropType<string>,
      default: '',
    },
  },
  setup(props, { attrs }) {
    const elRef = ref<Nullable<HTMLElement>>(null);

    const getIconRef = computed(() => {
      const { icon, prefix } = props;
      return `${prefix ? prefix + ':' : ''}${icon}`;
    });
35
陈文彬 authored
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    const update = async () => {
      const el = unref(elRef);
      if (el) {
        await nextTick();
        const icon = unref(getIconRef);

        const svg = Iconify.renderSVG(icon, {});

        if (svg) {
          el.textContent = '';
          el.appendChild(svg);
        } else {
          const span = document.createElement('span');
          span.className = 'iconify';
          span.dataset.icon = icon;
          el.textContent = '';
          el.appendChild(span);
        }
      }
    };

    const wrapStyleRef = computed((): any => {
      const { size, color } = props;
      let fs = size;
      if (isString(size)) {
        fs = parseInt(size, 10);
      }
      return {
        fontSize: `${fs}px`,
        color,
66
        display: 'inline-flex',
陈文彬 authored
67
68
69
      };
    });
vben authored
70
    watch(() => props.icon, update, { flush: 'post' });
71
陈文彬 authored
72
73
74
    onMounted(update);

    return () => (
75
      <div ref={elRef} class={[attrs.class, 'app-iconify anticon']} style={unref(wrapStyleRef)} />
陈文彬 authored
76
77
78
    );
  },
});