@inherits DComponentBase @implements IDisposable @* daisyUI Modal: https://daisyui.com/components/modal/ . Always rendered; the open state lives in C# (@bind-IsOpen) and shows as modal-open. On opening, the box takes the focus (Blazor's FocusAsync) so that Escape, handled on the root, closes it; the backdrop button stretches over the area outside the box and closes it on click, and a visible close button (ShowClose, named by CloseText) sits beside the heading, which stays in view while the box scrolls. *@ @code { [Parameter] public RenderFragment Heading { get; set; } [Parameter] public RenderFragment ChildContent { get; set; } [Parameter] public RenderFragment Actions { get; set; } [Parameter] public bool IsOpen { get; set; } [Parameter] public EventCallback IsOpenChanged { get; set; } [Parameter] public DModalPlacement Placement { get; set; } /// Close on a click outside the box (renders the modal-backdrop). [Parameter] public bool CloseOnBackdrop { get; set; } = true; /// Name of the close button and of the invisible backdrop button, read by screen readers. [Parameter] public string CloseText { get; set; } = "close"; /// A visible close button beside the heading. [Parameter] public bool ShowClose { get; set; } = true; [Parameter] public string BoxClass { get; set; } /// Where it says it is open, so the Back button closes it (cascaded by the app; none, none said). [CascadingParameter] IDOverlayHost Overlays { get; set; } readonly string uid = "d" + Guid.NewGuid().ToString("N")[..8]; IDisposable overlay; ElementReference box; bool wasOpen; bool focusPending; string CssClass => Cx("modal", PlacementClass, IsOpen ? "modal-open" : null, Class); string PlacementClass => Placement switch { DModalPlacement.Top => "modal-top", DModalPlacement.Bottom => "modal-bottom", DModalPlacement.Start => "modal-start", DModalPlacement.End => "modal-end", DModalPlacement.ResponsiveBottom => "modal-bottom md:modal-middle", _ => null }; protected override void OnParametersSet() { if (IsOpen && !wasOpen) focusPending = true; wasOpen = IsOpen; TrackOverlay(); } void TrackOverlay() { if (IsOpen && overlay is null) overlay = Overlays?.Open(() => InvokeAsync(async () => { await SetOpen(false); StateHasChanged(); })); else if (!IsOpen && overlay is not null) { overlay.Dispose(); overlay = null; } } public void Dispose() => overlay?.Dispose(); protected override async Task OnAfterRenderAsync(bool firstRender) { if (!focusPending) return; focusPending = false; await box.FocusAsync(); } Task Close() => SetOpen(false); Task OnKeyDown(KeyboardEventArgs e) => e.Key == "Escape" && IsOpen ? SetOpen(false) : Task.CompletedTask; async Task SetOpen(bool isOpen) { IsOpen = isOpen; wasOpen = isOpen; TrackOverlay(); await IsOpenChanged.InvokeAsync(isOpen); } }