Skip to content

Commit 741767e

Browse files
committed
Add loading spinner to HomeLive
Add a loading spinner to HomeLive so that it does not attempt to load two images and take a long time to connect the LiveView. This will prevent the static render from loading an image and blocking the LiveView connection until said image is loaded. Once the LiveView is connected an img will be rendered on the screen. Locally this was prone to a flash of unstyled content which I really don't like but I am curious if that's not a thing when deployed.
1 parent 2e47ad2 commit 741767e

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

Diff for: lib/samuel_willis_web/live/HomeLive.ex

+30-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,36 @@ defmodule SamuelWillisWeb.HomeLive do
1313
%{name: "house.jpg", alt: "Image of a house"}
1414
]
1515

16+
def mount(_params, _session, socket) do
17+
{:ok, assign(socket, :image, Enum.random(@images))}
18+
end
19+
1620
def render(assigns) do
1721
~H"""
18-
<div class="flex flex-col items-center sm:grid sm:grid-cols-2 sm:gap-6 space-y-6">
19-
<.home_image />
22+
<div class="flex-1 flex flex-col items-center sm:grid sm:grid-cols-2 sm:gap-6 space-y-6">
23+
<img
24+
src={~p"/images/#{@image.name}"}
25+
class="hidden w-96"
26+
alt={@image.alt}
27+
phx-connected={JS.show()}
28+
/>
29+
<div class="w-96 h-full flex justify-center items-center" phx-connected={JS.hide()}>
30+
<svg
31+
class="animate-spin h-5 w-5"
32+
xmlns="http://www.w3.org/2000/svg"
33+
fill="none"
34+
viewBox="0 0 24 24"
35+
>
36+
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4">
37+
</circle>
38+
<path
39+
class="opacity-75"
40+
fill="currentColor"
41+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
42+
>
43+
</path>
44+
</svg>
45+
</div>
2046
2147
<nav class="flex flex-col items-center">
2248
<ul class="space-y-4 text-xl font-bold">
@@ -56,11 +82,10 @@ defmodule SamuelWillisWeb.HomeLive do
5682
"""
5783
end
5884

59-
def home_image(assigns) do
60-
assigns = assign(assigns, :image, Enum.random(@images))
85+
attr :image, :map, required: true
6186

87+
def home_image(assigns) do
6288
~H"""
63-
<img src={~p"/images/#{@image.name}"} class="w-96" alt={@image.alt} />
6489
"""
6590
end
6691
end

0 commit comments

Comments
 (0)