Files
fizzy/app/javascript/controllers/bar_controller.js
T
Jason Zimdars 0c05694a3c This seems to have caused the unwanted effect of not focusing the field in Mobile Safari
Looks like it regressed in
https://github.com/basecamp/fizzy/commit/250935530f86eb628d6b54ec6b7212dac9e76f8b.
Let's see if we can get focus in Safari without sacrificing select in
other browsers by simply moving the nextFrame() call
2025-11-24 17:48:02 -06:00

77 lines
1.6 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
import { post } from "@rails/request.js"
import { nextFrame } from "helpers/timing_helpers";
export default class extends Controller {
static targets = [ "turboFrame", "search", "searchInput", "form", "buttonsContainer" ]
static outlets = [ "dialog" ]
static values = {
searchUrl: String,
}
dialogOutletConnected(outlet, element) {
outlet.close()
this.#clearTurboFrame()
}
reset() {
this.dialogOutlet.close()
this.#clearTurboFrame()
this.#showItem(this.buttonsContainerTarget)
this.#hideItem(this.searchTarget)
}
clearInput() {
if (this.searchInputTarget.value) {
this.searchInputTarget.value = ""
this.searchInputTarget.focus()
} else {
this.reset()
}
}
showModalAndSubmit(event) {
this.showModal()
this.formTarget.requestSubmit()
}
showModal() {
this.dialogOutlet.open()
}
search(event) {
this.#showItem(this.searchTarget)
this.#hideItem(this.buttonsContainerTarget)
if (this.searchInputTarget.value.trim()) {
this.showModalAndSubmit()
} else {
this.#loadTurboFrame()
}
}
#loadTurboFrame() {
this.turboFrameTarget.src = this.searchUrlValue
}
#clearTurboFrame() {
this.turboFrameTarget.removeAttribute("src")
this.turboFrameTarget.innerHtml = ""
}
async #showItem(element) {
element.removeAttribute("hidden")
const autofocusElement = element.querySelector("[autofocus]")
autofocusElement?.focus()
await nextFrame()
autofocusElement?.select()
}
#hideItem(element) {
element.setAttribute("hidden", "hidden")
}
}