Supposed that I have the following SVG:
<svg xmlns="http://www.w3.org/2000/svg" width="237" height="129" viewBox="0 0 237 129" fill="none">
<path d="M228 1H9C4.58172 1 1 4.58172 1 9V91V104V127L20 111.483L228 112C232.418 112 236 108.418 236 104V9C236 4.58172 232.418 1 228 1Z" fill="#F6F6F6" />
<path d="M1 104V9C1 4.58172 4.58172 1 9 1H228C232.418 1 236 4.58172 236 9V104C236 108.418 232.418 112 228 112L20 111.483L1 127V91" stroke="#E8E8E8" />
<foreignObject x="15" y="0" width="200" height="130">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fringilla quam eu faci lisis mollis. </p>
</foreignObject>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="235" height="86" viewBox="0 0 235 86" fill="none">
<path d="M8 0H227C231.418 0 235 3.58172 235 8V50V63V86L216 70.4828L8 71C3.58173 71 0 67.4183 0 63V8C0 3.58172 3.58173 0 8 0Z" fill="#5DB075"/>
<foreignObject x="15" y="0" width="200" height="80">
<p style="color: white">Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
</foreignObject>
</svg>
I was thinking to make the height dynamic based on text length.
For example the first SVG would be something like:
<svg xmlns="http://www.w3.org/2000/svg" width="237" height={50 + (27 * text.length / 27)} viewBox={`0 0 237 ${50 + (27 * text.length / 27)}`} fill="none">
<path d="M228 1H9C4.58172 1 1 4.58172 1 9V91V104V127L20 111.483L228 112C232.418 112 236 108.418 236 104V9C236 4.58172 232.418 1 228 1Z" fill="#F6F6F6" />
<path d="M1 104V9C1 4.58172 4.58172 1 9 1H228C232.418 1 236 4.58172 236 9V104C236 108.418 232.418 112 228 112L20 111.483L1 127V91" stroke="#E8E8E8" />
<foreignObject x="15" y="15" width="150" height={20 + (27 * text.length / 27)}>
<p>{text}</p>
</foreignObject>
</svg>
And the second SVG would be something like:
<svg xmlns="http://www.w3.org/2000/svg" width="235" height={50 + (27 * text.length / 27)} viewBox={`0 0 237 ${50 + (27 * text.length / 27)}`} fill="none">
<path d="M8 0H227C231.418 0 235 3.58172 235 8V50V63V86L216 70.4828L8 71C3.58173 71 0 67.4183 0 63V8C0 3.58172 3.58173 0 8 0Z" fill="#5DB075" />
<foreignObject x="15" y="15" width="150" height={20 + (27 * text.length / 27)}>
<p style="color: white">{text}</p>
</foreignObject>
</svg>
Current problem with this approach is, the path D wasn’t manipulated to be reflecting the actual height.
For example in snippet if text was short would look something like:
<svg xmlns="http://www.w3.org/2000/svg" width="237" height="50" viewBox="0 0 237 50" fill="none">
<path d="M228 1H9C4.58172 1 1 4.58172 1 9V91V104V127L20 111.483L228 112C232.418 112 236 108.418 236 104V9C236 4.58172 232.418 1 228 1Z" fill="#F6F6F6" />
<path d="M1 104V9C1 4.58172 4.58172 1 9 1H228C232.418 1 236 4.58172 236 9V104C236 108.418 232.418 112 228 112L20 111.483L1 127V91" stroke="#E8E8E8" />
<foreignObject x="15" y="0" width="200" height="130">
<p>test </p>
</foreignObject>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="235" height="50" viewBox="0 0 235 50" fill="none">
<path d="M8 0H227C231.418 0 235 3.58172 235 8V50V63V86L216 70.4828L8 71C3.58173 71 0 67.4183 0 63V8C0 3.58172 3.58173 0 8 0Z" fill="#5DB075"/>
<foreignObject x="15" y="0" width="200" height="80">
<p style="color: white">test </p>
</foreignObject>
</svg>
Is there any better way to update the SVG shape dynamically based on text length?
2
Answers
So, here one solution to mimic the style in html/css. You can adjust the css to match your liking: colors and bubble shapes and so on…
Standard HTML/CSS way: