Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix different tooltips offset when hovering #22276

Merged
merged 3 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ const Tooltip = (($) => {
tip.parentNode.removeChild(tip)
}

this._cleanTipClass()
this.element.removeAttribute('aria-describedby')
$(this.element).trigger(this.constructor.Event.HIDDEN)
this._isTransitioning = false
Expand Down Expand Up @@ -438,6 +439,23 @@ const Tooltip = (($) => {
return AttachmentMap[placement.toUpperCase()]
}

_cleanTipClass() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Popover class extends Tooltip class, so it's probably better to implement this method in Tooltip to handle only the tooltip case, and to override it in Popover to handle only the case of popover.
This is how it's implemented for other methods (for example isWithContent);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally I found a generic solution but thank you @vanduynslagerp 👍

Copy link
Contributor

@pvdlg pvdlg Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the CLASS_PREFIX is clever !
You can make this code shorter and more efficient thought:

// On top of the file (constant)
const TETHER_PREFIX_REGEX = new RegExp(`(^|\s)${CLASS_PREFIX}(\S*)`, 'g');

const $tip = $(this.getTipElement())
$tip.removeClass($tip.attr('class').match(TETHER_PREFIX_REGEX).join(' '));

That makes the code more readable and would avoid the multiple calls to Jquery to remove the classes one by one. It also avoid the loop and the assigning and reassigning of temporary variables multiple time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right nice regex thanks you @vanduynslagerp

const $tip = $(this.getTipElement())
let tabClassStr = $tip.attr('class')
let tabClass = tabClassStr.split(' ')
let i = 0
while (tabClassStr.indexOf(CLASS_PREFIX) !== -1) {
if (tabClass[i].indexOf(CLASS_PREFIX) !== -1) {
$tip.removeClass(tabClass[i])
tabClassStr = $tip.attr('class')
tabClass = tabClassStr.split(' ')
i = 0
} else {
i++
}
}
}

_setListeners() {
const triggers = this.config.trigger.split(' ')

Expand Down
17 changes: 17 additions & 0 deletions js/tests/unit/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,4 +869,21 @@ $(function () {
})
.modal('show')
})

QUnit.test('should reset tip classes when hidden event triggered', function (assert) {
assert.expect(2)
var done = assert.async()
var $el = $('<a href="#" rel="tooltip" title="Test tooltip"/>')
.appendTo('#qunit-fixture')
.bootstrapTooltip('show')
.on('hidden.bs.tooltip', function () {
var tooltip = $el.data('bs.tooltip')
var $tooltip = $(tooltip.getTipElement())
assert.ok($tooltip.hasClass('tooltip'))
assert.ok($tooltip.hasClass('fade'))
done()
})

$el.bootstrapTooltip('hide')
})
})