Created
December 2, 2022 05:23
-
-
Save dpaluy/c425963431be2c4c184806eacac0dbcf to your computer and use it in GitHub Desktop.
StimulusJS controller that will close a modal when the user clicks outside of it
This file contains hidden or 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" data-action-"click@document->modal#close"> | |
| <div class="modal" data-target="modal.modal"> | |
| ... modal content goes here | |
| </div> | |
| </div> |
This file contains hidden or 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 "stimulus" | |
| export default class extends Controller { | |
| static targets = ["modal"] | |
| close(event) { | |
| if (!this.modalTarget.contains(event.target)) { | |
| this.modalTarget.classList.remove("is-active") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment