Every fluent method on *Icon returns a new instance with deep-copied attributes, so the original is never modified and the same base icon can safely produce multiple variants.
Immutability
Branching from the same base icon produces independent copies:
import swarmicons "github.com/frostybee/go-swarm-icons"base := swarmicons.New("<path/>", nil)red := base.Fill("red")blue := base.Fill("blue")// red has fill="red", blue has fill="blue", base has no fill.Fluent methods
All methods return a new *Icon. Chain them freely.
| Method | Signature | Effect |
|---|---|---|
Size |
Size(size int) *Icon |
Sets both width and height to the same value. |
Width |
Width(w string) *Icon |
Sets width, derives height from viewBox aspect ratio. Supports CSS units and special values. |
Height |
Height(h string) *Icon |
Sets height, derives width from viewBox aspect ratio. Same special values as Width. |
Class |
Class(classes ...string) *Icon |
Appends CSS classes to any existing class attribute. Does not replace. |
Fill |
Fill(fill string) *Icon |
Sets the fill attribute. |
Stroke |
Stroke(stroke string) *Icon |
Sets the stroke attribute. |
StrokeWidth |
StrokeWidth(w string) *Icon |
Sets the stroke-width attribute. |
Opacity |
Opacity(o float64) *Icon |
Sets the opacity attribute. |
Rotate |
Rotate(degrees float64) *Icon |
Appends a CSS rotate(Xdeg) to the style transform. |
Flip |
Flip(direction string) *Icon |
Appends a CSS scale transform. Accepts "h", "v", "horizontal", "vertical", or "both". |
Title |
Title(title string) *Icon |
Prepends a <title> element (HTML-escaped) for screen-reader accessibility. |
Attr |
Attr(attrs map[string]string) *Icon |
Merges arbitrary attributes. Empty-string values are ignored. |
Chaining
import ( "fmt" swarmicons "github.com/frostybee/go-swarm-icons" "github.com/frostybee/go-swarm-icons/lucide")func main() { manager := swarmicons.Default("lucide", lucide.Provider()) icon, _ := manager.Get("star") html := icon.Size(24).Class("text-blue").Fill("currentColor").ToHTML() fmt.Println(html)}Sizing
Size (square)
Size(32) sets both width and height to "32":
icon.Size(32).ToHTML()// width="32" height="32"Width and Height (aspect-ratio aware)
Width and Height derive the other dimension from the icon's viewBox aspect ratio. They support CSS units:
// viewBox is "0 0 20 30" (aspect ratio 2:3)icon.Width("32") // width="32" height="48"icon.Width("1em") // width="1em" height="1.5em"icon.Height("48") // width="32" height="48"Special values:
| Value | Effect |
|---|---|
"auto" |
Resolves to the viewBox width and height. |
"unset", "none", "undefined" |
Removes both width and height attributes. |
icon.Width("auto") // width and height set to viewBox dimensionsicon.Width("unset") // both width and height removedRotation and flipping
Rotate and Flip both append CSS transform functions to the style attribute. Chaining them produces a single transform declaration:
icon.Rotate(90).Flip("h")// style="transform: rotate(90deg) scaleX(-1)"The order matters. Rotate(90).Flip("h") applies rotation first, then the flip. Reversing the call order reverses the transform order:
icon.Flip("v").Rotate(45)// style="transform: scaleY(-1) rotate(45deg)"Existing style declarations are preserved. The transform is appended to the existing style string:
icon.Attr(map[string]string{"style": "color: red"}).Rotate(90)// style="color: red; transform: rotate(90deg)"Flip directions
| Direction | CSS transform |
|---|---|
"h" or "horizontal" |
scaleX(-1) |
"v" or "vertical" |
scaleY(-1) |
"both" |
scale(-1, -1) |
| Any other value | Defaults to scaleX(-1) |
Accessibility with Title
Title prepends a <title> element to the SVG content. The value is HTML-escaped to prevent injection:
icon.Title("Home")// <svg ...><title>Home</title><path .../></svg>Setting a title affects ARIA behavior during attribute rendering: icons with a title that receive an aria-label attribute get role="img" instead of aria-hidden="true". See Attribute Management for the full ARIA injection rules.
Arbitrary attributes with Attr
Attr merges a map of key-value pairs into the icon's attributes. Empty-string values are skipped:
icon.Attr(map[string]string{ "data-icon": "home", "tabindex": "0", "fill": "", // ignored, does not overwrite existing fill})Output methods
| Method | Returns | Description |
|---|---|---|
ToHTML() |
string |
Complete <svg> element with attributes and inner content. |
String() |
string |
Same as ToHTML(). Implements fmt.Stringer. |
Content() |
string |
SVG inner content without the <svg> wrapper. |
Attributes() |
map[string]string |
Deep copy of the attribute map. |
IsEmpty() |
bool |
true if the icon has no inner content. |
ViewBox() |
(minX, minY, w, h int) |
Parses the viewBox attribute. Falls back to width/height. Returns 0,0,0,0 if neither is set. |
ViewBoxSize() |
(w, h int) |
Width and height from the viewBox. |
Attribute names are validated against the regex ^[a-zA-Z_:][\w:.\-]*$. Invalid names are silently dropped from ToHTML() output. Attribute values are always HTML-escaped.
See also
- Icon API reference: all constructors, fluent methods, and output methods
- Attribute Management: how attributes merge across five layers when the manager resolves an icon