Loader


Quick start

You can use the Loader component to show a loading indicator while waiting for data to load and display a message when an error occurs.

Global templates configuration

You can configure the global templates for the loader component using the BdkLoaderOptions class.

The following example shows how to configure the global templates for the loader component.

Loading content template example
BdkLoaderOptions.Loading.ChangeContent<LoadingContent>();
LoadingContentExample.razor
<div class="d-flex flex-column align-items-center justify-content-center">
    <div class="spinner-border text-danger" role="status">
        <span class="visually-hidden">Loading...</span>
    </div>
    
    <div class="mt-3">@(string.IsNullOrEmpty(Message) ? "Loading..." : Message)</div>
</div>

@code 
{
    [Parameter] public string Message { get; set; } = string.Empty;
}

The component created to be used as LoadingContent needs to have a parameter called Message of type string.

You can register a RenderFragment string or MarkupString in place of the component type, noting that only MarkupString does not support receiving the Message defined in the BdkLoader component.

Similarly, the registration of the component type to be used in error cases needs to have a parameter called ErrorResult of type BdkLoaderErrorResult, which can be used to access the exception that occurred and check if the retry button will be displayed. Below is an example of the registration and usage of this component:

Error content template example
BdkLoaderOptions.Error.RegisterContent<Exception, ErrorContentExample>();

You can register an ErrorContent for a specific exception type.

ErrorContentExample.razor
<div class="d-flex flex-column align-items-center justify-content-center">
    <div class="mt-3 text-danger">
        @ErrorResult.Exception.Message
    </div>

    @if (ErrorResult.Loader.CanRetry)
    {
        <div class="mt-3">
            <button class="btn btn-primary" @onclick="ErrorResult.Loader.ReloadAsync">@ErrorResult.Loader.CanRetryTitle</button>
        </div>
    }
</div>

@code
{
    [Parameter] public required BdkLoaderErrorResult ErrorResult { get; set; }
}

Basic

  • d10d0826-b066-4a0d-b960-74722a879cc1 - 12/22/2024 18:09:40 +00:00
  • 6415e3d7-b323-4896-8384-5b6a9e2dca2d - 12/22/2024 18:09:40 +00:00
  • 9141fab1-dfd7-4d19-8f7e-d366c4e4662e - 12/22/2024 18:09:40 +00:00
  • a5d7e2f1-21ba-4fcf-b7b5-40a09ce1af96 - 12/22/2024 18:09:40 +00:00
  • a49c4fbd-a50d-4f94-b692-7a4e72df8391 - 12/22/2024 18:09:40 +00:00
Blazor
@inject ISubjectService Service

<BdkLoader Load="Service.ListAsync">
    <ul>
        @foreach (var item in context)
        {
            <li>@item.Id - @item.CreatedOn</li>
        }
    </ul>
</BdkLoader>

Basic: no result

Attention: the BdkLoader context will be null, this mode can be used when a return value is not necessary.

Content displayed after loading

Blazor
@inject ISubjectService Service

<BdkLoader T="object?" Load="LoadAsync">
    <p>
        Content displayed after loading
    </p>
</BdkLoader>

@code
{
    private async Task<object?> LoadAsync()
    {
        await Service.ProcessAsync();
        return null;
    }
}

Basic with custom loading message

  • 4be4e877-2f45-4660-a497-2f9efef45426 - 12/22/2024 18:09:40 +00:00
  • f23ed10f-60f0-451a-aa60-38f75fca6c7c - 12/22/2024 18:09:40 +00:00
  • 75aaf085-a07b-4d88-88d4-09bf138b197c - 12/22/2024 18:09:40 +00:00
  • 32f20959-87ad-470e-9f5d-379117302795 - 12/22/2024 18:09:40 +00:00
  • 5b624f12-658b-4740-a59a-af199353e9a4 - 12/22/2024 18:09:40 +00:00
Blazor
@inject ISubjectService Service

<BdkLoader Load="Service.ListAsync" Message="This is a custom message">
    <ul>
        @foreach (var item in context)
        {
            <li>@item.Id - @item.CreatedOn</li>
        }
    </ul>
</BdkLoader>

Basic with error

Subject not found in the database
Blazor
@inject ISubjectService Service

<BdkLoader Load="LoadAsync">
    <ul>
        @foreach (var item in context)
        {
            <li>@item.Id - @item.CreatedOn</li>
        }
    </ul>
</BdkLoader>

@code
{
    private async Task<ResponseItem[]> LoadAsync()
    {
        await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
        return await Service.ListAsync();
    }
}

Basic with error and CanRetry

Subject not found in the database
Blazor
@inject ISubjectService Service

<BdkLoader Load="LoadAsync" CanRetry CanRetryTitle="Retry">
    <ul>
        @foreach (var item in context)
        {
            <li>@item.Id - @item.CreatedOn</li>
        }
    </ul>
</BdkLoader>

@code
{
    private async Task<ResponseItem[]> LoadAsync()
    {
        await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
        return await Service.ListAsync();
    }
}

Basic with error, CanRetry and local custom ErrorContent

Subject not found in the database
Blazor
@inject ISubjectService Service

<BdkLoader Load="LoadAsync" CanRetry CanRetryTitle="Retry">
    <ErrorContent Context="errorResult">

        <div class="d-flex flex-column align-items-center justify-content-center">
            <div class="mt-3 text-info">
                @errorResult.Exception.Message
            </div>

            @if (errorResult.Loader.CanRetry)
            {
                <div class="mt-3">
                    <button class="btn btn-danger" @onclick="errorResult.Loader.ReloadAsync">@errorResult.Loader.CanRetryTitle</button>
                </div>
            }
        </div>
        
    </ErrorContent>
    <ChildContent>
        Content here
    </ChildContent>
</BdkLoader>

@code
{
    private async Task<ResponseItem[]> LoadAsync()
    {
        await Service.ProcessAsync(throwErrorAfterProcess: true); // Simulate error
        return await Service.ListAsync();
    }
}

Basic with PreserveState

When the PreserveState mode is enabled, the BdkLoader will attempt to preserve the result of the Load function, avoiding going back to the server and causing an unwanted “Reload.”

  • Item: df240aee-9bba-4531-a5a3-649b54c9714d
  • Item: 02e09546-6cee-4203-a4bf-6db95b8233ab
  • Item: 3a0ad016-dfad-496c-ad4f-f414ccf8af79
  • Item: 8b32bc88-9e8d-418f-a274-b6676db183b1
  • Item: f9b5dd59-4fcb-4637-9aeb-fac5e7d90d81
Blazor
@inject ISubjectService Service

<BdkLoader Load="Service.ListAsync" PreserveState >
    <ul>
        @foreach (var result in context)
        {
            <li>Item: @result.Id</li>
        }
    </ul>
</BdkLoader>

Basic with PreserveState and not result

Content displayed after loading

Blazor
@inject ISubjectService Service

<BdkLoader T="object?" Load="LoadAsync" PreserveState>
    <p>
        Content displayed after loading
    </p>
</BdkLoader>

@code
{
    private async Task<object?> LoadAsync()
    {
        await Service.ProcessAsync();
        return null;
    }
}