-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolucide.go
142 lines (120 loc) · 2.72 KB
/
golucide.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package golucide
import (
"fmt"
"strings"
)
const svgTagTemplate string = `<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d" fill="%s" stroke="%s" stroke-width="%d" stroke-linecap="%s" stroke-linejoin="%s" class="%s" %s viewBox="0 0 24 24" >%s</svg>`
var DefaultIconAttributes = iconAttributes{
Width: 24,
Height: 24,
Fill: "none",
Stroke: "currentColor",
StrokeWidth: 2,
StrokeLineCap: "round",
StrokeLineJoin: "round",
Class: "lucide",
}
type iconAttributes struct {
Width int
Height int
Fill string
Stroke string
StrokeWidth int
StrokeLineCap string
StrokeLineJoin string
Class string
Extra []KeyValuePair
}
type KeyValuePair struct {
Key string
Value string
}
type iconOption func(*iconAttributes)
func Size(size int) iconOption {
return func(a *iconAttributes) {
a.Width = size
a.Height = size
}
}
func SizeHW(width, height int) iconOption {
return func(a *iconAttributes) {
a.Width = width
a.Height = height
}
}
func StrokeWidth(strokeWidth int) iconOption {
return func(a *iconAttributes) {
a.StrokeWidth = strokeWidth
}
}
func Fill(fill string) iconOption {
return func(a *iconAttributes) {
a.Fill = fill
}
}
func StrokeColor(stroke string) iconOption {
return func(a *iconAttributes) {
a.Stroke = stroke
}
}
func StrokeLineCap(lineCap string) iconOption {
return func(a *iconAttributes) {
a.StrokeLineCap = lineCap
}
}
func StrokeLineJoin(lineJoin string) iconOption {
return func(a *iconAttributes) {
a.StrokeLineJoin = lineJoin
}
}
func WithClasses(classes ...string) iconOption {
return func(a *iconAttributes) {
a.Class = fmt.Sprintf("%s %s", a.Class, strings.Join(classes, " "))
}
}
func WithID(id string) iconOption {
return WithAttribute("id", id)
}
func WithAttribute(name string, value string) iconOption {
return func(a *iconAttributes) {
a.Extra = append(a.Extra, KeyValuePair{
Key: name,
Value: value,
})
}
}
func GetIcon(iconName string, opts ...iconOption) string {
paths, exists := icons[iconName]
if !exists {
return ""
}
attrs := DefaultIconAttributes
for _, opt := range opts {
opt(&attrs)
}
var extraAttributes strings.Builder
if len(attrs.Extra) > 0 {
for _, pair := range attrs.Extra {
extraAttributes.WriteString(fmt.Sprintf(`%s="%s"`, pair.Key, pair.Value))
}
}
return fmt.Sprintf(svgTagTemplate,
attrs.Width,
attrs.Height,
attrs.Fill,
attrs.Stroke,
attrs.StrokeWidth,
attrs.StrokeLineCap,
attrs.StrokeLineJoin,
attrs.Class,
extraAttributes.String(),
paths,
)
}
func GetAvailableIconNames() []string {
names := []string{}
for name := range icons {
names = append(names, name)
}
return names
}