wd-icon.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <view @click="handleClick" :class="rootClass" :style="rootStyle">
  3. <image v-if="isImage" class="wd-icon__image" :src="name"></image>
  4. </view>
  5. </template>
  6. <script lang="ts">
  7. export default {
  8. name: 'wd-icon',
  9. options: {
  10. virtualHost: true,
  11. addGlobalClass: true,
  12. styleIsolation: 'shared'
  13. }
  14. }
  15. </script>
  16. <script lang="ts" setup>
  17. import { computed, type CSSProperties } from 'vue'
  18. import { addUnit, isDef, objToStyle } from '../common/util'
  19. import { iconProps } from './types'
  20. const props = defineProps(iconProps)
  21. const emit = defineEmits(['click', 'touch'])
  22. const isImage = computed(() => {
  23. return isDef(props.name) && props.name.includes('/')
  24. })
  25. const rootClass = computed(() => {
  26. const prefix = props.classPrefix
  27. return `${prefix} ${props.customClass} ${isImage.value ? 'wd-icon--image' : prefix + '-' + props.name}`
  28. })
  29. const rootStyle = computed(() => {
  30. const style: CSSProperties = {}
  31. if (props.color) {
  32. style['color'] = props.color
  33. }
  34. if (props.size) {
  35. style['font-size'] = addUnit(props.size)
  36. }
  37. return `${objToStyle(style)} ${props.customStyle}`
  38. })
  39. function handleClick(event: any) {
  40. emit('click', event)
  41. }
  42. </script>
  43. <style lang="scss" scoped>
  44. @import './index.scss';
  45. </style>