I am using vaadin flow v21. I like to create a custom component which is using an own svg icon set. I tried to create the set based on the vaadin-icon but the svg definition will not be copied over into the shadow root.
I did the following
- Created a
CustomIconComponent class which is derived fromcom.vaadin.flow.component.icon.Icon - added a
JSModulewhich will contain the new icon set as a polymer template.
Custom Component Class
@JsModule("./icons/custom-iconset-svg.js")
public class CustomIcon extends Icon {
public CustomIcon(String collection, String icon) {
super(collection,icon);
}
}
File "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);
Usage of the new "CustomIcon" Class
Icon icon = new CustomIcon("custom","abacus")
add(icon);
This creates later on the following html elements
- It adds in the
<head>section the custom set of icons<vaadin-iconset-svg name="custom" ... > - Creates the element
<vaadin-icon icon="custom:abacus">
The issue is that the <svg> section of the shadowroot for that new element is empty. So not sure what I missed here?
<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>