Skip to content

Commit c70463d

Browse files
authored
feat: Extend hovering area (#21)
1 parent b7a5c4f commit c70463d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Tooltip.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Tooltip {
6969
this.#target.setAttribute('style', 'position: relative')
7070

7171
this.#createTooltip()
72-
this.#tooltip.classList.add(this.#containerClassName || '__tooltip', `__tooltip-${this.#position}`)
72+
this.#tooltip.setAttribute('class', this.#containerClassName || `__tooltip __tooltip-${this.#position}`)
7373

7474
disabled ? this.#disableTarget() : this.#enableTarget()
7575

@@ -94,6 +94,7 @@ class Tooltip {
9494
const hasContentChanged = contentSelector !== this.#contentSelector || content !== this.#content
9595
const hasContainerClassNameChanged = containerClassName !== this.#containerClassName
9696
const hasPositionChanged = position !== this.#position
97+
const hasOffsetChanged = position !== this.#offset
9798
const hasToDisableTarget = disabled && this.#boundEnterHandler
9899
const hasToEnableTarget = !disabled && !this.#boundEnterHandler
99100

@@ -110,12 +111,12 @@ class Tooltip {
110111
this.#leaveDelay = leaveDelay || 0
111112
this.#offset = Math.max(offset || 10, 5)
112113

113-
if (hasContentChanged) {
114+
if (hasContentChanged || hasPositionChanged || hasOffsetChanged) {
114115
this.#removeTooltipFromTarget()
115116
this.#createTooltip()
116117
}
117118

118-
if (hasContainerClassNameChanged || hasContentChanged || hasPositionChanged) {
119+
if (hasContainerClassNameChanged || hasContentChanged || hasPositionChanged || hasOffsetChanged) {
119120
this.#tooltip.setAttribute('class', this.#containerClassName || `__tooltip __tooltip-${this.#position}`)
120121
}
121122

@@ -167,6 +168,38 @@ class Tooltip {
167168
const child = document.createTextNode(this.#content)
168169
this.#tooltip.appendChild(child)
169170
}
171+
172+
this.#createAndAddTooltipArea()
173+
}
174+
175+
#createAndAddTooltipArea() {
176+
const area = document.createElement('div')
177+
area.setAttribute('aria-hidden', 'true')
178+
area.setAttribute('class', '__tooltip-area')
179+
switch (this.#position) {
180+
case 'left': {
181+
area.setAttribute('style', `width: calc(100% + ${this.#offset}px)`)
182+
break
183+
}
184+
case 'right': {
185+
area.setAttribute(
186+
'style',
187+
`width: calc(100% + ${this.#offset}px); margin-left: calc(-0.5rem - ${this.#offset}px)`
188+
)
189+
break
190+
}
191+
case 'bottom': {
192+
area.setAttribute(
193+
'style',
194+
`height: calc(100% + ${this.#offset}px); margin-top: calc(-0.5rem - ${this.#offset}px)`
195+
)
196+
break
197+
}
198+
default: {
199+
area.setAttribute('style', `height: calc(100% + ${this.#offset}px)`)
200+
}
201+
}
202+
this.#tooltip.appendChild(area)
170203
}
171204

172205
#positionTooltip() {

src/useTooltip.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
border-style: solid;
1818
}
1919

20+
.__tooltip-area {
21+
position: absolute;
22+
background: transparent;
23+
width: 100%;
24+
height: 100%;
25+
margin: -0.5rem;
26+
}
27+
2028
.__tooltip-top::after {
2129
bottom: -10px;
2230
left: 50%;

0 commit comments

Comments
 (0)