Icon.vue 1.7 KB

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