Blazor - Customize the startup message
We all have seen the little “Loading…” message that appears
with you are starting up a Blazor client-side application. A quick text search you can see the text in
the index.html page under the wwwroot folder.
It is simple enough to change the text to anything you would like.
But what if you wanted to have a cool animated gif or a awesome CSS spinner? When I first thought about doing this, I googled for it. I did not find too many examples to work with. I did find that Telerik has a component for this, but you must buy their tool set. I took a step back and thought about it. We are talking about just an HTML page.
Standard Loading:
First Try
My first step was to create a class with a CSS spinner in it
and add that class to:
<app class=”myspinner”>
</app>
Surprisingly, the spinner showed during loading nicely. Unfortunately, once the first page of the
application loaded, the spinner was still going. I figured no problem. I fixed this with the page spinner I
previously built. I just need to hide it
once the page is loaded. And I can do
that in the page OnInitializedAsync() method.
But there is a problem, this is not with in a not a blazor component,
it is a DOM element. Well, I could use
JSInterOp, but I rather not. The
<app> is a component, but it the bootstrapper for our Blazor app and the
spinner is setting outside of it.
Second Try
With my first try a semi-failure, I decided to just put the spinner inside the <app> component, since that is where the “Loading…” text is and it disappears once the app is loaded. So, I added a div with my loader class attached to it. I added a little extra test as well.
<div class="loader">I am running a pretty spinner!!</div>
Third Try
At this point I am feeling pretty confident so I decided to
try an animated gif. That worked as
well,
<app>
<img src="img/giphy.gif" alt="Smiley face" height="450" width="450" class="center"/>
</app>
Turns out this is very straight forward, and you can add any HTML/CSS you would like to control how your application looks on start up. You are only limited my your imagination.
I have include the image and the CSS for the spinner I used in the source code in GitHub.
Comments
Post a Comment