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
Preloadcomponent 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.
Parameters
| Name | Type | Default | Required | Description | Added Version |
|---|---|---|---|---|---|
| ChildContent | RenderFragment | null | ✔️ | Gets or sets the content to be rendered within the component. | 1.1.0 |
| LoadingText | string? | null | Gets or sets the loading text. | 1.10.4 | |
| VisuallyHiddenText | string | Loading... | 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
| Name | Return Type | Description | Added Version |
|---|---|---|---|
| Show(SpinnerColor spinnerColor = SpinnerColor.Light) | void | Shows the preload. | 1.1.0 |
| Show(SpinnerColor spinnerColor = SpinnerColor.Light, string? loadingText = null) | void | Shows the preload. | 1.10.4 |
| Hide() | void | Hides 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(...), otherwiseLoadingText, otherwiseVisuallyHiddenText(default "Loading..."). SetVisuallyHiddenTextto localize that default. A new text passed toShow(...)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
- Add the
Preloadcomponent in MainLayout.razor page as shown below.
@using BlazorBootstrap
.
.
... MainLayout.razor code goes here ...
.
.
<Preload LoadingText="Loading..." />
- Inject
PreloadService, then call theShow()andHide()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();
}
}
}
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();
}
}
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; }
private async Task ShowSpinnerAsync(SpinnerColor spinnerColor)
{
PreloadService.Show(spinnerColor);
await Task.Delay(3000); // call the service/api
PreloadService.Hide();
}
}