Menu
As defined by the W3C:
A menu is a widget that offers a list of choices to the user, such as a set of actions or functions. Menu widgets behave like native operating system menus, such as the menus that pull down from the menubars commonly found at the top of many desktop application windows. A menu is usually opened, or made visible, by activating a menu button, choosing an item in a menu that opens a sub menu, or by invoking a command, such as Shift + F10 in Windows, that opens a context specific menu. When a user activates a choice in a menu, the menu usually closes unless the choice opened a submenu.
While any DOM content is permissible as a child of the menu, only <FluentMenuItem>
's and slotted content with a role of
menuitem
, menuitemcheckbox
, or menuitemradio
will receive keyboard support.
<FluentMenu>
applies <FluentMenuItem>
's startColumnCount
property based on an evaluation of all
of the <FluentMenuItem>
so the content text vertically aligns across all <FluentMenuItem>
. If any
<FluentMenuItem>
does not have a roll of checkbox
or radio
or the start
slot is not
passed, startColumnCount
is set to 0 which applies a indent-0
class to all the <FluentMenuItem>
s.
If any <FluentMenuItem>
has a roll of checkbox
or radio
or the start
slot exists,
startColumnCount
is set to 1 which applies a indent-1
class to all the <FluentMenuItem>
s. Or if any
<FluentMenuItem>
has a roll of checkbox
or radio
and the start
slot exists,
startColumnCount
is set to 2 which applies a indent-2
class to all the <FluentMenuItem>
s.
<FluentMenu>
wraps the <fluent-menu>
element, a web component implementation of a menu leveraging the
Fluent UI design system.
FluentMenuProvider
With version 4.9.4 of the library, we introduced the FluentMenuProvider
component. The Menu component has been updated to use this provider.
The <FluentMenuProvider />
needs to be placed at the bottom of your HTML page (just like the other ...Providers components).
It will render all menus (and menu items) at the provider location in the HTML structure. This allows for menus to appear above of other components.
You can disable this feature by adding the UseMenuService
parameter (with a value of "false") to you FluentMenu component. In this case, the menu will be rendered at the location it is placed at in the page.
Example
Service provider
The first example contains the UseMenuService="false"
attribute.
The menu is created where it is declared, which can pose encapsulation problems,
depending on the container used (in this case, a FluentCard
).
The second example uses the FluentMenuProvider
to display the menu.
In this case, the menu is generated at the end of the HTML page and attached to the button using the Anchor
tag.
Id
identifier on the clickable element
and an identical Anchor
value on the menu, to link the two elements.
Click this button to open a Menu.
Click this button to open a Menu.
Default
Click this button to open a Menu.
Simple context menu
to open a Context Menu.
Default menu
With Separator
With radio buttons and checkboxes
Detect when checked and unchecked (2-way binding)
The CheckedChanged
EventCallback is invoked when the FluentMenuItem
Role is MenuItemCheckbox
or MenuItemRadio
.
Role=MenuItemCheckbox
Role=MenuItemRadio
Nested
With icons
When using FluentIcon
s in a FluentMenuItem
, you will run into issues with indentation when putting the icons in slots.
This problem originates from the following: (from https://www.fast.design/docs/components/menu#setup):
fast-menu
appliesfast-menu-item
'sstartColumnCount
property based on an evaluation of all of thefast-menu-items
so the content text vertically aligns across allfast-menu-items
. If anyfast-menu-item
does not have a roll ofcheckbox
orradio
or thestart
slot is not passed,startColumnCount
is set to 0 which applies aindent-0
class to all thefast-menu-item
s. If anyfast-menu-item
has a roll ofcheckbox
orradio
or thestart
slot exists,startColumnCount
is set to 1 which applies aindent-1
class to all thefast-menu-item
s. Or if anyfast-menu-item
has a roll ofcheckbox
orradio
and thestart
slot exists,startColumnCount
is set to 2 which applies aindent-2
class to all thefast-menu-item
s.
Because we are rendering the web components indirectly fast-menu
doesn't know about the icons and their slots in the
fast-menu-items
when it renders (they don't exist in the DOM yet) so it can't determine the startColumnCount
.
As a workaround, place the FluentIcons
inside a <span slot="start"></span>
(or <span slot="end"></span
> in the menu items. For example:
<FluentMenu>
<FluentMenuItem>
Menu item 1
<span slot="start" >
<FluentIcon Icon="Icons.Regular.Size16.Globe" Slot="start" Color="Color.Neutral" />
</span>
<span slot="end">
<FluentIcon Icon="Icons.Regular.Size16.Globe" Slot="end" Color="Color.Neutral" />
</span>
</FluentMenuItem>
:
:
The script gets triggered then and the icons render in the right place. You can leave the Slot
parameter in
the FluentIcon
but in this case it doesn't have to be there anymore.
With standard elements
Documentation
FluentMenuItem Class
Parameters
Name | Type | Default | Description |
---|---|---|---|
Checked | bool | False | Gets or sets a value indicating whether the element is checked. |
ChildContent | RenderFragment? | Gets or sets the content to be rendered inside the component. | |
Disabled | bool | False | Gets or sets a value indicating whether the element is disabled. |
Expanded | bool | False | Gets or sets the expanded state of the element. |
KeepOpen | bool | False | Gets or sets a value indicates whether the FluentMenu should remain open after an action. |
Label | string? | Gets or sets the menu item label. | |
MenuItems | RenderFragment? | Gets or sets the list of sub-menu items. | |
Owner | FluentMenu | Gets or sets the owning FluentMenu. | |
Role | MenuItemRole? | Gets or sets the role of the element. |
EventCallbacks
Name | Type | Description |
---|---|---|
CheckedChanged | EventCallback<bool> | Event raised for checkbox and radio menuitems |
OnClick | EventCallback<MouseEventArgs> | Event raised when the user click on this item. |
FluentMenu Class
Parameters
Name | Type | Default | Description |
---|---|---|---|
Anchor | string | Gets or sets the identifier of the source component clickable by the end user. | |
Anchored | bool | True | Draw the menu below the component clicked (true) or using the mouse coordinates (false). |
ChildContent | RenderFragment? | Gets or sets the content to be rendered inside the component. | |
HorizontalInset | bool | True | Gets or sets a value indicating whether the region overlaps the anchor on the horizontal axis. Default is false which places the region adjacent to the anchor element. |
HorizontalPosition | HorizontalPosition | Unset | Gets or sets the horizontal menu position. |
HorizontalScaling | AxisScalingMode? | Gets or sets the horizontal scaling mode. | |
HorizontalThreshold | int | 200 | Gets or sets how narrow the space allocated to the default position has to be before the widest area is selected for layout. |
HorizontalViewportLock | bool | False | Gets or sets the Horizontal viewport lock. |
Open | bool | False | Gets or sets the Menu status. |
ServiceProvider | IServiceProvider? | ||
Trigger | MouseButton | None | Gets or sets the automatic trigger. See MouseButton Possible values are None (default), Left, Middle, Right, Back, Forward |
UseMenuService | bool | True | Use IMenuService to create the menu, if this service was injected. This value must be defined before the component is rendered (you can't change it during the component lifecycle). Default, true. |
VerticalInset | bool | False | Gets or sets a value indicating whether the region overlaps the anchor on the vertical axis. |
VerticalPosition | VerticalPosition | Bottom | Gets or sets the vertical menu position. |
VerticalThreshold | int | 200 | Gets or sets how short the space allocated to the default position has to be before the tallest area is selected for layout. |
Width | string? | Gets or sets the width of this menu. |
EventCallbacks
Name | Type | Description |
---|---|---|
OnCheckedChanged | EventCallback<FluentMenuItem> | Raised when FluentMenuItem Checked changed. |
OpenChanged | EventCallback<bool> | Raised when the FluentMenu.Open property changed. |
Methods
Name | Parameters | Type | Description |
---|---|---|---|
CloseAsync | Task | Close the menu. | |
OpenAsync | int screenWidth int screenHeight int x int y | Task | Method called from JavaScript to get the current mouse coordinates. |