Created
July 29, 2022 08:24
-
-
Save thomasvanholder/4e71ffea4bcdb4984b466c7758dbfa36 to your computer and use it in GitHub Desktop.
Create a modal with the HTML dialog element, Tailwind and Stimulus
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
@layer components { | |
label { | |
@apply text-sm font-medium text-gray-700; | |
} | |
input[type="text"] { | |
@apply block w-full border-gray-300 rounded-md shadow-sm sm:max-w-md sm:text-sm; | |
@apply focus:ring-blue-500 focus:border-blue-500; | |
} | |
} |
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
<div data-controller="modal"> | |
<button class="btn-outline" data-action="modal#open">Open modal</button> | |
<dialog data-modal-target="modal" class="p-0 backdrop:bg-gray-400 backdrop:bg-opacity-50 open:animate-fade-in open:backdrop:animate-fade-in"> | |
<button type="button" class="absolute top-4 right-4" data-action="modal#close">X</button> | |
<h1 class="p-4">Reset your password</h1> | |
<hr class="mb-4"> | |
<form class="p-4 space-y-4" data-modal-target="form"> | |
<label>Email</label> | |
<input type="text"> | |
<button type="button" data-action="modal#close" class="btn-outline">Close</button> | |
<button type="button" class="btn-blue">Reset Password</button> | |
</form> | |
</dialog> | |
</div> |
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
import { Controller } from "@hotwired/stimulus" | |
export default class extends Controller { | |
static targets = ["modal", "form"] | |
open(event) { | |
event.preventDefault(); | |
this.modalTarget.showModal(); | |
this.modalTarget.addEventListener('click', (e) => this._backdropClick(e)); | |
} | |
close(event) { | |
event.preventDefault(); | |
this.modalTarget.close(); | |
this.formTarget.reset() | |
} | |
_backdropClick(event) { | |
event.target === this.modalTarget && this.close(event) | |
} | |
} |
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
module.exports = { | |
mode: 'jit', | |
// ... | |
theme: { | |
extend: { | |
keyframes: { | |
"fade-in": { | |
'0%': { opacity: '0%' }, | |
'100%': { opacity: '100%' }, | |
} | |
}, | |
animation: { | |
"fade-in": 'fade-in 0.5s ease-in-out', | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tutorial