Gaiety's visual representation as a fursona (an original furry fandom character) depicted as an animated gif falling downward with a controlled hand outstretched. Faer hair and tail waves in the wind while their floppy dog ears flop about as well. Fae are an anthromorphic canine like a german shepherd clothed like a human in a t-shirt and denim shorts. Art is by Lynte.

Form Autofocusing in Posts

Autofocusing to Form Fields - Common Use Cases

Controlling form focusing is always a "it depends" kind of situation. However, we can often times make intelligent decisions rather than leave the user hanging.

Imagine a common scenario. I have a pastry shop app and I click "New Donut". A form pops up with inputs for attributes such as the Nickname, Toppings, and Dough type for this donut recipe. I begin typing... oops! I have to tab through every interactable element on the page until I find this newly appeared form. Eep!

In such a scenario it makes sense to write reusable components for our form elements. Consider the following:

<button>New Donut</button>

{{#if this.creatingNewDonut}}
  <UiForm
    @shouldAutofocus={{true}}
    @submit={{this.submitHandler}}>
    <UiLabel>
      Nickname
      <UiInput />
    </UiLabel>
    <UiLabel>
      Toppings
      <UiInput />
    </UiLabel>
    <UiLabel>
      Dough Type
      <UiInput />
    </UiLabel>
  </UiForm>
{{/if}}
html

The above code example is written in htmlbars (handlebars for Ember) but it's a generalized example that could apply to any framework.

Now in our UiForm component we can write some code into your framework's render hook like so:

let focusableQuerySuffix = ':not([readonly]):not([disabled]):not([tabindex="-1"])';
let focusableQueries = [
  `input${focusableQuerySuffix}`,
  `textarea${focusableQuerySuffix}`,
  `select${focusableQuerySuffix}`,
];
let element = this.element.querySelector(focusableQueries.join(',')); // this.element will vary for your framework of choice, replace it with however you target your component's dom element
if (!element) return;

element.focus();
js

Focusing an element is the easy part, simply call .focus() on any dom element. Finding which dom element to query is the tricky part. Generally avoiding anything with a readonly, disabled or tabindex=-1 attribute is safe so long as you follow html semantics carefull in your application.

This could be expanded to support radio buttons, button elements and other interactable dom elements should you so choose.

Focus responsibly!

ava@wroten.me

Wish to contact me about meetup or conference talks, work opportunities or otherwise? Reach out to me via email.

Resume

Work history, skills, volunteering and talks given all in a one page PDF.

Blog

Programming, art, design, and other queer things.

Git

Coding projects and such on a self hosted Forgejo instance.