Menu

Menu widget, displays a list of items which the user can select from using mouse or keyboard. A menu can also have submenus by configuring the menu config of a MenuItem. A menu is typically only shown when right-clicking an element in the DOM. Try right-clicking the DIV rectangle below.

Menu
//<code-header>
fiddle.title = 'Menu';
//</code-header>
const widget = new Widget({
    appendTo : targetElement,
    style    : {
        background        : '#777',
        color             : '#fff',
        display           : 'flex',
        'align-items'     : 'center',
        'justify-content' : 'center',
        padding           : '1em 2em'
    },
    html : 'Right-click to show menu'
});

widget.element.addEventListener('contextmenu', event => {
    event.preventDefault();

    if (!widget.menu) {
        widget.menu = new Menu({
            anchor : true,

            owner : widget, // Ensure menu is destroyed when the owning widget is destroyed
            items : [
                {
                    icon : 'b-fw-icon b-icon-add',
                    text : 'Add'
                },
                {
                    icon : 'b-fw-icon b-icon-trash',
                    text : 'Remove'
                },
                {
                    icon     : 'b-fw-icon b-icon-lock',
                    disabled : true,
                    text     : 'I am disabled'
                }
            ],
            // Method is called for all ancestor levels
            onItem({ item }) {
                Toast.show('You clicked ' + item.text);
            }
        });
    }

    widget.menu.showBy(widget);
});

A common usecase is to attach a menu to a Button, which is supported via the menu config.

Menu button
//<code-header>
fiddle.title = 'Menu button';
//</code-header>
new Button({
    appendTo  : targetElement,
    rendition : 'filled',
    text      : 'Button with menu',
    menu      : {
        anchor : true,
        items  : [
            {
                icon : 'b-fw-icon b-icon-add',
                text : 'Add'
            },
            {
                icon : 'b-fw-icon b-icon-trash',
                text : 'Remove'
            },
            {
                icon     : 'b-fw-icon b-icon-lock',
                disabled : true,
                text     : 'I am disabled'
            },
            {
                text : 'Sub menu',
                menu : [{
                    icon : 'b-fw-icon fa-play',
                    text : 'Play'
                }]
            }
        ],
        // Method is called for all ancestor levels
        onItem({ item }) {
            Toast.show('You clicked ' + item.text);
        }
    }
});

In the case of a menu which is part of a complex UI within a larger Bryntum widget, use of the string form for handlers is advised. A handler which starts with 'up.' will be resolved by looking in owning widgets of the Menu. For example a Calendar may have handlers for its MenuItems configured in:

new Calendar({
    appendTo : document.body,
    project  : myProjectConfig,
    tbar  : {
        items : {
            settings : {
                type : 'button',
                text : 'Settings',

                // High weight so it goes at the end
                weight : 800,
                menu   : [{
                    text     : 'Hide non-working days',
                    checked  : false,

                     // The Menu's ownership will be traversed to find this function name.
                    onToggle : 'up.toggleHideNonWorkingDays'
                }, {
                    text    : 'Clear changes',

                     // The Menu's ownership will be traversed to find this function name.
                    onItem : 'up.clearUncommittedChanges'
                }]
            }
        }
    },

    // Menu handlers found here
    toggleHideNonWorkingDays({ checked }) {
        // Use Calendar API which creates event in the selected date
        this.hideNonWorkingDays = checked;
    },

    clearUncommittedChanges() {
        // Clear changes to our event store which are not yet synced to the server
        this.eventStore.revertChanges();
    }
});
let menu = new Menu({
    forElement : btn.element,
    items      : [
        {
            icon : 'b-icon b-icon-add',
            text : 'Add'
        },
        {
            icon : 'b-icon b-icon-trash',
            text : 'Remove'
        },
        {
            icon     : 'b-icon b-icon-lock',
            disabled : true,
            text     : 'I am disabled'
        },
        {
            text : 'Sub menu',
            menu : [{
                icon : 'fa fa-play',
                text : 'Play'
            }]
        }
    ],
    // Method is called for all ancestor levels
    onItem({ item }) {
        Toast.show('You clicked ' + item.text);
    }
});

Configs

121

Common

listenersEvents

Other

focusOnHover: Boolean

Specify false to prevent the menu from getting focus when hovering items

columnWidget
defaultFocusContainer
drawerPanel
labelPositionContainer
modalPopup
renditionContainer
resizableResizable
rtlRTL
spanWidget

Accessibility

ariaLabelWidget
keyMapKeyMap

Content

bbarPanel
defaultsContainer
footerPanel
headerPanel
itemsContainer
lazyItemsContainer
namedItemsContainer
stripsPanel
tbarPanel
textContentContainer
toolsPanel

CSS

bodyClsPanel
borderContainer
clsWidget
colorWidget
htmlClsWidget
itemClsContainer
styleWidget
uiPanel

DOM

adoptWidget
appendToWidget
contentWidget
datasetWidget
htmlWidget
idWidget
tagWidget

Float & align

alignWidget
anchorWidget
centeredWidget
floatingWidget
xWidget
yWidget

Layout

alignSelfWidget
dockWidget
flexWidget
heightWidget
hiddenWidget
hideWhenEmptyContainer
layoutContainer
layoutStyleContainer
marginWidget
maxHeightWidget
maxWidthWidget
minHeightWidget
minWidthWidget
textAlignWidget
weightWidget
widthWidget

misc

tabBarItemsContainer

Misc

dataFieldWidget
disabledWidget
iconPanel
localeClassLocalizable
localizableLocalizable
maskedWidget
ownerWidget
readOnlyWidget
refWidget
rippleWidget
tabWidget
titlePanel
tooltipWidget

Record

recordContainer

Scrolling

State

stateIdState

Properties

102

Class hierarchy

isMenu: Boolean= truereadonly
Identifies an object as an instance of Menu class, or subclass thereof.
isMenu: Boolean= truereadonlystatic
Identifies an object as an instance of Menu class, or subclass thereof.
isContainerContainer
isDelayableDelayable
isEventsEvents
isKeyMapKeyMap
isLocalizableLocalizable
isPanelPanel
isPopupPopup
isResizableResizable
isStateState
isToolableToolable
isWidgetWidget

Other

Currently open sub menu, if any

isSubMenu: Booleanreadonly

Returns true if this menu is a sub menu. To find out which menu is the parent, check parentMenu.

Gets the parent Menu if this Menu is a submenu, or undefined.

selectedElement: HTMLElement

Get/set focused menu item. Shows submenu if newly focused item has a menu and is not disabled.

$namestaticWidget
columnWidget
firstItemContainer
hasChangesContainer
isValidContainer
itemsContainer
labelPositionContainer
lastItemContainer
renditionContainer
resizableResizable
rtlRTL
spanWidget
toolsPanel
typestaticWidget
valuesContainer

Accessibility

keyMapKeyMap

Content

bbarPanel
tbarPanel

CSS

clsWidget

DOM

appendToWidget
contentWidget
datasetWidget
elementWidget
htmlWidget
idWidget
styleWidget

Float & align

xWidget
yWidget

Layout

alignSelfWidget
flexWidget
heightWidget
layoutContainer
layoutStyleContainer
marginWidget
maxHeightWidget
maxWidthWidget
minHeightWidget
minWidthWidget
widthWidget

Lifecycle

configBase

Misc

cellInfoWidget
disabledWidget
localeHelperLocalizable
localeManagerLocalizable
readOnlyWidget
refWidget
tabWidget
titlePanel
tooltipWidget

Record

recordContainer

State

stateState

Visibility

hiddenWidget
isVisibleWidget

Widget hierarchy

ownerWidget
parentWidget
widgetMapContainer

Functions

75

Configuration

applyDefaultsstaticBase

Events

Float & align

alignToWidget
setXYWidget
showByWidget
toFrontWidget

Lifecycle

createstaticWidget
destroystaticBase
initClassstaticWidget

Misc

attachTooltipstaticWidget
fromElementstaticWidget
fromSelectorstaticWidget
getByIdstaticWidget
isOfTypeNamestaticBase
mixinstaticBase
optionalLstaticLocalizable

Other

addContainer
closePopup
composeWidget
createOnFrameDelayable
disableWidget
enableWidget
focusWidget
getAtContainer
getWidgetByIdContainer
insertContainer
LstaticLocalizable
maskWidget
onEvents
recomposeWidget
relayAllEvents
removeContainer
removeAllContainer
resetValuesContainer
setValuesContainer
triggerEvents
unEvents
unmaskWidget

State

Visibility

hideWidget
showWidget

Widget hierarchy

closestWidget
containsWidget
ownsWidget
queryWidget
queryAllWidget
upWidget

Events

24

A descendant menu item has been activated.

Note that this event bubbles up through parents and can be listened for on a top level Menu for convenience.

// Adding a listener using the "on" method
menu.on('item', ({ item, menu }) => {

});
ParameterTypeDescription
itemMenuItem

The menu item which is being actioned.

menuMenu

Menu containing the menu item

The checked state of a descendant menu item has changed.

Note that this event bubbles up through parents and can be listened for on a top level Menu for convenience.

// Adding a listener using the "on" method
menu.on('toggle', ({ item, menu, checked }) => {

});
ParameterTypeDescription
itemMenuItem

The menu item whose checked state changed.

menuMenu

Menu containing the menu item

checkedBoolean

The new checked state.

catchAllEvents
destroyEvents
expandPanel
focusInWidget
focusOutWidget
hideWidget
paintWidget
readOnlyWidget
recomposeWidget
resizeWidget
showWidget

Event handlers

24

A descendant menu item has been activated.

Note that this event bubbles up through parents and can be listened for on a top level Menu for convenience.

new Menu({
    onItem({ item, menu }) {

    }
});
ParameterTypeDescription
itemMenuItem

The menu item which is being actioned.

menuMenu

Menu containing the menu item

The checked state of a descendant menu item has changed.

Note that this event bubbles up through parents and can be listened for on a top level Menu for convenience.

new Menu({
    onToggle({ item, menu, checked }) {

    }
});
ParameterTypeDescription
itemMenuItem

The menu item whose checked state changed.

menuMenu

Menu containing the menu item

checkedBoolean

The new checked state.

onDestroyEvents
onFocusInWidget
onHideWidget
onPaintWidget
onResizeWidget
onShowWidget

Typedefs

7

CSS variables

63
NameDescription
--b-menu-border-radiusMenu border-radius
--b-menu-backgroundMenu background
--b-menu-paddingMenu padding