Back to Fluentui

Menu

packages/web-components/src/menu/README.md

4.40.2-hotfix27.7 KB
Original Source

Menu

A Menu component for handling menus and menu items in a user interface.

Design Spec

There is no design spec for the Menu component as the Menu has no visual styles. The design spec for the MenuList can be found at Fluent MenuList Spec

Engineering Spec

The Menu component is responsible for managing menus and their associated menu items. It handles the open/close functionality, focus management, and positioning strategy for showing the menu items.

Use Case

Creating a menu component that can be used to display a list of options or actions.

Class: Menu

Variables

Fields

NamePrivacyTypeDefaultDescription
menupublicHTMLElement[]The menu element(s) to be displayed
triggerpublicHTMLElement[]The trigger element(s) for opening/closing menu
openpublicbooleanfalseSpecifies if the menu is open or closed
menuContainerpublicHTMLElementThe container element for the menu items
openOnHoverpublicbooleanfalseSets whether the menu opens on hover of menu trigger
openOnContextpublicbooleanfalseOpens the menu on right click (context menu), removes all other menu open interactions
closeOnScrollpublicbooleanfalseClose when scroll outside of it
persistOnItemClickpublicbooleanfalseDetermines if the menu open state should persis on click of menu item

Methods

NamePrivacyDescriptionParametersReturn
setComponentpublicets the trigger and menu list elements and adds event listeners.void
setPositioningprotectedCalculates and applies the positioning of the menu list based on available viewport space.void
toggleMenupublicToggles the open state of the menu.void
closeMenupublicCloses the menu.void
openMenupublicOpens the menu.e?: Eventvoid
focusMenuListpublicFocuses on the menu list.void
focusTriggerpublicFocuses on the menu trigger.void
openChangedpublicCalled whenever the open state changes. Emits onOpenChange event.newValue: boolean, oldValue: booleanvoid
openOnHoverChangedpublicCalled whenever the 'openOnHover' property changes.newValue: boolean, oldValue: booleanvoid
persistOnItemClickChangedpublicCalled whenever the 'persisitOnItem' property changes.newValue: boolean, oldValue: booleanvoid
openOnContextChangedpublicCalled whenever the 'openOnContext' property changes.newValue: boolean, oldValue: booleanvoid
handleMenuKeydownpublicHandles keyboard interaction for the menu.e: KeyboardEventvoid
handleTriggerKeydownpublicHandles keyboard interaction for the menu trigger.e: KeyboardEventvoid

Events

NameTypeDescription
onOpenChangeemits custom onOpenChange event when opened state changes

Attributes

NameField
openopen
open-on-hoveropenOnHover
open-on-contextopenOnContext
close-on-scrollcloseOnScroll
persist-on-item-clickpersistOnItemClick

Slots

NameDescription
triggerThe trigger element for the menu
The menulist element

CSS Variables

NameDescription
z-index-menuUsed to set z-index of the Menu

Template

html
<slot name="trigger" ${slotted({ property: 'trigger', filter: elements() })}></slot>
<span class="menu-list-container" ${ref('menuListContainer')} ?hidden="${(x) => !x.open}">
  <slot ${slotted({ property: 'menu', filter: elements() })}></slot>
</span>

Accessibility

WAI-ARIA Roles, States, and Properties

  • aria-haspopup
  • aria-expanded
<hr />

Preparation

Fluent Web Component v3 v.s Fluent React 9

Component and Slot Mapping

Fluent UI React 9Fluent Web Components 3
<Menu><fluent-menu>
<MenuList><fluent-menu-list>
<MenuItem><fluent-menu-item>
Fluent UI React 9Fluent Web ComponentsDescription of difference
hasIconsReact implementation requires user to pass the hasIcons to align menu items with icons. The web components implementation aligns content by default.
hasCheckmarksReact implementation requires user to pass the hasCheckmarks to align menu items with checkmarks. The web components implementation aligns content by default.