Skip to content

Commit c9b4500

Browse files
committed
Extract async modal demo LiveComponent
1 parent bc708b5 commit c9b4500

File tree

6 files changed

+158
-139
lines changed

6 files changed

+158
-139
lines changed

lib/prima_web/live/demo_live.ex

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,11 @@ defmodule PrimaWeb.DemoLive do
44
import Prima.{Dropdown, Modal, Combobox}
55
embed_templates "demo_live/*"
66

7-
@options [
8-
"Cherry",
9-
"Kiwi",
10-
"Grapefruit",
11-
"Orange",
12-
"Banana"
13-
]
14-
157
@impl true
168
def mount(_params, _session, socket) do
179
socket =
1810
socket
1911
|> assign(form_modal_open?: false)
20-
|> assign(async_modal_open?: false)
2112
|> assign(submitted_form_data: nil)
2213

2314
{:ok, socket}
@@ -38,16 +29,6 @@ defmodule PrimaWeb.DemoLive do
3829
{:noreply, assign(socket, form_modal_open?: false)}
3930
end
4031

41-
@impl true
42-
def handle_event("open-async-modal", _params, socket) do
43-
{:noreply, assign(socket, async_modal_open?: true)}
44-
end
45-
46-
@impl true
47-
def handle_event("close-async-modal", _params, socket) do
48-
{:noreply, assign(socket, async_modal_open?: false)}
49-
end
50-
5132
@impl true
5233
def handle_info(:close_form_modal, socket) do
5334
{:noreply, assign(socket, form_modal_open?: false)}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
defmodule PrimaWeb.DemoLive.AsyncModalDemo do
2+
@moduledoc false
3+
use PrimaWeb, :live_component
4+
import Prima.Modal
5+
6+
@impl true
7+
def mount(socket) do
8+
socket = assign(socket, async_modal_open?: false)
9+
{:ok, socket}
10+
end
11+
12+
@impl true
13+
def render(assigns) do
14+
~H"""
15+
<div>
16+
<.button
17+
id="open-form-modal-button"
18+
type="button"
19+
phx-click={
20+
Prima.Modal.open("demo-form-modal") |> JS.push("open-async-modal", target: @myself)
21+
}
22+
class="inline-flex justify-center rounded-md bg-indigo-600 disabled:bg-gray-400 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
23+
>
24+
Open Async Modal
25+
</.button>
26+
27+
<.modal
28+
id="demo-form-modal"
29+
on_close={JS.push("close-async-modal", target: @myself)}
30+
class="relative z-10"
31+
>
32+
<.modal_overlay
33+
transition_enter={{"ease-out duration-300", "opacity-0", "opacity-100"}}
34+
transition_leave={{"ease-in duration-200", "opacity-100", "opacity-0"}}
35+
class="fixed inset-0 bg-gray-500/75 transition-opacity"
36+
/>
37+
38+
<div class="fixed inset-0 z-10 w-screen overflow-y-auto">
39+
<div class="flex min-h-full items-end justify-center p-4 sm:items-center sm:p-0">
40+
<.modal_loader>
41+
<svg
42+
class="w-8 h-8 mr-2 text-white/50 animate-spin fill-white"
43+
viewBox="0 0 100 100"
44+
fill="none"
45+
xmlns="http://www.w3.org/2000/svg"
46+
>
47+
<path
48+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
49+
fill="currentColor"
50+
/>
51+
<path
52+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
53+
fill="currentFill"
54+
/>
55+
</svg>
56+
</.modal_loader>
57+
58+
<.modal_panel
59+
:if={@async_modal_open?}
60+
id="demo-form-modal-panel"
61+
class="relative overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-sm sm:p-6"
62+
transition_enter={
63+
{"ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95",
64+
"opacity-100 translate-y-0 sm:scale-100"}
65+
}
66+
transition_leave={
67+
{"ease-in duration-200", "opacity-100 translate-y-0 sm:scale-100",
68+
"opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"}
69+
}
70+
>
71+
<div class="relative">
72+
<button
73+
class="absolute top-0 right-0"
74+
phx-click={Prima.Modal.close()}
75+
testing-ref="close-button"
76+
>
77+
<svg
78+
xmlns="http://www.w3.org/2000/svg"
79+
fill="none"
80+
viewBox="0 0 24 24"
81+
stroke-width="1.5"
82+
stroke="currentColor"
83+
class="text-gray-600 h-6 w-6"
84+
>
85+
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
86+
</svg>
87+
</button>
88+
<div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-blue-100">
89+
<svg
90+
class="h-6 w-6 text-blue-600"
91+
fill="none"
92+
viewBox="0 0 24 24"
93+
stroke-width="1.5"
94+
stroke="currentColor"
95+
aria-hidden="true"
96+
>
97+
<path
98+
stroke-linecap="round"
99+
stroke-linejoin="round"
100+
d="M11.25 11.25l.041-.02a.75.75 0 011.063.852l-.708 2.836a.75.75 0 001.063.853l3.5-1.75a.75.75 0 000-1.342l-3.5-1.75a.75.75 0 00-1.063.853l.708 2.836z"
101+
/>
102+
<path
103+
stroke-linecap="round"
104+
stroke-linejoin="round"
105+
d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
106+
/>
107+
</svg>
108+
</div>
109+
<div class="mt-3 text-center sm:mt-5">
110+
<h2
111+
class="text-base font-semibold leading-6 text-gray-900"
112+
id="demo-form-modal-title"
113+
>
114+
Data loaded successfully
115+
</h2>
116+
<div class="mt-2">
117+
<p class="text-sm text-gray-500">
118+
This modal demonstrates loading states and backend synchronization.
119+
</p>
120+
</div>
121+
</div>
122+
</div>
123+
<form class="mt-5 sm:mt-6">
124+
<button
125+
phx-click={JS.push("close-async-modal", target: @myself)}
126+
type="button"
127+
class="inline-flex w-full justify-center rounded-md bg-blue-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600"
128+
>
129+
Save
130+
</button>
131+
</form>
132+
</.modal_panel>
133+
</div>
134+
</div>
135+
</.modal>
136+
</div>
137+
"""
138+
end
139+
140+
@impl true
141+
def handle_event("open-async-modal", _params, socket) do
142+
{:noreply, assign(socket, async_modal_open?: true)}
143+
end
144+
145+
@impl true
146+
def handle_event("close-async-modal", _params, socket) do
147+
{:noreply, assign(socket, async_modal_open?: false)}
148+
end
149+
end

lib/prima_web/live/demo_live/async_modal_demo.html.heex

Lines changed: 0 additions & 111 deletions
This file was deleted.

lib/prima_web/live/demo_live/history_modal_demo.html.heex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<.link
2-
patch={~p"/demo/modal/history"}
2+
patch={~p"/modal/history"}
33
phx-click={Prima.Modal.open("demo-history-modal")}
44
class="phx-submit-loading:opacity-75 rounded-lg bg-zinc-900 hover:bg-zinc-700 py-2 px-3 text-sm font-semibold leading-6 text-white active:text-white/80"
55
>
@@ -9,7 +9,7 @@
99
<.modal
1010
show={@live_action == :modal_history}
1111
id="demo-history-modal"
12-
on_close={JS.patch(~p"/demo/modal")}
12+
on_close={JS.patch(~p"/modal")}
1313
class="relative z-10"
1414
>
1515
<.modal_overlay

lib/prima_web/live/demo_live/modal_page.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</p>
3737

3838
<div class="not-prose bg-white border border-gray-200 rounded-lg p-6 my-6">
39-
<.async_modal_demo {assigns} />
39+
<.live_component module={PrimaWeb.DemoLive.AsyncModalDemo} id="async-modal-demo" />
4040
</div>
4141

4242
<p>

test/prima_web/live/modal_demo_page_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule PrimaWeb.ModalDemoPageTest do
44

55
describe "modal demo page" do
66
test "loads without errors and shows basic page content", %{conn: conn} do
7-
{:ok, _view, html} = live(conn, "/demo/modal")
7+
{:ok, _view, html} = live(conn, "/modal")
88

99
assert html =~ "Modal"
1010
assert html =~ "fully-managed dialog component"
@@ -13,37 +13,37 @@ defmodule PrimaWeb.ModalDemoPageTest do
1313
end
1414

1515
test "renders minimal modal code example", %{conn: conn} do
16-
{:ok, _view, html} = live(conn, "/demo/modal")
16+
{:ok, _view, html} = live(conn, "/modal")
1717

1818
assert html =~ "Prima.Modal.open"
1919
assert html =~ "Open Modal"
2020
assert html =~ "Hello Modal"
2121
end
2222

2323
test "renders form modal events code example", %{conn: conn} do
24-
{:ok, _view, html} = live(conn, "/demo/modal")
24+
{:ok, _view, html} = live(conn, "/modal")
2525

2626
assert html =~ "handle_event"
2727
assert html =~ "open-form-modal"
2828
assert html =~ "close-form-modal"
2929
end
3030

3131
test "renders form modal template code example", %{conn: conn} do
32-
{:ok, _view, html} = live(conn, "/demo/modal")
32+
{:ok, _view, html} = live(conn, "/modal")
3333

3434
assert html =~ "modal_panel"
3535
end
3636

3737
test "renders history routes code example", %{conn: conn} do
38-
{:ok, _view, html} = live(conn, "/demo/modal")
38+
{:ok, _view, html} = live(conn, "/modal")
3939

4040
assert html =~ "live_action"
4141
assert html =~ "show_history_modal"
4242
assert html =~ "handle_params"
4343
end
4444

4545
test "applies syntax highlighting to code examples", %{conn: conn} do
46-
{:ok, _view, html} = live(conn, "/demo/modal")
46+
{:ok, _view, html} = live(conn, "/modal")
4747

4848
assert html =~ "athl"
4949
assert html =~ "prima-code-block"

0 commit comments

Comments
 (0)