Skip to content
Closed
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
21 changes: 20 additions & 1 deletion src/Aspire.Dashboard/Components/Controls/AspireMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
using Microsoft.JSInterop;

namespace Aspire.Dashboard.Components;

Expand Down Expand Up @@ -33,6 +34,19 @@ public partial class AspireMenu : FluentComponentBase
[Parameter]
public required IReadOnlyList<MenuButtonItem> Items { get; set; }

/// <summary>
/// Gets or sets a value indicating whether focus should return to <see cref="Anchor"/> after a menu item is clicked.
/// </summary>
/// <remarks>
/// Use this only for button-anchored menus where <see cref="Anchor"/> identifies the element that opened the menu.
/// Do not enable it for cursor-positioned or context menus where <see cref="Anchor"/> is only used for positioning.
/// </remarks>
[Parameter]
public bool RestoreFocusOnItemClick { get; set; }

[Inject]
public required IJSRuntime JS { get; init; }

// Each menu item is approximately 32px tall, plus 16px padding for the menu container.
private const int EstimatedItemHeight = 32;
private const int MenuVerticalPadding = 16;
Expand Down Expand Up @@ -105,7 +119,12 @@ private async Task HandleItemClicked(MenuButtonItem item)
{
await onClick();
}
Open = false;
await OnOpenChanged(false);

if (RestoreFocusOnItemClick && !string.IsNullOrEmpty(Anchor))
{
await JS.InvokeVoidAsync("focusElement", Anchor);
}
}

private Task OnOpenChanged(bool open)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
}
</FluentButton>

<AspireMenu Anchor="@MenuButtonId" @bind-Open="@_visible" Items="_items" />
<AspireMenu Anchor="@MenuButtonId" @bind-Open="@_visible" Items="_items" RestoreFocusOnItemClick="@RestoreFocusOnItemClick" />
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public partial class AspireMenuButton : FluentComponentBase
[Parameter]
public bool HideIcon { get; set; }

/// <summary>
/// Gets or sets a value indicating whether focus should return to this menu button after a menu item is clicked.
/// </summary>
/// <remarks>
/// Use this for button-anchored menus because the underlying menu anchor is the element that opened the menu.
/// Do not use this behavior for cursor-positioned or context menus where the anchor is only used for positioning.
/// </remarks>
[Parameter]
public bool RestoreFocusOnItemClick { get; set; }

protected override void OnParametersSet()
{
_icon = Icon ?? s_defaultIcon;
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Dashboard/Components/Pages/Resources.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<AspireMenuButton ButtonAppearance="Appearance.Stealth"
Icon="@(new Icons.Regular.Size20.Options())"
Items="@_resourcesMenuItems"
RestoreFocusOnItemClick="true"
Title="@Loc[nameof(Dashboard.Resources.Resources.ResourcesChangeViewOptions)]"
slot="end" />
}
Expand Down
111 changes: 111 additions & 0 deletions tests/Aspire.Dashboard.Components.Tests/Controls/AspireMenuTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Dashboard.Components.Tests.Shared;
using Aspire.Dashboard.Model;
using Bunit;
using Microsoft.FluentUI.AspNetCore.Components;
using Xunit;

namespace Aspire.Dashboard.Components.Tests.Controls;

public class AspireMenuTests : DashboardTestContext
{
[Fact]
public void ClickItem_RestoreFocusOnItemClickTrue_FocusesAnchor()
{
FluentUISetupHelpers.SetupFluentUIComponents(this);
FluentUISetupHelpers.SetupFluentMenu(this);
FluentUISetupHelpers.SetupFluentAnchoredRegion(this);
FluentUISetupHelpers.SetupFluentButton(this);

var anchor = "view-options-button";
var itemClicked = false;
var focusElementInvocationHandler = JSInterop.SetupVoid("focusElement", anchor);
var focusElementInvocationsDuringOnClick = -1;
var items = new List<MenuButtonItem>
{
new()
{
Text = "Show hidden resources",
OnClick = () =>
{
focusElementInvocationsDuringOnClick = focusElementInvocationHandler.Invocations.Count;
Assert.True(
focusElementInvocationsDuringOnClick == 0,
$"Focus should not be restored until item OnClick completes. Actual focusElement invocations during OnClick: {focusElementInvocationsDuringOnClick}.");
itemClicked = true;

return Task.CompletedTask;
}
}
};

var cut = Render(builder =>
{
builder.OpenComponent<FluentMenuProvider>(0);
builder.CloseComponent();
builder.OpenComponent<AspireMenuButton>(1);
builder.AddAttribute(2, nameof(AspireMenuButton.MenuButtonId), anchor);
builder.AddAttribute(3, nameof(AspireMenuButton.Title), "View options");
builder.AddAttribute(4, nameof(AspireMenuButton.Items), items);
builder.AddAttribute(5, nameof(AspireMenuButton.RestoreFocusOnItemClick), true);
builder.CloseComponent();
});

cut.Find($"#{anchor}").Click();
cut.WaitForElement("fluent-menu-item").Click();

Assert.True(itemClicked);
Assert.True(
focusElementInvocationsDuringOnClick == 0,
$"Expected zero focusElement invocations during item OnClick, but captured {focusElementInvocationsDuringOnClick}.");
var invocation = Assert.Single(focusElementInvocationHandler.Invocations);
Assert.Collection(invocation.Arguments,
argument => Assert.Equal(anchor, Assert.IsType<string>(argument)));
}

[Fact]
public void ClickItem_RestoreFocusOnItemClickFalse_DoesNotFocusAnchor()
{
FluentUISetupHelpers.SetupFluentUIComponents(this);
FluentUISetupHelpers.SetupFluentMenu(this);
FluentUISetupHelpers.SetupFluentAnchoredRegion(this);
FluentUISetupHelpers.SetupFluentButton(this);

var anchor = "view-options-button";
var itemClicked = false;
var items = new List<MenuButtonItem>
{
new()
{
Text = "Show hidden resources",
OnClick = () =>
{
itemClicked = true;
return Task.CompletedTask;
}
}
};

var cut = Render(builder =>
{
builder.OpenComponent<FluentMenuProvider>(0);
builder.CloseComponent();
builder.OpenComponent<AspireMenuButton>(1);
builder.AddAttribute(2, nameof(AspireMenuButton.MenuButtonId), anchor);
builder.AddAttribute(3, nameof(AspireMenuButton.Title), "View options");
builder.AddAttribute(4, nameof(AspireMenuButton.Items), items);
builder.CloseComponent();
});

cut.Find($"#{anchor}").Click();
cut.WaitForElement("fluent-menu-item").Click();

Assert.True(itemClicked);
var focusElementInvocations = JSInterop.Invocations
.Where(invocation => invocation.Identifier == "focusElement")
.ToArray();
Assert.Empty(focusElementInvocations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private static ResourceViewModel CreateResource(
}

[Fact]
public void ViewOptionsMenuIsVisibleWhenHiddenResourcesExist()
public void ViewOptionsMenu_WiresFocusRestorationWhenHiddenResourcesExist()
{
// Arrange
var viewport = new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false, IsUltraLowWidth: false);
Expand All @@ -411,9 +411,8 @@ public void ViewOptionsMenuIsVisibleWhenHiddenResourcesExist()
builder.AddCascadingValue(viewport);
});

// Assert - the menu button should be present (it contains the "Show hidden resources" option)
var menuButton = cut.FindComponent<AspireMenuButton>();
Assert.NotNull(menuButton);
Assert.True(menuButton.Instance.RestoreFocusOnItemClick);
}

[Fact]
Expand Down
Loading