Introducing ReUI:Open-source UI components and apps built with React, Next.js and Tailwind CSS
Browse ReUI

Metronic 9 reparent not working properly in VueJs


Hi there,

I'm running a VueJs app with Metronic 9. I have an element I'd like to use reparent on that sits inside one component, but the new parent element is in another component.

When I attempt to run reparent with both new parent and the original element in the same component it works properly, but as soon as I enter data-reparent-target to be an element in another component is doesn't work.

Are you aware of this bug? Can we get this fixed so reparent works seamlessly even if the new parent is not in the same component in VueJs?

Thanks!


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (13)


Hi,

Thank you for reaching out.

KTReparent works seamlessly with JS/HTML-based applications where elements are fully loaded on page initialization. However, in a Single Page Application (SPA), in your case, the Vue environment, where components may be rendered at different times, KTReparent might not behave as expected.

To better assist you, could you please share more details about your use case and provide relevant code samples via https://gist.github.com/ ? This will help us identify the best solution for your scenario.

How about trying to use Vue code to dynamically change the component parent?

Looking forward to your response.

Best regards,
Sean



I've tried initializing KTReparent via javascript, the dropdown menu does reparent, but when I click the dropdown button nothing happens.

Here is an example of what I have, but it doesn't work: https://gist.github.com/gespinha/e38641c57cda7268a951c5bd90ff34ec



Hi,

Please try to reparent using Vue native way. You can create a dropdown component and display it in a proper parent conditionally.

Reparent will work fine for JS/HTML mode, for React/Vue/Angular rendering won't work in such cases.

Regards,
Sean



I'm not aware of a native way to reparent in Vuejs. Can you share how you'd go about doing this?



Hi,

To fix this issue you should wrap your code with the nextTick function as shown below.


onMounted(() => {
nextTick(() => {
actionsMenuInstance.value = new KTReparent(actionsMenu.value, {
mode: "append",
target: "#redbox"
})
});
});


Regards,
Lauris Stepanovs,
Keenthemes Support Team



I've tried this and every time I click on a dropdown launch button I get this error:

https://imgur.com/a/mSm7htM

any idea why this is happening?



Hi,

We have the same error if we create KTReparent instance directly inside onMounted function, but if you wrap your code with nextTick it should solve the problem.

Replace this code

onMounted(() => {
actionsMenuInstance.value = new KTReparent(actionsMenu.value, {
mode: "append",
target: "#redbox"
})
})


to


onMounted(() => {
nextTick(() => {
actionsMenuInstance.value = new KTReparent(actionsMenu.value, {
mode: "append",
target: "#redbox"
})
});
});


Regards,
Lauris Stepanovs,
Keenthemes Support Team



I've tried that and it still doesn't work for me. Here is the code I'm using:

https://gist.github.com/gespinha/558d511cb4da95dd4732ca4f64267eec

When I embed that Vue component anywhere I get the same error I showed before when I click on '.dropdown-toggle'

I was looking at the dropdown.ts code an found that this is returning 'null':

'this._contentElement = this._element.querySelector('.dropdown-content, [data-dropdown-content="true"]')'

So there isn't any '.dropdown-content' found.



Hi,

We didn't get any errors when testing your code in our Vue example app.

Are you using the latest Metronic 9 version?

Do you still have our global components initialization code?

Regards,
Lauris Stepanovs,
Keenthemes Support Team



I put a quick example of the latest version of Metronic 9 together so you can see what I'm experiencing: https://codesandbox.io/p/github/gespinha/metronic-reparent-bug/main?import=true

(I'll be deleting the env after you check it out)

You'll see that I'm running the code you shared on ExampleMenu.vue and .dropdown-content is reparented to #redbox, but when I click .dropdown-toggle I'm getting the error I shared above:

"Uncaught TypeError: Cannot read properties of null (reading 'style')"



Hi,

To fix this you should reparent the entire menu element together with the toggle element.
You can just move the ref="actionsMenu" attribute to the root element of your component.

Regards,
Lauris Stepanovs,
Keenthemes Support Team



Right, but that means I'm reparenting both dropdown button and dropdown menu.

What I wanted was to have the button in one area of the and move the dropdown to another area.

Is this not possible?



Hi,

To clarify, you're looking to have a simple HTML menu for desktop, and on mobile, you want it to transform into a menu with a toggle button. Is that correct?

If so, you could approach this differently. First, add the menu directly into your container. Then, for mobile screens, use KTReparent to dynamically move the menu into the designated menu component.

Regards,
Lauris Stepanovs,
Keenthemes Support Team


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(