Replies: 16 comments 24 replies
-
That would be really nice! SV or SVELTE would be great. I like the vscode svelte extension - it would be nice to have the same DX. It’s unfortunate that svelte doesn’t have a way to declare props. |
Beta Was this translation helpful? Give feedback.
-
Agreed, I like the idea!
It is an idea worth exploring! I need to become more familiar with the frontend-related Phoenix to comment on more, so correct me if I'm wrong, but to me it sounds like we're exchanging some composability and interactivity with Elixir into JavaScript + Svelte?
Regarding dependencies, I could imagine Deno being a more portable dependency than NodeJS. There also is a Svelte Adapter for Deno. That said, I haven't tried Deno yet, and perhaps NodeJS could be an appropriate starting point for the initial implementation. |
Beta Was this translation helpful? Give feedback.
-
Single letter sigils are fascinating. The fact that the community has avoided conflicts with at most 26*2 characters is testimony to its good nature as well as its relatively small size. Small doesn’t mean bad — advantages include the ability to evoke change without a slow moving committee. |
Beta Was this translation helpful? Give feedback.
-
Can attr/3 define the props for a LiveSvelte component? |
Beta Was this translation helpful? Give feedback.
-
Single letters makes sense, just want to make sure it's not being used by something else yet, but I assume we can find a letter, even if it's not something like I think I need to make a POC and then just see where the pain points are, I'm not even sure we need a way to set attributes, they could be automatically set, just like |
Beta Was this translation helpful? Give feedback.
-
Took a look at it today, it's not so easy, if anyone is more familiar with macro's help is definitely welcome. I'll let it simmer for a bit in the back of my mind :) |
Beta Was this translation helpful? Give feedback.
-
I've got a prototype working, it's pretty cool: defmodule ExampleWeb.LiveSigil do
use ExampleWeb, :live_view
def render(assigns) do
~V"""
<script>
export let number = 5
let other = 1
$: combined = other + number
</script>
<div class="flex flex-col">
<p>This is number: {number}</p>
<p>This is other: {other}</p>
<p>This is other + number: {combined}</p>
<button phx-click="increment">Increment</button>
<button on:click={() => other += 1}>Increment</button>
</div>
"""
end
def mount(_params, _session, socket) do
{:ok, assign(socket, :number, 1)}
end
def handle_event("increment", _value, socket) do
{:noreply, assign(socket, :number, socket.assigns.number + 1)}
end
end Still need to test it in production. To make it work it's pretty straightforward actually, something like this works: defmodule LiveSvelte.Sigil do
@moduledoc false
import Phoenix.Component
@doc false
def get_props(assigns) do
prop_keys =
assigns
|> Map.get(:__changed__)
|> Map.keys()
assigns
|> Enum.filter(fn {k, _v} -> k in prop_keys end)
|> Enum.into(%{})
end
@doc false
defmacro sigil_V({:<<>>, _meta, [string]}, []) do
File.write!("./assets/svelte/_build/#{__CALLER__.module}.svelte", string)
quote do
~H"""
<LiveSvelte.render name={"_build/#{__MODULE__}"} props={get_props(assigns)} />
"""
end
end
end The Svelte file gets picked up and compiled, and we invoke LiveSvelte in the Heex template Still need to refine it more but it's exciting :) I'll also make a video and a blogpost about this. Also, probably need a Svelte syntax highlighter for VS Code and Neovim for the ~V sigil. |
Beta Was this translation helpful? Give feedback.
-
Works in production, version I'll wait on merging this with master until the Svelte hackathon is finished |
Beta Was this translation helpful? Give feedback.
-
Demo: 2.webm |
Beta Was this translation helpful? Give feedback.
-
Since this leverages HEEX I wonder if I can use Phoenix components like <.icon> |
Beta Was this translation helpful? Give feedback.
-
This awesome but I will need to turn off ssr conditionally ie
Another sigil? Is there another way to do this without dropping to HEEX manually? SSR seems like the exception for Phoenix apps. Perhaps it should default to false? |
Beta Was this translation helpful? Give feedback.
-
Can
|
Beta Was this translation helpful? Give feedback.
-
I think I already mentioned this and it’s a minor concern but it’s a bummer that the sigil would require a new vscode extension (like the heex one) instead of being able to use the existing svelte extension which is quite good. That is a reason to use the sigil for smallish amounts of content. |
Beta Was this translation helpful? Give feedback.
-
Version
Documentation can be found in the readme on the Release is scheduled for sometime next week after I finish a new blog post, new video and when Svelte Hack is over. |
Beta Was this translation helpful? Give feedback.
-
Are LiveView stream supported in LiveSvelte? |
Beta Was this translation helpful? Give feedback.
-
Sigils are now in 0.5.0, closing the discussion |
Beta Was this translation helpful? Give feedback.
-
So I had this idea that you can use a Sigil for defining Svelte inside the render function, inspired by https://surface-ui.org/
I think it should be possible, here's the idea:
Before
After
In the above code, you have just the
<LiveSvelte.render name="LogList" props={%{items: @items}} />
inside the render.It would make more sense to include the Svelte code directly in the render like so:
Discussion/thoughts
SVELTE
, or one letter? Which letter?if
statements etc and way better support for Javascript. It doesn't allow you to do elixir things inside the template though.Tagging a couple of people who might be interested:
@dev-guy @gevera @jrasanen @markevans
Beta Was this translation helpful? Give feedback.
All reactions