# \`autofocus\` on Windows

**URL:** <https://forums.creativeclouddeveloper.com/t/autofocus-on-windows/1535>\
**Category:** UXP Plugin API\
**Tags:** api-feedback\
**Created:** [January 5, 2020, 5:00pm UTC](https://forums.creativeclouddeveloper.com/t/autofocus-on-windows/1535 "2020-01-05T17:00:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pklaschka](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pklaschka/32/3162_2.png) [@pklaschka](https://forums.creativeclouddeveloper.com/u/pklaschka)\
**Post date:** [January 5, 2020, 5:00pm UTC](https://forums.creativeclouddeveloper.com/t/autofocus-on-windows/1535/1 "2020-01-05T17:00:35Z")

</div>

### Issue

On Windows, there appears to be some sort of racing condition sometimes stealing focus from elements with the attribute `autofocus="autofocus"`. This leads to the element inconsistently being selected or not when running the plugin. This **does not** occur on macOS.

### Steps to reproduce

- run the code listed as [Listing 1], using the `xd-dialog-helper`, which is installable with `npm install xd-dialog-helper`
- run the plugin in Adobe XD on Windows multiple times (inconsistency, sometimes it works, sometimes it doesn’t).

### Expected Behavior

On Windows, the button with the attribute (in the example, `okButton`), should consistently have focus when the dialog opens. It should therefore match the behavior on macOS, or, since @kerrishotts already confirmed it is a bug (see **Additional Information** below), the bug should be fixed 😉

### Actual Behavior

The button sometimes has focus and sometimes, it doesn’t when the dialog gets opened on Windows.

### Additional information

I had already discussed this (a few months ago, didn’t get to writing the bug report until now 😆) with @kerrishotts via Slack, who had the idea that it could be a racing condition and confirmed:

> So on my VM, it looks like it’s intermittent – sometimes it focuses the button, and sometimes it focuses the select – no rhyme or reason. Feels like a race condition on the autofocus handling.  
> […]  
> It’s a bug. macOS doesn’t have this behavior, and we didn’t intentionally change the focusing behavior for dialogs. So feel free to raise on the forums and we can go from there.  
> – (@kerrishotts in a DM on Slack)

### Known Workarounds

Replace `okButton.setAttribute('autofocus', 'autofocus');` with

```js
setTimeout(() => {
  okButton.focus();
}, 200);

```

### Listings

#### [Listing 01]

```js
const dialogHelper = require('xd-dialog-helper');

await dialogHelper.showDialog('my-dialog', 'Dialog', [
    { id: 'text', type: dialogHelper.types.TEXT_INPUT, label: 'Some input field' }
  ], {
  onBeforeShow: () => {
    const okButton = document.getElementById('lorem-main-dialogHelperBtnOk');
    okButton.setAttribute('autofocus', 'autofocus');
  }
});

```
