Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Created December 2, 2022 05:23
Show Gist options
  • Select an option

  • Save dpaluy/c425963431be2c4c184806eacac0dbcf to your computer and use it in GitHub Desktop.

Select an option

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
<div data-controller="modal" data-action-"click@document->modal#close">
<div class="modal" data-target="modal.modal">
... modal content goes here
</div>
</div>
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