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

[HxModal] New event "OnHiding" with ability to cancel the hiding. #533

Merged
merged 5 commits into from
Jun 28, 2023
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion Havit.Blazor.Components.Web.Bootstrap/Modals/HxModal.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ static HxModal()
[Parameter] public string FooterCssClass { get; set; }
protected string FooterCssClassEffective => this.FooterCssClass ?? this.GetSettings()?.FooterCssClass ?? GetDefaults().FooterCssClass;

/// <summary>
/// This event is fired immediately when the hide instance method has been called.<br/>
/// This can be caused by <see cref="HideAsync"/>, close-button, <kbd>Esc</kbd> key or other interaction.
/// Hiding can be cancelled by setting <see cref="ModalHidingEventArgs.Cancel"/> = <c>true</c>
/// </summary>
[Parameter] public EventCallback<ModalHidingEventArgs> OnHiding { get; set; }

/// <summary>
/// This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).<br/>
/// This can be caused by <see cref="HideAsync"/>, close-button, <kbd>Esc</kbd> key or other interaction.
Expand Down Expand Up @@ -220,6 +227,17 @@ public async Task HideAsync()
await jsModule.InvokeVoidAsync("hide", modalElement);
}

/// <summary>
/// Receives notification from JS for <c>hide.bs.modal</c> event.
/// </summary>
[JSInvokable("HxModal_HandleModalHide")]
public async Task<bool> HandleModalHide()
{
var eventArgs = new ModalHidingEventArgs();
await OnHiding.InvokeAsync(eventArgs);
return eventArgs.Cancel;
}

/// <summary>
/// Receives notification from JS for <c>hidden.bs.modal</c> event.
/// </summary>
Expand Down Expand Up @@ -256,7 +274,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
return;
}
await jsModule.InvokeVoidAsync("show", modalElement, dotnetObjectReference, this.CloseOnEscapeEffective);
await jsModule.InvokeVoidAsync("show", modalElement, dotnetObjectReference, this.CloseOnEscapeEffective, OnHiding.HasDelegate);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Havit.Blazor.Components.Web.Bootstrap;

public class ModalHidingEventArgs

{
/// <summary>
/// Set <c>true</c> to cancel modal hiding.
/// </summary>
public bool Cancel { get; set; }
}
22 changes: 20 additions & 2 deletions Havit.Blazor.Components.Web.Bootstrap/wwwroot/HxModal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export function show(element, hxModalDotnetObjectReference, closeOnEscape) {
export function show(element, hxModalDotnetObjectReference, closeOnEscape, subscribeToHideEvent) {
if (!element) {
return;
}

element.hxModalDotnetObjectReference = hxModalDotnetObjectReference;
if (subscribeToHideEvent)
element.addEventListener('hide.bs.modal', handleModalHide);
element.addEventListener('hidden.bs.modal', handleModalHidden);
element.addEventListener('shown.bs.modal', handleModalShown);

Expand All @@ -26,6 +28,21 @@ function handleModalShown(event) {
event.target.hxModalDotnetObjectReference.invokeMethodAsync('HxModal_HandleModalShown');
};

async function handleModalHide(event) {
let modalInstance = bootstrap.Modal.getInstance(event.target);

if (modalInstance.hidePreventionDisabled)
return;

event.preventDefault();

let cancel = await event.target.hxModalDotnetObjectReference.invokeMethodAsync('HxModal_HandleModalHide');
if (!cancel) {
modalInstance.hidePreventionDisabled = true;
modalInstance.hide();
}
};

function handleModalHidden(event) {
event.target.hxModalDotnetObjectReference.invokeMethodAsync('HxModal_HandleModalHidden');
dispose(event.target);
Expand All @@ -36,8 +53,9 @@ export function dispose(element) {
return;
}

element.removeEventListener('hide.bs.modal', handleModalHide);
element.removeEventListener('hidden.bs.modal', handleModalHidden);
element.removeEventListener('shown.bs.modal', handleModalHidden);
element.removeEventListener('shown.bs.modal', handleModalShown);
element.hxModalDotnetObjectReference = null;

var modal = bootstrap.Modal.getInstance(element);
Expand Down
Loading