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 tooltip when hovering a child element (delegateTarget) #30928

Merged
merged 4 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions js/src/dom/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function getEvent(element) {

function bootstrapHandler(element, fn) {
return function handler(event) {
event.delegateTarget = element
if (handler.oneOff) {
EventHandler.off(element, event.type, fn)
}
Expand All @@ -109,6 +110,7 @@ function bootstrapDelegationHandler(element, selector, fn) {
for (let { target } = event; target && target !== this; target = target.parentNode) {
for (let i = domElements.length; i--;) {
if (domElements[i] === target) {
event.delegateTarget = target
if (handler.oneOff) {
EventHandler.off(element, event.type, fn)
}
Expand Down
18 changes: 9 additions & 9 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ class Tooltip {

if (event) {
const dataKey = this.constructor.DATA_KEY
let context = Data.getData(event.target, dataKey)
let context = Data.getData(event.delegateTarget, dataKey)

if (!context) {
context = new this.constructor(
event.target,
event.delegateTarget,
this._getDelegateConfig()
)
Data.setData(event.target, dataKey, context)
Data.setData(event.delegateTarget, dataKey, context)
}

context._activeTrigger.click = !context._activeTrigger.click
Expand Down Expand Up @@ -587,14 +587,14 @@ class Tooltip {

_enter(event, context) {
const dataKey = this.constructor.DATA_KEY
context = context || Data.getData(event.target, dataKey)
context = context || Data.getData(event.delegateTarget, dataKey)

if (!context) {
context = new this.constructor(
event.target,
event.delegateTarget,
this._getDelegateConfig()
)
Data.setData(event.target, dataKey, context)
Data.setData(event.delegateTarget, dataKey, context)
}

if (event) {
Expand Down Expand Up @@ -627,14 +627,14 @@ class Tooltip {

_leave(event, context) {
const dataKey = this.constructor.DATA_KEY
context = context || Data.getData(event.target, dataKey)
context = context || Data.getData(event.delegateTarget, dataKey)

if (!context) {
context = new this.constructor(
event.target,
event.delegateTarget,
this._getDelegateConfig()
)
Data.setData(event.target, dataKey, context)
Data.setData(event.delegateTarget, dataKey, context)
}

if (event) {
Expand Down
22 changes: 22 additions & 0 deletions js/tests/unit/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,28 @@ describe('Tooltip', () => {
tooltip.show()
})

it('should show a tooltip when hovering a children element', done => {
fixtureEl.innerHTML =
'<a href="#" rel="tooltip" title="Another tooltip">' +
'<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">' +
'<rect width="100%" fill="#563d7c"/>' +
'<circle cx="50" cy="50" r="30" fill="#fff"/>' +
'</svg>' +
'</a>'

const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)

spyOn(tooltip, 'show')

tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bubbles: true is important here, otherwise it won't work


setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
done()
}, 0)
})

it('should show a tooltip on mobile', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'

Expand Down
11 changes: 11 additions & 0 deletions site/content/docs/5.0/components/tooltips.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ And with custom HTML added:
</button>
{{< /highlight >}}

With an SVG:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allows to test/demonstrate inside documentation, should be done another way I guess.

Without the delegateTarget fix, it does not show the tooltip when hovering and crashes when mouse leaving.


<div class="bd-example tooltip-demo">
<a href="#" data-toggle="tooltip" title="Default tooltip">
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">
<rect width="100%" height="100%" fill="#563d7c"/>
<circle cx="50" cy="50" r="30" fill="#007bff"/>
</svg>
</a>
</div>

## Usage

The tooltip plugin generates content and markup on demand, and by default places tooltips after their trigger element.
Expand Down