使用点亮元素的按钮Web组件

发布于 2025-02-12 17:46:48 字数 498 浏览 0 评论 0 原文

我是WebComponents的初学者,并且正在使用TypeScript点亮来构建自定义按钮Web组件。

这些规格要求我将< svg> 和a < p> 标记作为自定义组件中的孩子。例如:

<my-button>
   <svg slot="svg">...</svg>
   <p slot="label">sample text</p>
</my-button>

在我的自定义组件中,我有条件地渲染孩子。 SVG或标签或两者都将在条件下呈现。

为了造型开槽的元素,我尝试使用::插槽(p){color:white}:

我也想更改SVG的填充,这些样式应在自定义组合样式内部进行处理。但是尚未设定样式。

我还想将不同的颜色设置为&lt; p&gt; ,具体取决于禁用或ot。我该如何实现?

I am a beginner to webcomponents and I am using lit with TypeScript to build a custom button web component.

The specifications require me to render a <svg> and a <p> tag as children in the custom component. Eg:

<my-button>
   <svg slot="svg">...</svg>
   <p slot="label">sample text</p>
</my-button>

In my custom component, I am conditionally rendering the children.i:e; either svg or label or both will rendered depeding on a condition.

For styling the slotted elements, I tried using ::slotted(p){colour:white}:

I also want to change the fill of svg and these styles are to be handled inside the custom componenet style itself. But the styles are not being set.

I also want to set different colours to the <p> depending on wether the button is disabled or ot. How can i achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2025-02-19 17:46:48

这个问题有点广泛,但是我将尝试解决单独的部分,并解释我如何完成它们。由于问题询问了按钮的实现详细信息,因此有许多有效的答案。我专注于一个最小的答案,该答案解决了您的问题中的每个点。有一些文档链接以进行进一步阅读。

有关LIT游乐场中完成的现场演示,请参阅: https://lit.dev/playgrount/#gist = b630a85e7f9ff2db5300b5300b42f2f2f2f2f2f2f7a43f4

在实时链接中,您会找到 code> cody> cutam> custom-code-code> lit elements的列表标签。 display 属性有条件地渲染'ot'''svg',或'label'。还有一个禁用属性,可以禁用按钮并更改样式。

让我们分解。

使用反应性属性我们可以宣布显示什么按钮,以及是否显示公共属性被禁用。

custom-btn 类的上下文中,声明这些属性如下所示:

...
  @property()
  display: 'svg' | 'label' | 'both' = 'both'
      
  @property({type: Boolean})
  disabled = false;

渲染方法可以使用它们有条件地渲染命名插槽,并且还传播是否应使用使用来禁用该按钮?按钮,声明

...
  render() {
    return html`
        <button ?disabled=${this.disabled}>
          ${this.display === 'svg' || this.display === 'both'
                ? html`<slot name="svg"></slot>`
                : null}
          ${this.display === 'label' || this.display === 'both'
                ? html`<slot name="label"></slot>`
                : null}
        </button>
        `;
  }

href =“ https://lit.dev/docs/templates/express/w.boolean-attribute-expressions” rel =“ nofollow noreferrer”> boolean属性 问题。

为了添加样式,可以像您在问题中提到的那样选择插槽内容:

  static styles = css`
    ::slotted(*) {
      color: red;
    }
    :host([disabled]) ::slotted(*) {
      color: blue;
    }
  `

当启用按钮时,这将使所有插入的内容染色,并为所有插入的内容涂上蓝色,如果主机元素具有 nas code> disabled 布尔属性。

注意: > 选择所有插入的内容。

and :host [disabled] 选择 host ,即, custom-btn 具有残疾属性时的情况在上面。因此,当遇到&lt; custom-btn禁用时,选择器会激活。

为了使SVG填充颜色继承并使用CSS的颜色,SVG使用特殊关键字很重要: CurrentColor 。注意在实时repro: fill =“ CurrentColor” 设置在SVG元素上。

希望这可以解除您的按钮实现!

This question is a bit broad, but I'll try to address the separate pieces and explain how I accomplished them. Because the question asks about the implementation details of a button, there are many valid answers. I've focused on a minimal answer that addresses each point in your question. There are links to documentation for further reading.

For a finished live demo in the Lit playground, please see: https://lit.dev/playground/#gist=b630a85e7f9ff2db5300b42f2f7a43f4

In the live link, you'll find a list of custom-btn lit elements which take an svg and label. The display attribute conditionally renders either 'both', 'svg', or 'label'. And there is also a disabled attribute that disables the button and changes the styles.

Let's break it down.

Using reactive properties we can declare public attributes for what to display, and whether the button is disabled.

In the context of the custom-btn class, declaring these properties looks like this:

...
  @property()
  display: 'svg' | 'label' | 'both' = 'both'
      
  @property({type: Boolean})
  disabled = false;

The render method can use these to conditionally render the named slots, and also propagate whether the button should be disabled using ?button, a declarative boolean attribute expression:

...
  render() {
    return html`
        <button ?disabled=${this.disabled}>
          ${this.display === 'svg' || this.display === 'both'
                ? html`<slot name="svg"></slot>`
                : null}
          ${this.display === 'label' || this.display === 'both'
                ? html`<slot name="label"></slot>`
                : null}
        </button>
        `;
  }

This addresses the (non-styling) functionality in your question.

To add styles, the slotted contents can be selected like you mentioned in the question:

  static styles = css`
    ::slotted(*) {
      color: red;
    }
    :host([disabled]) ::slotted(*) {
      color: blue;
    }
  `

This will color all slotted contents red when the button is enabled, and color all slotted contents blue if the host element has the disabled boolean attribute.

Note: ::slotted(*) selects all slotted contents.

And :host[disabled] selects for the case when the host, i.e., custom-btn has a disabled attribute on it. So the selector activates when <custom-btn disabled> is encountered.

To make the SVG fill color inherit and use the color from CSS, it's important the SVG uses the special keyword: currentColor. Note in the live repro: fill="currentColor" is set on the svg elements.

Hope this unblocks your button implementation!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文