xN77
xN77

Reputation: 75

How to change the properties of MenuItem (like label) dynamically?

I need to change the properties of MenuItem Class dynamically. I am creating a context menu for my electron js desktop app.

It is the declaration of menu for right click context menu.

const menu = new Menu();
menu.append(new MenuItem({id: 'open', label: 'Open File Location', click() { console.log('item 1 clicked') }}));
menu.append(new MenuItem({id: 'search', label: `Search in Google `, click() { console.log('item 2 clicked') }}));

Then I change the properties of the MenuItem.

let search = menu.getMenuItemById('search');
search.label = `Search *new text* in Google`;
menu.popup({window: remote.getCurrentWindow()});

I want to achieve Search *new text* in Google in context-menu but it still shows Search in Google

ElectronJs docs states that context-menu properties can be changed dynamically. But how? Reference: Electron docs on MenuItem properties

While was finding a solution for this, I came to know that is can be achieved by re-initialling the menu (though that was for menubar). But I don't want to do that.

Upvotes: 0

Views: 381

Answers (1)

Erick
Erick

Reputation: 1245

The docs on the website are currently outdated. PR #21823 amended the documentation to note that labels cannot be dynamically changed.

That PR in turn references Issue #12633 (comment), where it is stated that this is currently not possible with how the menus are architected.

Our menu code is currently not written to be dynamic (and thus allow for this) and given that we would need to rearchitect our entire menu at this stage I don't believe we're prepare to do that. In that event, i'm going to close this at wontfix, but i'll let you know if something changes in the future!

Reinitializing the menu might be your best bet for now.

Upvotes: 1

Related Questions