diff --git a/docs/architecture.md b/docs/architecture.md index 05472c7..5e92c37 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -39,6 +39,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns: - `Alt(node, text)` - `AriaHidden(node, hidden)` - `TabIndex(node, index)` +- `AutoFocus(node)` ### Safety Guarantees diff --git a/node.go b/node.go index b90bcaf..e193f42 100644 --- a/node.go +++ b/node.go @@ -192,6 +192,11 @@ func TabIndex(n Node, index int) Node { return Attr(n, "tabindex", strconv.Itoa(index)) } +// AutoFocus sets the autofocus attribute on an element node. +func AutoFocus(n Node) Node { + return Attr(n, "autofocus", "autofocus") +} + func (n *elNode) Render(ctx *Context) string { if n == nil { return "" diff --git a/node_test.go b/node_test.go index 4b73b3e..f82cb9c 100644 --- a/node_test.go +++ b/node_test.go @@ -286,6 +286,15 @@ func TestTabIndexHelper(t *testing.T) { } } +func TestAutoFocusHelper(t *testing.T) { + ctx := NewContext() + node := AutoFocus(El("input")) + got := node.Render(ctx) + if !strings.Contains(got, `autofocus="autofocus"`) { + t.Errorf("AutoFocus() = %q, want autofocus=\"autofocus\"", got) + } +} + func TestElNode_MultipleAttrs(t *testing.T) { ctx := NewContext() node := Attr(Attr(El("a", Raw("link")), "href", "/home"), "class", "nav")