Name dialogs and contain native overlay keyboard focus
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
@implements IAsyncDisposable
|
||||
@inject IJSRuntime JS
|
||||
@using ZB.MOM.WW.ScadaBridge.CentralUI.Services
|
||||
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums
|
||||
|
||||
@@ -11,14 +13,15 @@
|
||||
{
|
||||
<div class="offcanvas-backdrop fade show" data-test="drawer-backdrop"
|
||||
@onclick="HandleClose"></div>
|
||||
<div class="offcanvas offcanvas-end show audit-drilldown-drawer"
|
||||
<div id="@_overlayId" role="dialog" aria-modal="true" aria-labelledby="@(_overlayId + "-title")"
|
||||
@onkeydown="HandleKeyDown" class="offcanvas offcanvas-end show audit-drilldown-drawer"
|
||||
tabindex="-1"
|
||||
style="visibility: visible;"
|
||||
data-test="audit-drilldown-drawer">
|
||||
<div class="offcanvas-header border-bottom">
|
||||
<div>
|
||||
<div class="text-muted small text-uppercase">Audit event</div>
|
||||
<h5 class="offcanvas-title mb-0">Audit Event @ShortEventId(Event.EventId)</h5>
|
||||
<h5 id="@(_overlayId + "-title")" class="offcanvas-title mb-0">Audit Event @ShortEventId(Event.EventId)</h5>
|
||||
</div>
|
||||
<button type="button" class="btn-close" aria-label="Close"
|
||||
data-test="drawer-close"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Microsoft.JSInterop;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
|
||||
|
||||
@@ -36,6 +37,39 @@ public partial class AuditDrilldownDrawer
|
||||
/// </summary>
|
||||
[Parameter] public EventCallback OnClose { get; set; }
|
||||
|
||||
private readonly string _overlayId = "sb-audit-drawer-" + Guid.NewGuid().ToString("N");
|
||||
private bool _activated;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
var visible = IsOpen && Event is not null;
|
||||
if (visible == _activated) return;
|
||||
_activated = visible;
|
||||
try
|
||||
{
|
||||
if (visible)
|
||||
{
|
||||
var activated = await JS.InvokeAsync<bool?>("sbDialog.activate", _overlayId);
|
||||
if (activated == false) await HandleClose();
|
||||
}
|
||||
else await JS.InvokeVoidAsync("sbDialog.deactivate", _overlayId);
|
||||
}
|
||||
catch (Microsoft.JSInterop.JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
try { await JS.InvokeVoidAsync("sbDialog.deactivate", _overlayId); }
|
||||
catch (Microsoft.JSInterop.JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
}
|
||||
|
||||
private Task HandleKeyDown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs e) =>
|
||||
e.Key == "Escape" ? HandleClose() : Task.CompletedTask;
|
||||
|
||||
private static string ShortEventId(Guid eventId)
|
||||
{
|
||||
// Mirror the "first 8 hex digits" presentation common across the UI.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Listens to DialogService.OnChange and renders the current dialog state.
|
||||
z-index ladder follows the same convention as ConfirmDialog/DiffDialog:
|
||||
Toast container 1090 > this modal 1055 > this backdrop 1040. *@
|
||||
@implements IDisposable
|
||||
@implements IAsyncDisposable
|
||||
@inject IDialogService Service
|
||||
@inject IJSRuntime JS
|
||||
|
||||
@@ -11,22 +11,24 @@
|
||||
@* `sb-modal-backdrop` is a tokenized hook: a separate task attaches the
|
||||
var-driven background rule to it. No inline style here. *@
|
||||
<div class="modal-backdrop fade show sb-modal-backdrop"></div>
|
||||
<div @ref="_modalRef"
|
||||
<div id="@_overlayId" @ref="_modalRef"
|
||||
class="modal fade show d-block"
|
||||
tabindex="-1"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="@TitleId"
|
||||
aria-describedby="@(state.Kind == DialogKind.Confirm ? BodyId : null)"
|
||||
@onkeydown="OnKeyDown">
|
||||
<div class="modal-dialog modal-dialog-centered@(state.Size is null ? "" : $" {state.Size}")" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">@state.Title</h5>
|
||||
<h5 id="@TitleId" class="modal-title">@state.Title</h5>
|
||||
<button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@if (state.Kind == DialogKind.Confirm)
|
||||
{
|
||||
<p class="mb-0">@state.Body</p>
|
||||
<p id="@BodyId" class="mb-0">@state.Body</p>
|
||||
}
|
||||
else if (state.Kind == DialogKind.Custom)
|
||||
{
|
||||
@@ -34,8 +36,8 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<label class="form-label">@state.Body</label>
|
||||
<input @ref="_promptInputRef"
|
||||
<label for="@PromptId" class="form-label">@state.Body</label>
|
||||
<input id="@PromptId" @ref="_promptInputRef"
|
||||
class="form-control form-control-sm"
|
||||
placeholder="@state.Placeholder"
|
||||
value="@_promptValue"
|
||||
@@ -63,6 +65,10 @@
|
||||
}
|
||||
|
||||
@code {
|
||||
private readonly string _overlayId = "sb-dialog-" + Guid.NewGuid().ToString("N");
|
||||
private string TitleId => _overlayId + "-title";
|
||||
private string BodyId => _overlayId + "-body";
|
||||
private string PromptId => _overlayId + "-prompt";
|
||||
private ElementReference _modalRef;
|
||||
private ElementReference _promptInputRef;
|
||||
private string _promptValue = string.Empty;
|
||||
@@ -77,9 +83,13 @@
|
||||
if (Service is DialogService svc) svc.OnChange += OnServiceChanged;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Service is DialogService svc) svc.OnChange -= OnServiceChanged;
|
||||
Cancel(); // A disposed host must not leave its local dialog result pending.
|
||||
try { await JS.InvokeVoidAsync("sbDialog.deactivate", _overlayId); }
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
}
|
||||
|
||||
private void OnServiceChanged()
|
||||
@@ -97,67 +107,32 @@
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
var current = (Service as DialogService)?.Current;
|
||||
if (current is not null)
|
||||
if (current is not null && !ReferenceEquals(current, _focusedForState))
|
||||
{
|
||||
try { await JS.InvokeVoidAsync("document.body.classList.add", "modal-open"); }
|
||||
catch { /* prerender: no JS — ignore */ }
|
||||
|
||||
// Focus once per opened dialog. Without this guard, every input
|
||||
// keystroke triggers a re-render which would re-focus the modal
|
||||
// element and yank the caret off the prompt input.
|
||||
if (!ReferenceEquals(current, _focusedForState))
|
||||
_focusedForState = current;
|
||||
try
|
||||
{
|
||||
_focusedForState = current;
|
||||
|
||||
// Stash the element that had focus when the dialog opened so it
|
||||
// can be restored on close. Must run BEFORE the modal steals
|
||||
// focus below, otherwise we'd capture a modal element.
|
||||
try { await JS.InvokeVoidAsync("sbDialog.captureActiveElement"); }
|
||||
catch { /* prerender: no JS — ignore */ }
|
||||
|
||||
try
|
||||
{
|
||||
if (current.Kind == DialogKind.Prompt)
|
||||
await _promptInputRef.FocusAsync();
|
||||
else
|
||||
await _modalRef.FocusAsync();
|
||||
}
|
||||
catch { /* element not yet attached: ignore */ }
|
||||
var activated = await JS.InvokeAsync<bool?>("sbDialog.activate", _overlayId,
|
||||
current.Kind == DialogKind.Prompt ? "#" + PromptId : null);
|
||||
if (activated == false) Cancel(); // Single-active-overlay contract.
|
||||
}
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
}
|
||||
else
|
||||
else if (current is null && _focusedForState is not null)
|
||||
{
|
||||
// Only clean up on the actual open→closed transition. Skipping when
|
||||
// _focusedForState is already null avoids spurious JS interop on
|
||||
// the initial render and every steady-state no-dialog render.
|
||||
if (_focusedForState is not null)
|
||||
{
|
||||
_focusedForState = null;
|
||||
try { await JS.InvokeVoidAsync("document.body.classList.remove", "modal-open"); }
|
||||
catch { /* prerender: no JS — ignore */ }
|
||||
|
||||
// Return focus to whatever element triggered the dialog so keyboard
|
||||
// users are not dumped at the top of the document on close.
|
||||
try { await JS.InvokeVoidAsync("sbDialog.restoreActiveElement"); }
|
||||
catch { /* prerender: no JS — ignore */ }
|
||||
}
|
||||
_focusedForState = null;
|
||||
try { await JS.InvokeVoidAsync("sbDialog.deactivate", _overlayId); }
|
||||
catch (JSDisconnectedException) { }
|
||||
catch (InvalidOperationException) { /* prerender */ }
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnKeyDown(KeyboardEventArgs e)
|
||||
private void OnKeyDown(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Escape")
|
||||
{
|
||||
Cancel();
|
||||
}
|
||||
else if (e.Key == "Tab")
|
||||
{
|
||||
// Trap Tab/Shift+Tab inside the modal so focus cannot escape to the
|
||||
// (inert) page behind the backdrop. The JS helper wraps focus at the
|
||||
// first/last focusable element and prevents the default move there.
|
||||
try { await JS.InvokeVoidAsync("sbDialog.focusTrap", _modalRef, e.ShiftKey); }
|
||||
catch { /* prerender / no JS: skip — native Tab order still applies */ }
|
||||
}
|
||||
// Native DOM keydown owns synchronous Tab containment; Escape retains
|
||||
// the service's existing typed cancellation path.
|
||||
if (e.Key == "Escape") Cancel();
|
||||
}
|
||||
|
||||
private void OnPromptInput(ChangeEventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user