I’m using Metronic with Laravel + Livewire, and everything works fine in general.
I have an accordion placed inside a Livewire component that renders as a modal. The issue is: when I open the modal, the built-in KTUI accordion JS does not work.
Do I need to reinitialize KTUI when the modal is opened, or is there another recommended approach to make the accordion work inside dynamically rendered modals? If I need to reinitialize KTUI, how can I do it?
Hi Adam Nielsen
Yes, you need to reinitialize KTUI components after Livewire updates the DOM.
// After Livewire updates
Livewire.hook("message.processed", (message, component) => {
// Re-initialize all KTUI components
KTComponents.init();
});
// If using Bootstrap modals
document.addEventListener("shown.bs.modal", function(event) {
const modal = event.target;
const accordions = modal.querySelectorAll("[data-kt-accordion]");
accordions.forEach(element => {
if (!element.hasAttribute("data-kt-accordion-initialized")) {
new KTAccordion(element);
}
});
});
Hey thank you. I am using Tailwind. Unfortunately, the
Livewire.hook("message.processed", (message, component) => {
// Re-initialize all KTUI components
console.log("hallo");
KTComponents.init();
});
<pre lang="html">
<x-modal>
@if($user)
<x-accordion >
@endif
</x-modal>
<pre lang="html">
<div>
<livewire:modal>
</div>
@push("scripts")
<script>
document.addEventListener("DOMContentLoaded", function () {
Livewire.hook("message.processed", (message, component) => {
// Re-initialize all KTUI components
KTComponents.init();
});
</script>
@endpush
Livewire.on("open-info-modal", () => {
console.log("reinit mtgf");
KTComponents.init();
});