Skip to main content
go-swarm-icons
On this page

Icon

Details how to build an Icon and chain size, color, rotation, and flip transforms.

The Icon type, its three constructors, all fluent transform methods, and the output methods that produce HTML.

Overview

The Icon type represents a parsed SVG icon with inner content and attributes. Every fluent method returns a new *Icon; the original is never modified.

For a usage guide covering chaining, transforms, and sizing, see Icon Manipulation.

Types

Icon

Go
type Icon struct {
// unexported fields
}

Fields (unexported):

  • content string: SVG inner markup (without the <svg> wrapper).
  • attributes map[string]string: SVG element attributes.

Constructors

New

Go
func New(content string, attrs map[string]string) *Icon

Creates an Icon from raw SVG inner content and an attribute map. The attribute map is deep-copied; the caller's map is not retained. Passing nil for attrs produces an icon with an empty attribute map.

FromFile

Go
func FromFile(path string) (*Icon, error)

Reads the SVG file at path, parses and sanitizes it, and returns the resulting Icon. Returns an error wrapping ErrProviderError on I/O failure or ErrInvalidSVG if the content has no <svg> element.

FromString

Go
func FromString(svg string) (*Icon, error)

Parses an SVG string and returns the resulting Icon. Returns ErrInvalidSVG if the input is empty or contains no <svg> element. The inner content is sanitized automatically. When the input contains multiple <svg> elements, only the first is parsed.

Fluent methods

All methods return a new *Icon with deep-copied attributes.

Attr

Go
func (ic *Icon) Attr(attrs map[string]string) *Icon

Merges the given attributes into the existing set. Empty-string values are ignored. For CSS classes, prefer Class (which appends) over Attr with a "class" key (which overwrites).

Class

Go
func (ic *Icon) Class(classes ...string) *Icon

Appends the given CSS classes to any existing class attribute, space-separated. Does not replace existing classes.

Size

Go
func (ic *Icon) Size(size int) *Icon

Sets both width and height to size.

Width

Go
func (ic *Icon) Width(w string) *Icon

Sets width to w and derives height from the viewBox aspect ratio. Supports CSS units ("24", "1.5em", "2rem"). Special values: "auto" resolves to the viewBox width/height; "unset", "none", or "undefined" remove both width and height attributes.

Height

Go
func (ic *Icon) Height(h string) *Icon

Sets height to h and derives width from the viewBox aspect ratio. Same CSS unit support and special values as Width.

Fill

Go
func (ic *Icon) Fill(fill string) *Icon

Sets the fill attribute.

Stroke

Go
func (ic *Icon) Stroke(stroke string) *Icon

Sets the stroke attribute.

StrokeWidth

Go
func (ic *Icon) StrokeWidth(w string) *Icon

Sets the stroke-width attribute.

Opacity

Go
func (ic *Icon) Opacity(o float64) *Icon

Sets the opacity attribute. Formatted with %g (e.g., 0.5 becomes "0.5", 1.0 becomes "1").

Rotate

Go
func (ic *Icon) Rotate(degrees float64) *Icon

Appends a CSS rotate(Xdeg) function to the style attribute's transform declaration. If a transform already exists in the style, the function is appended to it. Otherwise a new transform declaration is added.

Flip

Go
func (ic *Icon) Flip(direction string) *Icon

Appends a CSS scale transform to the style attribute. Accepted values:

Direction CSS function
"h" or "horizontal" scaleX(-1)
"v" or "vertical" scaleY(-1)
"both" scale(-1, -1)
Any other value scaleX(-1) (default)

Chaining Rotate and Flip produces a single transform declaration.

Title

Go
func (ic *Icon) Title(title string) *Icon

Prepends a <title> element to the SVG content. The title text is HTML-escaped via html.EscapeString.

Output methods

ToHTML

Go
func (ic *Icon) ToHTML() string

Renders the icon as a complete <svg> element string. Attributes are sorted alphabetically for deterministic output. Attribute names are validated against ^[a-zA-Z_:][\w:.\-]*$; invalid names are silently dropped. Attribute values are HTML-escaped.

String

Go
func (ic *Icon) String() string

Implements fmt.Stringer. Returns the same value as ToHTML.

Content

Go
func (ic *Icon) Content() string

Returns the SVG inner content without the <svg> wrapper.

Attributes

Go
func (ic *Icon) Attributes() map[string]string

Returns a deep copy of the icon's attribute map. Modifying the returned map does not affect the icon.

IsEmpty

Go
func (ic *Icon) IsEmpty() bool

Reports whether the icon has no inner content (empty string).

ViewBox

Go
func (ic *Icon) ViewBox() (minX, minY, w, h int)

Parses the viewBox attribute and returns its four components. Falls back to width/height attributes (with minX=0, minY=0) if viewBox is absent. Returns 0, 0, 0, 0 when neither is available.

ViewBoxSize

Go
func (ic *Icon) ViewBoxSize() (w, h int)

Returns the width and height from the viewBox. Convenience wrapper around ViewBox().

See also

Edit this page

Last updated: