import { Controller } from '@hotwired/stimulus'; export default class extends Controller { static targets = ['button', 'popup', 'backdrop']; toggle(event) { event.preventDefault(); if (this.buttonTarget.getAttribute('aria-expanded') == 'false') { this.show(); } else { this.hide(null); } } show() { this.buttonTarget.setAttribute('aria-expanded', 'true'); this.popupTarget.classList.remove('hidden'); } hide(event) { if (event) { const target = event.target; const clickedInsidePopup = this.popupTarget.contains(target); const clickedInsideButton = this.buttonTarget.contains(target); const clickedBackdrop = this.hasBackdropTarget && this.backdropTarget.contains(target); const clickedFormOrInput = target.tagName === 'FORM' || target.tagName === 'INPUT' || target.tagName === 'TEXTAREA'; // popup 또는 button 안을 클릭했는데 backdrop이 아니라면 -> 아무것도 안 함 if ((clickedInsidePopup || clickedInsideButton) && !clickedBackdrop) { return; } else if (clickedFormOrInput) { return; } } this.buttonTarget.setAttribute('aria-expanded', 'false'); this.popupTarget.classList.add('hidden'); } }