Logo

dev-resources.site

for different kinds of informations.

Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView

Published at
12/4/2024
Categories
elixir
erlang
phoenix
liveview
Author
herminiotorres
Categories
4 categories in total
elixir
open
erlang
open
phoenix
open
liveview
open
Author
14 person written this
herminiotorres
open
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView

Inspired by a conversation on Bsky (Thanks Netto) about the release of Phoenix LiveView 1.0.

The release of Phoenix LiveView 1.0 was announced yesterday! Hooorayyy! 🎉🥳

With numerous improvements aimed at simplifying the framework’s usage. One of the major enhancements stand out: the move from <%= %> to {} for rendering templates.

And I missed one more enhancements in terms of ergonomics when is calling components. Calling functional components in Phoenix LiveView is straightforward and seamless, making the developer experience intuitive. However, when dealing with live components, the syntax and approach differ significantly. This inconsistency can disrupt the ergonomics of the framework, especially for newcomers or teams striving for code simplicity and uniformity.

Here’s an example to ensure we’re on the same page and to illustrate the topic I’d like to explore further in this blog post.

Function Component

defmodule MyComponent do
  # In Phoenix apps, the line is typically: use MyAppWeb, :html
  use Phoenix.Component

  def greet(assigns) do
    ~H"""
    <p>Hello, {@name}!</p>
    """
  end
end
Enter fullscreen mode Exit fullscreen mode
<MyComponent.greet name="Jane" />
Enter fullscreen mode Exit fullscreen mode

Live Component

defmodule HeroComponent do
  # In Phoenix apps, the line is typically: use MyAppWeb, :live_component
  use Phoenix.LiveComponent

  def render(assigns) do
    ~H"""
    <div class="hero">{@content}</div>
    """
  end
end
Enter fullscreen mode Exit fullscreen mode
<.live_component module={HeroComponent} id="hero" content={@content} />
Enter fullscreen mode Exit fullscreen mode

I'll present a solution, hack—that bridges, enabling a more cohesive and developer-friendly way to invoke live components. By aligning the ergonomics of both functional and live components.

defmodule HeroComponent do
  # In Phoenix apps, the line is typically: use MyAppWeb, :live_component
  use Phoenix.LiveComponent

+  def call(assigns) do
+    ~H"""
+    <.live_component module={__MODULE__} {assigns}/>
+    """
+  end

  def render(assigns) do
    ~H"""
    <div class="hero">{@content}</div>
    """
  end
end
Enter fullscreen mode Exit fullscreen mode

Here’s how you can call it:

<HeroComponent.call id="hero" content={@content} />
Enter fullscreen mode Exit fullscreen mode

The next step for this implementation is to encapsulate it in a macro, allowing you to reuse it wherever needed.

elixir Article's
30 articles in total
Favicon
A RAG for Elixir in Elixir
Favicon
Enhancing Elixir Development in LazyVim: Quick Documentation Access by Telescope
Favicon
Learning Elixir: Control Flow with If and Unless
Favicon
Pseudolocalization in Phoenix with gettext_pseudolocalize
Favicon
The Journey of Optimization
Favicon
How to use queue data structure in programming
Favicon
Phoenix LiveView, hooks and push_event: json_view
Favicon
🥚 Crack Open These 20+ Elixir Goodies
Favicon
Learning Elixir: Understanding Atoms, Booleans and nil
Favicon
Unlocking the Power of Elixir Phoenix and Rust: A Match Made for High-Performance Web Applications
Favicon
Elixir: Concurrency & Fault-Tolerance
Favicon
Enhancements to dbg in elixir 1.18
Favicon
Learning Elixir: Working with Strings
Favicon
Leverage ETS for Shared State in Phoenix
Favicon
Elixir em Foco em 2024
Favicon
Building HTTP/JSON API In Gleam: Introduction
Favicon
Phoenix
Favicon
Sql commenter with postgrex
Favicon
Learning Elixir: Understanding Numbers
Favicon
For loops and comprehensions in Elixir - transforming imperative code
Favicon
Phoenix LiveView is slot empty?
Favicon
Customizing IEx: Personalizing Your Elixir Shell Environment
Favicon
Masters of Elixir: A Comprehensive Collection of Learning Resources
Favicon
Leveraging GenServer and Queueing Techniques: Handling API Rate Limits to AI Inference services
Favicon
Chekhov's gun principle for testing
Favicon
Solving Advent Of Code 2024 on a elixir project
Favicon
Bridging the Gap: Simplifying Live Component Invocation in Phoenix LiveView
Favicon
Harness PubSub for Real-Time Features in Phoenix Framework
Favicon
Debugging with dbg: Exploring Elixir's Built-in Debugger
Favicon
New to dev.to and Excited to Share ProxyConf: My Elixir-Powered API Control Plane

Featured ones: