Skip to content

Commit

Permalink
Allow Tooltips/Popovers to work in shadow DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Mar 8, 2018
1 parent 2b86193 commit 4e87108
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ const Tooltip = (($) => {
if (this.isWithContent() && this._isEnabled) {
$(this.element).trigger(showEvent)

const shadowRoot = Util.findShadowRoot(this.element)
const isInTheDom = $.contains(
this.element.ownerDocument.documentElement,
shadowRoot !== null ? shadowRoot : this.element.ownerDocument.documentElement,
this.element
)

Expand Down
19 changes: 19 additions & 0 deletions js/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ const Util = (($) => {
}
}
}
},

findShadowRoot(element) {
// Can find the shadow root otherwise it'll return the document
if (typeof element.getRootNode === 'function') {
const tmpRoot = element.getRootNode()
return tmpRoot instanceof ShadowRoot ? tmpRoot : null
}

if (element instanceof ShadowRoot) {
return element
}

// when we don't find a shadow root
if (!element.parentNode) {
return null
}

return Util.findShadowRoot(element.parentNode)
}
}

Expand Down
20 changes: 20 additions & 0 deletions js/tests/unit/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ $(function () {
id2 = Util.getUID('test')
assert.ok(id !== id2, id + ' !== ' + id2)
})

QUnit.test('Util.findShadowRoot should find the shadow DOM root', function (assert) {
// Only for newer browsers
if (typeof document.body.attachShadow !== 'function') {
assert.expect(0)
return
}

assert.expect(2)
var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))
var shadowRoot = $div[0].attachShadow({
mode: 'open'
})

assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot))

shadowRoot.innerHTML = '<button>Shadow Button</button>'

assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot.firstChild))
})
})
22 changes: 21 additions & 1 deletion js/tests/visual/tooltip.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ <h1>Tooltip <small>Bootstrap Visual Test</small></h1>
Tooltip with HTML
</button>
</p>
<div id="target" title="Test tooltip on transformed element"></div>
<div class="row">
<div class="col-sm-3">
<div id="target" title="Test tooltip on transformed element"></div>
</div>
<div id="shadow" class="pt-5"></div>
</div>
</div>

<script src="../../../assets/js/vendor/jquery-slim.min.js"></script>
Expand All @@ -57,6 +62,21 @@ <h1>Tooltip <small>Bootstrap Visual Test</small></h1>

<script>
$(function () {
if (typeof document.body.attachShadow === 'function') {
var shadowRoot = $('#shadow')[0].attachShadow({ mode: 'open' })
shadowRoot.innerHTML =
'<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top in a shadow dom">' +
' Tooltip on top in a shadow dom' +
'</button>' +
'<button id="secondTooltip" type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top in a shadow dom with container option">' +
' Tooltip on top in a shadow dom' +
'</button>'

$(shadowRoot.firstChild).tooltip()
$(shadowRoot.getElementById('secondTooltip')).tooltip({
container: shadowRoot
})
}
$('[data-toggle="tooltip"]').tooltip()
$('#target').tooltip({
placement : 'top',
Expand Down

0 comments on commit 4e87108

Please sign in to comment.