How to restore focus to the previously accessed node or link after closing a dialog/modal?

Hi,

We’re working on a GoJS diagram where we have interactive elements like context menus, add node buttons, etc. During keyboard navigation, when a user focuses on one of these external elements (like opening a context menu or triggering an add node button), a dialog/modal is opened for additional actions.

Once the user finishes the operation and the dialog/modal is closed, we want to programmatically restore the focus to the last accessed diagram element — be it a node or a link — that the user interacted with before the modal opened.

Is there a recommended way in GoJS to track and programmatically set focus back to the previously focused node or link after closing a modal?

I tried using the lostFocus and regainFocus events. The lostFocus event gets triggered when the modal opens, but regainFocus doesn’t fire after the modal is closed.

I know that calling this.diagram.focus() can help, but the modal is triggered from an external component that’s not under our control. Is there a way to handle this scenario and properly regain focus back to the diagram?

I believe that the KeyboardCommandHandler’s state does not change as the diagram loses or gains keyboard focus.

Does the diagram and its keyboard focus Adornment change when your modal component is shown?

Nope. Once we open the model the diagram itself lost the focus. When we close the model now the diagram doesnt has focus thats the issue. But, to make focus on the diagram we don’t have control over the model close related events.

No component should be randomly grabbing focus, because that would lead to conflicts. So the GoJS library does not do that except when there is a mouse-down event in the viewport/Div, or when finishing text editing.

The GoJS library cannot know about the behavior of other components, so it is the app’s responsibility to manage keyboard focus.