@inject IJSRuntime JS
@* A modal dialog (SPEC ยง10.7): title with icon, content, buttons at the bottom right. Escape and the close button
call OnClose; the focus moves into the dialog when it opens. *@
@if (Open)
{
@if (Icon is not null)
{
}
@Title
@ChildContent
@if (Footer is not null)
{
@Footer
}
}
@code {
private static int _next;
private readonly string _id = "kh-dlg" + Interlocked.Increment(ref _next).ToString(System.Globalization.CultureInfo.InvariantCulture);
private ElementReference _dialog;
private bool _wasOpen;
/// Whether the dialog is shown.
[Parameter]
public bool Open { get; set; }
/// The title.
[Parameter]
public string? Title { get; set; }
/// The icon before the title.
[Parameter]
public string? Icon { get; set; }
/// The width: sm (420 px), md (640 px), lg (860 px).
[Parameter]
public string Size { get; set; } = "md";
/// Called by Escape and the close button.
[Parameter]
public EventCallback OnClose { get; set; }
/// The content.
[Parameter]
public RenderFragment? ChildContent { get; set; }
/// The buttons.
[Parameter]
public RenderFragment? Footer { get; set; }
private string SizeClass => "kh-" + Size;
///
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (Open && !_wasOpen)
{
await JS.InvokeVoidAsync("khaemhat.focusFirst", _dialog);
}
_wasOpen = Open;
}
private async Task OnKeyDownAsync(KeyboardEventArgs e)
{
if (e.Key == "Escape")
{
await CloseAsync();
}
}
private Task CloseAsync() => OnClose.InvokeAsync();
}