Skip to content

Commit dbe9621

Browse files
committed
Set up some nested components that set flash messages on click
1 parent ade8834 commit dbe9621

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule PhoenixLiveviewFlashComponentsExampleWeb.FirstComponent do
2+
use PhoenixLiveviewFlashComponentsExampleWeb, :live_component
3+
4+
alias PhoenixLiveviewFlashComponentsExampleWeb.SecondComponent
5+
6+
def render(assigns) do
7+
~H"""
8+
<div class="m-2 p-4 border border-blue-800">
9+
<p>
10+
First-level component <code class="font-bold">@flash</code>:<br />
11+
<code><%= inspect(@flash) %></code>
12+
</p>
13+
14+
<.button class="m-2 !bg-blue-500 !hover:bg-blue-700" phx-click="click" phx-target={@myself}>
15+
Click me in the first-level component
16+
</.button>
17+
18+
<.live_component module={SecondComponent} id="nested-component" />
19+
</div>
20+
"""
21+
end
22+
23+
def handle_event("click", _params, socket) do
24+
{:noreply, put_flash(socket, :info, "First-level component button clicked!")}
25+
end
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule PhoenixLiveviewFlashComponentsExampleWeb.SecondComponent do
2+
use PhoenixLiveviewFlashComponentsExampleWeb, :live_component
3+
4+
def render(assigns) do
5+
~H"""
6+
<div class="m-2 p-4 border border-green-800">
7+
<p>
8+
Second-level component <code class="font-bold">@flash</code>: <br />
9+
<code><%= inspect(@flash) %></code>
10+
</p>
11+
12+
<.button class="m-2 !bg-green-700 !hover:bg-green-800" phx-click="click" phx-target={@myself}>
13+
Click me in the second-level component
14+
</.button>
15+
</div>
16+
"""
17+
end
18+
19+
def handle_event("click", _params, socket) do
20+
{:noreply, put_flash(socket, :info, "Second-level component button clicked!")}
21+
end
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule PhoenixLiveviewFlashComponentsExampleWeb.HomeLive do
2+
use PhoenixLiveviewFlashComponentsExampleWeb, :live_view
3+
4+
alias PhoenixLiveviewFlashComponentsExampleWeb.FirstComponent
5+
6+
def render(assigns) do
7+
~H"""
8+
<div class="text-center m-2 p-4 -mt-12 border border-red-800">
9+
<p>
10+
LiveView <code class="font-bold">@flash</code>:<br />
11+
<code><%= inspect(@flash) %></code>
12+
</p>
13+
14+
<.button class="m-2 !bg-red-700 !hover:bg-red-800" phx-click="click">
15+
Click me in the LiveView
16+
</.button>
17+
18+
<.live_component module={FirstComponent} id="nested-component" />
19+
</div>
20+
"""
21+
end
22+
23+
def handle_event("click", _params, socket) do
24+
{:noreply, put_flash(socket, :info, "LiveView button clicked!")}
25+
end
26+
end

lib/phoenix_liveview_flash_components_example_web/router.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule PhoenixLiveviewFlashComponentsExampleWeb.Router do
1717
scope "/", PhoenixLiveviewFlashComponentsExampleWeb do
1818
pipe_through :browser
1919

20-
get "/", PageController, :home
20+
live "/", HomeLive
2121
end
2222

2323
# Other scopes may use custom stacks.

0 commit comments

Comments
 (0)