Sunday, March 2, 2025

How to Create a Simple Alpine.js Modal with Tailwind CSS in Laravel

If you're working on a Laravel project and need a lightweight, easy-to-use modal, Alpine.js and Tailwind CSS make a perfect combination. In this guide, we’ll walk through creating a simple, responsive modal that works smoothly in a Laravel Blade template.

Why Use Alpine.js for Modals?

Unlike jQuery or Vue.js, Alpine.js is a lightweight JavaScript framework designed for simple interactivity. It allows us to handle modals efficiently with minimal code.

✅ No need for extra dependencies
✅ Works seamlessly with Laravel Blade
✅ Lightweight and easy to maintain


Step 1: Install Tailwind CSS in Laravel

If you haven't installed Tailwind CSS in your Laravel project, follow these steps:

1️⃣ Install Tailwind CSS via npm

Run this command in your Laravel project:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

2️⃣ Configure Tailwind to Work with Laravel

Open tailwind.config.js and add the following:

module.exports = {
    content: [
        "./resources/**/*.blade.php",
        "./resources/**/*.js",
        "./resources/**/*.vue",
    ],
    theme: {
        extend: {},
    },
    plugins: [],
};

3️⃣ Add Tailwind to Your Stylesheet

In resources/css/app.css, add:

@tailwind base;
@tailwind components;
@tailwind utilities;

4️⃣ Compile Assets

Run Vite to compile Tailwind CSS:

npm run dev

Step 2: Add Alpine.js to Your Laravel Project

We’ll use Alpine.js to control the modal’s visibility.

👉 Add this Alpine.js script inside the <head> section of your Laravel Blade file:

<script src="//unpkg.com/alpinejs" defer></script>

Step 3: Create a Simple Alpine.js Modal in a Laravel Blade View

Now, let's create a modal component inside a Blade template (resources/views/modal.blade.php).

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Alpine.js Modal in Laravel</title>
    @vite('resources/css/app.css') <!-- Load Tailwind CSS -->
    <script src="//unpkg.com/alpinejs" defer></script>
</head>
<body class="flex items-center justify-center min-h-screen bg-gray-100">
    <!-- Modal Component -->
    <div x-data="{ open: false }">
        <!-- Button to open modal -->
        <button @click="open = true" class="px-4 py-2 text-white bg-blue-600 rounded hover:bg-blue-700">
            Open Modal
        </button>

        <!-- Modal Background -->
        <div x-show="open" class="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50"
            x-transition.opacity
            @click.away="open = false"
            @keydown.escape.window="open = false">
            
            <!-- Modal Content -->
            <div class="bg-white rounded-lg shadow-lg w-96 p-6">
                <h2 class="text-xl font-bold">Modal Title</h2>
                <p class="mt-2 text-gray-600">This is a simple modal using Alpine.js and Tailwind CSS.</p>

                <!-- Close Button -->
                <button @click="open = false"
                    class="mt-4 px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700">
                    Close
                </button>
            </div>
        </div>
    </div>
</body>
</html>

Step 4: How the Alpine.js Modal Works

  • x-data="{ open: false }" initializes the modal as hidden.
  • @click="open = true" on the button opens the modal.
  • x-show="open" controls the visibility of the modal.
  • Clicking outside (@click.away="open = false") or pressing Escape (@keydown.escape.window="open = false") closes the modal.
  • Smooth transitions are handled by x-transition.opacity.

Step 5: Serve Your Laravel Project

Now, visit your Laravel project in the browser by running:

php artisan serve

Go to http://127.0.0.1:8000/modal and test your modal!


Bonus: Add More Features

Want to make your modal even better? Here are some ideas: ✅ Add a fade-in animation using x-transition.scale.95
✅ Make it full-screen with w-full h-full
✅ Use Laravel Livewire for server-side interaction


Conclusion

With just a few lines of code, you've created a fully functional Alpine.js modal in Laravel with Tailwind CSS! 🎉 This method is lightweight, easy to use, and requires no extra JavaScript dependencies.

💬 What do you think? Would you like more Laravel tutorials? Let me know in the comments! 🚀 

No comments:

Post a Comment

How to Create a Simple Alpine.js Modal with Tailwind CSS in Laravel

If you're working on a Laravel project and need a lightweight, easy-to-use modal, Alpine.js and Tailwind CSS make a perfect combinatio...