Stateful Phoenix LiveComponents with the same DSL as Lavash.LiveView.
Components can declare prop, state, calculate, actions, and render
with the ~L sigil. Inside a Lavash component, phx-target={@myself} is
auto-injected on phx-* attrs and __lavash_client_bindings__ propagates
to nested <.lavash_component> calls.
Example
defmodule MyAppWeb.ProductCard do
use Lavash.Component
prop :product, :map, required: true
state :expanded, :boolean, from: :socket, default: false, optimistic: true
state :hovered, :boolean, default: false, optimistic: true
calculate :show_actions, rx(@expanded or @hovered)
actions do
action :toggle_expand do
set :expanded, rx(not @expanded)
end
end
render fn assigns ->
~L"""
<div phx-click="toggle_expand">
<h3>{@product.name}</h3>
<div :if={@show_actions}>...</div>
</div>
"""
end
endExtensions
Add behavior plugins via the extensions option:
use Lavash.Component, extensions: [Lavash.Overlay.Modal.Dsl]Available extensions:
Lavash.Overlay.Modal.Dsl— modal phase machine (entering/visible/exiting)Lavash.Overlay.Flyover.Dsl— slide-in panel from screen edges
State persistence modes
Components use the same from: field as LiveViews. from: :socket is
particularly useful for components — the per-component-id state map
travels through the LiveView's assigns and survives reconnects.
Options
:extensions(list of module that adoptsSpark.Dsl.Extension) - A list of DSL extensions to add to theSpark.Dsl:otp_app(atom/0) - The otp_app to use for any application configurable options:fragments(list ofmodule/0) - Fragments to include in theSpark.Dsl. See the fragments guide for more.