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
18 changes: 7 additions & 11 deletions src/Core.Scripts/src/Components/Dialog/FluentDialog.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
export namespace Microsoft.FluentUI.Blazor.Components.Dialog {

const getDeepActiveElement = (): HTMLElement | null => {
let activeElement: Element | null = document.activeElement;
while (activeElement instanceof HTMLElement && activeElement.shadowRoot?.activeElement) {
activeElement = activeElement.shadowRoot.activeElement;
}

return activeElement instanceof HTMLElement ? activeElement : null;
};

/**
* Tag names of non-modal, transient elements (e.g. toasts) that reuse the
* dialog toggle plumbing but must never restore focus when they open or close.
Expand Down Expand Up @@ -112,11 +103,16 @@ export namespace Microsoft.FluentUI.Blazor.Components.Dialog {
return false;
}

const activeElement = getDeepActiveElement();
if (!activeElement || !dialog.contains(activeElement)) {
const activeElement = document.activeElement;
if (!(activeElement instanceof HTMLElement) || !dialog.contains(activeElement)) {
return false;
}

// The dialog surface itself receives focus when no interactive content does (e.g. MessageBox).
if (activeElement === dialog) {
return true;
}

// Keep shortcuts active for explicit dialog action surfaces.
return !!activeElement.closest('[slot="action"], [slot="footer"], [slot="close"], [slot="title-action"]');
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Core/Components/Dialog/FluentMessageBoxTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public FluentMessageBoxTests()
{
JSInterop.Mode = JSRuntimeMode.Loose;
JSInterop.Setup<bool>("Microsoft.FluentUI.Blazor.Components.Dialog.ShouldHandleShortcut", _ => true).SetResult(true);
Services.AddFluentUIComponents(options => options.UseGlobalOverlay = false);

DialogService = Services.GetRequiredService<IDialogService>();
Expand Down Expand Up @@ -154,6 +155,22 @@
DialogProvider.Verify();
}

[Theory(Timeout = TEST_TIMEOUT)]
[InlineData("Y", false)]
[InlineData("N", true)]
public async Task ShowConfirmationAsync_ShortcutPressed_ReturnsExpectedResult(string shortcut, bool expectedCancelled)
Comment thread
dvoituron marked this conversation as resolved.
{
// Arrange
var dialogTask = DialogService.ShowConfirmationAsync("My message");

// Act
DialogProvider.Find("fluent-dialog").KeyDown(shortcut);
var result = await dialogTask;

// Assert
Assert.Equal(expectedCancelled, result.Cancelled);
}

[Fact(Timeout = TEST_TIMEOUT)]
public async Task FluentMessageBox_EmptyOptions()
{
Expand Down
Loading