Skip to content
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

Merged
merged 1 commit into from
May 20, 2024

Conversation

msfstef
Copy link
Contributor

@msfstef msfstef commented May 20, 2024

Checks whether container element has already been added before proceeding to add toolbar.

With React's StrictMode and other cases the addToolbar call might occur twice, handling it with a warning I believe is better than letting it add a broken DOM element.

Copy link
Contributor

@samwillis samwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@msfstef msfstef merged commit f4af7f6 into main May 20, 2024
10 checks passed
@msfstef msfstef deleted the msfstef/initialize-toolbar-once branch May 20, 2024 15:34
Copy link
Contributor

@thruflo thruflo left a 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?

@samwillis
Copy link
Contributor

@thruflo

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.

@thruflo
Copy link
Contributor

thruflo commented May 20, 2024

I get it, I just don't get why the toolbar can't handle unmount, clean itself up and be mounted again.

@msfstef
Copy link
Contributor Author

msfstef commented May 21, 2024

@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 React.StrictMode) and probably annoying since it will minimize the toolbar as well if you're looking at it.

Either way here's a PR modifying this fix to allow for proper remounting #1287

msfstef added a commit that referenced this pull request Jun 4, 2024
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
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.

None yet

3 participants