Blazor Preload
Indicate the loading state of a page with Blazor Bootstrap preload component. The examples below cover global preload service for the application, change loading text, change spinner color and accessibility.
Things to know when using the blazor preload component #
- Add the Preload component to your current page or your layout page.
- Inject
PreloadService. - Call
preloadService.Show()before you make any call to the API. - Call
preloadService.Hide()after you get the response from the API.
Global preload service for the application #
1. Add the
Preload component in MainLayout.razor page as shown below.@using BlazorBootstrap
.
.
... MainLayout.razor code goes here ...
.
.
<Preload LoadingText="Loading..." />2. Inject
PreloadService, then call the Show() and Hide() methods before and after the Service/API, respectively, as shown below.@code {
[Inject] protected PreloadService PreloadService { get; set; } = default!;
private void GetEmployees()
{
try
{
PreloadService.Show();
// call the service/api to get the employees
}
catch
{
// handle exception
}
finally
{
PreloadService.Hide();
}
}
}Change loading text #
<Button Color="ButtonColor.Primary" @onclick="ShowLoadingDataAsync">Show Loading data...</Button>
<Button Color="ButtonColor.Dark" @onclick="ShowSavingDataAsync">Show Saving data...</Button>
@code {
[Inject] protected PreloadService PreloadService { get; set; } = default!;
private async Task ShowLoadingDataAsync()
{
PreloadService.Show(SpinnerColor.Light, "Loading data...");
await Task.Delay(3000); // call the service/api
PreloadService.Hide();
}
private async Task ShowSavingDataAsync()
{
PreloadService.Show(SpinnerColor.Light, "Saving data...");
await Task.Delay(3000); // call the service/api
PreloadService.Hide();
}
}Change spinner color #
Change the default spinner color by passing the
SpinnerColor enum to the Show(...) method.
The color applies to the spinner only; the loading text stays light so it remains readable.
In the below example, we are using a global preload service, as shown in the above section.
<Button Color="ButtonColor.Primary" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Primary)">Primary Spinner</Button>
<Button Color="ButtonColor.Secondary" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Secondary)">Secondary Spinner</Button>
<Button Color="ButtonColor.Success" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Success)">Success Spinner</Button>
<Button Color="ButtonColor.Danger" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Danger)">Danger Spinner</Button>
<Button Color="ButtonColor.Warning" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Warning)">Warning Spinner</Button>
<Button Color="ButtonColor.Info" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Info)">Info Spinner</Button>
<Button Color="ButtonColor.Light" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Light)">Light Spinner</Button>
<Button Color="ButtonColor.Dark" @onclick="async () => await ShowSpinnerAsync(SpinnerColor.Dark)">Dark Spinner</Button>
@code {
[Inject] protected PreloadService PreloadService { get; set; } = default!;
private async Task ShowSpinnerAsync(SpinnerColor spinnerColor)
{
PreloadService.Show(spinnerColor);
await Task.Delay(3000); // call the service/api
PreloadService.Hide();
}
}Accessibility #
- When the preload shows, screen readers announce it as a status message, without moving focus. They hear the text passed to
Show(...), otherwiseLoadingText, otherwiseVisuallyHiddenText(default "Loading..."), which you can set to localize it. A new text passed toShow(...)while the preload is shown is announced again. - While the preload is shown, the page behind it can't be reached with the keyboard or by assistive technology, just as the backdrop stops mouse clicks. When it hides, focus returns to the element that had it.
- If you pass your own
ChildContent, its text is what screen readers announce, so include visible text such as "Uploading files...". - The loading text is always light, and the backdrop dims the page enough for the text to stay readable over any page, in light and dark mode. Custom content gets the same light text unless it sets its own color. A colored spinner shown without loading text may not stand out enough from the page, so add loading text or use
SpinnerColor.Light.