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.
//<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.
//<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);
}
}
});Menu item interaction handling in a complex widget
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
Configs
121Common
Other
Specify false to prevent the menu from getting focus when hovering items
Content
CSS
DOM
Float & align
Layout
misc
Misc
Scrolling
Properties
102
Properties
102Class hierarchy
Other
Currently open sub menu, if any
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.
Get/set focused menu item. Shows submenu if newly focused item has a menu and is not disabled.
CSS
DOM
Layout
Misc
State
Widget hierarchy
Functions
75
Functions
75Configuration
Events
Misc
Other
Widget hierarchy
Events
24
Events
24A 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 }) => {
});| Parameter | Type | Description |
|---|---|---|
item | MenuItem | The menu item which is being actioned. |
menu | Menu | 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 }) => {
});| Parameter | Type | Description |
|---|---|---|
item | MenuItem | The menu item whose checked state changed. |
menu | Menu | Menu containing the menu item |
checked | Boolean | The new checked state. |
Event handlers
24
Event handlers
24A 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 }) {
}
});| Parameter | Type | Description |
|---|---|---|
item | MenuItem | The menu item which is being actioned. |
menu | Menu | 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 }) {
}
});| Parameter | Type | Description |
|---|---|---|
item | MenuItem | The menu item whose checked state changed. |
menu | Menu | Menu containing the menu item |
checked | Boolean | The new checked state. |
Typedefs
7
Typedefs
7CSS variables
63
CSS variables
63| Name | Description |
|---|---|
--b-menu-border-radius | Menu border-radius |
--b-menu-background | Menu background |
--b-menu-padding | Menu padding |