Skip to content

fix(b-tooltip): Updated tooltip to work under shadowDOM #6997

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/components/tooltip/helpers/bv-tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import {
contains,
getAttr,
getById,
getShadowRootOrRoot,
hasAttr,
hasClass,
isConnectedToDOM,
isDisabled,
isElement,
isVisible,
Expand Down Expand Up @@ -262,7 +264,7 @@ export const BVTooltip = /*#__PURE__*/ Vue.extend({

this.$nextTick(() => {
const target = this.getTarget()
if (target && contains(document.body, target)) {
if (target && (target.isConnected || isConnectedToDOM(target))) {
// Copy the parent's scoped style attribute
this.scopeId = getScopeId(this.$parent)
// Set up all trigger handlers and listeners
Expand Down Expand Up @@ -420,7 +422,7 @@ export const BVTooltip = /*#__PURE__*/ Vue.extend({
const target = this.getTarget()
if (
!target ||
!contains(document.body, target) ||
!isConnectedToDOM(target) ||
!isVisible(target) ||
this.dropdownOpen() ||
((isUndefinedOrNull(this.title) || this.title === '') &&
Expand Down Expand Up @@ -567,8 +569,9 @@ export const BVTooltip = /*#__PURE__*/ Vue.extend({
getContainer() {
// Handle case where container may be a component ref
const container = this.container ? this.container.$el || this.container : false
const body = document.body
const target = this.getTarget()
const body = getShadowRootOrRoot(target)

// If we are in a modal, we append to the modal, If we
// are in a sidebar, we append to the sidebar, else append
// to body, unless a container is specified
Expand Down
22 changes: 19 additions & 3 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ export const isActiveElement = el => isElement(el) && el === getActiveElement()

// Determine if an HTML element is visible - Faster than CSS check
export const isVisible = el => {
if (!isElement(el) || !el.parentNode || !contains(DOCUMENT.body, el)) {
// Note this can fail for shadow dom elements since they
// are not a direct descendant of document.body
if (!isElement(el) || !el.parentNode || !isConnectedToDOM(el)) {
// Fail for IE11 Shadow DOM. Fixed for all other Browsers
return false
}
if (getStyle(el, 'display') === 'none') {
Expand All @@ -100,6 +99,23 @@ export const isVisible = el => {
return !!(bcr && bcr.height > 0 && bcr.width > 0)
}

// used to grab either the shadow root in a web component or the main document body
export const getShadowRootOrRoot = el => {
if (el.getRootNode == null) {
return DOCUMENT.body
}
const root = el.getRootNode()
if (root.nodeName === '#document') {
return root.body
}
return root
}

export const isConnectedToDOM = el => {
// If node.isConnected undefined then fallback to IE11 compliant check
return el.isConnected == null ? contains(DOCUMENT.body, el) : el.isConnected
}

// Determine if an element is disabled
export const isDisabled = el =>
!isElement(el) || el.disabled || hasAttr(el, 'disabled') || hasClass(el, 'disabled')
Expand Down
Loading