0

I have a problem when calling a modal through a javascript function. It happens that the const $targetEl takes it as null, but for the const modal if it finds the the Div in the dom. I have followed the flowbite documentation and according to it it should work. What am I doing wrong? The console gives me the following errors (see image)

enter image description here

<script setup lang="ts">
import { Modal } from 'flowbite';
import { ref } from 'vue'

const props = defineProps(['editorials']);
const editorials = props.editorials;
console.log('editorials : ',editorials)
let name:string = '';
let subname:string = '';

const $targetEl = document.getElementById('modalTest');
console.log("$targetEl : ",$targetEl);

const modal = new Modal($targetEl);
console.log("modal : ", modal)

function modalUpdate(data:any){
    console.log('updateData : ',data)
    modal.show();
}

</script>

<template>
<br>
<div class="container mx-auto">
  <div class="shadow-lg bg-slate-300">
    <div class="max-w-sl p-6 bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
        <a href="#">
            <h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Editoriales</h5>
        </a>
        <div class="relative overflow-x-auto shadow-md sm:rounded-lg">
            <table class="w-full text-sl text-left text-gray-500 dark:text-gray-400 table-hover">
                <thead class="text-sl text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
                    <tr>
                        <th scope="col" class="px-6 py-3">
                            Nombre
                        </th>
                        <th scope="col" class="px-6 py-3">
                            Sub-nombre
                        </th>
                        <th scope="col" class="px-6 py-3">
                            Estado
                        </th>
                        <th scope="col" class="px-6 py-3">
                            País
                        </th>
                        <th scope="col" class="px-6 py-3">
                            Opciones
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="editorial in editorials" class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
                        <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                            {{ editorial.edi_name }}
                        </th>
                        <td class="px-6 py-4">
                            {{ editorial.edi_sub_name }}
                        </td>
                        <td class="px-6 py-4">
                            {{ editorial.edi_enabled }}
                        </td>
                        <td class="px-6 py-4">
                            {{ editorial.country_name }}
                        </td>
                        <td class="px-6 py-4">
                            <span class="material-icons pointer" title="Editar" @click="modalUpdate(editorial)">edit</span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
  </div>
</div>

<div id="modalTest" ref="modalTest" tabindex="-1" aria-hidden="true" class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-[calc(100%-1rem)] max-h-full">
    <div class="relative w-full max-w-2xl max-h-full">
        <!-- Modal content -->
        <div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
            <!-- Modal header -->
            <div class="flex items-start justify-between p-5 border-b rounded-t dark:border-gray-600">
                <h3 class="text-xl font-semibold text-gray-900 lg:text-2xl dark:text-white">
                    Terms of Service
                </h3>
                <button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ml-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white">
                    <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
                    </svg>
                    <span class="sr-only">Close modal</span>
                </button>
            </div>
            <!-- Modal body -->
            <div class="p-6 space-y-6">
                <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
                    With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
                </p>
                <p class="text-base leading-relaxed text-gray-500 dark:text-gray-400">
                    The European Union’s General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
                </p>
            </div>
            <!-- Modal footer -->
            <div class="flex items-center p-6 space-x-2 border-t border-gray-200 rounded-b dark:border-gray-600">
                <button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">I accept</button>
                <button type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-blue-300 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600">Decline</button>
            </div>
        </div>
    </div>
</div>
</template>
Kvothe_0077
  • 109
  • 1
  • 12
  • Which documentation? [Flowbite modal docs](https://flowbite-vue.com/components/modal) has code very different from yours. Most Vue apps shouldn't use `document` interface or its functions, e.g. `getElementById()`. It can intefere with Vue's managing of the DOM. The Flowbite docs don't use `document` functions, not sure why yours does. – yoduh Aug 06 '23 at 14:56
  • I'm using this, shouldn't I? https://flowbite.com/docs/components/modal/ – Kvothe_0077 Aug 06 '23 at 15:01
  • @Kvothe_0077 No, you shouldn't, always stick to Vue-specific libs where possible, you can check for Vue+jQuery topic, etc on possible implications of not doing this. There's already flowbite-vue – Estus Flask Aug 07 '23 at 12:47

0 Answers0