SP TextField Focus Issue

I have a dialog which contains an SP-Textfield

<div style="padding:15px;">
     <sp-label slot="label"> PX:</sp-label>
     <sp-textfield type="number" id="imageSize"></sp-textfield>
 </div>

On macOSX when the Dialog opens, the Textfield has focus with the cursor blinking and is sat waiting for some input.

On Windows11 the Textfield doesnt have focus until you click in it.

Is this just a Windows / mac thing ?

What gets focused by default can vary based on platform and host app.

If you want to be sure a specific control gets focused when your dialog shows up, you can leverage the load event for the dialog:

document.querySelector("#yourDialog").addEventListener("load", () => {
    // wait a frame for things to settle
    setTimeout(() => document.querySelector("#imageSize").focus(), 16);
});
1 Like

Using your example with the SetTimeout It now works on both Mac and Windows 11.

Thank You