虽然在服务器/JVM 端编写所有东西看起来很诱人,但最终会在代码中乱扔样式操作。
通常在应用程序的实际样式中设置类似的东西会更好。这是一个现在做的例子(使用 v13 beta 2,代码是 Groovy - 带走的只是为元素添加一个主题)。
<dom-module id="vertical-layout-different-spacing" theme-for="vaadin-vertical-layout">
<template>
<style>
:host([theme~="spacing"][theme~="xs"]) ::slotted(*) {
margin-top: 8px;
}
:host([theme~="spacing"][theme~="xl"]) ::slotted(*) {
margin-top: 32px;
}
</style>
</template>
</dom-module>
def demoContent = { theme ->
new VerticalLayout(
*[1, 2, 3].collect{ new Div(new Text("Text $it")) }
).tap {
element.themeList.add(theme)
}
}
content.add(
// styles `xs` and `xl` are defined in the style override
demoContent('xs'),
demoContent('m'),
demoContent('xl'),
)
如果您正在使用 Lumo 并且您已经在 v13 上,则有一个主题的 compact 变体,如果这就是您所追求的:
https://vaadin.com/releases/vaadin-13#compact-theme
如果您使用的是 Material 主题,那么已经内置了对不同间距的支持。见https://cdn.vaadin.com/vaadin-material-styles/1.2.0/demo/ordered-layouts.html;要添加的主题的名称是例如spacing-xl