Menu
Official
A component for context menus.
Import:
import { Menu } from 'obsidian';Signature:
export class Menu extends Component implements HistoryHandlerExtends: Component
Implements: HistoryHandler
Constructor
new Menu()Constructor.
To get the constructor instance, use getMenuConstructor from obsidian-typings/implementations.
Properties
| Property | Type | Description | |
|---|---|---|---|
| _children | Component[] | Child Components attached to current component, will be unloaded on unloading parent component. (Inherited from Component) | |
| _events | EventRef[] | Events that are attached to the current component, will be detached on unloading parent component. (Inherited from Component) | |
| _loaded | boolean | Whether the component and its children are loaded. (Inherited from Component) | |
| bgEl | HTMLElement | Background for the suggestion menu. | |
| currentSubmenu? | Menu | The currently active submenu, if any. | |
| dom | HTMLElement | DOM element of the menu. | |
| items | (MenuItem | MenuSeparator)[] | Items and separators contained in the menu. | |
| parentMenu | Menu | null | Parent menu of the current menu. | |
| scope | Scope | obsidian#Scope in which the menu is active. | |
| scrollEl | HTMLElement | The scrollable container element of the menu. | |
| sections | string[] | Sections within the menu. | |
| selected | number | Which menuitem is currently selected. | |
| showMacWritingTools | boolean | Whether to show the macOS writing-tools entries. | |
| submenuConfig | MenuSubmenuConfigRecord | Configurations for the submenu configs. | |
| submenuConfigs | MenuSubmenuConfigRecord | Configuration for menu sections that open as submenus, keyed by section name. | |
| unloading | boolean | Whether the submenu is currently unloading. | |
| useNativeMenu | boolean | Whether the menu is rendered in native mode. |
Methods
| Method | Returns | Description | |
|---|---|---|---|
| addChild(component) | T | Adds a child component, loading it if this component is loaded. (Inherited from Component) | |
| addItem(cb) | this | Adds a menu item. Only works when menu is not shown yet. | |
| addSections(items) | this | Add a section to the menu. | |
| addSeparator() | this | Adds a separator. Only works when menu is not shown yet. | |
| close() | void | Close the menu. | |
| closeSubmenu() | void | Close the currently open submenu. | |
| forEvent(evt) | Menu | ||
| handleClickOutside(evt) | void | Hides the menu when a click occurs outside of it. | |
| hide() | this | Hide the menu. | |
| hideCallback() | void | Callback to execute when the menu is hidden. | |
| isInside(e) | boolean | Check whether the clicked element is inside the menu. | |
| load() | void | Load this component and its children. (Inherited from Component) | |
| onArrowDown(e) | boolean | Move selection to the next item in the menu. | |
| onArrowLeft(e) | boolean | Move selection out of the submenu. | |
| onArrowRight(e) | boolean | Move selection into the submenu. | |
| onArrowUp(e) | boolean | Move selection to the previous item in the menu. | |
| onEnter(e) | boolean | Execute selected menu item (does nothing if item is submenu). | |
| onHide(callback) | void | Add a callback to be called when the menu is hidden. | |
| onHistoryBack() | void | Called when the user navigates back in the history. (Inherited from HistoryHandler) | |
| onHistoryForward() | void | Called when the user navigates forward in the history. (Inherited from HistoryHandler) | |
| onload() | void | Override this to load your component. (Inherited from Component) | |
| onMenuClick(e) | void | Preemptively closes the menu if click is registered on menu item. | |
| onMouseOver(e) | boolean | Opens submenu if mouse is hovering over item with submenu. | |
| onunload() | void | Override this to unload your component (Inherited from Component) | |
| openSubmenu(item) | void | Registers dom events and scope for the menu. | |
| openSubmenuSoon() | void | Callback that opens the submenu after a delay. | |
| register(cb) | void | Registers a callback to be called when unloading. (Inherited from Component) | |
| registerDomEvent(el, type, callback, options?) | void | Registers a DOM event to be detached when unloading. (Inherited from Component) | |
| registerDomEvent(el, type, callback, options?) | void | Registers a DOM event to be detached when unloading (Inherited from Component) | |
| registerDomEvent(el, type, callback, options?) | void | Registers a DOM event to be detached when unloading (Inherited from Component) | |
| registerEvent(eventRef) | void | Registers an event to be detached when unloading. (Inherited from Component) | |
| registerInterval(id) | number | Registers 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) | void | Register a scope keymap event handler to be removed on unload. (Inherited from Component) | |
| removeChild(component) | T | Removes a child component, unloading it. (Inherited from Component) | |
| select(index) | void | Select the item at the specified index (after either hovering or arrowing over it). | |
| selectElement(el, immediate?) | void | Selects the menu item containing the given element, optionally opening its submenu immediately. | |
| setNoIcon() | this | Set the menu to not use an icon. | |
| setParentElement(el) | this | Set the parent element of the menu (i.e. for workspace leaf context menu). | |
| setSectionSubmenu(section, submenu) | this | Add a section to the submenu config. | |
| setShowMacWritingTools(show) | this | Sets whether to show the macOS writing-tools entries. | |
| setUseNativeMenu(useNativeMenu) | this | Force this menu to use native or DOM. (Only works on the desktop app) | |
| showAtMouseEvent(evt) | this | Show the menu at the position of the mouse event. | |
| showAtPosition(position, doc?) | this | Show the menu at a specific position. | |
| sort() | void | Sort the items in the menu. | |
| unload() | void | Override this to unload your component. (Inherited from Component) | |
| unselect() | void | Unselect the currently selected item and closes the submenu. |
Links to this page: