Closed leolorenzoluis closed 2 years ago
Don't remember exactly why but Lit requires you to use svg
instead of html
to render templates within SVG: https://lit.dev/docs/api/templates/#svg
With Fable.Lit you just need to replace html
with svg
. The tricky part is that when rendering the outer <svg></svg>
element is OK to use html
but not with a nested template. So in your example above you need to change the first html
in createBar
but not the one in chart()
. There's a sample in this repo: https://github.com/fable-compiler/Fable.Lit/blob/6317d3a32ed91927984b1fc03aa7da3fc02affa3/sample/Clock.fs#L99-L114
Note that when creating the SVG element html
is used: https://github.com/fable-compiler/Fable.Lit/blob/6317d3a32ed91927984b1fc03aa7da3fc02affa3/sample/Clock.fs#L177-L194
Sorry this is a bit confusing, it's like this in the Lit library and I didn't find a way to circumvent it.
No worries, still trying to grok web components too. Is this related? https://twitter.com/rich_harris/status/1141831377828102145?lang=en
For future readers here's the solution:
let createBar index value =
let ratio = 240 / 10
svg $"""
<rect
fill="red"
x={( 320 / 10 ) * index}
y={240 - ( value * ratio )}
width={320 / 10}
height="{value * ratio}"/>
"""
let chart () =
html $"""
<svg width="320" height="240">
{
seq { 1..11 } |> Seq.mapi(createBar)
}
</svg>
"""
Oh, maybe... I thought Lit templates were independent of web components, but apparently in this case they're related. This is what the Lit documentation says:
I am not sure if this is Lit related or just Fable, but given the following code:
It seems that the
<rect>
is not being rendered properly. I can't seem to find any difference between the text, but when I try to edit the HTML and just remove either the space in between<rect
> or something then the rect renders.It's being called from a HookComponent
I tried changing
Seq.mapi
toLit.mapUnique
but that didn't make a difference.Also when I try to set a breakpoint on
chart
function then the function is called twice? Is that expected? Is it due to some lifecycle?