fix: Focus problems with `FluentDialog`
🐛 Bug Report
When opening more than one FluentDialog without the dialog service, I can't focus anything anymore and I get the following error:
Uncaught RangeError: Maximum call stack size exceeded.
at Po.focusFirstElement (web-components-v2.5.16.min.js:21:3198)
at Po.handleDocumentFocus (web-components-v2.5.16.min.js:21:2694)
at Po.focusFirstElement (web-components-v2.5.16.min.js:21:3198)
at Po.handleDocumentFocus (web-components-v2.5.16.min.js:21:2694)
at Po.focusFirstElement (web-components-v2.5.16.min.js:21:3198)
at Po.handleDocumentFocus (web-components-v2.5.16.min.js:21:2694)
at Po.focusFirstElement (web-components-v2.5.16.min.js:21:3198)
at Po.handleDocumentFocus (web-components-v2.5.16.min.js:21:2694)
at Po.focusFirstElement (web-components-v2.5.16.min.js:21:3198)
at Po.handleDocumentFocus (web-components-v2.5.16.min.js:21:2694)
💁 Possible Solution
I don't know for sure, but I suppose this happens due to the TrapFocus parameter in FluentDialog being overriden when not using the service for showing dialogs.
I'm also seeing this error. It also happens when using the Dialog service and opening more than one Dialog. (In my case opening a new dialog from another.)
//Action from OnClick of a button to replicate.
public async Task Open()
{
IDialogReference dialog = await DialogService.ShowDialogAsync(typeof(SearchDialog<Models.Contact>), ContactSearchRequest, new DialogParameters()
{
Title = "Search",
Alignment = HorizontalAlignment.Center,
Width = "90vw",
Height = "90vh"
});
IDialogReference dialog2 = await DialogService.ShowDialogAsync(typeof(SearchDialog<Models.Contact>), ContactSearchRequest, new DialogParameters()
{
Title = "Search",
Alignment = HorizontalAlignment.Center,
Width = "80vw",
Height = "80vh"
});
}
Opening Multiple Dialogs or opening Dialogs from inside another Dialog is not an option we support as there is strong advice against that in the Fluent guidelines.
If the problem is indeed caused by the TrapFocus parameter, is is not even going to be solvable form our side as we merely transport it to the underlying web component
Set the TrapFocus parameter to false
You can handle this problem quite easily by “closing” the previous dialog and then possibly opening it again depending on the action of the following dialog. My favorite scenario is a request message box with yes/no (as we know it from the windows world).
To make dialog management easier, a model with corresponding methods such as open or close is stored for each dialog:
public class FormModel<T> where T : new()
{
public string Titel { get; set; } = "";
public bool Hidden { get; set; } = true;
public bool Visible { get; set; } = false;
public bool Modal { get; set; } = false;
public bool TrapFocus { get; set; } = true;
public bool TrapFocusSub { get; set; } = true;
public bool PreventScroll { get; set; } = true;
public void OpenModal()
{
Hidden = false;
}
public void CloseModal()
{
Hidden = true;
}
etc.
After that, closing and opening modal forms is easy, you just have to work out the logic so that it does not happen that a form is no longer opened, although this would be expected:
FluentDialog.CloseModal();
await SetMessageDialogParameter(
Title: UserMana!.T("Close"),
Subtitle: "",
Message: UserMana!.T("Close modal dialog?"),
Icon: new Icons.Regular.Size32.QuestionCircle(),
PrimaryButton: UserMana!.T("Yes"),
SecondaryButton: UserMana!.T("No")
);
if (MessageDialog != null && parameters != null)
{
IDialogReference dialog = await DialogService!.ShowDialogAsync<MessageDialog>(MessageDialog!, parameters!);
DialogResult? resultdialog = await dialog.Result;
if (resultdialog.Data is not null)
etc.
By the way, I don't think it's so bad to do it this way, because it increases the visibility of the app if only one modal dialog is displayed.
Also, since these are modal windows, you can only have one window active at a time, not two or three windows at the same time! So it really doesn't matter whether the previous window is visible in the background or not, because it is fully covered by the active window.
pc
My problem was i opened a new Dialog from the Panel which is technically also a Dialog.
The solution was to simple call _myPanelDialog!.Hide(); before opening the Dialog. Where _myPanelDialog is the ref of the dialog:
<FluentDialog @ref="_myPanelDialog" ...
I don't mind it but can understand that for someone it can be a UX Problem.
Yeah, easy workaround: Inject the dialog (if not directly extending FluentDialog):
[CascadingParameter] public FluentDialog Dialog { get; set; } = null!;
then call Dialog.Hide() before opening another dialog and then Dialog.Show() after the new dialog has completed to re-show the first one.