Lavash.Overlay.Modal.Dsl (Lavash v0.4.0-rc.3)

Copy Markdown View Source

Spark DSL extension for modal behavior.

Adds modal-specific state and actions to a Lavash Component:

  • Open/close state management (modal owns its state)
  • Escape key handling
  • Backdrop click handling
  • Auto-injected :close and :noop actions

Usage

defmodule MyApp.EditModal do
  use Lavash.Component, extensions: [Lavash.Overlay.Modal.Dsl]

  modal do
    open_field :product_id  # nil = closed, non-nil = open with this ID
    close_on_escape true
    close_on_backdrop true
  end

  # Define an :open action for the parent to invoke
  actions do
    action :open, [:product_id] do
      set :product_id, &(&1.params.product_id)
    end
  end

  render fn assigns ->
    ~H"..."
  end
end

Parent opens the modal via invoke:

invoke "my-modal", :open,
  module: MyApp.EditModal,
  params: [product_id: 123]

The plugin will:

  1. Inject the open_field as ephemeral state (if not already defined)
  2. Inject :close action that sets open_field to nil
  3. Inject :noop action for backdrop click handling
  4. Generate render/1 with modal chrome wrapping your content

Summary

Functions

modal(body)

(macro)