Menu

A menu of choices or commands
New Tab New Window History Downloads Bookmarks Zoom Settings

Demo: A Menu attached to a MenuButton

Overview

Menu is used to present a set of choices or commands as a menu. Menu holds the contents of a menu popup — the choices or commands in the menu — and is not the top-level UI element that invokes the menu. For that, see MenuButton, DropdownList, or the more fundamental PopupSource.

Menu can be used with MenuItem and MenuSeparator children, but those simply provide standard appearances. You can use any type of child element inside a Menu. For example, this DropdownList example uses custom elements as the children of the Menu:

Demo: DropdownList showing custom elements as menu choices

Usage

import Menu from 'elix/src/Menu.js';
const Menu = new Menu(); // or
const Menu = document.createElement('elix-menu');

In HTML:

<elix-menu>
  <elix-menu-item>New</elix-menu-item>
  <elix-menu-item>Open</elix-menu-item>
  <elix-menu-separator></elix-menu-separator>
  <elix-menu-item>Save</elix-menu-item>
  <elix-menu-separator></elix-menu-separator>
  <elix-menu-item>Close</elix-menu-item>
</elix-menu>

Keyboard behavior

A Menu is similar to a ListBox in behavior, particularly keyboard behavior. However, one way in which menus are different from lists is that a list will typically present itself as a single focusable item; the list items themselves are not independently focusable. In contrast, a Menu presents each of its children as a separately focusable item. This is done in order to ensure that assistive technologies such as screen readers interpret the menu items correctly.

See also

API

Ancestry: Menu → ReactiveElement → HTMLElement

Built with mixins AriaMenuMixin, AttributeMarshallingMixin, DelegateFocusMixin, DirectionSelectionMixin, FocusVisibleMixin, GenericMixin, ItemsTextMixin, KeyboardDirectionMixin, KeyboardMixin, KeyboardPagedSelectionMixin, KeyboardPrefixSelectionMixin, LanguageDirectionMixin, ReactiveMixin, SelectedItemTextValueMixin, SelectionInViewMixin, ShadowTemplateMixin, SingleSelectionMixin, SlotItemsMixin, and TapSelectionMixin.

This element is used to fill a role in DropdownList and MenuButton.

$ property

A convenient shortcut for looking up an element by ID in the component's Shadow DOM subtree.

Example: if component's template contains a shadow element <button id="foo">, you can use the reference this.$.foo to obtain the corresponding button in the component instance's shadow tree. The $ function is simply a shorthand for getElementById, so this.$.foo is the same as this.shadowRoot.getElementById('foo').

Type: object

Defined by ShadowTemplateMixin

canSelectNext property

True if the selection can be moved to the next item, false if not (the selected item is the last item in the list).

Type: boolean

Defined by SingleSelectionMixin

canSelectPrevious property

True if the selection can be moved to the previous item, false if not (the selected item is the first one in the list).

Type: boolean

Defined by SingleSelectionMixin

defaultState property

The default state for the component. This can be extended by mixins and classes to provide additional default state.

Type: State

Defined by ReactiveMixin

highlightSelectedItem() method

Highlight the selected item.

By default, this uses a heuristic to guess whether the menu was closed by a keyboard or mouse. If so, the menu flashes the selected item off then back on, emulating the menu item selection effect in macOS. Otherwise, it does nothing.

items property

The current set of items drawn from the element's current state.

Type: Array.|null

Defined by ContentItemsMixin

items-changed event

Raised when the items property changes.

Defined by ContentItemsMixin

pageDown() method

Scroll down one page.

Defined by KeyboardPagedSelectionMixin

pageUp() method

Scroll up one page.

Defined by KeyboardPagedSelectionMixin

render() method

Render pending component changes to the DOM.

This method does nothing if the state has not changed since the last render call.

This method invokes all internal render methods. It then invoked componentDidMount (for first render) or componentDidUpdate (for subsequent renders).

Defined by ReactiveMixin

scrollSelectionIntoView() method

Scroll the selected item element completely into view, minimizing the degree of scrolling performed.

Blink has a scrollIntoViewIfNeeded() function that does something similar, but unfortunately it's non-standard, and in any event often ends up scrolling more than is absolutely necessary.

This scrolls the containing element defined by the scrollTarget property. By default, it will scroll the element itself.

Defined by SelectionInViewMixin

selected-index-changed event

Raised when the selectedIndex property changes.

Defined by SingleSelectionMixin

selectedIndex property

The index of the currently-selected item, or -1 if no item is selected.

Type: number

Defined by SingleSelectionMixin

selectedItem property

The currently-selected item, or null if no item is selected.

Type: Element

Defined by SingleSelectionMixin

selectFirst() method

Select the first item in the list.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectionRequired property

True if the list should always have a selection (if it has items).

Type: boolean

Default: false

Defined by SingleSelectionMixin

selectionWraps property

True if selection navigations wrap from last to first, and vice versa.

Type: boolean

Default: false

Defined by SingleSelectionMixin

selectItemWithTextPrefix(prefix) method

Select the first item whose text content begins with the given prefix.

Parameters:

  • prefix: stringThe prefix string to search for

Returns: boolean

Defined by KeyboardPrefixSelectionMixin

selectLast() method

Select the last item in the list.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectNext() method

Select the next item in the list.

If the list has no selection, the first item will be selected.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectPrevious() method

Select the previous item in the list.

If the list has no selection, the last item will be selected.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

setState(changes) method

Update the component's state by merging the specified changes on top of the existing state. If the component is connected to the document, and the new state has changed, this returns a promise to asynchronously render the component. Otherwise, this returns a resolved promise.

Parameters:

  • changes: objectthe changes to apply to the element's state

Returns: Promise - resolves when the new state has been rendered

Defined by ReactiveMixin

state property

The component's current state. The returned state object is immutable. To update it, invoke setState.

Type: State

Defined by ReactiveMixin

suppressFocusVisibility() method

Temporarily suppress visibility of the keyboard focus until the next keydown event.

This can be useful in components like Menu that actively manage where the focus is in response to mouse hover activity. If the user uses the keyboard to invoke a menu, then changes to using the mouse, it can be distracting to see the focus indicator moving as well. In such situations, the component can invoke this method (e.g., in response to mousemove) to temporarily suppress focus visibility.

Defined by FocusVisibleMixin

[symbols.contentSlot] property

Defined by SlotContentMixin

[symbols.delegatesFocus] property

Returns true if the component is delegating its focus.

A component using DelegateFocusMixin will always have this property be true unless a class takes measures to override it.

Type: boolean

Default: true

Defined by DelegateFocusMixin

[symbols.getItemText](item) method

Extract the text from the given item.

The default implementation returns an item's alt attribute or its textContent, in that order.

Parameters:

  • item: HTMLElement|SVGElement

Returns: string

Defined by ItemsTextMixin

[symbols.goDown]() method

Invokes selectNext to select the next item.

Defined by DirectionSelectionMixin

[symbols.goEnd]() method

Invokes selectLast to select the next item.

Defined by DirectionSelectionMixin

[symbols.goLeft]() method

Invokes selectPrevious to select the previous item.

If the element has a rightToLeft property and it is true, then this selects the next item.

Defined by DirectionSelectionMixin

[symbols.goRight]() method

Invokes selectNext to select the next item.

If the element has a rightToLeft property and it is true, then this selects the previous item.

Defined by DirectionSelectionMixin

[symbols.goStart]() method

Invokes selectFirst to select the first item.

Defined by DirectionSelectionMixin

[symbols.goUp]() method

Invokes selectPrevious to select the previous item.

Defined by DirectionSelectionMixin

[symbols.itemMatchesState](item, state) method

Returns true if the given item should be shown in the indicated state.

Parameters:

  • item: HTMLElement|SVGElement
  • state: PlainObject

Returns: boolean

Defined by ContentItemsMixin

[symbols.keydown]() method

See the symbols documentation for details.

Defined by KeyboardMixin

[symbols.render](changed) method

Internal method for rendering any recent changes in state to the DOM.

The default implementation of this method does nothing. Override this method in your component to update your component's host element and any shadow elements to reflect the component's new state. See the rendering example.

Be sure to call super in your method implementation so that your component's base classes and mixins have a chance to perform their own render work.

Parameters:

  • changed: objectdictionary of flags indicating which state members have changed since the last render

Defined by ReactiveMixin

[symbols.scrollTarget] property

The element that will be scrolled when the user presses Page Up or Page Down. The default value is calculated by defaultScrollTarget.

See symbols.scrollTarget.

Type: HTMLElement

Defined by KeyboardPagedSelectionMixin

value property

The text content of the selected item.

Setting this value to a string will attempt to select the first list item whose text content match that string. Setting this to a string not matching any list item will result in no selection.

Type: string

Defined by SelectedItemTextValueMixin