D3vugu-components/utils/attribute-map.go
David Vogel e9c2353da3 Add utils package
- Add AttributesAppend type
- Add icon class and add styles to icons
- Change elements to use AttributesAppend in vg-attr
- Move example icons into their own component
2023-05-15 08:58:12 +02:00

41 lines
982 B
Go

package utils
import "github.com/vugu/vugu"
// AttributesAppend can be used to dynamically add entries to AttrMap attributes.
// This way you can add a class to a component that will not be overwritten by attributes set by the parent.
type AttributesAppend struct {
AttrMap vugu.AttrMap
Classes string
}
// AttributeList returns an attribute list corresponding to this map using the rules from VGNode.AddAttrInterface.
func (a AttributesAppend) AttributeList() (ret []vugu.VGAttribute) {
combinedClasses := a.Classes
if a.AttrMap != nil {
if attrMapClassInt, ok := a.AttrMap["class"]; ok {
if attrMapClass, ok := attrMapClassInt.(string); ok {
if combinedClasses != "" {
combinedClasses += " "
}
combinedClasses += attrMapClass
}
}
}
var n vugu.VGNode
if combinedClasses != "" {
n.AddAttrInterface("class", combinedClasses)
}
for k := range a.AttrMap {
if k != "class" {
n.AddAttrInterface(k, a.AttrMap[k])
}
}
return n.Attr
}