使用 vaadin 的 Web 组件和使用 svelte 的 rollup:主按钮忽略主题属性
也许有人以前尝试过这个并且能够给我一个提示。 我使用了正常的 svelte 设置(在主页中提到)来构建应用程序;
npx degit sveltejs/template my-svelte-project
我想在 Svelte 中使用 vaadin Web 组件。我已经安装了;
npm install @vaadin/vaadin
main.ts 的代码:
<script lang="ts">
import '@vaadin/button/theme/material'
</script>
<main>
<vaadin-button theme="primary">Primary</vaadin-button>
<vaadin-button theme="secondary">Sec</vaadin-button>
</main>
<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
事实是它几乎可以工作:) 按钮已设计样式,我可以单击它们,但是......主题被忽略;
主要应该有一个背景颜色,如文档中所述; https://vaadin.com/docs/latest/ds/components/button /#styles
有什么想法吗???
Maybe someone tried this before and is able to give me a hint.
I have used normal svelte setup (mentioned in the main page) which scaffolds the app;
npx degit sveltejs/template my-svelte-project
I wanted to use vaadin web components in Svelte. I've installed it;
npm install @vaadin/vaadin
the code of main.ts:
<script lang="ts">
import '@vaadin/button/theme/material'
</script>
<main>
<vaadin-button theme="primary">Primary</vaadin-button>
<vaadin-button theme="secondary">Sec</vaadin-button>
</main>
<style>
main {
text-align: center;
padding: 1em;
max-width: 240px;
margin: 0 auto;
}
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
And the thing is that it almost works :) The buttons are styled, I can click on them but... the theme is ignored;
The primary should have a background color like stated in docs;
https://vaadin.com/docs/latest/ds/components/button/#styles
any idea???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为 Svelte 在自定义元素上设置数据的方式。如果元素上存在与您设置的属性同名的属性,Svelte 将设置该属性而不是属性。否则就会回落到属性上。因此,以下...
...被编译为类似的内容:
通常这非常有效,尤其是在设置数组和对象属性时。但是,vaadin-button 样式期望设置
theme
attribute,而不是属性。因为 Svelte 设置了该属性,所以样式不适用。我认为这是 Vaadin 的一个错误 - 如果您公开相同数据的属性和属性,那么消费者设置哪一个并不重要。设置属性应该与设置属性具有相同的效果。解决此问题的一个快速方法是让 vaadin-button 反映主题属性,以便设置
theme
也会设置该属性。下面介绍了如何在 Lit 中执行此操作。但是,这种更改需要组件库作者来实现。作为该库的使用者,您还可以通过使用 action 强制 Svelte 设置属性。
我在 CSS-Tricks,如果您想要更深入的解释。
This is happening because of how Svelte sets data on custom elements. If a property exists on the element with the same name as the attribute you set, Svelte will set the property instead of the attribute. Otherwise, it will fall back to the attribute. So, the following...
...gets compiled to something like:
Normally this works great, especially when setting array and object properties. However, the vaadin-button styles expect the
theme
attribute to be set, not the property. Because Svelte sets the property instead, the styles don't apply.I would argue that this is a Vaadin bug - if you expose an attribute and a property for the same data, it shouldn't matter which one the consumer sets. Setting the property should have the same effect as setting the attribute. A quick way to fix this would be for vaadin-button to reflect the theme property, so that setting
theme
also sets the attribute. Here's how to do that in Lit.However, that change requires the component library authors to implement it. As a consumer of the library, you can also work around this in Svelte by using an action to force Svelte to set the attribute instead.
I wrote an article about this behavior and other workarounds at CSS-Tricks, if you want a more in-depth explanation.
您似乎正在导入 Button 组件的 Material 主题版本。仅当您使用默认 Lumo 主题时,“主要”主题变体才可用。要导入它,请使用
import '@vaadin/button';
对于 Material 主题,您可以使用“outlined”和“contained”主题变体:https://cdn.vaadin.com/vaadin-material-styles/1.3.2/demo/buttons.html
You seem to be importing the Material theme version of the Button component. The "primary" theme variant is only available if you use the default Lumo theme. To import that, use
import '@vaadin/button';
For the Material theme, you can use the "outlined" and "contained" theme variants instead: https://cdn.vaadin.com/vaadin-material-styles/1.3.2/demo/buttons.html