@inject IJSRuntime jsRuntime @implements IDialog @code { [Parameter] public string? Title { get; set; } [Parameter] public RenderFragment ChildContent { get; set; } [Parameter] public int WidthPixels { set => width = $"{value}px"; } [Parameter] public int HeightPixels { set => height = $"{value}px"; } [Parameter] public int HeightPercent { set => height = $"{value}%"; } public bool Visible { get => visible; set { visible = value; jsRuntime.InvokeVoidAsync( "setDialogVisibility", new object?[] { dialogDiv, value }); } } private bool visible = false; private string? width; private string? height; private ElementReference dialogDiv; public void Show() => Visible = true; public void Hide() => Visible = false; protected override async void OnAfterRender(bool firstRender) { if(firstRender) { await jsRuntime.InvokeVoidAsync("dialogAddObjectReference", new object[] { dialogDiv, DotNetObjectReference.Create(this) }); } } [JSInvokable("KeyHandler")] public void KeyHandler(string key) { if(key == "Escape") { Hide(); return; } } private string sizeStyle => widthStyle + heightStyle; private string widthStyle => $"{(width is null ? "" : $"width:{width};")}"; private string heightStyle => $"{(height is null ? "" : $"max-height:{height};")}"; }