Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Masatoshi Nishiguchi
Masatoshi Nishiguchi

Posted on

     

Loading Icon Component for Phoenix LiveView

Today I am going to put together how to make a loading iconLiveComponent forPhoenix LiveView.

Get a loader CSS

First, I look for an open-source loader CSS because I am poor at designing and writing CSS. Luke Haas'sSingle Element CSS Spinners is nice.

Make a loader CSS file

I createassets/css/_loader.scss file in my phoenix project and paste the loader CSS into that file.

I make sure that my loader CSS file is imported intoassets/css/app.scss file.

+ @import 'loader';
Enter fullscreen modeExit fullscreen mode

Make a loading icon component

First I makelib/mnishiguchi_web/live/components/loading_component.ex file. I make the component configurable for the CSS class name bacause I might want to use different loaders in different locations.

defmoduleMnishiguchiWeb.LoadingComponentdo@moduledoc"""  Shows a loading icon.  ## Examples      <%= live_component @socket,                          MnishiguchiWeb.LoadingComponent,                          class: "loader7" %>      <%= live_component @socket,                          MnishiguchiWeb.LoadingComponent,                          loading: @loading,                          class: "loader7" %>      <%= live_component @socket,                          MnishiguchiWeb.LoadingComponent,                          loading: !connected?(@socket),                          class: "loader7" %>  """useMnishiguchiWeb,:live_component@default_assigns%{loading:true,class:"loader"}@implPhoenix.LiveComponentdefupdate(assigns,socket)dosocket=socket|>assign(@default_assigns)|>assign(assigns){:ok,socket}end@implPhoenix.LiveComponentdefrender(assigns)do~L"""    <%= if @loading do %>      <divo"><%=@class%>">        Loading...      </div>    <% end %>    """endend
Enter fullscreen modeExit fullscreen mode

That's it!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

たのしむ
  • Location
    🇯🇵
  • Work
    ソフトウエアエンジニア
  • Joined

More fromMasatoshi Nishiguchi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp