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(custom-element): update initial render for defineCustomElement to run after nextTick #11335

Closed
wants to merge 5 commits into from

Conversation

jcbond92
Copy link

@jcbond92 jcbond92 commented Jul 11, 2024

Problem

There is an edge case where using Vue generated custom elements inside of a Vue app with v-model will cause duplication inside of the ShadowRoot.

Solution

Waiting for the nextTick seems to resolve a collision between v-model and the custom element's initial render. This fix also limits impact to custom element users rather than all Vue users.

For example, you can also resolve the issue by putting a small timeout (or presumably also waiting for nextTick) around the initial setting of the value in vModel.ts.

mounted(el, { value }) {
    setTimeout(() => {
      el.value = value == null ? '' : value
    }, 1)
  },

Testing the fix

Here are some examples in Stackblitz and the SFC playground to test the change:

close: #9885

@jcbond92 jcbond92 changed the title fix(v-model): update initial render for defineCustomElement to run after nextTick fix(custom-element): update initial render for defineCustomElement to run after nextTick Jul 11, 2024
@jcbond92
Copy link
Author

jcbond92 commented Jul 12, 2024

Found a better solution that can be implemented by library authors instead of making the change here in Vue core.

#9885 (comment)

Edit: the solution I thought would fix this that we could implement in our components didn't actually work in all scenarios. Updating this PR to get tests passing.

@jcbond92 jcbond92 closed this Jul 12, 2024
@jcbond92 jcbond92 reopened this Jul 12, 2024
@@ -54,7 +54,7 @@ const sfcOptions = computed(
template: {
isProd: productionMode.value,
compilerOptions: {
isCustomElement: (tag: string) => tag === 'mjx-container',
isCustomElement: (tag: string) => tag === 'mjx-container' || tag.startsWith('my-'),
Copy link
Author

Choose a reason for hiding this comment

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

This can be removed once reviews are complete, I just wanted to add a quick way to show the fix in the SFC playground for reviewers.

@yyx990803 yyx990803 closed this in 978ff3c Aug 7, 2024
@jcbond92 jcbond92 deleted the v-model-custom-element branch August 7, 2024 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom element wrapped in HTMLElement duplicates component when using v-model
2 participants