Skip to content

Instantly share code, notes, and snippets.

@jvanbaarsen
Last active February 22, 2022 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanbaarsen/34f27b181d7728a4791fa877525caec4 to your computer and use it in GitHub Desktop.
Save jvanbaarsen/34f27b181d7728a4791fa877525caec4 to your computer and use it in GitHub Desktop.
import { Controller } from "@hotwired/stimulus";
import { useTransition } from "stimulus-use";
// Connects to data-controller="profile-menu"
export default class extends Controller {
static targets = ["menu"];
connect() {
this.addTransitionStyling();
useTransition(this, { element: this.menuTarget });
}
toggle() {
this.toggleTransition();
}
hide(event) {
if (
!this.element.contains(event.target) &&
!this.menuTarget.classList.contains("hidden")
) {
this.leave();
}
}
addTransitionStyling() {
var transitionList = {
transitionEnterActive: "transition ease-out duration-200",
transitionEnterFrom: "transform opacity-0 scale-95",
transitionEnterTo: "transform opacity-100 scale-100",
transitionLeaveActive: "transition ease-in duration-75",
transitionLeaveFrom: "transform opacity-100 scale-100",
transitionLeaveTo: "transform opacity-0 scale-95",
};
Object.keys(transitionList).forEach((key) => {
this.menuTarget.dataset[key] = transitionList[key];
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment