Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ end
Then just make sure you've passed it to the `live_group` component as seen above.


### Using `live_render`

When rendering a LiveView within another LiveView, the flash assigns (like all other assigns) are not shared.
This means that the nested LiveView must use its own "flash group" (or equivalent, e.g. toast group) in its HTML.
When doing this, it is prudent to set the `:show_client_and_server_flashes` attr to `false`.
That way the nested LiveView will not show client/server error flashes, which will already be shown by the parent LiveView.


### JavaScript Options

You can also change some options about the LiveView hook when it is initialized. Such as:
Expand Down
6 changes: 6 additions & 0 deletions lib/live_toast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ defmodule LiveToast do
doc: "function to override the toast classes"
)

attr(:show_client_and_server_flashes, :boolean,
default: true,
doc: "optional boolean to include/exclude the client-error and server-error flashes"
)

@doc """
Renders a group of toasts and flashes.

Expand All @@ -229,6 +234,7 @@ defmodule LiveToast do
group_class_fn={@group_class_fn}
f={@flash}
kinds={@kinds}
show_client_and_server_flashes={@show_client_and_server_flashes}
/>
<Components.flash_group
:if={!@connected}
Expand Down
2 changes: 2 additions & 0 deletions lib/live_toast/components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ defmodule LiveToast.Components do
data-duration={@duration}
data-corner={@corner}
class={@toast_class_fn.(assigns)}
data-role={"#{@kind}-toast"}
{@rest}
>
<%= if @component do %>
Expand Down Expand Up @@ -86,6 +87,7 @@ defmodule LiveToast.Components do
"group-has-[[data-part='title']]/toast:absolute",
"right-[5px] top-[5px] rounded-md p-[5px] text-black/50 transition-opacity hover:text-black focus:opacity-100 focus:outline-none focus:ring-1 group group-hover:opacity-100"
]}
data-role="close-toast-button"
aria-label="close"
{
if Phoenix.Flash.get(@flash, @kind),
Expand Down
10 changes: 7 additions & 3 deletions lib/live_toast/live_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ defmodule LiveToast.LiveComponent do

@impl Phoenix.LiveComponent
def render(assigns) do
assigns = assigns
|> assign_new(:id, fn -> "toast-group" end)
|> assign_new(:show_client_and_server_flashes, fn -> true end)

~H"""
<div id={assigns[:id] || "toast-group"} class={@group_class_fn.(assigns)}>
<div class="contents" id="toast-group-stream" phx-update="stream">
<div id={@id} class={@group_class_fn.(assigns)}>
<div class="contents" id={@id <> "-stream"} phx-update="stream" data-role="toast-group-stream">
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the important bit.
Without being able to change this HTML ID, we get duplicate HTML IDs when we have 2 toast groups rendered, e.g. when doing live_render

<Components.toast
:for={
{dom_id,
Expand Down Expand Up @@ -69,7 +73,7 @@ defmodule LiveToast.LiveComponent do
</Components.toast>
</div>

<Components.flashes f={@f} corner={@corner} toast_class_fn={@toast_class_fn} kinds={@kinds} />
<Components.flashes :if={@show_client_and_server_flashes} f={@f} corner={@corner} toast_class_fn={@toast_class_fn} kinds={@kinds} />
</div>
"""
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule LiveToast.MixProject do
{:dialyxir, ">= 0.0.0", only: [:dev], runtime: false},
{:doctor, ">= 0.0.0", only: [:dev], runtime: false},
{:ex_doc, "~> 0.32.2", only: [:dev], runtime: false},
{:gettext, "~> 0.24.0"},
{:gettext, ">= 0.24.0"},
{:mix_audit, ">= 0.0.0", only: [:dev], runtime: false},
{:styler, "~> 0.11.9", only: [:dev, :test], runtime: false},
{:makeup, "1.1.2", only: [:dev], runtime: false},
Expand Down