Skip to content

Instantly share code, notes, and snippets.

@terryupton
Created April 9, 2020 18:18
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terryupton/287ca7c3023fa59e7d24da302e1d03d5 to your computer and use it in GitHub Desktop.
Save terryupton/287ca7c3023fa59e7d24da302e1d03d5 to your computer and use it in GitHub Desktop.
Ajax Loading a page into a modal with Alpine JS
<section x-data="{showModal: false, html: ''}">
<button
@click="html='loading...'; showLoading = true; showModal = !showModal;
fetch('{{ entry.url }}', {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
})
.then(response => response.text())
.then(text => {
html = text
showLoading = false;
})"
class="flex justify-center animate-grow-on-hover--small"
>
click me
</button>
<div id="bgMask" class="absolute z-90 inset-0 bg-black opacity-75" x-show="showModal"></div>
<div
class="fixed z-90 inset-0 overflow-scroll my-6 md:my-12 mx-auto max-w-5xl w-2/3 md:rounded shadow-2xl bg-white "
x-show="showModal"
@click.away="showModal = false"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="opacity-0 transform scale-90"
x-transition:enter-end="opacity-100 transform scale-100"
x-transition:leave="transition ease-in duration-200"
x-transition:leave-start="opacity-100 transform scale-100"
x-transition:leave-end="opacity-0 transform scale-90"
>
<div class="relative h-full w-full ">
<div class="absolute right-0 top-0 z-90 text-gray-700 hover:text-red-500 m-2" @click="showModal = false">
{{ svg('@icons/close.svg', class='w-5 h-5 fill-current') }}
</div>
<div
x-show="showLoading"
x-transition:leave="transition ease-out duration-200"
x-transition:leave-start="opacity-100"
x-transition:leave-end="opacity-0"
class="absolute h-full w-full inset-0 z-20 flex flex-col justify-center items-center text-gray-600 bg-white"
{# class="absolute h-full w-full inset-0 z-20 flex flex-col justify-center items-center text-gray-100 bg-black" #}
>
<svg version="1.1" id="L9" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" enable-background="new 0 0 0 0" xml:space="preserve" class="w-16 h-16 fill-current">
<path d="M73,50c0-12.7-10.3-23-23-23S27,37.3,27,50 M30.9,50c0-10.5,8.5-19.1,19.1-19.1S69.1,39.5,69.1,50">
<animateTransform
attributeName="transform"
attributeType="XML"
type="rotate"
dur="1s"
from="0 50 50"
to="360 50 50"
repeatCount="indefinite" />
</path>
</svg>
Loading&hellip;
</div>
<div x-html="html"></div>
</div>
</div>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment