Source Matters
Source Matters

Reputation: 1221

Using Tailwind/Flowbite and can't get "top-center" modal directive to work

I'm using Tailwindcss and Flowbite, and have a working modal. I'm now attempting to have it positioned "top-center", and according to the docs (https://flowbite.com/docs/components/modal/#placement), I should be able to simply add the data-modal-placement data attribute to my modal div for this.

I've attempted this (with below code), but modal behaves the same as default. In dev console, I can see the modal getting the default classes assigned as well (those being: "justify-center items-center flex").

What is causing this to not "take" properly?

<div id="modal-detail" tabindex="-1" 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" data-modal-placement="top-center">
    <div class="relative w-full max-w-7xl max-h-full">
        <!-- Modal content -->
        <div class="relative bg-white rounded-lg shadow dark:bg-zinc-900">
            <button type="button" id="close-detail-modal" class="absolute top-3 right-2.5 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>
            <!-- Modal header -->
            <div class="flex flex-col justify-start px-6 py-4 border-b rounded-t dark:border-gray-600">
                <h3 class="text-base font-normal text-gray-900 lg:text-lg dark:text-white">Item Detail</h3>
            </div>
            <!-- Modal body -->
            <div class="p-6 overflow-y-auto">

            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 1238

Answers (1)

Source Matters
Source Matters

Reputation: 1221

I hate finding the same issue on SO and then poster never circles back with the fix, but marks it as "Answered". So in order to not be that guy, I have to post, even if I was the culprit of the issue. :) (they call this "growth" as a developer right?)

@Wongjn's comment made me re-look at my code since he proved that "the code works" for the modal.

What I found, is that in my referencing of the modal for click handling purposes, I found I was setting a position value in the options... and here is the root cause code:

// get reference to modal element
const $targetEl = document.getElementById('modal-detail');

// options with default values
const options = {
    placement: 'center-center', <----------- here's the culprit!
    backdrop: 'dynamic',
    backdropClasses: 'bg-zinc-950 bg-opacity-50 dark:bg-opacity-80 fixed inset-0 z-40',
    closable: true
};

modal_detail = new Modal($targetEl, options);

Upvotes: 1

Related Questions