Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 33 additions & 3 deletions src/Core.Scripts/src/Components/Dialog/FluentDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@ export namespace Microsoft.FluentUI.Blazor.Components.Dialog {
}

/**
* Hide the fluent-dialog with the given id
* @param id The id of the fluent-dialog to hide
*/
* Hide the fluent-dialog with the given id
* @param id The id of the fluent-dialog to hide
*/
export function Hide(id: string): void {
const dialog = document.getElementById(id) as any;
dialog?.hide();
FocusOnPreviousActiveElement(id);
}

/**
* Save the element that was active before the dialog was opened
* @param id
*/
export function DialogToggle_PreviousActiveElement(id: string, newState: string): void {
const dialog = document.getElementById(id) as any;
if (dialog) {
if (newState === 'open') {
dialog.previousActiveElement = document.activeElement;
}
else if (newState === 'closed') {
FocusOnPreviousActiveElement(id);
}
}
}

/**
* Focus on the element that was active before the dialog was opened
* @param id
*/
export function FocusOnPreviousActiveElement(id: string): void {
const dialog = document.getElementById(id) as any;
if (dialog) {
setTimeout(() => {
dialog.previousActiveElement?.focus();
}, 25);
}
}
}
5 changes: 4 additions & 1 deletion src/Core.Scripts/src/FluentUICustomEvents.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Microsoft as FluentDialogFile } from "./Components/Dialog/FluentDialog";

export namespace Microsoft.FluentUI.Blazor.FluentUICustomEvents {

/**
Expand Down Expand Up @@ -28,6 +30,7 @@ export namespace Microsoft.FluentUI.Blazor.FluentUICustomEvents {
blazor.registerCustomEventType('dialogbeforetoggle', {
browserEventName: 'beforetoggle',
createEventArgs: (event: any) => {
FluentDialogFile.FluentUI.Blazor.Components.Dialog.DialogToggle_PreviousActiveElement(event.target.id, event.detail?.newState ?? event.newState);
return {
id: event.target.id,
type: event.type,
Expand Down Expand Up @@ -130,7 +133,7 @@ export namespace Microsoft.FluentUI.Blazor.FluentUICustomEvents {
newState: event.detail?.newState ?? event.newState,
};
}
});
});
}

// [^^^ Add your other custom events before this line ^^^]
Expand Down
8 changes: 2 additions & 6 deletions src/Core/Components/Dialog/FluentDialog.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.FluentUI.AspNetCore.Components;

/// <summary>
/// The dialog component is a window overlaid on either the primary window or another dialog window.
/// Windows under a modal dialog are inert.
/// Windows under a modal dialog are inert.
/// </summary>
public partial class FluentDialog : FluentComponentBase
{
Expand Down Expand Up @@ -75,11 +75,7 @@ public FluentDialog(LibraryConfiguration configuration) : base(configuration)
[Parameter]
public EventCallback<DialogEventArgs> OnStateChange { get; set; }

/// <summary>
///
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
/// <summary />
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && LaunchedFromService)
Expand Down
11 changes: 8 additions & 3 deletions src/Core/Components/Dialog/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;

namespace Microsoft.FluentUI.AspNetCore.Components;

Expand All @@ -13,6 +15,7 @@ namespace Microsoft.FluentUI.AspNetCore.Components;
public partial class DialogService : FluentServiceBase<IDialogInstance>, IDialogService
{
private readonly IServiceProvider _serviceProvider;
private readonly IJSRuntime _jsRuntime;

/// <summary>
/// Initializes a new instance of the <see cref="DialogService"/> class.
Expand All @@ -25,6 +28,7 @@ public partial class DialogService : FluentServiceBase<IDialogInstance>, IDialog
public DialogService(IServiceProvider serviceProvider, IFluentLocalizer? localizer)
{
_serviceProvider = serviceProvider;
_jsRuntime = serviceProvider.GetRequiredService<IJSRuntime>();
Localizer = localizer ?? FluentLocalizerInternal.Default;
}

Expand Down Expand Up @@ -104,11 +108,11 @@ public virtual async Task<DialogResult> ShowDialogAsync([DynamicallyAccessedMemb
/// <param name="dialog"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
internal Task RemoveDialogFromProviderAsync(IDialogInstance? dialog)
internal async Task RemoveDialogFromProviderAsync(IDialogInstance? dialog)
{
if (dialog is null)
{
return Task.CompletedTask;
return;
}

// Remove the HTML code from the DialogProvider
Expand All @@ -117,6 +121,7 @@ internal Task RemoveDialogFromProviderAsync(IDialogInstance? dialog)
throw new InvalidOperationException($"Failed to remove dialog from DialogProvider: the ID '{dialog.Id}' doesn't exist in the DialogServiceProvider.");
}

return ServiceProvider.OnUpdatedAsync.Invoke(dialog);
await ServiceProvider.OnUpdatedAsync.Invoke(dialog);
await _jsRuntime.InvokeVoidAsync("Microsoft.FluentUI.Blazor.Components.Dialog.FocusOnPreviousActiveElement", dialog.Id);
}
}
Loading