feat(html): add aria-expanded helper
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 18:15:48 +00:00
parent c240116c1d
commit ec2ccc7653
3 changed files with 26 additions and 0 deletions

View file

@ -42,6 +42,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
- `Dir(node, direction)`
- `Alt(node, text)`
- `AriaHidden(node, hidden)`
- `AriaExpanded(node, expanded)`
- `TabIndex(node, index)`
- `AutoFocus(node)`

View file

@ -230,6 +230,15 @@ func AriaHidden(n Node, hidden bool) Node {
return n
}
// node.go: AriaExpanded sets the aria-expanded attribute on an element node.
// Example: AriaExpanded(El("button"), true).
func AriaExpanded(n Node, expanded bool) Node {
if expanded {
return Attr(n, "aria-expanded", "true")
}
return Attr(n, "aria-expanded", "false")
}
// node.go: TabIndex sets the tabindex attribute on an element node.
// Example: TabIndex(El("button"), 0).
func TabIndex(n Node, index int) Node {

View file

@ -330,6 +330,22 @@ func TestAriaHiddenHelper(t *testing.T) {
}
}
func TestAriaExpandedHelper(t *testing.T) {
ctx := NewContext()
expanded := AriaExpanded(El("button", Raw("menu")), true)
gotExpanded := expanded.Render(ctx)
if !strings.Contains(gotExpanded, `aria-expanded="true"`) {
t.Errorf("AriaExpanded(true) = %q, want aria-expanded=\"true\"", gotExpanded)
}
collapsed := AriaExpanded(El("button", Raw("menu")), false)
gotCollapsed := collapsed.Render(ctx)
if !strings.Contains(gotCollapsed, `aria-expanded="false"`) {
t.Errorf("AriaExpanded(false) = %q, want aria-expanded=\"false\"", gotCollapsed)
}
}
func TestTabIndexHelper(t *testing.T) {
ctx := NewContext()
node := TabIndex(El("button", Raw("action")), -1)