Skip to content

Commit

Permalink
Convert modals to using JS instead of C# for displaying.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Mar 7, 2024
1 parent 26f521d commit c6c1c21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
37 changes: 14 additions & 23 deletions Server/Components/ModalHarness.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@inject IModalService ModalService
@inject IJsInterop JsInterop
@inject IJSRuntime JsRuntime

<div class="modal fade @_showClass" style="display: @_displayStyle; background-color: rgba(0,0,0,0.35)" @onclick="CloseModal">
<div @ref="_modalRef" id="defaultModal" class="modal fade" style="background-color: rgba(0,0,0,0.35)">
<div class="modal-dialog modal-dialog-scrollable" role="document" @onclick:stopPropagation>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@ModalService.Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="alert" @onclick="CloseModal"></button>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body" >
@if (ModalService.RenderBody is not null)
Expand All @@ -27,9 +28,9 @@
<div class="modal-footer">
@foreach (var button in ModalService.Buttons)
{
<button @key="button" type="button" class="@button.Class" @onclick="() => ExecuteButtonAction(button.OnClick)">@button.Text</button>
<button @key="button" type="button" data-bs-dismiss="modal" class="@button.Class" @onclick="() => ExecuteButtonAction(button.OnClick)">@button.Text</button>
}
<button type="button" class="btn btn-secondary" data-dismiss="modal" @onclick="CloseModal">Close</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
Expand All @@ -39,36 +40,26 @@
private string? _showClass;

Check warning on line 40 in Server/Components/ModalHarness.razor

View workflow job for this annotation

GitHub Actions / build (Release)

The field 'ModalHarness._showClass' is never used
private string? _displayStyle;

Check warning on line 41 in Server/Components/ModalHarness.razor

View workflow job for this annotation

GitHub Actions / build (Release)

The field 'ModalHarness._displayStyle' is never used

protected override Task OnAfterRenderAsync(bool firstRender)
private IJSObjectReference? _module;
private ElementReference _modalRef;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_module = await JsRuntime.InvokeAsync<IJSObjectReference>("import", "./Components/ModalHarness.razor.js");
ModalService.ModalShown += async (sender, args) =>
{
_displayStyle = "block";
await InvokeAsync(StateHasChanged);
// The fade animation won't work without a delay here.
await Task.Delay(100);
_showClass = "show";
await _module.InvokeVoidAsync("showModal", _modalRef);
await InvokeAsync(StateHasChanged);
};
}
return base.OnAfterRenderAsync(firstRender);
}


private async Task CloseModal()
{
_showClass = null;
await InvokeAsync(StateHasChanged);
await Task.Delay(100);
_displayStyle = null;
await InvokeAsync(StateHasChanged);
await base.OnAfterRenderAsync(firstRender);
}

private async Task ExecuteButtonAction(Action onclick)
private Task ExecuteButtonAction(Action onclick)
{
onclick.Invoke();
await CloseModal();
return Task.CompletedTask;
}
}
8 changes: 8 additions & 0 deletions Server/Components/ModalHarness.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
* @param {HTMLElement} modal
*/
export function showModal(modal) {
const modalApi = new bootstrap.Modal(modal);
modalApi.show();
}

0 comments on commit c6c1c21

Please sign in to comment.