Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disposal race condition #488

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Blazored.Modal/Blazored.Modal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<Authors>Chris Sainty</Authors>
<Authors>Matěj Štágl, Chris Sainty</Authors>
<Company></Company>
<Copyright>Copyright 2021 (c) Chris Sainty. All rights reserved.</Copyright>

Expand All @@ -14,7 +14,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<!-- NuGet settings -->
<PackageId>Blazored.Modal</PackageId>
<PackageId>BlazorModalExperimental</PackageId>
<PackageTags>Blazored;Blazor;Razor;Components;Modal;Dialogue;ASP.NET Core;CSharp;Web</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/Blazored/Modal</PackageProjectUrl>
Expand All @@ -25,6 +25,9 @@
<!-- SourceLink settings -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Version>1.0.4</Version>
<PackageReleaseNotes>fix</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand Down
24 changes: 20 additions & 4 deletions src/Blazored.Modal/BlazoredModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
[Parameter] public ModalAnimationType? AnimationType { get; set; }
[Parameter] public bool? UseCustomLayout { get; set; }
[Parameter] public bool? ActivateFocusTrap { get; set; }
[Parameter] public int? CloseDelay { get; set; }
[Parameter] public string? CloseCss { get; set; }

private readonly Collection<ModalReference> _modals = new();
private readonly ModalOptions _globalModalOptions = new();
Expand Down Expand Up @@ -61,13 +63,19 @@

_globalModalOptions.UseCustomLayout = UseCustomLayout;
_globalModalOptions.ActivateFocusTrap = ActivateFocusTrap;
_globalModalOptions.CloseDelay = CloseDelay;
_globalModalOptions.CloseCss = CloseCss;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_styleFunctions = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/Blazored.Modal/BlazoredModal.razor.js");
try
{
_styleFunctions = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/Blazored.Modal/BlazoredModal.razor.js");
}
catch {}
}
}

Expand Down Expand Up @@ -131,7 +139,11 @@
_haveActiveModals = true;
if (_styleFunctions is not null)
{
await _styleFunctions.InvokeVoidAsync("setBodyStyle");
try
{
await _styleFunctions.InvokeVoidAsync("setBodyStyle");
}
catch {}
}
}

Expand All @@ -140,13 +152,17 @@

private ModalReference? GetModalReference(Guid id)
=> _modals.SingleOrDefault(x => x.Id == id);

private async Task ClearBodyStyles()
{
_haveActiveModals = false;
if (_styleFunctions is not null)
{
await _styleFunctions.InvokeVoidAsync("removeBodyStyle");
try
{
await _styleFunctions.InvokeVoidAsync("removeBodyStyle");
}
catch {}
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/Blazored.Modal/BlazoredModalInstance.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public partial class BlazoredModalInstance : IDisposable
private bool ActivateFocusTrap { get; set; }
public bool UseCustomLayout { get; set; }
public FocusTrap? FocusTrap { get; set; }
private int? CloseDelay { get; set; }
private string? CloseCssClas { get; set; }


[SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "This is assigned in Razor code and isn't currently picked up by the tooling.")]
Expand Down Expand Up @@ -100,6 +102,13 @@ public async Task CloseAsync(ModalResult modalResult)

await Task.Delay(400); // Needs to be a bit more than the animation time because of delays in the animation being applied between server and client (at least when using blazor server side), I think.
}
else if (AnimationType is ModalAnimationType.Custom)
{
OverlayAnimationClass += $" {CloseCssClas}";
StateHasChanged();

await Task.Delay(CloseDelay ?? 0);
}

await Parent.DismissInstance(Id, modalResult);
}
Expand Down Expand Up @@ -129,6 +138,8 @@ private void ConfigureInstance()
ActivateFocusTrap = SetActivateFocusTrap();
OverlayAnimationClass = SetAnimationClass();
Parent.OnModalClosed += AttemptFocus;
CloseDelay = SetCloseDelay();
CloseCssClas = SetCloseCss();
}

private void AttemptFocus()
Expand Down Expand Up @@ -267,9 +278,11 @@ private string SetModalClass()

private ModalAnimationType SetAnimation()
=> Options.AnimationType ?? GlobalModalOptions.AnimationType ?? ModalAnimationType.FadeInOut;

private int SetCloseDelay() => Options.CloseDelay ?? GlobalModalOptions.CloseDelay ?? 0;
private string SetCloseCss() => Options.CloseCss ?? GlobalModalOptions.CloseCss ?? "";

private string SetAnimationClass()
=> AnimationType is ModalAnimationType.FadeInOut ? "fade-in" : string.Empty;
private string SetAnimationClass() => AnimationType is ModalAnimationType.FadeInOut ? "fade-in" : AnimationType is ModalAnimationType.Custom ? OverlayCustomClass ?? "" : string.Empty;

private bool SetHideHeader()
{
Expand Down
6 changes: 5 additions & 1 deletion src/Blazored.Modal/CascadingBlazoredModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
OverlayCustomClass="@OverlayCustomClass"
Size="@Size"
SizeCustomClass="@SizeCustomClass"
ActivateFocusTrap="@ActivateFocusTrap"/>
ActivateFocusTrap="@ActivateFocusTrap"
CloseCss="@CloseCss"
CloseDelay="CloseDelay"/>
@ChildContent
</CascadingValue>

Expand All @@ -31,4 +33,6 @@
[Parameter] public bool? ActivateFocusTrap { get; set; }
[Parameter] public string? PositionCustomClass { get; set; }
[Parameter] public string? SizeCustomClass { get; set; }
[Parameter] public int? CloseDelay { get; set; }
[Parameter] public string? CloseCss { get; set; }
}
3 changes: 2 additions & 1 deletion src/Blazored.Modal/Configuration/ModalAnimationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum ModalAnimationType
{
FadeInOut,
None
None,
Custom
}
2 changes: 2 additions & 0 deletions src/Blazored.Modal/Configuration/ModalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class ModalOptions
public ModalAnimationType? AnimationType { get; set; }
public bool? UseCustomLayout { get; set; }
public bool? ActivateFocusTrap { get; set; }
public int? CloseDelay { get; set; }
public string? CloseCss { get; set; }
}