@stimulus-components/dropdown
Version:
A Stimulus controller to create a dropdown.
29 lines (23 loc) • 617 B
text/typescript
import { Controller } from "@hotwired/stimulus"
import { useTransition } from "stimulus-use"
export default class Dropdown extends Controller {
menuTarget: HTMLElement
toggleTransition: (event?: Event) => void
leave: (event?: Event) => void
transitioned: false
static targets = ["menu"]
connect(): void {
useTransition(this, {
element: this.menuTarget,
})
}
toggle(): void {
this.toggleTransition()
}
hide(event: Event): void {
// @ts-ignore
if (!this.element.contains(event.target) && !this.menuTarget.classList.contains("hidden")) {
this.leave()
}
}
}