import { Controller } from '@hotwired/stimulus' // Moves focus into an element as soon as it lands in the DOM: only rendered // on markup that arrives through a Turbo Stream (the review form's response), // never on a normal page load. Focuses the first invalid field if there is // one (or the first focusable thing inside it, e.g. the rating stars), // otherwise the element itself (give it tabindex="-1"). export default class extends Controller { connect() { const invalid = this.element.querySelector('[aria-invalid="true"]') const target = invalid?.matches(FOCUSABLE) ? invalid : invalid?.querySelector(FOCUSABLE) ;(target ?? this.element).focus() } } const FOCUSABLE = 'input:not([type="hidden"]), textarea, select, button, [tabindex]'