Added in v1.1
Blazorise LoadingIndicator component
A wrapper component that adds a loading indicator to child content. Default spinner courtesy of icons8.com.
To use the LoadingIndicator component, install the Blazorise.LoadingIndicator package first.
Installation
NuGet
Install extension from NuGet.Hide code example
Copy code
dotnet add package Blazorise.LoadingIndicator
Imports
In your main_Imports.razor add:
Hide code example
Copy code
@using Blazorise.LoadingIndicator
Static files
Add these links inside the<head> section:
<link href="_content/Blazorise.LoadingIndicator/blazorise.loadingindicator.css?v=2.4.1.0" rel="stylesheet" />
Hide code example
Copy code
Update
wwwroot/index.html.
Basic example
The basic usage scenario forLoadingIndicator with default settings using a LoadingIndicator object reference.
Hide code example
Copy code
<LoadingIndicator @ref="loadingIndicator"> <LineChart TItem="double" Data="lineChartData" /> </LoadingIndicator> <Button Clicked="UpdateChart" Color="Color.Primary">Update</Button>
@code { LoadingIndicator loadingIndicator; async Task UpdateChart() { await loadingIndicator.Show(); await Task.Delay(3000); // Do work ... await loadingIndicator.Hide(); } // sample data ChartData<double> lineChartData = new() { Labels = new() { "Jan", "Feb", "Mar", "Apr", "May", "Jun" }, Datasets = new() { new LineChartDataset<double>() { Data = new List<double>() { 70, 90, 50, 60, 80, 100 }, }} }; }
Two-way Binding
Having a more declarative approach to control the indicator is also supported. To make it work, you just need to bind a variable to theVisible parameter, and it will work.
Hide code example
Copy code
<LoadingIndicator @bind-Visible=""> <LineChart TItem="double" Data="lineChartData" /> </LoadingIndicator> <Button Clicked="UpdateChart" Color="Color.Primary">Update</Button>
@code { bool visible; async Task UpdateChart() { visible = true; await Task.Delay( 3000 ); // Do work ... visible = false; } // sample data ChartData<double> lineChartData = new() { Labels = new() { "Jan", "Feb", "Mar", "Apr", "May", "Jun" }, Datasets = new() { new LineChartDataset<double>() { Data = new List<double>() { 70, 90, 50, 60, 80, 100 }, }} }; }
Status text
Display progress text by providingIndicatorTemplate. The template receives a LoadingIndicatorContext with Text and Progress updated via SetStatus.
Hide code example
Copy code
<LoadingIndicator @ref="loadingIndicator" FullScreen> <ChildContent> <Button Clicked="ShowIndicator" Color="Color.Primary">Show indicator</Button> </ChildContent> <IndicatorTemplate> <Div Flex="Flex.Column.AlignItems.Center" Gap="Gap.Is2"> <SpinKit Type="SpinKitType.Chase" Size="Size.ExtraLarge" Color="Color.Primary" /> @if ( !string.IsNullOrWhiteSpace( context.Text ) ) { <Span>@context.Text</Span> } @if ( context.Progress.HasValue ) { <Span>@($"{context.Progress.Value}%")</Span> } </Div> </IndicatorTemplate> </LoadingIndicator>
@code { LoadingIndicator loadingIndicator; async Task ShowIndicator() { await loadingIndicator.SetStatus( text: "Loading files", progress: 30 ); await loadingIndicator.Show(); await Task.Delay( 1000 ); await loadingIndicator.SetStatus( text: "Loading other data", progress: 60 ); await Task.Delay( 1000 ); await loadingIndicator.SetStatus( text: "Finalizing", progress: 90 ); await Task.Delay( 1000 ); await loadingIndicator.Hide(); } }
Application busy service
UseApplicationLoadingIndicator to add a global application busy UI. Register ILoadingIndicatorService as a scoped service in your configuration. ApplicationLoadingIndicator automatically registers with ILoadingIndicatorService if one is registered or unless Service property is set directly. Use dependency injection to obtain a reference to the ILoadingIndicatorService to control ApplicationLoadingIndicator. The application busy UI comes with an awesome Blazorise logo spinner, which is recommended, but if you prefer the default spinner set IndicatorTemplate to null.
Add
ILoadingIndicatorService as a scoped service.
Hide code example
Copy code
services.AddLoadingIndicator();
Add
ApplicationLoadingIndicator to your App.razor file.
Hide code example
Copy code
<Router AppAssembly="typeof(App).Assembly"> <Found>...</Found> <NotFound>...</NotFound> </Router> <ApplicationLoadingIndicator />
Inject
ILoadingIndicatorService into your component where you want to trigger the application busy UI from.
Hide code example
Copy code
@inject ILoadingIndicatorService ApplicationLoadingIndicatorService
@code { async Task DoWork() { await ApplicationLoadingIndicatorService.Show(); // do work ... await ApplicationLoadingIndicatorService.Hide(); } }
Cascading Usage
You can also use the wrapper as a cascading parameter.
To implement a global application busy UI using cascading parameter add the wrapper to your main layout.
Hide code example
Copy code
<LoadingIndicator FullScreen> @Body </LoadingIndicator>
Declare a
CascadingParameter of type LoadingIndicator in your component.
Hide code example
Copy code
<Button Clicked="DoWork" />
@code { [CascadingParameter] LoadingIndicator loadingIndicator; async Task DoWork() { await loadingIndicator.Show(); // do work ... await loadingIndicator.Hide(); } }
Disabling input controls
While the busy screen prevents click events from reaching child content it has no effect on keyboard input. You may want to disable your input controls such as textboxes and buttons while your component is busy.
Using the
Visible property directly.
Hide code example
Copy code
<LoadingIndicator @ref="loadingIndicator"> <Button Disabled="loadingIndicator.Visible" Clicked="DoWork"/> </LoadingIndicator>
@code { LoadingIndicator loadingIndicator; async Task DoWork() { if ( !loadingIndicator.Visible ) { await loadingIndicator.Show(); // do work... await loadingIndicator.Hide(); } } }
Disabling your controls using
LoadingIndicatorService.
Hide code example
Copy code
@inject ILoadingIndicatorService ApplicationLoadingIndicatorService <Button Disabled="ApplicationLoadingIndicatorService.Visible" />
Disable the entire input form using
<fieldset>Hide code example
Copy code
@inject ILoadingIndicatorService ApplicationLoadingIndicatorService <form action="/action_page.php"> <fieldset disabled="ApplicationLoadingIndicatorService.Visible"> <label for="fname">Message:</label> <input type="text" id="message" name="message"> <input type="submit" value="Send"> </fieldset> </form>
Using animation
Loading indicator can animate its visibility state by fading into view. This is on by default forApplicationLoadingIndicator. You can also use the Animate component to add a visual effect.
Hide code example
Copy code
<LoadingIndicator @ref="loadingIndicator" FullScreen FadeIn> <ChildContent> <Button Clicked="ShowIndicator" Color="Color.Primary">Show indicator</Button> </ChildContent> <IndicatorTemplate> <Animate Animation="Animations.FadeDownRight" Auto Duration="TimeSpan.FromMilliseconds( 700 )"> <Div> <SpinKit Type="SpinKitType.Wave" Size="Size.ExtraLarge" /> </Div> </Animate> </IndicatorTemplate> </LoadingIndicator>
@code { LoadingIndicator loadingIndicator; async Task ShowIndicator() { await loadingIndicator.Show(); await Task.Delay( 3000 ); // Do work ... await loadingIndicator.Hide(); } }
Custom colorsAdded in v2.3
Combine contextual colors and custom CSS colors withSpinnerColor.
Success
Lavender
Accent
Hide code example
Copy code
<Row> <Column ColumnSize="ColumnSize.IsFull.OnMobile.Is4.OnTablet" Margin="Margin.Is3.FromBottom"> <Paragraph>Success</Paragraph> <LoadingIndicator Visible Height="Height.Px(100)" SpinnerColor="Color.Success" /> </Column> <Column ColumnSize="ColumnSize.IsFull.OnMobile.Is4.OnTablet" Margin="Margin.Is3.FromBottom"> <Paragraph>Lavender</Paragraph> <LoadingIndicator Visible Height="Height.Px(100)" SpinnerColor="@("#DBB5E6")" /> </Column> <Column ColumnSize="ColumnSize.IsFull.OnMobile.Is4.OnTablet" Margin="Margin.Is3.FromBottom"> <Paragraph>Accent</Paragraph> <LoadingIndicator Visible Height="Height.Px(100)" SpinnerColor="@CssColor.Variable( "--spinner-accent", "#0F766E" )" /> </Column> </Row>
API
See the API reference for the parameters, events, methods, and related types available to the components covered on this page.