@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}%"; } [Parameter] public int MinHeightPixels { set => minHeight = $"{value}px"; } [Parameter] public int MinHeightPercent { set => minHeight = $"{value}%"; } [Parameter] public int MaxHeightPixels { set => maxHeight = $"{value}px"; } [Parameter] public int MaxHeightPercent { set => maxHeight = $"{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 string? minHeight; private string? maxHeight; 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 => string.Join("", new[] { widthStyle, heightStyle, minHeightStyle, maxHeightStyle }); private string widthStyle => $"{(width is null ? "" : $"width:{width};")}"; private string heightStyle => $"{(height is null ? "" : $"height:{height};")}"; private string minHeightStyle => $"{(minHeight is null ? "" : $"min-height:{minHeight};")}"; private string maxHeightStyle => $"{(maxHeight is null ? "" : $"max-height:{maxHeight};")}"; }