Skip to content

Conversation

@Code-Victor
Copy link
Collaborator

@Code-Victor Code-Victor commented Jan 22, 2026

Fix: Prevent App Crash When Closing Editor Tab with Selection

Closes #193
Closes #181

Problem

The app crashes with a Failed to execute 'removeChild' on 'Node' error when closing an editor tab that has a text selection. This happens because Monaco's addContentWidget moves React-managed DOM nodes to its overlay container. When the tab closes, React tries to unmount nodes that are no longer in their expected location.

Solution

Create widget containers imperatively (outside of React's DOM tree) using document.createElement(), and render content into them using React portals. This way:

  • React doesn't try to manage the container's lifecycle
  • Monaco can freely move the containers to its overlay
  • Cleanup functions properly remove containers from wherever they are in the DOM

Changes

  1. edit-code-widget.tsx - Create suggestion widget container imperatively and use createPortal
  2. generate-widget.tsx - Apply same pattern for generate widget containers
  3. useMonacoEditor.ts - Add widget refs, cleanup functions, and proper useEffect cleanup returns
  4. project-layout.tsx - Call cleanup functions before removing tabs

Testing

  • Open a file, select text, close the tab → No crash
  • Open multiple files, select text in one, close it → Other tabs still work
  • AI Edit widget appears/disappears correctly
  • Generate widget functions normally

…DOM conflicts

Monaco moves contentWidget DOM nodes to its overlay container. When React
tries to unmount these nodes, they're no longer in their expected location,
causing 'removeChild' errors. This fix creates the widget container via
document.createElement() and uses React portals to render content into it.

Fixes jamesmurdza#193
… DOM conflicts

Apply the same fix pattern to GenerateWidget - create anchor and widget
containers via document.createElement() and use React portals to render
content. This prevents Monaco from causing DOM node conflicts when the
editor tab is closed.

Related to jamesmurdza#193
- Add suggestionWidgetRef to store widget instance for proper cleanup
- Add cleanup return to suggestion widget useEffect
- Add cleanup return to generate widget useEffect
- Add cleanupWidgets function for manual widget cleanup
- Update refs to MutableRefObject for imperative container assignment

Related to jamesmurdza#193
Call cleanupWidgets and reset widget states (isSelected, generate.show)
before removing the tab to ensure Monaco widgets are properly disposed
before React unmounts the component.

Related to jamesmurdza#193
@vercel
Copy link

vercel bot commented Jan 22, 2026

@Code-Victor is attempting to deploy a commit to the jamesmurdza's projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Editor] App crashes when closing editor tab with active selection [Editor] Closing a file gives client side exception

1 participant