vanilla JS webcomponent 的用户样式

User's style for vanilla JS webcomponent

我有这个组件:

index.html

<html>

<head>
    <title></title>
    <meta charset="UTF-8">
    <script src="clock.js"></script>
    <style>
        clock-digital div {
            background-color: green;
        }
    </style>
</head>

<body>
    <clock-digital></clock-digital>
</body>

</html>

clock.js

customElements.define('clock-digital', class extends HTMLElement {

    constructor() {
        super();
        var shadowRoot = this.attachShadow({
            mode: 'open'
        });
        this._clockID = setInterval(function () {
            var currentdate = new Date();
            var hours = ( (currentdate.getHours() < 10) ? '0' : '') + currentdate.getHours();
            var minutes = ( (currentdate.getMinutes() < 10) ? '0' : '') + currentdate.getMinutes();
            var seconds = ( (currentdate.getSeconds() < 10) ? '0' : '') + currentdate.getSeconds();
            shadowRoot.innerHTML = `
            <style>
                div {
                    display: inline-block;
                    width: 65px;
                    text-align: center;
                    background-color: whitesmoke;
                    font-style: italic;
                    border: 1px solid lightgray;
                    border-radius: 3px;
                    box-shadow: 2px 2px 3px;
                }
            </style>
            <div>
                ${hours}:${minutes}:${seconds}
            </div>`;
        }, 500);
    }

});

我希望组件的用户可以在时钟上定义他的样式。我试过:

<style>
        clock-digital div {
            background-color: green;
        }
    </style>

但它不起作用。我应该在 shadow root 的某个地方使用插槽标签吗?实现该目标的最佳做法是什么?

您可以在可以在外部设置的自定义元素中公开 CSS properties

在您的示例中,您的元素可以定义 --clock-background-color,它设置 div 的背景颜色:

shadowRoot.innerHTML = 
  `<style>
     div {
       background-color: var(--clock-background-color, whitesmoke);
       /* ... */
     }
   </style>
   <div>
     ${hours}:${minutes}:${seconds}
   </div>`;

然后,您元素的用户可以通过以下方式将背景颜色更改为绿色:

<style>
  clock-digital {
    --clock-background-color: green;
  }
</style>
<clock-digital></clock-digital>

codepen

请注意,Codepen 演示对非 Chrome 浏览器使用 Web Components polyfill,但您可以将其注释掉以查看它在 Chrome 中仍然可以正常工作。

您还可以利用用户定义的 CSS 规则优先于 :host 选择器,代表影子 DOM 根(即自定义元素本身)。

在您的自定义元素中,使用 :host 设置内容样式:

shadowRoot.innerHTML = `
    <style>
        :host {
            display: inline-block;
            width: 65px;
            text-align: center;
            background-color: whitesmoke;
            font-style: italic;
            border: 1px solid lightgray;
            border-radius: 3px;
            box-shadow: 2px 2px 3px;
        }
    </style>
    <div>
        ${hours}:${minutes}:${seconds}
    </div>`

现在用户可以使用标准 CSS 符号定义自己的样式,就像任何 HTML 元素一样。这将覆盖 :host 规则定义的样式。

<style>
     clock-digital {
         background-color: green;
         color: white;
     }
</style>

customElements.define('clock-digital', class extends HTMLElement {
    constructor() {
        super();
        var shadowRoot = this.attachShadow({ mode: 'open' });
        this._clockID = setInterval(function () {
            var currentdate = new Date();
            var hours = ('0'+currentdate.getHours()).substr(-2,2)
            var minutes = ('0'+currentdate.getMinutes()).substr(-2,2)
            var seconds = ('0'+currentdate.getSeconds()).substr(-2,2) 
            shadowRoot.innerHTML = `
            <style>
                :host {
                    display: inline-block;
                    width: 65px;
                    text-align: center;
                    background-color: whitesmoke;
                    font-style: italic;
                    border: 1px solid lightgray;
                    border-radius: 3px;
                    box-shadow: 2px 2px 3px;
                }
            </style>           
            ${hours}:${minutes}:${seconds}`;
        }, 500);
    }
});
clock-digital  {
  background-color: green;
  color: white;
  font-weight: bold;
}
<clock-digital></clock-digital>