我是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?
发布评论
评论(1)
这个问题有点广泛,但是我将尝试解决单独的部分,并解释我如何完成它们。由于问题询问了按钮的实现详细信息,因此有许多有效的答案。我专注于一个最小的答案,该答案解决了您的问题中的每个点。有一些文档链接以进行进一步阅读。
有关LIT游乐场中完成的现场演示,请参阅: https://lit.dev/playgrount/#gist = b630a85e7f9ff2db5300b5300b42f2f2f2f2f2f2f7a43f4
在实时链接中,您会找到
code> cody> cutam> custom-code-code> lit elements的列表标签。
display
属性有条件地渲染'ot''
,'svg'
,或'label'
。还有一个禁用
属性,可以禁用按钮并更改样式。让我们分解。
使用反应性属性我们可以宣布显示什么按钮,以及是否显示公共属性被禁用。
在
custom-btn
类的上下文中,声明这些属性如下所示:渲染方法可以使用它们有条件地渲染命名插槽,并且还传播是否应使用
使用
来禁用该按钮?按钮
,声明href =“ https://lit.dev/docs/templates/express/w.boolean-attribute-expressions” rel =“ nofollow noreferrer”> boolean属性 问题。
为了添加样式,可以像您在问题中提到的那样选择插槽内容:
当启用按钮时,这将使所有插入的内容染色,并为所有插入的内容涂上蓝色,如果主机元素具有
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. Thedisplay
attribute conditionally renders either'both'
,'svg'
, or'label'
. And there is also adisabled
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: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:This addresses the (non-styling) functionality in your question.
To add styles, the slotted contents can be selected like you mentioned in the question:
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 thehost
, 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!