【发布时间】:2021-12-04 12:14:23
【问题描述】:
我正在使用 vaadin flow v21。我喜欢创建一个使用自己的 svg 图标集的自定义组件。我尝试根据 vaadin-icon 创建集合,但 svg 定义不会被复制到影子根目录中。
我做了以下
- 创建了一个从
com.vaadin.flow.component.icon.Icon派生的CustomIcon组件类 - 添加了一个
JSModule,它将包含作为聚合物模板设置的新图标。
自定义组件类
@JsModule("./icons/custom-iconset-svg.js")
public class CustomIcon extends Icon {
public CustomIcon(String collection, String icon) {
super(collection,icon);
}
}
文件“custom-iconset-svg.js”
import '@vaadin/vaadin-icon/vaadin-iconset.js';
import '@vaadin/vaadin-icon/vaadin-icon.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<vaadin-iconset-svg name="custom" size="16">
<svg>
<defs>
<vaadin-iconset name="vaadin" size="16">
<svg><defs>
<g id="custom:abacus"><path d="..."></path></g>
</defs>
</svg>
</vaadin-iconset-svg>`;
document.head.appendChild($_documentContainer.content);
使用新的“CustomIcon”类
Icon icon = new CustomIcon("custom","abacus")
add(icon);
这稍后会在以下 html 元素上创建
- 它在
<head>部分添加了自定义图标集<vaadin-iconset-svg name="custom" ... > - 创建元素
<vaadin-icon icon="custom:abacus">
问题在于该新元素的 shadowroot 的 <svg> 部分为空。所以不确定我在这里错过了什么?
<vaadin-icon style="width: var(--lumo-icon-size-s); height: var(--lumo-icon-size-s); margin-right: var(--lumo-space-s);" icon="custom:abacus">
#shadow-root
<style>...</<style>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
<!---->
</svg>
</vaadin-icon>
【问题讨论】:
-
即使内置图标使用
id="vaadin:abacus",我还是会尝试创建一个没有附加前缀的自定义图标集,如id="abacus"。至少 Lumo 图标集是这样定义的:github.com/vaadin/web-components/blob/master/packages/…
标签: javascript java svg polymer vaadin-flow