-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPageLocker.cs
55 lines (43 loc) · 1.47 KB
/
PageLocker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
namespace Mirality.Blazor.Routing;
/// <summary>Component which conditionally locks navigation on a page.</summary>
public class PageLocker : ComponentBase, IDisposable
{
[Inject] private ILockableNavigationManager Nav { get; set; } = default!;
/// <summary>When true, navigation is locked.</summary>
[Parameter] public bool IsLocked { get; set; }
/// <summary>Raised when navigation is attempted while locked.</summary>
[Parameter] public EventCallback<LocationChangedEventArgs> NavigationBlocked { get; set; }
private IDisposable? _Lock;
/// <inheritdoc />
protected override void OnInitialized()
{
Nav.NavigationBlocked += Nav_NavigationBlocked;
base.OnInitialized();
}
/// <inheritdoc />
public void Dispose()
{
Nav.NavigationBlocked -= Nav_NavigationBlocked;
_Lock?.Dispose();
}
/// <inheritdoc />
protected override void OnParametersSet()
{
if (_Lock == null && IsLocked)
{
_Lock = Nav.LockNavigation();
}
else if (_Lock != null && !IsLocked)
{
_Lock.Dispose();
_Lock = null;
}
base.OnParametersSet();
}
private void Nav_NavigationBlocked(object? sender, LocationChangedEventArgs e)
{
_ = NavigationBlocked.InvokeAsync(e);
}
}