feat(html): add class helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
a925142e4e
commit
a5e02f6472
3 changed files with 18 additions and 0 deletions
|
|
@ -42,6 +42,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
|
|||
- `Lang(node, locale)`
|
||||
- `Dir(node, direction)`
|
||||
- `Alt(node, text)`
|
||||
- `Class(node, classes...)`
|
||||
- `AriaHidden(node, hidden)`
|
||||
- `AriaExpanded(node, expanded)`
|
||||
- `TabIndex(node, index)`
|
||||
|
|
|
|||
7
node.go
7
node.go
|
|
@ -228,6 +228,13 @@ func Alt(n Node, text string) Node {
|
|||
return Attr(n, "alt", text)
|
||||
}
|
||||
|
||||
// node.go: Class sets the class attribute on an element node.
|
||||
// Example: Class(El("div"), "card", "card--primary").
|
||||
// Multiple class tokens are joined with spaces.
|
||||
func Class(n Node, classes ...string) Node {
|
||||
return Attr(n, "class", strings.Join(classes, " "))
|
||||
}
|
||||
|
||||
// node.go: AriaHidden sets the aria-hidden attribute on an element node.
|
||||
// Example: AriaHidden(El("svg"), true).
|
||||
func AriaHidden(n Node, hidden bool) Node {
|
||||
|
|
|
|||
10
node_test.go
10
node_test.go
|
|
@ -324,6 +324,16 @@ func TestAltHelper(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClassHelper(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := Class(El("div", Raw("content")), "card", "card--primary")
|
||||
got := node.Render(ctx)
|
||||
want := `<div class="card card--primary">content</div>`
|
||||
if got != want {
|
||||
t.Errorf("Class() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAriaHiddenHelper(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue