diff --git a/components/core/Component/overlay/Overlay.razor.cs b/components/core/Component/overlay/Overlay.razor.cs index 337665ae..87b78040 100644 --- a/components/core/Component/overlay/Overlay.razor.cs +++ b/components/core/Component/overlay/Overlay.razor.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using System.Threading.Tasks; using AntDesign.JsInterop; using Microsoft.AspNetCore.Components; @@ -55,6 +56,9 @@ namespace AntDesign.Internal [Parameter] public bool HiddenMode { get; set; } = false; + [Inject] + private DomEventService DomEventService { get; set; } + private bool _hasAddOverlayToBody = false; private bool _isPreventHide = false; private bool _isChildOverlayShow = false; @@ -79,7 +83,7 @@ namespace AntDesign.Internal private const int VERTICAL_ARROW_SHIFT = 5; private int _overlayClientWidth = 0; - + protected override async Task OnParametersSetAsync() { if (!_isOverlayShow && Trigger.Visible && !_preVisible) @@ -100,6 +104,7 @@ namespace AntDesign.Internal if (firstRender) { await JsInvokeAsync(JSInteropConstants.AddClsToFirstChild, Ref, $"{Trigger.PrefixCls}-trigger"); + DomEventService.AddEventListener("window", "beforeunload", Reloading, false); } if (_lastDisabledState != Trigger.Disabled) @@ -130,7 +135,7 @@ namespace AntDesign.Internal protected override void Dispose(bool disposing) { - if (_hasAddOverlayToBody) + if (_hasAddOverlayToBody && !_isReloading) { _ = InvokeAsync(async () => { @@ -138,7 +143,7 @@ namespace AntDesign.Internal await JsInvokeAsync(JSInteropConstants.DelElementFrom, Ref, Trigger.PopupContainerSelector); }); } - + DomEventService.RemoveEventListerner("window", "beforeunload", Reloading); base.Dispose(disposing); } @@ -265,6 +270,12 @@ namespace AntDesign.Internal return _isOverlayHiding; } + /// + /// Indicates that a page is being refreshed + /// + private bool _isReloading; + private void Reloading(JsonElement jsonElement) => _isReloading = true; + private async Task AddOverlayToBody() { if (!_hasAddOverlayToBody) diff --git a/components/core/Component/overlay/OverlayTrigger.razor.cs b/components/core/Component/overlay/OverlayTrigger.razor.cs index 32a15700..564500cd 100644 --- a/components/core/Component/overlay/OverlayTrigger.razor.cs +++ b/components/core/Component/overlay/OverlayTrigger.razor.cs @@ -118,6 +118,7 @@ namespace AntDesign.Internal protected override void Dispose(bool disposing) { DomEventService.RemoveEventListerner("document", "mouseup", OnMouseUp); + DomEventService.RemoveEventListerner("window", "resize", OnMouseUp); base.Dispose(disposing); }