cl-icon.uvue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <text class="cl-icon" :class="[ptClassName]" :style="iconStyle" :key="cache.key">
  3. {{ icon.text }}
  4. </text>
  5. </template>
  6. <script setup lang="ts">
  7. import { computed, type PropType } from "vue";
  8. import { forInObject, get, has, parsePt, useCache, isDark, ctx, hasTextColor } from "@/cool";
  9. import { icons } from "@/icons";
  10. import { useSize } from "../../hooks";
  11. defineOptions({
  12. name: "cl-icon"
  13. });
  14. // 定义组件属性
  15. const props = defineProps({
  16. // 透传样式
  17. pt: {
  18. type: Object,
  19. default: () => ({})
  20. },
  21. // 图标名称
  22. name: {
  23. type: String,
  24. default: ""
  25. },
  26. // 图标大小
  27. size: {
  28. type: [String, Number] as PropType<string | number>,
  29. default: 32
  30. },
  31. // 图标高度
  32. height: {
  33. type: [String, Number] as PropType<string | number>,
  34. default: null
  35. },
  36. // 图标宽度
  37. width: {
  38. type: [String, Number] as PropType<string | number>,
  39. default: null
  40. },
  41. // 图标颜色
  42. color: {
  43. type: String,
  44. default: ""
  45. }
  46. });
  47. // 透传样式类型定义
  48. type PassThrough = {
  49. className?: string;
  50. };
  51. // 解析透传样式
  52. const pt = computed(() => parsePt<PassThrough>(props.pt));
  53. // 缓存
  54. const { cache } = useCache(() => [props.color]);
  55. // 字号
  56. const { getRpx, ptClassName } = useSize(() => pt.value.className ?? "");
  57. // 图标类型定义
  58. type Icon = {
  59. font: string; // 字体名称
  60. text: string; // 图标文本
  61. };
  62. // 图标信息
  63. const icon = computed<Icon>(() => {
  64. let font = "";
  65. let text = "";
  66. try {
  67. let code = "";
  68. // 遍历字体库查找对应图标
  69. forInObject(icons, (value, key) => {
  70. if (has(value, props.name)) {
  71. font = key;
  72. code = get(value, props.name) as string;
  73. }
  74. });
  75. text = String.fromCharCode(parseInt(code, 16));
  76. } catch (e) {
  77. console.error(`图标 ${props.name} 不存在`, e);
  78. }
  79. return {
  80. font,
  81. text
  82. };
  83. });
  84. // 图标颜色
  85. const color = computed(() => {
  86. if (props.color != "") {
  87. switch (props.color) {
  88. case "primary":
  89. return ctx.color["primary-500"] as string;
  90. case "success":
  91. return "#22c55e";
  92. case "warn":
  93. return "#eab308";
  94. case "error":
  95. return "#ef4444";
  96. case "info":
  97. return ctx.color["surface-500"] as string;
  98. case "dark":
  99. return ctx.color["surface-700"] as string;
  100. case "light":
  101. return ctx.color["surface-50"] as string;
  102. case "disabled":
  103. return ctx.color["surface-300"] as string;
  104. default:
  105. return props.color;
  106. }
  107. }
  108. return isDark.value ? "white" : (ctx.color["surface-700"] as string);
  109. });
  110. // 图标样式
  111. const iconStyle = computed(() => {
  112. const style = {};
  113. // 判断是不是有颜色样式
  114. if (!hasTextColor(ptClassName.value)) {
  115. style["color"] = color.value;
  116. }
  117. // 设置字体
  118. if (icon.value.font != "") {
  119. style["fontFamily"] = icon.value.font;
  120. }
  121. // 设置字体大小
  122. style["fontSize"] = getRpx(props.size!);
  123. // 设置高度
  124. style["height"] = getRpx(props.height ?? props.size!);
  125. style["lineHeight"] = getRpx(props.size!);
  126. // 设置宽度
  127. style["width"] = getRpx(props.width ?? props.size!);
  128. return style;
  129. });
  130. </script>