【问题标题】:Global CSS effect on Shadow element. Why?阴影元素的全局 CSS 效果。为什么?
【发布时间】:2018-07-16 04:05:41
【问题描述】:

两种 CSS(一种在 light dom 中,一种在 shadown dom 中)对标签都有影响。
姓名和电话受全局 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>

【问题讨论】:

  • this.createShadowRoot is not a function 在最新的 FF 中。

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


【解决方案1】:

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

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

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

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

然后,您可以使用 !important 修饰符重载 Shadow DOM &lt;style&gt; 中的 CSS 规则:

第 21 行:

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

下面是完整的sn-p:

//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>

【讨论】:

  • 感谢 Supersharp,这是我收到的最佳答案。我现在要深入研究自定义元素 v1。
猜你喜欢
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2014-10-27
  • 2015-02-17
相关资源
最近更新 更多