# Menu ## Background ### Definition This spec defines the default function of a `Menu` as an interactive component that displays a list of options that can be represented by a range possible states. Possible variants are defined in [the relevant section](#variants) The `Menu` should be displayed on a temporary surface that interrupts the normal flow of content. The temporary surface should be triggered by an external user action such as (but not limited to) a click on a button or other UI control. The interactions that result in the dismiss/removal of the `Menu` component should be configurable. ## Prior art As a part of the spec definitions in Fluent UI, a research effort has been made through [Open UI](https://open-ui.org/). The current research proposal is available as an open source contribution undergoing review ([research proposal](https://github.com/WICG/open-ui/pull/249)) ## Comparison of `@fluentui/react` and `@fluentui/react-northstar` - All mentions of v7 or v8 == `@fluentui/react` ([docsite](https://developer.microsoft.com/en-us/fluentui#/)) - All mentions of v0 == `@fluentui/react-northstar` ([docsite](https://fluentsite.z22.web.core.windows.net/)) The most relevant comparison that can be achieved between the two libraries is between `ContextualMenu` in v7 and a combination of `Menu`, `Popup` and `ToolbarItem` in v0. v0 suffers from a consistency issue that the control used in `Menu` and the menu variant of `ToolbarItem` are not actually the same component and have different behavior. However, semantically for the purposes of this spec, they representthe same control that will be implemented. Note that the below code samples are not meant to be complete, but to highlight differences between the two libraries. Please refer to official docsites for actual API references. ### Positioning Please refer to the `react-popover` spec for more detailled comparison of positionining between v9 and v0. ### Trigger vs target The v7 `ContextualMenu` has a prop `target` which is intended to be a ref to the DOM element that the positioning logic anchors to. The usage of this prop requires the visibility state of the component to be controlled using React state by the consumer. The same prop exists on the v0 `Popup` component that is intended to perform the same function. ```tsx const buttonRef = React.useRef(
  • ``` ### Menu divider ```html ``` ### Custom rendering and data v7 provides render callbacks that can be used to render either the entire menu list or specific slots of menut items. Each call back provides the props avaialble to that slot and a `defaultRender` which allows to easily extend the original render, if required. ```tsx // v7 custom rendering const menuProps = { onRenderMenuList: (props: IContextualMenuListProps, defaultRenderer) => {}, onRenderSubMenu: (props: IContextualMenuProps, defaultRenderer) => {}, items: [ { onRender: ( item: any, dismissMenu: (ev?: any, dismissAll?: boolean) => void ) => React.ReactNode } {onRenderContent: (props: IContextualMenuItemProps, defaultRenderer) => {}}, {onRenderIcon: (props: IContextualMenuItemProps, defaultRenderer) => {}}, ] } ``` Custom data can also be associated with menu items ```tsx const menuProps = { items: [{ ... data: { foo: "bar" } }] } ``` v0 custom rendering through shorthand components is a consistent experience through all shorthand components, but provide a smaller API surface (whether this is simpler or less powerful can be subjective). Custom rendering in the case of the `Menu` component would be done through the use of `children` prop either through the standard React child component API or through shorthand as a callback function. ```tsx // v0 shorthand children render callback const items = [ { key: 'editorials', children: (El, props) => {props.key} }, ] // v0 children API custom render Editorials CustomContent {/*Not recommended but definitely possible*/}
    custom item
    ``` ## Variants ### Nested menus A `Menu` should be able to trigger an additional instance of itself as a part of one or more of its options. The nested `Menu` component should have the same functional capabilities as the root `Menu` component. The actions that trigger the the nested `Menu` should be be consistent with the actions that can trigger any root `Menu` from a similar UI control. We advise that no more than two nested `Menu` components be used, but this spec does not functionally apply that constrain to the implementation of the `Menu` component. ### Selection state A `Menu` should be able to track and represent the selection state of all or some of its options if required. When an options is associated with a selection state. The `Menu`, either root or nested, should control its dismiss behavior accordingly based on configuration. ### Sections A `Menu` can be partitioned into sections using visible dividers in its list of options. Each section can contain a heading title that announces or briefly describes the options in the particular section ### Secondary label An option of a `Menu` component should be able to declare additional secondary label that can provide additional context describing the option or its usage. For example a secondary label can be a label that shows a keyboard shortcut that will perform an equivalent action of the option of the `Menu` component. ### Split option with nesting An option of a `Menu` component can trigger a nested `Menu` component and also perform its default action by splitting the option into two interactable areas that handle each action separately. ### Disabled option(s) All options in a `Menu` component can be disabled and should provide a visible indication for this purpose. User interaction should be defined for disabled options ### Scrollable A `Menu` should display a vertical scrollbar when the number of options exceeds the height of the component ### Standalone/No surface A `Menu` can be used without the temporary popup surface and its trigger. This will allow `Menu` components to be permanent page content or used in custom surfaces with a wider range of UI components. ### Custom content ``` This variant is still a work in progress and needs additional thought ``` Any custom content can be used in the rendering of the Menu, all interactions and accessibility is left to the discretion of the consumer. ## API The `Menu` should implement a `children` based API as is the standard across all the surveyed alternatives as a part of Open UI research in [Prior Art](#prior-art). The component will leverage the use of `context` in the interaction and data flows of child components. Sample usages will be give in the following section of this document [Sample code](#sample-code) ### Menu The root level component serves as a simplified interface (sugar) for popup positioning and triggering. ```typescript export type MenuProps = MenuListProps & Pick'> & { /** * Explicitly require children */ children: React.ReactNode; /** * Whether the popup is open */ open?: boolean; /** * Call back when the component requests to change value * The `open` value is used as a hint when directly controlling the component */ onOpenChange?: (e: MenuOpenEvents, data: MenuOpenChangeData) => void; /** * Whether the popup is open by default */ defaultOpen?: boolean; /** * Wrapper to style and add events for the popup */ menuPopup?: ShorthandProps>; /* * Opens the menu on hover */ openOnHover?: boolean; /** * Opens the menu on right click (context menu), removes all other menu open interactions */ openOnContext?: boolean; /** * Root menus are rendered out of DOM order on `document.body`, use this to render the menu in DOM order * This option is disregarded for submenus */ inline?: boolean; }; ``` ### MenuTrigger A non-visual component that wraps its child and configures them to be the trigger that will open a menu. This component should only accept one child ```typescript export type MenuTriggerProps = { /** * Explicitly require single child */ children: React.ReactElement; }; ``` ### MenuList This component is used internally by `Menu` and manages the context and layout its items. `MenuList` can also be used separately as the standalone variant of the `Menu`, since it should not control popup positioning or triggers. It is the only component in the API that can be used standalone. Envisioned to be used with more complex popup or trigger scenarios where the `Menu` component does not provide enough control for these situations. ```typescript export type MenuListProps = ComponentProps & React.HTMLAttributes & { /** * Callback when checked items change for value with a name * * @param name - the name of the value * @param checkedItems - the items for this value that are checked */ onCheckedValueChange?: (e: React.MouseEvent | React.KeyboardEvent, name: string, checkedItems: string[]) => void; /** * Map of all checked values */ checkedValues?: Record; /** * Default values to be checked on mount */ defaultCheckedValues?: Record; /** * States that menu items can contain icons and reserve slots for item alignment */ hasIcons?: boolean; /** * States that menu items can contain selectable items and reserve slots for item alignment */ hasCheckmarks?: boolean; }; ``` ### MenuGroup Creates a group inside a `MenuList`, setting up header layout and dividers between `MenuItems`. The MenuGroup is also a useful component to declare different selection groups (checkbox/radio) in a `MenuList`. > This component only accepts native DOM attributes as props. ### MenuGroupHeader Creates a section header element with appropriate styling. Will set correct `aria-labelledby` relationship if it is instantiated within a [MenuGroup](#menugroup) > This component only accepts native DOM attributes as props. ### MenuDivider Creates a divider element in the `MenuList` with correct HTML and aria semantics for divider. This divider is purely a visual cue. To ensure consistent narration experience across all screenreaders [MenuGroup](#menugroup) should be used > This component only accepts native DOM attributes as props. ### MenuItem ```typescript export type MenuItemProps = ComponentProps & React.HTMLAttributes & { /** * Icon slot rendered before children content */ icon?: ShorthandProps>; /** * A helper slot for alignment when a menu item is used with selectable menuitems * Avoid using this slot as a replacement for MenuItemCheckbox and MenuItemRadio components */ checkmark?: ShorthandProps>; /** * Icon slot that shows the indicator for a submenu */ submenuIndicator?: ShorthandProps>; /** * Component children are placed in this slot * Avoid using the `children` property in this slot in favour of Component children whenever possible */ content?: ShorthandProps>; /** * Secondary content rendered opposite the primary content (e.g Shortcut text) */ secondaryContent?: ShorthandProps>; /** * If the menu item is a trigger for a submenu */ hasSubmenu?: boolean; /** * Applies disabled styles to menu item but remains focusable */ disabled?: boolean; /** * Clicking on the menu item will not dismiss an open menu */ persistOnClick?: boolean; }; ``` ### MenuItemCheckbox/Radio Variants of `MenuItem` that allows a single or multiple selection state based on the value that it represents. API is intended to mirror that of HTML inputs - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio ```typescript /** * Props for selecatble menu items */ export type MenuItemSelectableProps = React.HTMLAttributes & { /** * Follows input convention * https://www.w3schools.com/jsref/prop_checkbox_name.asp */ name: string; /** * Follows input convention * https://www.w3schools.com/jsref/prop_checkbox_value.asp */ value: string; /** * Whether the selectable item is disabled */ disabled?: boolean; }; export type MenuItemCheckboxProps = ComponentProps & React.HTMLAttributes & MenuItemProps & MenuItemSelectableProps & { /** * Slot for the checkmark indicator */ checkmark?: ShorthandProps>; }; export type MenuItemRadioProps = ComponentProps & React.HTMLAttributes & MenuItemProps & MenuItemSelectableProps & { checkmark?: ShorthandProps>; }; ``` ## Sample code The below samples do not represent the definitive props of the final implemented component, but represent the ideal final implementations. Can be subject to change during the implementation phase. ### Basic Menu ```tsx const menu = ( Option 1 Option 2 Option 3 ) ``` ```html
    Option 1
    Option 2
    Option 3
    ``` ### Menu items with icons ```tsx const menu = ( }>Option 1 }>Option 2 }>Option 3 ) ``` ```html
    FileIcon Option 1
    BellIcon Option 2
    LinkIcon Option 3
    ``` ### Sections ```tsx const menu = ( Option 1 Section Option 1 Section Option 2 Section Option 3 ) ``` ```html
    Option 1
    Section Option 1
    Section Option 2
    Section Option 3
    ``` Custom section headings can also be used, but must be used within a [MenuGroup](#menugroup) to ensure correct narration experience ```tsx const menu = ( Option 1 {children} Section Option 1 Section Option 2 Section Option 3 ) ``` ```html
    Option 1
    Section Option 1
    Section Option 2
    Section Option 3
    ``` ### Submenus ```tsx const menu = ( Option 1 Open submenu Option 1 Option 2 Option 3 ) ``` ```html
    Option 1
    Option 1
    Option 2
    Option 3
    ``` ### Standlone ```tsx const [open] = React.useState(false); const menu = ( Option 1 Option 2 Option 3 ) ``` ```html
    Option 1
    Option 2
    Option 3
    ``` ### Selection ```tsx const trigger = const [selectedItems, setSelectedItems] = React.useState([]); // basic checkbox example const menuCheckbox = ( Option 1 Option 2 Option 3 ) // leverage MenuGroup for different selection groups const menuSelectableSections = ( Option 1 Option 2 Option 3 Option 1 Option 2 Option 3 ) ``` ```html
    Option 1
    Option 2
    Option 3
    Option 1
    Option 2
    Option 3
    Option 1
    Option 2
    Option 3
    ``` ### Split button ```tsx const trigger = // basic checkbox example const menuSplitbutton= ( Option 1 Main action Option 1 Option 2 Option 3 ) ``` ```html
    Option 1
    Option 1
    content slot
    Option 1
    Option 2
    Option 3
    ``` ## Behaviors ### Useful references The below references were used to decide n appropriate keyboard interactions from an a11y perspective. - https://www.w3.org/TR/wai-aria-practices/examples/menubar/menubar-1/menubar-1.html# - https://www.w3.org/TR/wai-aria-practices/examples/menu-button/menu-button-links.html - https://www.w3.org/WAI/tutorials/menus/application-menus/ ### Mouse/Keyboard interactions Below is a set of diagrams that tries to illustrates all the interactions menus and nested menus support in an easily understandable way. > TODO convert these diagrams to excalidraw or smth that is text format > TODO add extra descriptions to diagrams ### Split button MenuItem submenu All of the above Mouse events seen previously should apply to the part of the split button that is intended to open a submenu. > TODO convert these diagrams to excalidraw or smth that is text format > TODO add extra descriptions to diagrams ### MenuItem selection Below are the interactions that should be supported for all menu items that are required to handle a selection state. In the event that the selection method is a radio, the previous selected item must be unselected. | Type | Action | Result | Details | | -------- | ------ | ------ | -------------------------------------------- | | Keyboard | Space | Toggle | Toggle the selection status of the menu item | | Keyboard | Enter | Toggle | Toggle the selection status of the menu item | | Mouse | Click | Toggle | Toggle the selection status of the menu item | #### Linking keyboard navigation and mouse hover When a user sets focus on menu items using keyboard navigation, and then switches to mouse hover there should be one unique 'active' state for menu items. There should not be two different indicators at this point for hover and focus. Below is a GIF of the `ContextualMenu` in v7 that also supports this behaviour. ![Linked keyboard and mouse navigation](../etc/images/linked-keyboard-mouse-navigation.gif) ### Positioning #### Inline vs portal rendering A menu should be positioned so that it can be rendered either out of order on the DOM (e.g. portal to body) or inline in DOM order. #### Submenu positioning The default positioning for a submenu should be the standard seen in both v7 and v0. Submenu should be placed after the menu item trigger and aligned with the top edge. Although this should not be recommended, for the purposes of compatibility with v7, all positioning aspects should be configurable for submenus. ## Accessibiltiy Accessibility behaviour is built into the spec as much as possible. This section addresses specific issues that don't fit well with the standard definition of the component. ## Migration The immediate candidates for adoption for a converged `Menu` component which are hinted at the beginning of the spec are: - [ContextualMenu](https://developer.microsoft.com/en-us/fluentui#/controls/web/contextualmenu) for v7 - [Menu](https://fluentsite.z22.web.core.windows.net/0.52.0/components/menu/definition) for v0 This component has characteristics that should probably be considered for the following components in terms of future migrations: - [Toolbar](https://fluentsite.z22.web.core.windows.net/0.52.0/components/toolbar/definition) in v0 which shares a menu component. The component itself also should use similar behaviour and interactions to `Menu` - [Dropdown](https://fluentsite.z22.web.core.windows.net/0.52.0/components/dropdown/definition) in v0 contains a menu. Dropdown semantics are different to that of standard menus in accessibility, but certain behaviours such as keyboard navigation and selection can be reused for such a component - [CommandBar](https://developer.microsoft.com/en-us/fluentui#/controls/web/commandbar) in v7 also contains a menu subcomponent as well as behaviour similar to `Menu` semantics - [Nav](https://developer.microsoft.com/en-us/fluentui#/controls/web/nav) in v7 could reuse certain behaviours in `Menu` such as keyboard navigation, but will use different DOM and aria semantics, this could be achieved through state hook variants or composition - [OverflowSet](https://developer.microsoft.com/en-us/fluentui#/controls/web/overflowset) in v7 contains a submenu component - [PivotSet](https://developer.microsoft.com/en-us/fluentui#/controls/web/pivot) could be considered as a component variant of `Menu` - [Breadcrumb](https://developer.microsoft.com/en-us/fluentui#/controls/web/breadcrumb) in v7 contains a submenu component and could also be considered as a component variant of `Menu ### Creating sections or groups within a menu ⚠️ When using [MenuDivider](#menudivider) without [MenuGroup](#menugroup) The [MenuDivider](#menudivider) is a purely visual component. The component is only intended to be used as visual 'sugar'. When meaningful partitions [MenuItems](#menuitem) exists, [MenuGroup](#menugroup) should be used to provide the correct experience for narration. ⚠️ When using [MenuSectionHeader](#menudivider) [MenuGroup](#menugroup) as a parent component ensures that correct `aria-labelledby` relationship is defined between the header and the group. ### Disabled menu items Disabled menu items should be focusable ### Linking keyboard navigation and mouse hover This can be difficult to impleemnt correctly without introducing a11y issues. The mouse should only apply focus if it is certain that the user is actively using the mouse on the page. If a menu is opened with keyboard interaction, and contains the mouse cursor by chance focus should not be applied.