import { Controller } from '@hotwired/stimulus' /* * The navigation (sidebar or header tabs) as a drawer on small screens: a modal (target * "dialog"), so focus stays inside and Escape closes it. A click on the dimmed page beside it closes it * too, and so does widening the window until the navigation shows (lg). */ export default class extends Controller { static targets = ['dialog'] connect () { this.wide = window.matchMedia('(min-width: 64rem)') this.onWide = (event) => event.matches && this.close() this.wide.addEventListener('change', this.onWide) } disconnect () { this.wide.removeEventListener('change', this.onWide) } open () { this.dialogTarget.showModal() } close () { this.dialogTarget.close() } // a click on the dialog element itself, not on its content, is a click on the backdrop closeOnBackdrop (event) { if (event.target === this.dialogTarget) { this.close() } } }