Skip to content

Commit

Permalink
Fix setContent
Browse files Browse the repository at this point in the history
Remove 'data-bs-original-title' trick
fix class selector on `_popperConfig`
  • Loading branch information
GeoSot committed Aug 31, 2021
1 parent ad2026d commit 2bb08b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
37 changes: 17 additions & 20 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const HOVER_STATE_SHOW = 'show'
const HOVER_STATE_OUT = 'out'

const SELECTOR_TOOLTIP_INNER = '.tooltip-inner'
const SELECTOR_TOOLTIP_ARROW = '.tooltip-arrow'
const SELECTOR_MODAL = `.${CLASS_NAME_MODAL}`

const EVENT_MODAL_HIDE = 'hide.bs.modal'
Expand Down Expand Up @@ -234,14 +235,6 @@ class Tooltip extends BaseComponent {
return
}

// A trick to recreate a tooltip in case a new title is given by using the NOT documented `data-bs-original-title`
// This will be removed later in favor of a `setContent` method
if (this.constructor.NAME === 'tooltip' && this.tip && this.getTitle() !== this.tip.querySelector(SELECTOR_TOOLTIP_INNER).innerHTML) {
this._disposePopper()
this.tip.remove()
this.tip = null
}

const tip = this.getTipElement()
const tipId = getUID(this.constructor.NAME)

Expand Down Expand Up @@ -372,7 +365,19 @@ class Tooltip extends BaseComponent {
}

setContent(content) {
let isShown = false
if (this.tip) {
isShown = this.tip.classList.contains(CLASS_NAME_SHOW)
this.tip.remove()
}

this._disposePopper()

this.tip = this._getTemplateFactory(content).toHtml()

if (isShown) {
this.show()
}
}

_getTemplateFactory(content) {
Expand All @@ -397,9 +402,7 @@ class Tooltip extends BaseComponent {
}

getTitle() {
const title = this._element.getAttribute('data-bs-original-title') || this._config.title

return this._resolvePossibleFunction(title)
return this._resolvePossibleFunction(this._config.title) || this._element.getAttribute('title')
}

updateAttachment(attachment) {
Expand Down Expand Up @@ -463,7 +466,7 @@ class Tooltip extends BaseComponent {
{
name: 'arrow',
options: {
element: `.${this.constructor.NAME}-arrow`
element: SELECTOR_TOOLTIP_ARROW
}
},
{
Expand Down Expand Up @@ -534,15 +537,9 @@ class Tooltip extends BaseComponent {

_fixTitle() {
const title = this._element.getAttribute('title')
const originalTitleType = typeof this._element.getAttribute('data-bs-original-title')

if (title || originalTitleType !== 'string') {
this._element.setAttribute('data-bs-original-title', title || '')
if (title && !this._element.getAttribute('aria-label') && !this._element.textContent) {
this._element.setAttribute('aria-label', title)
}

this._element.setAttribute('title', '')
if (title && !this._element.getAttribute('aria-label') && !this._element.textContent) {
this._element.setAttribute('aria-label', title)
}
}

Expand Down
16 changes: 8 additions & 8 deletions site/assets/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@
clipboard.on('success', function (e) {
var tooltipBtn = bootstrap.Tooltip.getInstance(e.trigger)

e.trigger.setAttribute('data-bs-original-title', 'Copied!')
tooltipBtn.show()

e.trigger.setAttribute('data-bs-original-title', 'Copy to clipboard')
tooltipBtn.setContent({ '.tooltip-inner': 'Copied!' })
e.trigger.addEventListener('hidden.bs.tooltip', function () {
tooltipBtn.setContent({ '.tooltip-inner': 'Copy to clipboard' })
}, { once: true })
e.clearSelection()
})

Expand All @@ -157,10 +157,10 @@
var fallbackMsg = 'Press ' + modifierKey + 'C to copy'
var tooltipBtn = bootstrap.Tooltip.getInstance(e.trigger)

e.trigger.setAttribute('data-bs-original-title', fallbackMsg)
tooltipBtn.show()

e.trigger.setAttribute('data-bs-original-title', 'Copy to clipboard')
tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg })
e.trigger.addEventListener('hidden.bs.tooltip', function () {
tooltipBtn.setContent({ '.tooltip-inner': 'Copy to clipboard' })
}, { once: true })
})

anchors.options = {
Expand Down

0 comments on commit 2bb08b9

Please sign in to comment.