Mentorat Amara du 29 septembre 2024

Créer un une fenêtre de message avec Tailwind et alpine.js

<div x-data="{ show: false }" 
    x-show="show" 
    x-transition:enter="transition ease-out duration-500" 
    x-transition:enter-start="opacity-0" 
    x-transition:enter-end="opacity-100"
    x-transition:leave="transition ease-in duration-1000" 
    x-transition:leave-start="opacity-100" 
    x-transition:leave-end="opacity-0"
    @htmx:load.window="show = true; setTimeout(() => show = false, 4000)" 
    class="fixed top-4 left-1/2 transform -translate-x-1/2 bg-green-500 text-white px-6 py-3 
    		rounded-lg shadow-lg w-1/2 z-[1000]"
    x-init="$watch('show', value => value ? setTimeout(() => show = false, 4000) : '')"
>
    <!-- Contenu du message avec position relative -->
    <div class="relative">
        <span> {{ message }} </span>
        
        <!-- Bouton de fermeture en position absolue -->
        <button @click="show = false" 
                class="absolute top-0 right-0 mt-1 mr-1 text-white">
            <!-- Icone "X" de Heroicons -->
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
            </svg>

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

Créer un une fenêtre modale avec Tailwind et alpine.js

<div x-data="{ open: false }" class="flex justify-center " x-on:form-success.window="open = false">
    <!-- Sur le form: utiliser x-on:form-success.window="$el.reset()"
	<!-- Modal -->
    <div x-show="open" style="display: none" x-on:keydown.escape.prevent.stop="open = false"
        role="dialog" aria-modal="true" x-id="['modal-title']" :aria-labelledby="$id('modal-title')"
        class="fixed inset-0 z-10 overflow-y-auto"
    >
        <!-- Overlay -->
        <div x-show="open" x-transition.opacity class="fixed inset-0 bg-black bg-opacity-50"></div>

        <!-- Panel -->
        <div
            x-show="open" x-transition x-on:click="open = false"
            class="relative flex min-h-screen items-center justify-center p-4"
        >
            <div
                x-on:click.stop x-trap.noscroll.inert="open"
                class="relative w-full max-w-2xl overflow-y-auto rounded-xl bg-white p-12 shadow-lg"
            >
                <!-- Title -->
                <h2 class="text-3xl font-bold" :id="$id('modal-title')">Titre de la modale</h2>

                <!-- Content -->
                <p class="mt-2 text-gray-600">Are you sure you want to learn how to create an awesome modal?</p>

                <!-- Buttons -->
                <div class="mt-8 flex space-x-2">
                    <button type="button" x-on:click="open = false" class="rounded-md border border-gray-200 bg-white px-5 py-2.5">
                        Confirm
                    </button>

                    <button type="button" x-on:click="open = false" class="rounded-md border border-gray-200 bg-white px-5 py-2.5">
                        Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>