woutdp / live_svelte

Svelte inside Phoenix LiveView with seamless end-to-end reactivity
https://hexdocs.pm/live_svelte
MIT License
1.01k stars 38 forks source link

Allow for setting of the attributes directly instead of using props keyword in the component #17

Closed woutdp closed 1 year ago

woutdp commented 1 year ago

Current:

<LiveSvelte.render name="SomeComponent" props={%{number: @number}} />

New:

<LiveSvelte.render name="SomeComponent" number={@number} />

We can probably use @rest for this. We should still allow props as that can be a way to add keywords that are taken such as the class and name keyword (or even the props keyword, which is very meta)

woutdp commented 1 year ago

Tried it out and it's working but it gives a warning when providing an attribute that is not specifically defined.

code: https://github.com/woutdp/live_svelte/tree/nicer-prop-passing

image

If we can make the warning disappear somehow we can add it.

terrisgit commented 1 year ago

This is exactly what I've been asking you regarding props. Just saw this. This will be very useful enhancement.

woutdp commented 1 year ago

I think this issue is useful, but also wanted to mention a third way of defining props.

What if you could do something like this in the mount:

{:ok, assign(socket, :svelte, %{number: 1})}

And then the prop number would be automatically picked up since we know that's what we want to pass

<LiveSvelte.render name="Example"/>

Actually we can probably even get away with the following:

{:ok, assign(socket, :number, 1)}

And make sure it's automatically picked up, but then we'd have to deprecate the previous way I think.

One thing is that let's say you have 2 Svelte components in one LiveView, you might only want to pass certain things to one of them and certain things to the other, don't know if it's really an issue though

Something to think about

terrisgit commented 1 year ago

That is better. I think the sigil will become the preferred method so whatever works with that will be my go to.

woutdp commented 1 year ago

Will be closed by: https://github.com/woutdp/live_svelte/pull/50