When I am trying to use the below code & dynamically pass the value to render the bar it does not show the svg with color :
below is the code for refrence
<template>
<label>
<strong>Test Bar1</strong>
</label>
<svg height="24" width="400" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grayGradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#f5f5f5" />
<stop offset="100%" stop-color="#e5e5e5" />
</linearGradient>
<linearGradient id="greenGradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#4dcb00" />
<stop offset="100%" stop-color="#286900" />
</linearGradient>
</defs>
<rect fill="url(#grayGradient)" x="0" y="0" width="100%" height="100%" rx="12" ry="12" />
<rect id="greenId" fill="url(#greenGradient)" x="0" y="0" width="0%" height="100%" rx="12" ry="12" />
</svg>
<label>
<strong>Test 2 Bar</strong>
</label>
<svg height="24" width="400" xmlns="http://www.w3.org/2000/svg">
<rect fill="url(#grayGradient)" x="0" y="0" width="100%" height="100%" rx="12" ry="12" />
<rect id="greenId1" fill="url(#greenGradient)" x="0" y="0" width="0%" height="100%" rx="12" ry="12" />
</svg>
</template>
2
Answers
Like commented you cannot have duplicate id values.
Move the defs element out into is own SVG. The SVG should be hidden (here I set the width and the height to
0
). Remove id values from the rect elements in the template and use class names instead.You can use duplicate
id
values in SVGs when you place the SVG in shadowDOM.No need to use LWC – Lightning Web Components for that
A native Web Component
<gradient-bar>
to create the SVG is: