Skip to content

Commit 84f471c

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. Also, this causes a _ton_ of screen jumping on mobile screens which I really really don't like. This is partially because the image renders above the links on mobile screens which means that the text gets pushed down when the image finally kicks in... This SVG approach might need adjusting to use a placeholder image that keeps the same width + height dimensions that the actual images do to prevent these screen jumps.
1 parent 2e47ad2 commit 84f471c

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

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

+28-10
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 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">
@@ -55,12 +81,4 @@ defmodule SamuelWillisWeb.HomeLive do
5581
</div>
5682
"""
5783
end
58-
59-
def home_image(assigns) do
60-
assigns = assign(assigns, :image, Enum.random(@images))
61-
62-
~H"""
63-
<img src={~p"/images/#{@image.name}"} class="w-96" alt={@image.alt} />
64-
"""
65-
end
6684
end

0 commit comments

Comments
 (0)