Badge.svelte 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <script>
  2. // 徽标文案
  3. // badge text
  4. export let text = '';
  5. // 圆角风格
  6. // radius style
  7. export let radius = 'full';
  8. // 是否位于左侧
  9. // is on the left
  10. export let isLeft = false;
  11. // 是否显示
  12. // is show
  13. export let isShow = true;
  14. // 上下偏移量
  15. // offset top
  16. export let offsetY = 0;
  17. // 左右偏移量
  18. // offset left
  19. export let offsetX = 0;
  20. // 是否位于内部
  21. // is inner
  22. export let isInner = false;
  23. // 注入CSS
  24. // inject CSS
  25. export let injClass = '';
  26. // 圆角风格样式
  27. // radius style
  28. const radiusObj = {
  29. full: ' rounded-full',
  30. base: ' rounded',
  31. none: ' rounded-none',
  32. leftLeaf: ' rounded-full rounded-br-none',
  33. rightLeaf: ' rounded-full rounded-bl-none',
  34. };
  35. // 通过radius配置圆角风格或叶形
  36. // set radius style or leaf style by radius
  37. $: radiusClass = radius === 'leaf' ? (isLeft ? radiusObj.leftLeaf : radiusObj.rightLeaf) : radiusObj[radius] || radiusObj.full;
  38. </script>
  39. {#if isInner}
  40. <div
  41. class={`px-1 text-xs text-white whitespace-nowrap${radiusClass}${
  42. text === '' ? ' w-3 h-3' : ' '
  43. } text-center leading-4 transition-all bg-[red]${injClass === '' ? '' : ` ${injClass}`}`}
  44. style={`transform:${isShow ? ' scale(1)' : ' scale(0)'}`}
  45. >
  46. {text}
  47. </div>
  48. {:else}
  49. <div class="relative">
  50. <slot />
  51. <div
  52. class={`absolute${text === '' ? ' -top-1.5' : ' -top-2'}${
  53. isLeft ? ' left-0' : ' right-0'
  54. } px-1 text-xs text-white whitespace-nowrap${radiusClass}${
  55. text === '' ? ' w-3 h-3' : ' '
  56. } text-center leading-4 transition-all bg-[red]${injClass === '' ? '' : ` ${injClass}`}`}
  57. style={`transform: translateX(calc(${isLeft ? '-' : ''}50% ${isLeft ? '+' : '-'} ${offsetX}px)) translateY(${offsetY}px)${
  58. isShow ? ' scale(1)' : ' scale(0)'
  59. }`}
  60. >
  61. {text}
  62. </div>
  63. </div>
  64. {/if}