Icon.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <!-- eslint-disable vue/multi-word-component-names -->
  2. <template>
  3. <!--本地 icon 资源, uniapp 打包到 app 仅支持标签 image, 打包小程序和 H5 均可支持标签 img -->
  4. <div
  5. :class="['common-icon-container', !isPC && 'common-icon-container-mobile']"
  6. :style="{
  7. padding: iconHotAreaSize,
  8. }"
  9. @click="handleImgClick"
  10. >
  11. <image
  12. v-if="isApp"
  13. class="common-icon"
  14. :src="props.file"
  15. :style="{ width: iconWidth, height: iconHeight }"
  16. />
  17. <img
  18. v-else
  19. class="common-icon"
  20. :src="props.file"
  21. :style="{ width: iconWidth, height: iconHeight }"
  22. >
  23. </div>
  24. </template>
  25. <script setup lang="ts">
  26. import { withDefaults, computed } from '../../adapter-vue';
  27. import { isApp, isPC } from '../../utils/env';
  28. interface IProps {
  29. file: string;
  30. size?: string;
  31. width?: string;
  32. height?: string;
  33. hotAreaSize?: number | string;
  34. }
  35. interface IEmits {
  36. (key: 'onClick', e: Event): void;
  37. }
  38. const emits = defineEmits<IEmits>();
  39. const props = withDefaults(defineProps<IProps>(), {
  40. file: '',
  41. width: '20px',
  42. height: '20px',
  43. });
  44. const iconHotAreaSize = computed<undefined | string>(() => {
  45. if (!props.hotAreaSize) {
  46. return undefined;
  47. }
  48. if (isNaN(Number(props.hotAreaSize))) {
  49. return String(props.hotAreaSize);
  50. }
  51. return `${props.hotAreaSize}px`;
  52. });
  53. const iconWidth = computed(() => {
  54. return props.size ? props.size : props.width;
  55. });
  56. const iconHeight = computed(() => {
  57. return props.size ? props.size : props.height;
  58. });
  59. const handleImgClick = (e: Event) => {
  60. emits('onClick', e);
  61. };
  62. </script>
  63. <style lang="scss" scoped>
  64. .common-icon-container {
  65. display: flex;
  66. justify-content: center;
  67. align-items: center;
  68. cursor: pointer;
  69. -webkit-tap-highlight-color: transparent;
  70. }
  71. .common-icon-container-mobile{
  72. cursor: none;
  73. }
  74. </style>