diff --git a/docs/architecture.md b/docs/architecture.md index 40bee78..201b430 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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)` diff --git a/node.go b/node.go index 99c2d10..64534bc 100644 --- a/node.go +++ b/node.go @@ -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 { diff --git a/node_test.go b/node_test.go index 19b90a4..3d2296c 100644 --- a/node_test.go +++ b/node_test.go @@ -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)