Skip to main content

Blazor Preload

Show the loading state of a page with the Blazor Bootstrap preload service, app-wide or for one page, with accessible markup, custom loading text and a spinner color.

Things to know when using the 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.
Blazor Bootstrap: Blazor Preload Component - Default

Parameters​

NameTypeDefaultRequiredDescriptionAdded Version
ChildContentRenderFragmentnull✔️Gets or sets the content to be rendered within the component.1.1.0
LoadingTextstring?nullGets or sets the loading text.1.10.4
VisuallyHiddenTextstringLoading...Gets or sets the text screen readers announce while the overlay is shown and no loading text is set.4.1.0

Preload Service​

Methods​

NameReturn TypeDescriptionAdded Version
Show(SpinnerColor spinnerColor = SpinnerColor.Light)voidShows the preload.1.1.0
Show(SpinnerColor spinnerColor = SpinnerColor.Light, string? loadingText = null)voidShows the preload.1.10.4
Hide()voidHides the preload.1.1.0

Accessibility​

  • Screen readers: when the preload shows, screen readers announce it as a status message, without moving focus. They hear the text passed to Show(...), otherwise LoadingText, otherwise VisuallyHiddenText (default "Loading..."). Set VisuallyHiddenText to localize that default. A new text passed to Show(...) while the preload is shown is announced again; nothing is announced when it hides.
  • Keyboard: while the preload is shown, the page behind it can't be reached with the keyboard or by assistive technology, just as the backdrop already stops mouse clicks. When it hides, the page is reachable again, and focus returns to the element that had it.
  • Custom content: if you pass your own ChildContent, its text is what screen readers announce, so include visible text such as "Uploading files...". Content with only an image or a spinner is not announced.
  • Contrast: the loading text is always light, and the backdrop dims the page enough for the text to stay readable (WCAG 1.4.3) over any page, in light and dark mode. Custom content gets the same light text unless it sets its own color. If you show a colored spinner without loading text, the spinner alone may not stand out enough from the page; add loading text, or use SpinnerColor.Light.

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..." />
  1. Inject PreloadService, then call the Show() and Hide() methods before and after the Service/API call, respectively, as shown below.
@code {

[Inject] protected PreloadService PreloadService { get; set; }

private void GetEmployees()
{
try
{
PreloadService.Show();

// call the service/api to get the employees
}
catch
{
// handle exception
}
finally
{
PreloadService.Hide();
}
}
}

See Preload demo here.

Change loading text​

Blazor Bootstrap: Blazor Preload Component - 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; }

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();
}
}

See Preload demo here.

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.

Blazor Bootstrap: Blazor Preload Component - Change spinner color
<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; }

private async Task ShowSpinnerAsync(SpinnerColor spinnerColor)
{
PreloadService.Show(spinnerColor);

await Task.Delay(3000); // call the service/api

PreloadService.Hide();
}
}

See Preload demo here.

More libraries from the team

Free, open-source .NET libraries built by the same team.

UnlimitedPDF

A pure .NET library to create, merge and split PDFs, and convert HTML to PDF.

BlazorExpress.ChartJS

Chart.js charts for Blazor, configured in C#.

BlazorExpress.Bulma

Enterprise-class Blazor components built on the Bulma CSS framework.