【问题标题】:CSS: How to target ::slotted siblings in Shadow DOM root?CSS:如何在 Shadow DOM 根目录中定位 ::slotted 兄弟?
【发布时间】:2018-09-15 15:05:52
【问题描述】:

我知道规范目前只允许::slotted 的复合选择器,即::slotted(my-first + my-second) 是不允许的,但是这样的东西应该起作用吗?

::slotted(x-first) + ::slotted(x-second) { /* css */ }

有什么方法可以定位到有槽的兄弟姐妹(除了全局 css)吗?如果没有,我会在哪里提出这样的请求?谢谢。

【问题讨论】:

  • 这与 ::slotted(x-first + x-second) 有何不同?

标签: css web-component shadow-dom native-web-component


【解决方案1】:

放置到插槽中的 DOM 应该由拥有该 DOM 的 CSS 控制,而不是由自定义元素控制。

Web 组件允许对放置在 Slot 中的 DOM 进行非常小的 CSS 控制。几乎只是顶级元素(以及由子节点自动继承的东西。)

这是一个有意识的决定,可能永远不会改变。

【讨论】:

  • 谢谢,我还了解到,在定位 descendents 时,禁止使用 ::slotted 的复杂选择器的决定与性能有关。因此,我希望有一种方法可以定位开槽的 siblings,因为那些 顶级元素。
【解决方案2】:

当然你可以选择siblings of slots / slotted

你可以做的事情是选择一个已被开槽且不是顶级节点的元素。

选择兄弟姐妹:

slot[name=<slotname>] ~ <selector>

选择开槽的顶级节点

::slotted(<compound-selector>)

compound-selector 包含标签/类/id/名称等,但不得包含任何combinators。比如<space>

.myClass好的

<anyTag>[<anyAttribute>[=<anyValue>]]好的

.<myClass> > .<anotherClass>

示例

var element = document.querySelector('.templateMe');
var shadow = element.attachShadow({mode: 'open'});
var template = document.querySelector('.myTemplate');
shadow.appendChild(template.content.cloneNode(true));
<template class="myTemplate">
  <style type="text/css">
    ::slotted([slot=slot1]) { /* slot1 every slotted element - YES */
      color: red;
    }
    
    slot[name=slot1] { /* slot1 itself - YES */
      text-decoration: underline;
    }
    
    slot[name=slot1] + .siblingA { /* slot1 siblingA (direct sibling) - YES */
      color: green;
    }
    
    slot[name=slot1]  ~ .siblingB { /* slot1 siblingB (any sibling) - YES */
      color: orange;
    }
    
    slot[name=slot2]::slotted(.selectMeA) { /* slot2 TOP-LEVEL CHILD (slotted) - YES */
      color: purple;
    }
    
    slot[name=slot2]::slotted(.selectMeB) { /* slot2 NOT TOP-LEVEL CHILD - NO */
      font-weight: bold;
    }
    
    slot[name=slot2]::slotted(.selectMeC[name=myName]) { /* slot2 TOP-LEVEL CHILD (slotted) - YES */
      color: khaki;
    }
    
    slot[name=slot2] + .siblingC { /* slot2 sibling - YES */
      color: blue;
    }
    
  </style>
  <div>
    <slot name="slot1"></slot>
    <div class="siblingA">Sibling A of Slot 1</div>
    <div class="siblingB">Sibling B of Slot 1</div>
  </div>
  <hr/>
  <div>
    <slot name="slot2"></slot>
    <div class="siblingC">Sibling C of Slot 2</div>
  </div>
</template>

<div class='templateMe'>
  <span slot="slot1">Im in Solt 1</span>
  <span slot="slot2" class="selectMeA">
    Im in Solt 2, im selectable.
    <div class='selectMeB'>
      NOT selectable (because no top level node of slotted)!
    </div>
  </span>
  <span slot="slot2" class="selectMeC" name="myName">Im in Solt 2 too and selectable!</span>
</div>

更多here.

开槽元素(来自轻量级 DOM), ::slotted(selector) 允许选择开槽元素本身,但不能选择它们的子元素。

【讨论】:

  • 感谢您的详细回答。在您的示例中,我正在寻找的是:slot[name=slot2]::slotted(.selectMeA + .selectMeC),如果我理解正确,这是不可能的,尽管两者都是顶级孩子。
  • 这是正确的,因为您使用的是 +,它是一个组合器
  • 我试过 ::slotted(.selectMeA) + ::slotted(.selectMeC) 但这也不是很好。 ::slotted 似乎已经结束了。
  • 有人知道这是出于某种原因这样做,还是组合器将来可能会起作用但还没有?我想要他们?
猜你喜欢
  • 2012-05-24
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
相关资源
最近更新 更多