Badge.svelte 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <script lang="ts">
  2. import type { BadgeProps } from '../../types';
  3. let {
  4. text = '',
  5. radius = 'full',
  6. isLeft = false,
  7. isShow = true,
  8. offsetY = 0,
  9. offsetX = 0,
  10. isInner = false,
  11. injClass = '',
  12. children,
  13. }: BadgeProps = $props();
  14. // 圆角风格样式
  15. // radius style
  16. const radiusObj = {
  17. full: ' rounded-full',
  18. base: ' rounded',
  19. none: ' rounded-none',
  20. leftLeaf: ' rounded-full rounded-br-none',
  21. rightLeaf: ' rounded-full rounded-bl-none',
  22. };
  23. // 通过radius配置圆角风格或叶形
  24. // set radius style or leaf style by radius
  25. const radiusClass = radius === 'leaf' ? (isLeft ? radiusObj.leftLeaf : radiusObj.rightLeaf) : radiusObj[radius] || radiusObj.full;
  26. </script>
  27. {#if isInner}
  28. <div
  29. class="px-1 text-xs text-white whitespace-nowrap{radiusClass}{text === ''
  30. ? ' w-3 h-3'
  31. : ' '} text-center leading-4 transition-all bg-[red]{injClass === '' ? '' : ` ${injClass}`}"
  32. style="transform:{isShow ? ' scale(1)' : ' scale(0)'}"
  33. >
  34. {text}
  35. </div>
  36. {:else}
  37. <div class="relative">
  38. {@render children?.()}
  39. <div
  40. class="absolute{text === '' ? ' -top-1.5' : ' -top-2'}{isLeft
  41. ? ' left-0'
  42. : ' right-0'} px-1 text-xs text-white whitespace-nowrap{radiusClass}{text === ''
  43. ? ' w-3 h-3'
  44. : ' '} text-center leading-4 transition-all bg-[red]{injClass === '' ? '' : ` ${injClass}`}"
  45. style="transform: translateX(calc({isLeft ? '-' : ''}50% {isLeft ? '+' : '-'} {offsetX}px)) translateY({offsetY}px){isShow
  46. ? ' scale(1)'
  47. : ' scale(0)'}"
  48. >
  49. {text}
  50. </div>
  51. </div>
  52. {/if}