Documentation
¶
Overview ¶
Package components provides high-level components and helpers that are composed of low-level elements and attributes.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinAttrs ¶ added in v1.2.0
JoinAttrs joins attributes with the given name on the first level of the given nodes. Attributes on non-direct descendants are ignored. Non-empty attribute values are joined by spaces into a single attribute. Empty, whitespace-only, and boolean (valueless) attributes are deduplicated and discarded if a non-empty value exists. If only boolean/empty attributes match, a single boolean attribute is emitted. When both boolean and valued attributes match, the valued form takes precedence. Note that this renders all first-level attributes to check whether they should be processed.
Example ¶
package main
import (
"os"
g "maragu.dev/gomponents"
. "maragu.dev/gomponents/components"
. "maragu.dev/gomponents/html"
)
// myButton is a reusable button component that accepts children and adds a "button" class.
// JoinAttrs merges all class attributes from children with the component's own class.
func myButton(children ...g.Node) g.Node {
return Div(JoinAttrs("class", g.Group(children), Class("button")))
}
// myPrimaryButton builds on myButton, adding a "primary" class.
// The classes are merged: "primary" from here and "button" from myButton.
func myPrimaryButton(text string) g.Node {
return myButton(Class("primary"), g.Text(text))
}
func main() {
danceButton := myPrimaryButton("Dance")
_ = danceButton.Render(os.Stdout)
}
Output: <div class="primary button">Dance</div>
Types ¶
type Classes ¶
Classes is a map of strings to booleans, which Renders to an attribute with name "class". The attribute value is a sorted, space-separated string of all the map keys, for which the corresponding map value is true.
Example ¶
e := g.El("div", Classes{"party-hat": true, "boring-hat": false})
_ = e.Render(os.Stdout)
Output: <div class="party-hat"></div>