modal.blade.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. @props([
  2. 'name',
  3. 'show' => false,
  4. 'maxWidth' => '2xl'
  5. ])
  6. @php
  7. $maxWidth = [
  8. 'sm' => 'sm:max-w-sm',
  9. 'md' => 'sm:max-w-md',
  10. 'lg' => 'sm:max-w-lg',
  11. 'xl' => 'sm:max-w-xl',
  12. '2xl' => 'sm:max-w-2xl',
  13. ][$maxWidth];
  14. @endphp
  15. <div
  16. x-data="{
  17. show: @js($show),
  18. focusables() {
  19. // All focusable element types...
  20. let selector = 'a, button, input:not([type=\'hidden\']), textarea, select, details, [tabindex]:not([tabindex=\'-1\'])'
  21. return [...$el.querySelectorAll(selector)]
  22. // All non-disabled elements...
  23. .filter(el => ! el.hasAttribute('disabled'))
  24. },
  25. firstFocusable() { return this.focusables()[0] },
  26. lastFocusable() { return this.focusables().slice(-1)[0] },
  27. nextFocusable() { return this.focusables()[this.nextFocusableIndex()] || this.firstFocusable() },
  28. prevFocusable() { return this.focusables()[this.prevFocusableIndex()] || this.lastFocusable() },
  29. nextFocusableIndex() { return (this.focusables().indexOf(document.activeElement) + 1) % (this.focusables().length + 1) },
  30. prevFocusableIndex() { return Math.max(0, this.focusables().indexOf(document.activeElement)) -1 },
  31. }"
  32. x-init="$watch('show', value => {
  33. if (value) {
  34. document.body.classList.add('overflow-y-hidden');
  35. {{ $attributes->has('focusable') ? 'setTimeout(() => firstFocusable().focus(), 100)' : '' }}
  36. } else {
  37. document.body.classList.remove('overflow-y-hidden');
  38. }
  39. })"
  40. x-on:open-modal.window="$event.detail == '{{ $name }}' ? show = true : null"
  41. x-on:close-modal.window="$event.detail == '{{ $name }}' ? show = false : null"
  42. x-on:close.stop="show = false"
  43. x-on:keydown.escape.window="show = false"
  44. x-on:keydown.tab.prevent="$event.shiftKey || nextFocusable().focus()"
  45. x-on:keydown.shift.tab.prevent="prevFocusable().focus()"
  46. x-show="show"
  47. class="fixed inset-0 overflow-y-auto px-4 py-6 sm:px-0 z-50"
  48. style="display: {{ $show ? 'block' : 'none' }};"
  49. >
  50. <div
  51. x-show="show"
  52. class="fixed inset-0 transform transition-all"
  53. x-on:click="show = false"
  54. x-transition:enter="ease-out duration-300"
  55. x-transition:enter-start="opacity-0"
  56. x-transition:enter-end="opacity-100"
  57. x-transition:leave="ease-in duration-200"
  58. x-transition:leave-start="opacity-100"
  59. x-transition:leave-end="opacity-0"
  60. >
  61. <div class="absolute inset-0 bg-gray-500 dark:bg-gray-900 opacity-75"></div>
  62. </div>
  63. <div
  64. x-show="show"
  65. class="mb-6 bg-white dark:bg-gray-800 rounded-lg overflow-hidden shadow-xl transform transition-all sm:w-full {{ $maxWidth }} sm:mx-auto"
  66. x-transition:enter="ease-out duration-300"
  67. x-transition:enter-start="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  68. x-transition:enter-end="opacity-100 translate-y-0 sm:scale-100"
  69. x-transition:leave="ease-in duration-200"
  70. x-transition:leave-start="opacity-100 translate-y-0 sm:scale-100"
  71. x-transition:leave-end="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  72. >
  73. {{ $slot }}
  74. </div>
  75. </div>