Move dialog focus handling into the dialog controller

... instead of a separate dialog focus controller
This commit is contained in:
Adrien Maston
2026-01-19 11:42:43 +01:00
parent 600ee87d51
commit 44732a345b
10 changed files with 17 additions and 25 deletions
@@ -1,8 +1,9 @@
import { Controller } from "@hotwired/stimulus"
import { orient } from "helpers/orientation_helpers"
import { isTouchDevice } from "helpers/platform_helpers"
export default class extends Controller {
static targets = [ "dialog" ]
static targets = [ "dialog", "focusMouse", "focusTouch" ]
static values = {
modal: { type: Boolean, default: false },
sizing: { type: Boolean, default: true },
@@ -10,6 +11,7 @@ export default class extends Controller {
}
connect() {
this.#setupFocus()
this.dialogTarget.setAttribute("aria-hidden", "true")
if (this.autoOpenValue) this.open()
}
@@ -63,4 +65,10 @@ export default class extends Controller {
captureKey(event) {
if (event.key !== "Escape") { event.stopPropagation() }
}
#setupFocus() {
const touch = isTouchDevice()
if (this.hasFocusMouseTarget) this.focusMouseTarget.autofocus = !touch
if (this.hasFocusTouchTarget) this.focusTouchTarget.autofocus = touch
}
}