css - 我如何在带作用域的 CSS 的影子 DOM 中使用 Tippy?

标签 css shadow-dom popper.js tippyjs

我想使用 Tippy.js在我的影子 DOM 中。

由于脚本可以访问我的影子 DOM 但样式不能,我尝试在影子 DOM 中内联 Tippy CSS 并在外部链接 Popper 和 Tippy 的 JS。 Here是它不起作用的演示。

我需要 CSS 的作用域,所以我有以下约束:

<style>
    :host {
      all: initial; /* 1st rule so subsequent properties are reset. */
      display: block;
      contain: content; /* Boom. CSS containment FTW. */
      /* any other CSS, so this is where I inlined the Tippy's */
    }
</style>

最佳答案

对于从 google 来到这里并徘徊将 tippy 放入 shadow dom 的当前过程的人们(例如与浏览器扩展一起使用)。以下对我有用:


const shadowRoot = initShadowRoot()

function initShadowRoot() {
    const shadowContainer = document.createElement("div")

    const shadowRoot = shadowContainer.attachShadow({mode: "open"})
    let style = document.createElement('style')
    style.innerText = shadowCss // inline or use a bundler to give you a string representation 

    shadowRoot.appendChild(style)
    document.body.appendChild(shadowContainer)

    return shadowRoot
}

//---
//elsewhere when initializing tippy:

tippy(elment, {
    // ...
    // in shadow dom to avoid affecting the page styles
    appendTo: () => shadowRoot,
    //...
})

关于css - 我如何在带作用域的 CSS 的影子 DOM 中使用 Tippy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611322/

相关文章:

jquery - 如何在悬停时让 Bootstrap 弹出窗口保持事件状态?

javascript - bootstrap 4 中的 popper.js 给出 SyntaxError Unexpected token export

html - 内联响应式表单元素

html - 我想减少导航栏中一个 flex 元素的宽度

TinyMCE 4.9.x 仍然不能在 shadow dom 中工作。有什么想法如何解决吗?

web-component - Web 组件嵌套插槽

html - 图片在我的 Bootstrap div 中没有正确放置

html - 如何在 CSS/HTML 中的菜单项之间留出空间

google-chrome-extension - Bootstrap JS 的替代品在 shadow dom 中不起作用

javascript - 如何让 popper 在不适合视口(viewport)时显示滚动条?