Files
fizzy/app/javascript/controllers/soft_keyboard_controller.js
T
David Heinemeier Hansson d3fa62d4c2 Inline touch check thats only used once
Not worth loading an entire file for this.
2025-04-20 16:21:08 +02:00

34 lines
931 B
JavaScript

import { Controller } from "@hotwired/stimulus"
import { nextEventNamed } from "helpers/timing_helpers"
export default class extends Controller {
// Only load for touch devices
static get shouldLoad() {
return "ontouchstart" in window && navigator.maxTouchPoints > 0
}
// Use a fake input to trigger the soft keyboard on actions that load async content
// See https://gist.github.com/cathyxz/73739c1bdea7d7011abb236541dc9aaa
async open(event) {
const fakeInput = this.#focusOnFakeInput()
this.#removeOnFocusOut(fakeInput)
}
#focusOnFakeInput() {
const fakeInput = document.createElement("input")
fakeInput.setAttribute("type", "text")
fakeInput.setAttribute("class", "input--invisible")
this.element.appendChild(fakeInput)
fakeInput.focus()
return fakeInput
}
async #removeOnFocusOut(element) {
await nextEventNamed("focusout", element)
element.remove()
}
}