The StyleManager provides a structured way to handle complex CSS features like pseudo-classes (e.g., :hover), animations, and media queries directly in Go. It automatically handles class name generation and style deduplication.
To use StyleManager:
- Initialize it with
styles.NewStyleManager(). - Define a
styles.CompositeStyle containing Default props and a map of PseudoClasses. - Apply the generated class name to an element.
- Crucially, render the element using
RenderWithOptions and pass the StyleManager instance in the elem.RenderOptions.
// Initialize StyleManager
styleMgr := styles.NewStyleManager()
// Define styles with a hover effect
buttonClass := styleMgr.AddCompositeStyle(styles.CompositeStyle{
Default: styles.Props{
styles.BackgroundColor: "green",
styles.Color: "white",
styles.Padding: "10px 20px",
styles.Border: "none",
styles.Cursor: "pointer",
},
PseudoClasses: map[string]styles.Props{
styles.PseudoHover: {
styles.BackgroundColor: "darkgreen",
},
},
})
// Create a button and apply the generated class name
button := elem.Button(
attrs.Props{attrs.Class: buttonClass},
elem.Text("Hover Over Me"),
)
// Use RenderWithOptions to apply the style definitions effectively
htmlOutput := button.RenderWithOptions(elem.RenderOptions{StyleManager: styleMgr})