diff --git a/docs/architecture.md b/docs/architecture.md index edea62b..25f5c38 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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)` diff --git a/node.go b/node.go index 31a9047..f1e606f 100644 --- a/node.go +++ b/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 { diff --git a/node_test.go b/node_test.go index df28f39..d4f2f84 100644 --- a/node_test.go +++ b/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 := `
content
` + if got != want { + t.Errorf("Class() = %q, want %q", got, want) + } +} + func TestAriaHiddenHelper(t *testing.T) { ctx := NewContext()