Bind ref in SearchBox.html and call Focus() in OnMounted.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
SearchBox.html:
<input id="search" aria-label="Search" ref="{Search}" />
The example is the full program below: unzip it and dotnet run.
OneUI generates the code that binds the reference.
The full program
Pair SearchBox.html with SearchBox.cs and SearchPanel.html with SearchPanel.cs: each template and partial class share a name and folder. Clicking Search sets IsOpen, so OneUI mounts SearchBox and focuses its input. The console sample does this in a ReferenceDocument without opening a window.
SearchBox.cs:
using OneUI.Components; public partial class SearchBox { ElementRef Search { get; } = new(); protected override void OnMounted() => Search.Focus(); }
SearchPanel.html:
<button id="open" on:click="Open">Search</button> <template if="{IsOpen}"> <SearchBox /> </template>
SearchPanel.cs:
using OneUI.Components; public partial class SearchPanel { [State] public partial bool IsOpen { get; set; } void Open() => IsOpen = true; }
Program.cs:
using OneUI.Components; using OneUI.Components.Reference; var doc = new ReferenceDocument(); var ui = new UiContext(doc); doc.Context = ui; ui.Mount(new SearchPanel()); Console.WriteLine($"Before: {FocusedId()}"); doc.Click("#open"); Console.WriteLine($"After: {FocusedId()}"); string FocusedId() => doc.ActiveElement?.GetAttribute("id") ?? "none";
dotnet run prints:
Before: none After: search
Calling Focus() before OneUI binds the reference throws InvalidOperationException. A bound input that’s disabled or hidden won’t take focus.
Variations
For focus as soon as the panel opens, replace the state declaration in SearchPanel.cs with the code below. The search box now mounts with its parent and the Before line already reports search.
[State] public partial bool IsOpen { get; set; } public SearchPanel() => IsOpen = true;
Related recipes
The short answer
Put ref="{Search}" on the input and declare an ElementRef named Search on its partial class. Call Search.Focus() in OnMounted to focus a newly added input. If a button reveals the input later, put the input in a child component and mount that child conditionally. Its callback then runs when it appears, not when the whole screen first opened.
How do you focus an input on a button click after it’s added?
When the button handler sets IsOpen, OneUI doesn’t immediately insert the input in the middle of the handler. Event dispatch batches state changes, and the document applies the resulting changes together. Trying to focus the not-yet-created input immediately after assigning the flag can fail. Use the new child’s OnMounted callback, which runs after the commit that inserts its nodes. OnMounted isn’t a callback for every update: a parent that’s already mounted doesn’t get another call when its child appears.
Why doesn’t the input get keyboard focus?
Distinguish an unbound reference from a bound reference that can’t take focus. An unbound reference throws: check that the ref is on the input itself and that the conditional branch currently creates it. A focus request on a disabled, hidden or detached element has no effect. If the input already exists and is focusable, you can call Focus() in a handler without remounting the component. Use ActiveElement in the reference document to see which element received focus. See the focus lifecycle for what counts as focusable and which events the document sends back.