web-component - Shadow 元素的全局 CSS 效果。为什么?

标签 web-component shadow-dom custom-element google-web-component native-web-component

两种 CSS(一种在 light dom 中,一种在 shadown dom 中)对标签的影响。
Name 和 Phone 也受全局 CSS at 和 Shadow Scope CSS 影响!!!
为什么 ?????我不要它。
我预计它们只是受到模板范围内的 Sahdow 范围 CSS 的影响。
我希望我能从你那里得到一些想法。

https://plnkr.co/edit/Asr1S1UFvhmhtZeWm5k8

//CREATE CUSTOM ELEMENT
var MyContactProtype = Object.create(HTMLElement.prototype);
MyContactProtype.createdCallback = function() {
  //retrieve template
  var tpl = document.querySelector('#contact-form-tpl');
  var tpl_ct = document.importNode(tpl.content, true);

  //this <=> my-contact element -> create shadow
  var shadowRoot = this.createShadowRoot();

  //show template in shadow DOM
  shadowRoot.appendChild(tpl_ct);
};

//REGISTER CUSTOM ELEMENT
document.registerElement("my-contact", {
  prototype: MyContactProtype
});
span {/* global CSS*/
  text-decoration: line-through
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/document-register-element/1.11.0/document-register-element.js"></script>
<span>HELLO</span>
<!--line through by css at LINE 6: OK-->
<my-contact>
  <h1 class="header-contact-form">Contact X</h1>
  <span class="name">this is my name</span>
  <span class="phone">this is my phone</span>
</my-contact>


<template id="contact-form-tpl">
  <style>
    span {/* shadow scope CSS*/
      text-decoration: underline
    }
  </style>
  <fieldset>
    <legend>
      <content select="h1.header-contact-form"></content>
      <!--
      Name and Phone are effected by CSS at line 6 and 21 too!!!
      WHY ????? I dont want it.
      I expected they are just effeted by CSS line 21 which is in template scope.
      -->
      <div>
        Name: <span><content select="span.name"></content></span>
      </div>
      <div>
        Phone: <content select="span.phone"><span></span></content>
      </div>
      <span>TEST</span><!-- only apply css at line 21: OK-->
    </legend>
  </fieldset>
</template>

最佳答案

这是 CSS 样式的正常行为。使用 <content> 插入到 Shadow DOM 中的元素(在您的示例中:<span class=name>...</span>)实际上是普通 DOM 的一部分,因此受到全局 CSS 样式的影响。

如果你不想要它,你应该尝试另一种技术,比如将 light DOM 元素复制到 Shadow DOM 而不是使用 <content> .

此外,您应该使用 Custom Elements v1 (和 customElements.define() )和 Shadow DOM v1 (和 <slot> )而不是已弃用的自定义元素 v0 ( registerElement() ) 和 Shadow DOM v0 ( <content> )。

使用 Shadow DOM v1,您可以使用 ::slotted() 在 Shadow DOM 中选择和设置插入元素的样式。

然后您可以在 Shadow DOM 中重载 CSS 规则 <style>!important修饰符:

第 21 行:

<style>
    ::slotted( span ) {
      text-decoration: underline!important
    }
</style>

下面是完整的片段:

//CREATE CUSTOM ELEMENT
class MyContact extends HTMLElement {
  constructor() {
     super()
    //retrieve template
    var tpl = document.querySelector('#contact-form-tpl');
    var tpl_ct = document.importNode(tpl.content, true);

    //this <=> my-contact element -> create shadow
    var shadowRoot = this.attachShadow( {mode:'open'}) //createShadowRoot();

    //show template in shadow DOM
    shadowRoot.appendChild(tpl_ct);
  }
}

//REGISTER CUSTOM ELEMENT
customElements.define("my-contact", MyContact);
span {
  text-decoration: line-through
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/document-register-element/1.11.0/document-register-element.js"></script>
<span>HELLO</span>
<!--line through by css at LINE 6: OK-->
<my-contact>
  <h1 slot=header>Contact X</h1>
  <span slot=name>this is my name</span>
  <span slot=phone>this is my phone</span>
</my-contact>


<template id="contact-form-tpl">
  <style>   
    span ::slotted( span ), 
    span { 
        text-decoration:underline!important  
    }
  </style>
  <fieldset>
    <legend>
      <slot name=header></slot>
      <div>
        Name: <span><slot name="name"></slot></span>
      </div>
      <div>
        Phone: <slot name="phone"><span></span></slot>
      </div>
      <span>TEST</span><!-- only apply css at line 21: OK-->
    </legend>
  </fieldset>
</template>

关于web-component - Shadow 元素的全局 CSS 效果。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51354449/

相关文章:

javascript - 使用 Chrome DevTools 将鼠标悬停在自定义元素上时,自定义元素不会被标记

javascript - 合成 LWC Shadow DOM 插槽与原生 shadow DOM 插槽

javascript - 使用JS输入文字,在一个文本框中输入文字,已经输入的值被删除

javascript - 从 React 中的自定义元素修改属性

javascript - CSS有没有可能逃脱shadow dom(Polymer)?

html - 为什么我的自定义元素在使用 Polymer 时没有显示?

Angular 7 + ShadowDOM - @angular/material 皮肤的加载策略

html - 有没有办法在 Shadow-DOM 中访问 CSS 中的 HTML 标签属性?

javascript - CSS 渲染后自定义元素中是否有回调?

javascript - Polymer:使用不同 View 模型多次使用元素