Skip to content

Commit 6f077ff

Browse files
authored
Clean tooltip component unneeded functionality (#32692)
1 parent 374eeec commit 6f077ff

File tree

3 files changed

+13
-67
lines changed

3 files changed

+13
-67
lines changed

js/src/popover.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Tooltip from './tooltip'
1515
const NAME = 'popover'
1616
const DATA_KEY = 'bs.popover'
1717
const EVENT_KEY = `.${DATA_KEY}`
18-
const CLASS_PREFIX = 'bs-popover'
1918

2019
const SELECTOR_TITLE = '.popover-header'
2120
const SELECTOR_CONTENT = '.popover-body'
@@ -90,10 +89,6 @@ class Popover extends Tooltip {
9089
return this._resolvePossibleFunction(this._config.content)
9190
}
9291

93-
_getBasicClassPrefix() {
94-
return CLASS_PREFIX
95-
}
96-
9792
// Static
9893
static jQueryInterface(config) {
9994
return this.each(function () {

js/src/tooltip.js

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import TemplateFactory from './util/template-factory'
2929
const NAME = 'tooltip'
3030
const DATA_KEY = 'bs.tooltip'
3131
const EVENT_KEY = `.${DATA_KEY}`
32-
const CLASS_PREFIX = 'bs-tooltip'
3332
const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn'])
3433

3534
const CLASS_NAME_FADE = 'fade'
@@ -232,13 +231,6 @@ class Tooltip extends BaseComponent {
232231

233232
this._element.setAttribute('aria-describedby', tip.getAttribute('id'))
234233

235-
const placement = typeof this._config.placement === 'function' ?
236-
this._config.placement.call(this, tip, this._element) :
237-
this._config.placement
238-
239-
const attachment = this._getAttachment(placement)
240-
this._addAttachmentClass(attachment)
241-
242234
const { container } = this._config
243235
Data.set(tip, this.constructor.DATA_KEY, this)
244236

@@ -250,6 +242,10 @@ class Tooltip extends BaseComponent {
250242
if (this._popper) {
251243
this._popper.update()
252244
} else {
245+
const placement = typeof this._config.placement === 'function' ?
246+
this._config.placement.call(this, tip, this._element) :
247+
this._config.placement
248+
const attachment = AttachmentMap[placement.toUpperCase()]
253249
this._popper = Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
254250
}
255251

@@ -295,7 +291,6 @@ class Tooltip extends BaseComponent {
295291
tip.remove()
296292
}
297293

298-
this._cleanTipClass()
299294
this._element.removeAttribute('aria-describedby')
300295
EventHandler.trigger(this._element, this.constructor.Event.HIDDEN)
301296

@@ -346,6 +341,8 @@ class Tooltip extends BaseComponent {
346341

347342
const tip = templateFactory.toHtml()
348343
tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
344+
// todo on v6 the following can be done on css only
345+
tip.classList.add(`bs-${this.constructor.NAME}-auto`)
349346

350347
const tipId = getUID(this.constructor.NAME).toString()
351348

@@ -463,19 +460,8 @@ class Tooltip extends BaseComponent {
463460
options: {
464461
element: SELECTOR_TOOLTIP_ARROW
465462
}
466-
},
467-
{
468-
name: 'onChange',
469-
enabled: true,
470-
phase: 'afterWrite',
471-
fn: data => this._handlePopperPlacementChange(data)
472-
}
473-
],
474-
onFirstUpdate: data => {
475-
if (data.options.placement !== data.placement) {
476-
this._handlePopperPlacementChange(data)
477463
}
478-
}
464+
]
479465
}
480466

481467
return {
@@ -484,14 +470,6 @@ class Tooltip extends BaseComponent {
484470
}
485471
}
486472

487-
_addAttachmentClass(attachment) {
488-
this.getTipElement().classList.add(`${this._getBasicClassPrefix()}-${this.updateAttachment(attachment)}`)
489-
}
490-
491-
_getAttachment(placement) {
492-
return AttachmentMap[placement.toUpperCase()]
493-
}
494-
495473
_setListeners() {
496474
const triggers = this._config.trigger.split(' ')
497475

@@ -652,33 +630,6 @@ class Tooltip extends BaseComponent {
652630
return config
653631
}
654632

655-
_cleanTipClass() {
656-
const tip = this.getTipElement()
657-
const basicClassPrefixRegex = new RegExp(`(^|\\s)${this._getBasicClassPrefix()}\\S+`, 'g')
658-
const tabClass = tip.getAttribute('class').match(basicClassPrefixRegex)
659-
if (tabClass !== null && tabClass.length > 0) {
660-
for (const tClass of tabClass.map(token => token.trim())) {
661-
tip.classList.remove(tClass)
662-
}
663-
}
664-
}
665-
666-
_getBasicClassPrefix() {
667-
return CLASS_PREFIX
668-
}
669-
670-
_handlePopperPlacementChange(popperData) {
671-
const { state } = popperData
672-
673-
if (!state) {
674-
return
675-
}
676-
677-
this.tip = state.elements.popper
678-
this._cleanTipClass()
679-
this._addAttachmentClass(this._getAttachment(state.placement))
680-
}
681-
682633
_disposePopper() {
683634
if (this._popper) {
684635
this._popper.destroy()

js/tests/unit/tooltip.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,12 @@ describe('Tooltip', () => {
465465
})
466466

467467
tooltipEl.addEventListener('inserted.bs.tooltip', () => {
468-
expect(tooltip.getTipElement().classList.contains('bs-tooltip-bottom')).toEqual(true)
468+
expect(tooltip.getTipElement().classList.contains('bs-tooltip-auto')).toEqual(true)
469469
})
470470

471471
tooltipEl.addEventListener('shown.bs.tooltip', () => {
472-
const tooltipShown = document.querySelector('.tooltip')
473-
474-
expect(tooltipShown.classList.contains('bs-tooltip-bottom')).toEqual(true)
472+
expect(tooltip.getTipElement().classList.contains('bs-tooltip-auto')).toEqual(true)
473+
expect(tooltip.getTipElement().getAttribute('data-popper-placement')).toEqual('bottom')
475474
done()
476475
})
477476

@@ -699,6 +698,7 @@ describe('Tooltip', () => {
699698

700699
setTimeout(() => {
701700
expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
701+
expect(document.querySelectorAll('.tooltip').length).toEqual(1)
702702
done()
703703
}, 200)
704704
}, 0)
@@ -1092,7 +1092,7 @@ describe('Tooltip', () => {
10921092
})
10931093

10941094
tooltipEl.addEventListener('inserted.bs.tooltip', () => {
1095-
expect(tooltip.getTipElement().classList.contains('bs-tooltip-end')).toEqual(true)
1095+
expect(tooltip.getTipElement().classList.contains('bs-tooltip-auto')).toEqual(true)
10961096
done()
10971097
})
10981098

@@ -1108,7 +1108,7 @@ describe('Tooltip', () => {
11081108
})
11091109

11101110
tooltipEl.addEventListener('inserted.bs.tooltip', () => {
1111-
expect(tooltip.getTipElement().classList.contains('bs-tooltip-start')).toEqual(true)
1111+
expect(tooltip.getTipElement().classList.contains('bs-tooltip-auto')).toEqual(true)
11121112
done()
11131113
})
11141114

0 commit comments

Comments
 (0)