-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.go
129 lines (115 loc) · 2.29 KB
/
link.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
package carbon
import (
"io"
"slices"
)
type link struct {
attr []Attr
children any
size string
href string
inline bool
icon Component
disabled bool
visited bool
}
func Link(children ...any) *link {
return &link{
attr: nil,
children: children,
size: "",
href: "",
inline: false,
icon: nil,
disabled: false,
visited: false,
}
}
func (l *link) Attr(name string, value string) *link {
l.attr = append(l.attr, Attr{name, value})
return l
}
func (l *link) Size(size string) *link {
sizes := []string{"", "sm", "lg"}
if !slices.Contains(sizes, size) {
panic("invalid size")
}
l.size = size
return l
}
func (l *link) Href(href string) *link {
l.href = href
return l
}
func (l *link) Inline(inline bool) *link {
l.inline = inline
return l
}
func (l *link) Icon(icon Component) *link {
l.icon = icon
return l
}
func (l *link) Disabled(disabled bool) *link {
l.disabled = disabled
return l
}
func (l *link) Visited(visited bool) *link {
l.visited = visited
return l
}
func (l *link) Render(w io.Writer) {
if l.disabled {
w.Write([]byte(`<p class="bx--link bx--link--disabled`))
if l.inline {
w.Write([]byte(` bx--link--inline`))
}
if l.visited {
w.Write([]byte(` bx--link--visited`))
}
w.Write([]byte(` `))
io.WriteString(w, getAttr(l.attr, "class"))
w.Write([]byte(`"`))
renderAttrsWithoutClass(w, l.attr)
w.Write([]byte(`>`))
{
renderAny(w, l.children)
if !l.inline && l.icon != nil {
w.Write([]byte(`<div class="bx--link__icon">`))
l.icon.Render(w)
w.Write([]byte(`</div>`))
}
}
w.Write([]byte(`</p>`))
return
}
w.Write([]byte(`<a class="bx--link`))
if l.inline {
w.Write([]byte(` bx--link--inline`))
}
if l.visited {
w.Write([]byte(` bx--link--visited`))
}
if l.size != "" {
w.Write([]byte(` bx--link--`))
io.WriteString(w, l.size)
}
w.Write([]byte(` `))
io.WriteString(w, getAttr(l.attr, "class"))
w.Write([]byte(`"`))
if l.href != "" {
w.Write([]byte(` href="`))
io.WriteString(w, l.href)
w.Write([]byte(`"`))
}
renderAttrsWithoutClass(w, l.attr)
w.Write([]byte(`>`))
{
renderAny(w, l.children)
if !l.inline && l.icon != nil {
w.Write([]byte(`<div class="bx--link__icon">`))
l.icon.Render(w)
w.Write([]byte(`</div>`))
}
}
w.Write([]byte(`</a>`))
}