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 Dec 5, 2018
1 parent 1a171b8 commit f751c98
Show file tree
Hide file tree
Showing 4 changed files with 84 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 @@ -244,8 +244,9 @@ class 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
23 changes: 23 additions & 0 deletions js/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ const Util = {
}
}
}
},

findShadowRoot(element) {
if (!document.documentElement.attachShadow) {
return null
}

// Can find the shadow root otherwise it'll return the document
if (typeof element.getRootNode === 'function') {
const root = element.getRootNode()
return root instanceof ShadowRoot ? root : 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
38 changes: 38 additions & 0 deletions js/tests/unit/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,42 @@ $(function () {
assert.expect(1)
assert.ok(Util.supportsTransitionEnd())
})

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

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

assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot))
shadowRoot.innerHTML = '<button>Shadow Button</button>'
assert.equal(shadowRoot, Util.findShadowRoot(shadowRoot.firstChild))
})

QUnit.test('Util.findShadowRoot should return null when attachShadow is not available', function (assert) {
assert.expect(1)

var $div = $('<div id="test"></div>').appendTo($('#qunit-fixture'))
if (!document.documentElement.attachShadow) {
assert.equal(null, Util.findShadowRoot($div[0]))
} else {
var sandbox = sinon.createSandbox()

sandbox.replace(document.documentElement, 'attachShadow', function () {
// to avoid empty function
return $div
})

assert.equal(null, Util.findShadowRoot($div[0]))
sandbox.restore()
}
})
})
22 changes: 21 additions & 1 deletion js/tests/visual/tooltip.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ <h1>Tooltip <small>Bootstrap Visual Test</small></h1>
</button>
</p>
</div>
<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 id="customContainer"></div>
</div>

Expand All @@ -69,6 +74,21 @@ <h1>Tooltip <small>Bootstrap Visual Test</small></h1>
<script src="../../dist/tooltip.js"></script>
<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 f751c98

Please sign in to comment.