1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-08 06:36:33 +02:00

More tooltip refactoring (#35546)

* Tooltip.js: move `shown` check to method

* Tooltip.js: move Popper's creation to method

* Tooltip.js: merge checks before `hide`

* Tooltip.js: minor refactoring on `toggle` method
This commit is contained in:
GeoSot
2022-01-30 17:39:03 +02:00
committed by GitHub
parent e1020a43a5
commit 74f24cdf24

View File

@@ -178,15 +178,17 @@ class Tooltip extends BaseComponent {
} else { } else {
context._leave() context._leave()
} }
} else {
if (this._getTipElement().classList.contains(CLASS_NAME_SHOW)) { return
}
if (this._isShown()) {
this._leave() this._leave()
return return
} }
this._enter() this._enter()
} }
}
dispose() { dispose() {
clearTimeout(this._timeout) clearTimeout(this._timeout)
@@ -234,11 +236,7 @@ class Tooltip extends BaseComponent {
if (this._popper) { if (this._popper) {
this._popper.update() this._popper.update()
} else { } else {
const placement = typeof this._config.placement === 'function' ? this._createPopper(tip)
this._config.placement.call(this, tip, this._element) :
this._config.placement
const attachment = AttachmentMap[placement.toUpperCase()]
this._popper = Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
} }
tip.classList.add(CLASS_NAME_SHOW) tip.classList.add(CLASS_NAME_SHOW)
@@ -268,7 +266,7 @@ class Tooltip extends BaseComponent {
} }
hide() { hide() {
if (!this._popper) { if (!this._isShown()) {
return return
} }
@@ -291,6 +289,7 @@ class Tooltip extends BaseComponent {
this._activeTrigger[TRIGGER_CLICK] = false this._activeTrigger[TRIGGER_CLICK] = false
this._activeTrigger[TRIGGER_FOCUS] = false this._activeTrigger[TRIGGER_FOCUS] = false
this._activeTrigger[TRIGGER_HOVER] = false this._activeTrigger[TRIGGER_HOVER] = false
this._isHovered = false
const complete = () => { const complete = () => {
if (this._isWithActiveTrigger()) { if (this._isWithActiveTrigger()) {
@@ -308,7 +307,6 @@ class Tooltip extends BaseComponent {
} }
this._queueCallback(complete, this.tip, this._isAnimated()) this._queueCallback(complete, this.tip, this._isAnimated())
this._isHovered = false
} }
update() { update() {
@@ -356,7 +354,7 @@ class Tooltip extends BaseComponent {
setContent(content) { setContent(content) {
let isShown = false let isShown = false
if (this.tip) { if (this.tip) {
isShown = this.tip.classList.contains(CLASS_NAME_SHOW) isShown = this._isShown()
this.tip.remove() this.tip.remove()
this.tip = null this.tip = null
} }
@@ -404,6 +402,18 @@ class Tooltip extends BaseComponent {
return this._config.animation || (this.tip && this.tip.classList.contains(CLASS_NAME_FADE)) return this._config.animation || (this.tip && this.tip.classList.contains(CLASS_NAME_FADE))
} }
_isShown() {
return this.tip && this.tip.classList.contains(CLASS_NAME_SHOW)
}
_createPopper(tip) {
const placement = typeof this._config.placement === 'function' ?
this._config.placement.call(this, tip, this._element) :
this._config.placement
const attachment = AttachmentMap[placement.toUpperCase()]
this._popper = Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
}
_getOffset() { _getOffset() {
const { offset } = this._config const { offset } = this._config
@@ -532,7 +542,7 @@ class Tooltip extends BaseComponent {
} }
_enter() { _enter() {
if (this._getTipElement().classList.contains(CLASS_NAME_SHOW) || this._isHovered) { if (this._isShown() || this._isHovered) {
this._isHovered = true this._isHovered = true
return return
} }