Skip to content

Instantly share code, notes, and snippets.

@MatinMN
Last active June 5, 2024 10:36
Show Gist options
  • Save MatinMN/06f3a488dc6619e3efb8db743cfc73c4 to your computer and use it in GitHub Desktop.
Save MatinMN/06f3a488dc6619e3efb8db743cfc73c4 to your computer and use it in GitHub Desktop.
Laravel Modal Component
// open modal from livewire component
$this->dispatch('open-modal', name: 'user-details');
// close modal
$this->dispatch('close-modal');
@props(['name', 'title'])
<div
x-data="{ show : false , name : '{{ $name }}' }"
x-show="show"
x-on:hashchange.window="show = (location.hash === '#'+name)"
x-on:open-modal.window="($event.detail.name === name) ? (show = true, location.hash = '#'+name) : '';"
x-on:close-modal.window="location.hash = '#'"
x-on:keydown.escape.window="location.hash = '#'"
style="display:none;" class="fixed z-50 inset-0" x-transition.duration>
{{-- Gray Background --}}
<div x-on:click="location.hash = '#'" class="fixed inset-0 bg-gray-300 opacity-40"></div>
{{-- Modal Body --}}
<div class="bg-white rounded m-auto fixed inset-0 max-w-2xl overflow-y-auto" style="max-height:500px">
@if (isset($title))
<div class="px-4 py-3 flex items-center justify-between border-b border-gray-300">
<div class="text-xl text-gray-800">{{ $title }}</div>
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</a>
</div>
@endif
<div class="p-4">
{{ $body }}
</div>
</div>
</div>
@props(['name', 'title'])
<div x-data="{ show : false , name : '{{ $name }}' }"
x-show="show"
x-on:open-modal.window="show = ($event.detail.name === name)"
x-on:close-modal.window="show = false"
x-on:keydown.escape.window="show = false"
style="display:none;" class="fixed z-50 inset-0" x-transition.duration>
{{-- Gray Background --}}
<div x-on:click="show = false" class="fixed inset-0 bg-gray-300 opacity-40"></div>
{{-- Modal Body --}}
<div class="bg-white rounded m-auto fixed inset-0 max-w-2xl overflow-y-auto" style="max-height:500px">
@if (isset($title))
<div class="px-4 py-3 flex items-center justify-between border-b border-gray-300">
<div class="text-xl text-gray-800">{{ $title }}</div>
<button x-on:click="$dispatch('close-modal')">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
@endif
<div class="p-4">
{{ $body }}
</div>
</div>
</div>
<x-modal name="contactus" title="Contact Us Modal">
<x-slot:body>
<section class="bg-white dark:bg-gray-900">
<div class="py-8 lg:py-16 px-4 mx-auto max-w-screen-md">
<h2 class="mb-4 text-4xl tracking-tight font-extrabold text-center text-gray-900 dark:text-white">Contact Us</h2>
<p class="mb-8 lg:mb-16 font-light text-center text-gray-500 dark:text-gray-400 sm:text-xl">Got a technical issue? Want to send feedback about a beta feature? Need details about our Business plan? Let us know.</p>
<form action="#" class="space-y-8">
<div>
<label for="email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300">Your email</label>
<input type="email" id="email" class="shadow-sm bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500 dark:shadow-sm-light" placeholder="name@flowbite.com" required>
</div>
<div>
<label for="subject" class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300">Subject</label>
<input type="text" id="subject" class="block p-3 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 shadow-sm focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500 dark:shadow-sm-light" placeholder="Let us know how we can help you" required>
</div>
<div class="sm:col-span-2">
<label for="message" class="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400">Your message</label>
<textarea id="message" rows="6" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg shadow-sm border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="Leave a comment..."></textarea>
</div>
<button type="submit" class="py-3 px-5 text-sm font-medium text-center text-white rounded-lg bg-primary-700 sm:w-fit hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">Send message</button>
</form>
</div>
</section>
</x-slot>
</x-modal>
<button class="text-white px-3 py-1 bg-blue-500 rounded text-xs" x-data
@click="$dispatch('open-modal',{name:'contactus'})"> Modal 2 </button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment