Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/Core/Components/Dialog/FluentDialogProvider.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,34 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

private void ShowDialog(IDialogReference dialogReference, Type? dialogComponent, DialogParameters parameters, object content)
{
if (_module is null)
{
throw new InvalidOperationException("JS module is not loaded.");
}

InvokeAsync(async () =>
{
var previouslyFocusedElement = await _module.InvokeAsync<IJSObjectReference>("getActiveElement");
var previouslyFocusedElement = await GetPreviouslyFocusedElementAsync();

DialogInstance dialog = new(dialogComponent, parameters, content, previouslyFocusedElement);
dialogReference.Instance = dialog;

_internalDialogContext.References.Add(dialogReference);
});
}

private async Task<IDialogReference> ShowDialogAsync(IDialogReference dialogReference, Type? dialogComponent, DialogParameters parameters, object content)
private async Task<IJSObjectReference?> GetPreviouslyFocusedElementAsync()
{
if (_module is null)
// If the module hasn't been loaded then the page hasn't rendered yet, so there is no previously focused element.
IJSObjectReference? previouslyFocusedElement = null;
if (_module is not null)
{
throw new InvalidOperationException("JS module is not loaded.");
previouslyFocusedElement = await _module.InvokeAsync<IJSObjectReference>("getActiveElement");
}

return previouslyFocusedElement;
}

private async Task<IDialogReference> ShowDialogAsync(IDialogReference dialogReference, Type? dialogComponent, DialogParameters parameters, object content)
{
return await Task.Run(async () =>
{
var previouslyFocusedElement = await _module.InvokeAsync<IJSObjectReference>("getActiveElement");
var previouslyFocusedElement = await GetPreviouslyFocusedElementAsync();

DialogInstance dialog = new(dialogComponent, parameters, content, previouslyFocusedElement);
dialogReference.Instance = dialog;
Expand Down Expand Up @@ -166,12 +169,12 @@ internal void DismissInstance(string id, DialogResult result)

internal async Task ReturnFocusAsync(IJSObjectReference element)
{
if (_module is null)
// Module should always be loaded here, but check just in case.
if (_module is not null)
{
throw new InvalidOperationException("JS module is not loaded.");
await _module.InvokeVoidAsync("focusElement", element);
}

await _module.InvokeVoidAsync("focusElement", element);
await element.DisposeAsync();
}

Expand Down
Loading