defmodule RiverConnectWeb.Components.UI.AlertDialog do
@moduledoc """
Implementation of Alert Dialog component for SaladUI framework.
Alert Dialogs are modal dialogs that require a user action before they can be dismissed,
used to confirm user decisions or provide critical information.
## Examples:
<.alert_dialog id="delete-confirmation">
<.alert_dialog_trigger>
<.button variant="destructive">Delete Account
<.alert_dialog_content>
<.alert_dialog_header>
<.alert_dialog_title>Are you absolutely sure?
<.alert_dialog_description>
This action cannot be undone. This will permanently delete your
account and remove your data from our servers.
<.alert_dialog_footer>
<.alert_dialog_cancel>Cancel
<.alert_dialog_action>Continue
"""
use RiverConnectWeb.Components.UI, :component
@doc """
The main alert dialog component.
## Options
* `:id` - Required unique identifier for the alert dialog.
* `:open` - Whether the alert dialog is initially open. Defaults to `false`.
* `:on-open` - Handler for alert dialog open event.
* `:on-close` - Handler for alert dialog close event.
* `:class` - Additional CSS classes.
"""
attr :id, :string, required: true, doc: "Unique identifier for the alert dialog"
attr :open, :boolean, default: false, doc: "Whether the alert dialog is initially open"
attr :"on-open", :any, default: nil, doc: "Handler for alert dialog open event"
attr :"on-close", :any, default: nil, doc: "Handler for alert dialog close event"
attr :class, :string, default: nil
attr :rest, :global
slot :inner_block, required: true
def alert_dialog(assigns) do
# Collect event mappings
event_map =
%{}
|> add_event_mapping(assigns, "opened", :"on-open")
|> add_event_mapping(assigns, "closed", :"on-close")
assigns =
assigns
|> assign(:event_map, json(event_map))
|> assign(:initial_state, if(assigns.open, do: "open", else: "closed"))
|> assign(
:options,
json(%{
animations: get_animation_config()
})
)
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The trigger element that opens the alert dialog.
"""
attr :class, :string, default: nil
attr :as, :any, default: "div"
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_trigger(assigns) do
~H"""
<.dynamic data-part="trigger" data-action="open" tag={@as} class={classes(["", @class])} {@rest}>
{render_slot(@inner_block)}
"""
end
@doc """
The content container of the alert dialog.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_content(assigns) do
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The header section of the alert dialog.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_header(assigns) do
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The title of the alert dialog.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_title(assigns) do
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The description of the alert dialog.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_description(assigns) do
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The footer section of the alert dialog containing action buttons.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
attr :rest, :global
def alert_dialog_footer(assigns) do
~H"""
{render_slot(@inner_block)}
"""
end
@doc """
The cancel button for the alert dialog.
"""
attr :class, :string, default: nil
attr :variant, :string,
values: ~w(default secondary destructive outline ghost link),
default: "outline"
attr :size, :string, values: ~w(default sm lg icon), default: "default"
attr :rest, :global
slot :inner_block, required: true
def alert_dialog_cancel(assigns) do
assigns =
assign(
assigns,
:variant_class,
button_variant(%{variant: assigns.variant, size: assigns.size})
)
~H"""
"""
end
@doc """
The primary action button for the alert dialog.
"""
attr :class, :string, default: nil
attr :variant, :string,
values: ~w(default secondary destructive outline ghost link),
default: "default"
attr :size, :string, values: ~w(default sm lg icon), default: "default"
attr :rest, :global
slot :inner_block, required: true
def alert_dialog_action(assigns) do
assigns =
assign(
assigns,
:variant_class,
button_variant(%{variant: assigns.variant, size: assigns.size})
)
~H"""
"""
end
defp get_animation_config do
%{
"open_to_closed" => %{
duration: 130,
target_part: "content"
}
}
end
end