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(main-scroll): obtain observed container element reliably to work with any Vue version #1552

Merged
merged 3 commits into from
Jul 9, 2024

Conversation

joseacabaneros
Copy link
Member

@joseacabaneros joseacabaneros commented Jul 3, 2024

Obtain observed container element reliably to work with any Vue version

With the following code, we are able to retrieve the root element of a renderless component in both Vue 2 and Vue 3:

onMounted(() => {
   containerRef.value = getCurrentInstance()?.proxy?.$el;
});
...
return () => slots.default?.()[0];

In Vue 2 and Vue 3:

  • slots.default?.() -> Gives an array of vNodes | [0] Gets the vNode root and renders it

IF we would return the full array of vNodes return () => slots.default?.():

  • In Vue 2: Renders the vNodes as it is without problem, and we can get the root element with getCurrentInstance()?.proxy?.$el. Only if the default slot has a single node, otherwise, Vue will throw an error
    [Vue warn]: Multiple root nodes returned from render function. Render function should return a single root node.
  • In Vue 3: Renders the vNodes as a Fragment (because in Vue3 supports multiple root nodes) and when we do getCurrentInstance()?.proxy?.$el it gives a #text node because the root node is the Fragment.

One more thing, this scope will support attrs inheritance in both Vue versions ✌🏾

@joseacabaneros joseacabaneros requested a review from a team as a code owner July 3, 2024 17:50
@diegopf diegopf merged commit 0b4e415 into main Jul 9, 2024
4 checks passed
@diegopf diegopf deleted the fix-main-scroll-vue-3 branch July 9, 2024 16:05
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.

2 participants