Last active
March 18, 2024 16:55
-
-
Save jakebrinkmann/e8280d945616da17bef8f4fdada07b57 to your computer and use it in GitHub Desktop.
js-alpinejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@1.2.0/dist/tailwind.min.css"> | |
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@2.8.2/dist/alpine.min.js" | |
defer crossorigin="anonymous"></script> | |
</head> | |
<body class="bg-gray-300 text-gray-800"> | |
<div class="container mx-auto px-4 xl:px-64 mt-12 mb-12"> | |
<h2 class="text-2xl font-bold">Modal</h2> | |
<!-- NOTE: ADD x-data here --> | |
<div x-data="{ isOpen: false }"> | |
<!-- NOTE: USE nextTick TO RUN VANILLA JS AFTER CLICK --> | |
<button | |
class="bg-blue-700 text-white px-4 py-3 mt-4 text-sm rounded" | |
@click="isOpen = true | |
$nextTick(() => $refs.modalCloseButton.focus()) | |
" | |
> | |
Open Modal | |
</button> | |
<div | |
style="background-color: rgba(0, 0, 0, .5)" | |
class="mx-auto absoluteE top-0 left-0 w-full h-full flex items-center shadown-lg overflow-y-auto" | |
x-show="isOpen" | |
> | |
<div class="container mx-auto rounded p-4 mt-2 overflow-y-auto"> | |
<div class="bg-white rounded px-8 py-8"> | |
<h1 class="font-bold text-2xl mb-3">Modal Title</h1> | |
<div class="modal-body"> | |
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p> | |
</div> | |
<div class="mt-4"> | |
<!-- NOTE: CREATE A REFERENCE --> | |
<button | |
class="bg-blue-700 text-white px-4 py-3 mt-4 text-sm rounded" | |
@click="isOpen = false" | |
x-ref="modalCloseButton" | |
> | |
Close Modal | |
</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@1.2.0/dist/tailwind.min.css"> | |
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@2.8.2/dist/alpine.min.js" | |
defer crossorigin="anonymous"></script> | |
</head> | |
<body class="bg-gray-300 text-gray-800"> | |
<header class="border-t-4 border-blue-700 bg-white shadow-md"> | |
<nav class="container mx-auto px-4 flex items-center justify-between py-4"> | |
<ul class="flex"> | |
<li><a href="#">Home</a></li> | |
</ul> | |
<!--- NOTE: ADD x-data here, so button can toggle --> | |
<div class="relative" x-data="{ isOpen: false }"> | |
<button | |
class="flex items-center" | |
x-on:click="isOpen = !isOpen" | |
x-on:keydown.escape="isOpen = false" | |
> | |
<img | |
src="https://www.gravatar.com/avatar?d=mm" | |
alt="avatar" | |
class="w-8 h-8 rounded-full" | |
> | |
<svg | |
fill="currentColor" | |
xmlns="http://www.w3.org/2000/svg" | |
viewBox="0 0 24 24" | |
width="24" | |
height="24" | |
class="heroicon-ui" | |
> | |
</svg> | |
</button> | |
<ul | |
class="absolute font-normal bg-white shadow overflow-hidden rounded w-48 border mt-2 py-1 right-0 z-20" | |
x-show='isOpen' | |
x-on:click.away="isOpen = false" | |
> | |
<li>X</li> | |
</ul> | |
</div> | |
</nav> | |
</header> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment