Focus
Give an input browser focus from the server, with no JavaScript of your own.
Overview
The ability to give focus to an element in the browser from the server-side. Add hlivekit.Focus() to any element and, the next time the diff applies, the browser calls element.focus() on it. Nothing removes the attribute automatically, so if you don't want the element re-focused on every future render, remove it yourself once it's done its job — typically from a focus event bound with l.OnOnce.
API
hlivekit.Focus() l.Attributer— add to an element to focus it on the next diff.hlivekit.FocusRemove(tag l.Adder)— remove the focus attribute so it doesn't refocus on a later render.
Use case
A to-do list row that flips into an inline edit form when you click an edit button — the text input should be focused the instant it becomes visible, so the user can start typing immediately instead of clicking into it themselves.
Code sample
Adapted from the To Do List example:
Button("Edit", l.On("click", func(ctx context.Context, _ l.Event) {
labelForm.Add(l.StyleOff{"display"})
labelInput.Add(hlivekit.Focus(), l.OnOnce("focus", func(ctx context.Context, _ l.Event) {
// Don't refocus this input on every future render.
hlivekit.FocusRemove(labelInput)
l.Render(ctx)
}))
}))