feat(html): add aria-expanded helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
c240116c1d
commit
ec2ccc7653
3 changed files with 26 additions and 0 deletions
|
|
@ -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)`
|
||||
|
||||
|
|
|
|||
9
node.go
9
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 {
|
||||
|
|
|
|||
16
node_test.go
16
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue