-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(toolbar): Guard against adding toolbar to DOM twice #1280
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this dom checking approach really the best solution? Just strikes me that presumably this is an indication that we’re doing something non idiomatic.
Why is rendering the toolbar twice problematic in the first place?
I think this is a good way to guard in this situation. Say a user embeds two separate Electric apps in a page, and both have their own copy of the toolbar in dev mode. It's possible that there are two separate bundled versions of the toolbar loaded. Using the DOM as a guard prevents the toolbar being added twice from the two copies. The alternative is to save a global on the window object, but I think I prefer this method. The double rendering (so far) is a side effect of react strict mode. The toolbar isn't part of the users react app, it's a separate react app that is injected into the body (and so works with Vue and Svelte or anything else). If the user initiates it in a useEffect then under strict mode it injected twice. Essentially, the guard needs to be global, and outside of the scope of the toolbar package. So either the dom or window object. |
I get it, I just don't get why the toolbar can't handle unmount, clean itself up and be mounted again. |
@thruflo that's also a possibility if we think that gives a better DX - I'd still keep a warning though since I would imagine in most cases it would be accidental (like using Either way here's a PR modifying this fix to allow for proper remounting #1287 |
Instead of guarding against remounting the toolbar, clean it up by removing the whole shadow dom root and remount it. Main modification required was to deep clone the style from the template rather than just import it. Alternative fix to #1280
Checks whether container element has already been added before proceeding to add toolbar.
With React's
StrictMode
and other cases theaddToolbar
call might occur twice, handling it with a warning I believe is better than letting it add a broken DOM element.