Skip to content

Menu

Official

A component for context menus.

Import:

import { Menu } from 'obsidian';

Signature:

export class Menu extends Component implements HistoryHandler

Extends: Component

Implements: HistoryHandler

Constructor

new Menu()

Constructor.

To get the constructor instance, use getMenuConstructor from obsidian-typings/implementations.

Properties

PropertyTypeDescription
_childrenComponent[]Child Components attached to current component, will be unloaded on unloading parent component.
(Inherited from Component)
_eventsEventRef[]Events that are attached to the current component, will be detached on unloading parent component.
(Inherited from Component)
_loadedbooleanWhether the component and its children are loaded.
(Inherited from Component)
bgElHTMLElementBackground for the suggestion menu.
currentSubmenu?MenuThe currently active submenu, if any.
domHTMLElementDOM element of the menu.
items(MenuItem | MenuSeparator)[]Items and separators contained in the menu.
parentMenuMenu | nullParent menu of the current menu.
scopeScopeobsidian#Scope in which the menu is active.
scrollElHTMLElementThe scrollable container element of the menu.
sectionsstring[]Sections within the menu.
selectednumberWhich menuitem is currently selected.
showMacWritingToolsbooleanWhether to show the macOS writing-tools entries.
submenuConfigMenuSubmenuConfigRecordConfigurations for the submenu configs.
submenuConfigsMenuSubmenuConfigRecordConfiguration for menu sections that open as submenus, keyed by section name.
unloadingbooleanWhether the submenu is currently unloading.
useNativeMenubooleanWhether the menu is rendered in native mode.

Methods

MethodReturnsDescription
addChild(component)TAdds a child component, loading it if this component is loaded.
(Inherited from Component)
addItem(cb)thisAdds a menu item. Only works when menu is not shown yet.
addSections(items)thisAdd a section to the menu.
addSeparator()thisAdds a separator. Only works when menu is not shown yet.
close()voidClose the menu.
closeSubmenu()voidClose the currently open submenu.
forEvent(evt)Menu
handleClickOutside(evt)voidHides the menu when a click occurs outside of it.
hide()thisHide the menu.
hideCallback()voidCallback to execute when the menu is hidden.
isInside(e)booleanCheck whether the clicked element is inside the menu.
load()voidLoad this component and its children.
(Inherited from Component)
onArrowDown(e)booleanMove selection to the next item in the menu.
onArrowLeft(e)booleanMove selection out of the submenu.
onArrowRight(e)booleanMove selection into the submenu.
onArrowUp(e)booleanMove selection to the previous item in the menu.
onEnter(e)booleanExecute selected menu item (does nothing if item is submenu).
onHide(callback)voidAdd a callback to be called when the menu is hidden.
onHistoryBack()voidCalled when the user navigates back in the history.
(Inherited from HistoryHandler)
onHistoryForward()voidCalled when the user navigates forward in the history.
(Inherited from HistoryHandler)
onload()voidOverride this to load your component.
(Inherited from Component)
onMenuClick(e)voidPreemptively closes the menu if click is registered on menu item.
onMouseOver(e)booleanOpens submenu if mouse is hovering over item with submenu.
onPointerOver(evt)voidHandles the pointer moving over a menu element.
onunload()voidOverride this to unload your component
(Inherited from Component)
openSubmenu(item)voidRegisters dom events and scope for the menu.
openSubmenuSoon()voidCallback that opens the submenu after a delay.
register(cb)voidRegisters a callback to be called when unloading.
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading.
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidRegisters a DOM event to be detached when unloading
(Inherited from Component)
registerEvent(eventRef)voidRegisters an event to be detached when unloading.
(Inherited from Component)
registerInterval(id)numberRegisters an interval (from setInterval) to be cancelled when unloading. Use window.setInterval instead of setInterval to avoid TypeScript confusing between NodeJS vs Browser API
(Inherited from Component)
registerScopeEvent(keymapEventHandler)voidRegister a scope keymap event handler to be removed on unload.
(Inherited from Component)
removeChild(component)TRemoves a child component, unloading it.
(Inherited from Component)
select(index)voidSelect the item at the specified index (after either hovering or arrowing over it).
selectElement(el, immediate?)voidSelects the menu item containing the given element, optionally opening its submenu immediately.
setNoIcon()thisSet the menu to not use an icon.
setParentElement(el)thisSet the parent element of the menu (i.e. for workspace leaf context menu).
setSectionSubmenu(section, submenu)thisAdd a section to the submenu config.
setShowMacWritingTools(show)thisSets whether to show the macOS writing-tools entries.
setUseNativeMenu(useNativeMenu)thisForce this menu to use native or DOM. (Only works on the desktop app)
showAtMouseEvent(evt)thisShow the menu at the position of the mouse event.
showAtPosition(position, doc?)thisShow the menu at a specific position.
sort()voidSort the items in the menu.
unload()voidOverride this to unload your component.
(Inherited from Component)
unselect()voidUnselect the currently selected item and closes the submenu.

Links to this page: