Last active
April 8, 2022 10:27
-
-
Save usausa/8bdf81de05eb6500b5a9f532234f573b to your computer and use it in GitHub Desktop.
BusyState
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.showDialog = (id) => { | |
document.getElementById(id).showModal(); | |
} | |
window.closeDialog = (id) => { | |
document.getElementById(id).close(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
<button class="btn btn-primary" @onclick="OnExecuteClock">Execute</button> | |
@code { | |
[CascadingParameter] | |
[AllowNull] | |
public ProgressState Progress { get; set; } | |
private async Task OnExecuteClock() | |
{ | |
Progress.IsBusy = true; | |
await Task.Delay(3000); | |
Progress.IsBusy = false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
public class ProgressState : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler? PropertyChanged; | |
private bool isBusy; | |
public bool IsBusy | |
{ | |
get => isBusy; | |
set | |
{ | |
if (isBusy != value) | |
{ | |
isBusy = value; | |
OnPropertyChanged(); | |
} | |
} | |
} | |
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implements IDisposable | |
@inject IJSRuntime Script | |
<CascadingValue Value="@progress"> | |
@ChildContent | |
</CascadingValue> | |
<dialog id="loading" class="loading" style="visibility: @visibility"></dialog> | |
@code { | |
private readonly ProgressState progress = new(); | |
[Parameter] | |
[AllowNull] | |
public RenderFragment ChildContent { get; set; } | |
private string visibility = "hidden"; | |
protected override void OnInitialized() | |
{ | |
progress.PropertyChanged += ProgressOnPropertyChanged; | |
} | |
public void Dispose() | |
{ | |
progress.PropertyChanged -= ProgressOnPropertyChanged; | |
} | |
private void ProgressOnPropertyChanged(object? sender, PropertyChangedEventArgs e) | |
{ | |
StateHasChanged(); | |
visibility = progress.IsBusy ? "visible" : "hidden"; | |
Script.InvokeVoidAsync(progress.IsBusy ? "showDialog" : "closeDialog", "loading"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.loading { | |
left: 0; | |
top: 0; | |
width: 100%; | |
height: 100%; | |
max-width: 100%; | |
max-height: 100%; | |
border: 0; | |
margin: 0; | |
padding: 0; | |
background-color: #fff; | |
opacity: 0.5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment