Skip to content

Commit 340c859

Browse files
authored
feat: Add offset prop (#20)
1 parent c4fe4ba commit 340c859

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ yarn add @untemps/svelte-use-tooltip
121121
| `animated` | boolean | false | Flag to animate tooltip transitions. |
122122
| `animationEnterClassName` | string | null | Class name to apply to the tooltip enter transition. |
123123
| `animationLeaveClassName` | string | null | Class name to apply to the tooltip leave transition. |
124-
| `enterDelay` | number | 0 | Delay before showing the tooltip. |
125-
| `leaveDelay` | number | 0 | Delay before hiding the tooltip. |
124+
| `enterDelay` | number | 0 | Delay before showing the tooltip in milliseconds. |
125+
| `leaveDelay` | number | 0 | Delay before hiding the tooltip in milliseconds. |
126+
| `offset` | number | 10 | Distance between the tooltip and the target in pixels. |
126127
| `disabled` | boolean | false | Flag to disable the tooltip content. |
127128

128129
### Content Actions

dev/src/App.svelte

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
let useCustomAnimationLeaveClass = false
1111
let tooltipEnterDelay = 200
1212
let tooltipLeaveDelay = 200
13+
let tooltipOffset = 10
1314
1415
const _onTooltipClick = (arg) => {
1516
console.log(arg)
@@ -38,6 +39,7 @@
3839
animationLeaveClassName: useCustomAnimationLeaveClass ? 'tooltip-leave' : null,
3940
enterDelay: tooltipEnterDelay,
4041
leaveDelay: tooltipLeaveDelay,
42+
offset: tooltipOffset,
4143
disabled: isTooltipDisabled
4244
}}
4345
class="target"
@@ -105,6 +107,12 @@
105107
<input type="number" step={100} min={0} bind:value={tooltipLeaveDelay} />
106108
</label>
107109
</fieldset>
110+
<fieldset>
111+
<label>
112+
Tooltip Offset (px):
113+
<input type="number" step={1} min={5} bind:value={tooltipOffset} />
114+
</label>
115+
</fieldset>
108116
<fieldset>
109117
<label>
110118
Disable Tooltip:
@@ -128,7 +136,8 @@
128136
max-width: 640px;
129137
display: flex;
130138
flex-direction: column;
131-
row-gap: 1rem;
139+
align-items: center;
140+
row-gap: 3rem;
132141
}
133142
134143
.target {
@@ -175,6 +184,7 @@
175184
}
176185
177186
.settings__form input {
187+
width: 6rem;
178188
margin: 0;
179189
}
180190

src/Tooltip.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { DOMObserver } from '@untemps/dom-observer'
22

33
class Tooltip {
4-
static get GAP() {
5-
return 10
6-
}
7-
84
static #instances = []
95

106
#observer = null
@@ -28,6 +24,7 @@ class Tooltip {
2824
#animated = false
2925
#animationEnterClassName = null
3026
#animationLeaveClassName = null
27+
#offset = 10
3128

3229
static destroy() {
3330
Tooltip.#instances.forEach((instance) => {
@@ -49,6 +46,7 @@ class Tooltip {
4946
animationLeaveClassName,
5047
enterDelay,
5148
leaveDelay,
49+
offset,
5250
disabled
5351
) {
5452
this.#target = target
@@ -63,6 +61,7 @@ class Tooltip {
6361
this.#animationLeaveClassName = animationLeaveClassName || '__tooltip-leave'
6462
this.#enterDelay = enterDelay || 0
6563
this.#leaveDelay = leaveDelay || 0
64+
this.#offset = Math.max(offset || 10, 5)
6665

6766
this.#observer = new DOMObserver()
6867

@@ -89,6 +88,7 @@ class Tooltip {
8988
animationLeaveClassName,
9089
enterDelay,
9190
leaveDelay,
91+
offset,
9292
disabled
9393
) {
9494
const hasContentChanged = contentSelector !== this.#contentSelector || content !== this.#content
@@ -108,6 +108,7 @@ class Tooltip {
108108
this.#animationLeaveClassName = animationLeaveClassName || '__tooltip-leave'
109109
this.#enterDelay = enterDelay || 0
110110
this.#leaveDelay = leaveDelay || 0
111+
this.#offset = Math.max(offset || 10, 5)
111112

112113
if (hasContentChanged) {
113114
this.#removeTooltipFromTarget()
@@ -174,28 +175,28 @@ class Tooltip {
174175
switch (this.#position) {
175176
case 'left': {
176177
this.#tooltip.style.top = `${-(tooltipHeight - targetHeight) >> 1}px`
177-
this.#tooltip.style.left = `${-tooltipWidth - Tooltip.GAP}px`
178+
this.#tooltip.style.left = `${-tooltipWidth - this.#offset}px`
178179
this.#tooltip.style.bottom = null
179180
this.#tooltip.style.right = null
180181
break
181182
}
182183
case 'right': {
183184
this.#tooltip.style.top = `${-(tooltipHeight - targetHeight) >> 1}px`
184-
this.#tooltip.style.right = `${-tooltipWidth - Tooltip.GAP}px`
185+
this.#tooltip.style.right = `${-tooltipWidth - this.#offset}px`
185186
this.#tooltip.style.bottom = null
186187
this.#tooltip.style.left = null
187188
break
188189
}
189190
case 'bottom': {
190191
this.#tooltip.style.left = `${-(tooltipWidth - targetWidth) >> 1}px`
191-
this.#tooltip.style.bottom = `${-tooltipHeight - Tooltip.GAP}px`
192+
this.#tooltip.style.bottom = `${-tooltipHeight - this.#offset}px`
192193
this.#tooltip.style.right = null
193194
this.#tooltip.style.top = null
194195
break
195196
}
196197
default: {
197198
this.#tooltip.style.left = `${-(tooltipWidth - targetWidth) >> 1}px`
198-
this.#tooltip.style.top = `${-tooltipHeight - Tooltip.GAP}px`
199+
this.#tooltip.style.top = `${-tooltipHeight - this.#offset}px`
199200
this.#tooltip.style.right = null
200201
this.#tooltip.style.bottom = null
201202
}

src/useTooltip.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const useTooltip = (
1616
animationLeaveClassName,
1717
enterDelay,
1818
leaveDelay,
19+
offset,
1920
disabled,
2021
}
2122
) => {
@@ -32,6 +33,7 @@ const useTooltip = (
3233
animationLeaveClassName,
3334
enterDelay,
3435
leaveDelay,
36+
offset,
3537
disabled
3638
)
3739

@@ -48,6 +50,7 @@ const useTooltip = (
4850
animationLeaveClassName: newAnimationLeaveClassName,
4951
enterDelay: newEnterDelay,
5052
leaveDelay: newLeaveDelay,
53+
offset: newOffset,
5154
disabled: newDisabled,
5255
}) =>
5356
tooltip.update(
@@ -62,6 +65,7 @@ const useTooltip = (
6265
newAnimationLeaveClassName,
6366
newEnterDelay,
6467
newLeaveDelay,
68+
newOffset,
6569
newDisabled
6670
),
6771
destroy: () => tooltip.destroy(),

0 commit comments

Comments
 (0)