From 2724d4bbd5614d5771d1b7e42869fac23dae9e73 Mon Sep 17 00:00:00 2001 From: lofcz Date: Tue, 10 Jan 2023 03:41:30 +0100 Subject: [PATCH 1/2] Fix disposal race condition Update BlazoredModal.razor --- src/Blazored.Modal/BlazoredModal.razor | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Blazored.Modal/BlazoredModal.razor b/src/Blazored.Modal/BlazoredModal.razor index fe16c9d0..55b166c1 100644 --- a/src/Blazored.Modal/BlazoredModal.razor +++ b/src/Blazored.Modal/BlazoredModal.razor @@ -67,7 +67,11 @@ { if (firstRender) { - _styleFunctions = await JsRuntime.InvokeAsync("import", "./_content/Blazored.Modal/BlazoredModal.razor.js"); + try + { + _styleFunctions = await JsRuntime.InvokeAsync("import", "./_content/Blazored.Modal/BlazoredModal.razor.js"); + } + catch {} } } @@ -131,7 +135,11 @@ _haveActiveModals = true; if (_styleFunctions is not null) { - await _styleFunctions.InvokeVoidAsync("setBodyStyle"); + try + { + await _styleFunctions.InvokeVoidAsync("setBodyStyle"); + } + catch {} } } @@ -140,13 +148,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 {} } } From bbb9d68d65c0914854bbcc391283cc31e441a78b Mon Sep 17 00:00:00 2001 From: lofcz Date: Fri, 24 Mar 2023 04:06:30 +0100 Subject: [PATCH 2/2] Custom close delay and css support --- src/Blazored.Modal/Blazored.Modal.csproj | 7 +++++-- src/Blazored.Modal/BlazoredModal.razor | 4 ++++ .../BlazoredModalInstance.razor.cs | 17 +++++++++++++++-- src/Blazored.Modal/CascadingBlazoredModal.razor | 6 +++++- .../Configuration/ModalAnimationType.cs | 3 ++- .../Configuration/ModalOptions.cs | 2 ++ 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Blazored.Modal/Blazored.Modal.csproj b/src/Blazored.Modal/Blazored.Modal.csproj index ef116780..1eed6ebf 100644 --- a/src/Blazored.Modal/Blazored.Modal.csproj +++ b/src/Blazored.Modal/Blazored.Modal.csproj @@ -5,7 +5,7 @@ enable enable - Chris Sainty + Matěj Štágl, Chris Sainty Copyright 2021 (c) Chris Sainty. All rights reserved. @@ -14,7 +14,7 @@ snupkg - Blazored.Modal + BlazorModalExperimental Blazored;Blazor;Razor;Components;Modal;Dialogue;ASP.NET Core;CSharp;Web MIT https://github.com/Blazored/Modal @@ -25,6 +25,9 @@ true true + 1.0.4 + fix + true diff --git a/src/Blazored.Modal/BlazoredModal.razor b/src/Blazored.Modal/BlazoredModal.razor index 55b166c1..d25158ea 100644 --- a/src/Blazored.Modal/BlazoredModal.razor +++ b/src/Blazored.Modal/BlazoredModal.razor @@ -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 _modals = new(); private readonly ModalOptions _globalModalOptions = new(); @@ -61,6 +63,8 @@ _globalModalOptions.UseCustomLayout = UseCustomLayout; _globalModalOptions.ActivateFocusTrap = ActivateFocusTrap; + _globalModalOptions.CloseDelay = CloseDelay; + _globalModalOptions.CloseCss = CloseCss; } protected override async Task OnAfterRenderAsync(bool firstRender) diff --git a/src/Blazored.Modal/BlazoredModalInstance.razor.cs b/src/Blazored.Modal/BlazoredModalInstance.razor.cs index 5fc5ca2c..d179bbca 100644 --- a/src/Blazored.Modal/BlazoredModalInstance.razor.cs +++ b/src/Blazored.Modal/BlazoredModalInstance.razor.cs @@ -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.")] @@ -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); } @@ -129,6 +138,8 @@ private void ConfigureInstance() ActivateFocusTrap = SetActivateFocusTrap(); OverlayAnimationClass = SetAnimationClass(); Parent.OnModalClosed += AttemptFocus; + CloseDelay = SetCloseDelay(); + CloseCssClas = SetCloseCss(); } private void AttemptFocus() @@ -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() { diff --git a/src/Blazored.Modal/CascadingBlazoredModal.razor b/src/Blazored.Modal/CascadingBlazoredModal.razor index 15357ad6..24633136 100644 --- a/src/Blazored.Modal/CascadingBlazoredModal.razor +++ b/src/Blazored.Modal/CascadingBlazoredModal.razor @@ -12,7 +12,9 @@ OverlayCustomClass="@OverlayCustomClass" Size="@Size" SizeCustomClass="@SizeCustomClass" - ActivateFocusTrap="@ActivateFocusTrap"/> + ActivateFocusTrap="@ActivateFocusTrap" + CloseCss="@CloseCss" + CloseDelay="CloseDelay"/> @ChildContent @@ -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; } } diff --git a/src/Blazored.Modal/Configuration/ModalAnimationType.cs b/src/Blazored.Modal/Configuration/ModalAnimationType.cs index 17f7c076..29135856 100644 --- a/src/Blazored.Modal/Configuration/ModalAnimationType.cs +++ b/src/Blazored.Modal/Configuration/ModalAnimationType.cs @@ -3,5 +3,6 @@ public enum ModalAnimationType { FadeInOut, - None + None, + Custom } \ No newline at end of file diff --git a/src/Blazored.Modal/Configuration/ModalOptions.cs b/src/Blazored.Modal/Configuration/ModalOptions.cs index 702284fe..983df418 100644 --- a/src/Blazored.Modal/Configuration/ModalOptions.cs +++ b/src/Blazored.Modal/Configuration/ModalOptions.cs @@ -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; } } \ No newline at end of file