Svelte inside Phoenix LiveView with seamless end-to-end reactivity
LiveSvelte is a library that combines the power of Phoenix LiveView and Svelte to enable easy client-side state management and seamless end-to-end reactivity in server-rendered HTML. By leveraging Phoenix LiveView’s websocket communication, LiveSvelte allows for real-time updates to the DOM without the need to write client-side JavaScript code. This library also provides support for server-side rendering (SSR) using Node.js and offers features such as Sigil as an alternative LiveView DSL, Svelte preprocessing support, Tailwind CSS integration, and more.
To install LiveSvelte, follow these steps:
live_svelte
to the list of dependencies in your mix.exs
file:defp deps do
[
...
{:live_svelte, "~> 0.5"},
...
]
end
setup
and assets.deploy
aliases in your mix.exs
file:defp setup do
[
{:commands, ["app.setup", "assets.link", "ecto.setup", "live_svelte.setup"]},
{:atomic, true}
]
end
defp assets.deploy do
[
{:copy_static_ass"
4. Add the `import LiveSvelte` statement in the `html_helpers/0` function inside `/lib/<app_name>_web.ex`:
```elixir
def html_helpers do
quote do
...
import LiveSvelte
...
end
end
(Optional) For Tailwind support, add "./svelte/**/*.svelte"
to the content
array in the tailwind.config.js
file.
Remove the esbuild configuration from the config/config.exs
file and remove the esbuild
dependency from the deps
function in your mix.exs
file.
LiveSvelte is a powerful library that combines the benefits of Phoenix LiveView and Svelte to create reactive and dynamic user interfaces in server-rendered HTML. With seamless end-to-end reactivity, server-side rendering support, and a range of other features such as Sigil DSL, Svelte preprocessing, and Tailwind integration, LiveSvelte offers a comprehensive solution for building interactive web applications. The easy installation process and compatibility with Phoenix LiveView make LiveSvelte a convenient choice for developers looking to enhance their LiveView projects with client-side state management and advanced frontend capabilities.