|
16 | 16 | children: Snippet; |
17 | 17 | } |
18 | 18 |
|
19 | | - const { text, delay = 700, align = 'center', position = 'bottom', children }: Props = $props(); |
| 19 | + const { text, delay = 700, align, position, children }: Props = $props(); |
20 | 20 |
|
21 | 21 | let targetEl: HTMLElement | undefined = $state(); |
22 | 22 | let tooltipEl: HTMLElement | undefined = $state(); |
|
38 | 38 | show = false; |
39 | 39 | } |
40 | 40 |
|
| 41 | + function isNoSpaceOnRight() { |
| 42 | + if (!targetEl || !tooltipEl) return false; |
| 43 | +
|
| 44 | + const tooltipRect = tooltipEl.getBoundingClientRect(); |
| 45 | + const targetChild = targetEl.children[0]; |
| 46 | + const targetRect = targetChild.getBoundingClientRect(); |
| 47 | +
|
| 48 | + return targetRect.left + tooltipRect.width / 2 > window.innerWidth; |
| 49 | + } |
| 50 | +
|
| 51 | + function isNoSpaceOnLeft() { |
| 52 | + if (!targetEl || !tooltipEl) return false; |
| 53 | +
|
| 54 | + const tooltipRect = tooltipEl.getBoundingClientRect(); |
| 55 | + const targetChild = targetEl.children[0]; |
| 56 | + const targetRect = targetChild.getBoundingClientRect(); |
| 57 | +
|
| 58 | + return targetRect.left - tooltipRect.width / 2 < 0; |
| 59 | + } |
| 60 | +
|
41 | 61 | function adjustPosition() { |
42 | 62 | if (!targetEl || !tooltipEl) return; |
43 | 63 |
|
|
52 | 72 | let transformOriginLeft = 'center'; |
53 | 73 | const gap = 4; |
54 | 74 |
|
55 | | - if (position === 'bottom') { |
56 | | - top = targetRect.bottom + window.scrollY + gap; |
57 | | -
|
58 | | - transformOriginTop = 'top'; |
59 | | - } else if (position === 'top') { |
60 | | - top = targetRect.top - tooltipRect.height + window.scrollY - gap; |
61 | | -
|
62 | | - transformOriginTop = 'bottom'; |
63 | | - } |
64 | | -
|
65 | | - if (align === 'start') { |
| 75 | + function alignLeft() { |
66 | 76 | left = targetRect.left + window.scrollX; |
67 | 77 | transformOriginLeft = 'left'; |
68 | | - } else if (align === 'end') { |
| 78 | + } |
| 79 | +
|
| 80 | + function alignRight() { |
69 | 81 | left = targetRect.right - tooltipRect.width + window.scrollX; |
70 | 82 | transformOriginLeft = 'right'; |
71 | | - } else if (align === 'center') { |
| 83 | + } |
| 84 | +
|
| 85 | + function alignCenter() { |
72 | 86 | left = targetRect.left + targetRect.width / 2 - tooltipRect.width / 2 + window.scrollX; |
73 | 87 | transformOriginLeft = 'center'; |
74 | 88 | } |
75 | 89 |
|
| 90 | + function positionTop() { |
| 91 | + top = targetRect.top - tooltipRect.height + window.scrollY - gap; |
| 92 | + transformOriginTop = 'bottom'; |
| 93 | + } |
| 94 | +
|
| 95 | + function positionBottom() { |
| 96 | + top = targetRect.bottom + window.scrollY + gap; |
| 97 | + transformOriginTop = 'top'; |
| 98 | + } |
| 99 | +
|
| 100 | + // Vertical position |
| 101 | + if (position) { |
| 102 | + if (position === 'bottom') { |
| 103 | + positionBottom(); |
| 104 | + } else if (position === 'top') { |
| 105 | + positionTop(); |
| 106 | + } |
| 107 | + } else { |
| 108 | + positionBottom(); |
| 109 | + } |
| 110 | +
|
| 111 | + // Auto check horizontal position |
| 112 | + if (align) { |
| 113 | + if (align === 'start') { |
| 114 | + alignLeft(); |
| 115 | + } else if (align === 'end') { |
| 116 | + alignRight(); |
| 117 | + } else if (align === 'center') { |
| 118 | + alignCenter(); |
| 119 | + } |
| 120 | + } else { |
| 121 | + if (isNoSpaceOnLeft()) { |
| 122 | + alignLeft(); |
| 123 | + } else if (isNoSpaceOnRight()) { |
| 124 | + alignRight(); |
| 125 | + } else { |
| 126 | + alignCenter(); |
| 127 | + } |
| 128 | + } |
| 129 | +
|
76 | 130 | tooltipEl.style.top = `${top}px`; |
77 | 131 | tooltipEl.style.left = `${left}px`; |
78 | 132 | tooltipEl.style.transformOrigin = `${transformOriginTop} ${transformOriginLeft}`; |
|
0 commit comments