-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.go
233 lines (199 loc) · 5.45 KB
/
button.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package carbon
import (
"fmt"
"io"
"github.com/valyala/bytebufferpool"
"golang.org/x/exp/slices"
)
type button struct {
attrs []Attr
children any
kind string
size string
expressive bool
isSelected bool
icon *iconComponent
iconDescription string
tooltipAlignment string
tooltipPosition string
disabled bool
href string
type_ string
}
var _ Component = (*button)(nil)
func Button(children ...any) *button {
return &button{
children: children,
attrs: nil,
kind: "primary",
size: "",
expressive: false,
isSelected: false,
icon: nil,
iconDescription: "",
tooltipAlignment: "center",
tooltipPosition: "bottom",
disabled: false,
href: "",
type_: "button",
}
}
func (b *button) Attr(name string, value string) *button {
b.attrs = append(b.attrs, Attr{name, value})
return b
}
func (b *button) Kind(kind string) *button {
var kinds = []string{"primary", "secondary", "tertiary", "ghost", "danger", "danger-tertiary", "danger-ghost"}
if slices.Contains(kinds, kind) == false {
panic("invalid kind")
}
b.kind = kind
return b
}
func (b *button) Size(size string) *button {
var sizes = []string{"", "field", "sm", "lg", "xl"}
if slices.Contains(sizes, size) == false {
panic("invalid size, options are: " + fmt.Sprintf("%v", sizes))
}
b.size = size
return b
}
func (b *button) Expressive(v bool) *button {
b.expressive = v
return b
}
func (b *button) Selected(v bool) *button {
b.isSelected = v
return b
}
func (b *button) Icon(icon *iconComponent) *button {
b.icon = icon
return b
}
func (b *button) IconDescription(iconDescription string) *button {
b.iconDescription = iconDescription
return b
}
func (b *button) TooltipAlignment(tooltipAlignment string) *button {
b.tooltipAlignment = tooltipAlignment
return b
}
func (b *button) TooltipPosition(tooltipPosition string) *button {
b.tooltipPosition = tooltipPosition
return b
}
func (b *button) Disabled(v bool) *button {
b.disabled = v
return b
}
func (b *button) Href(href string) *button {
b.href = href
return b
}
func (b *button) Type(type_ string) *button {
b.type_ = type_
return b
}
func (b *button) Render(w io.Writer) {
hasIconOnly := b.icon != nil && b.children == nil
if hasIconOnly && b.iconDescription == "" {
panic("iconDescription is required when icon is specified")
}
ariaPressed := ternary(hasIconOnly && b.kind == "ghost" && b.href == "", b.isSelected, false)
renderIconInButton := renderIconInButtonClosure(b.icon, b.iconDescription)
class := bytebufferpool.Get()
defer bytebufferpool.Put(class)
class.WriteString(`bx--btn`)
if b.expressive {
class.WriteString(` bx--btn--expressive`)
}
if b.size == "sm" && !b.expressive {
class.WriteString(` bx--btn--sm`)
}
if !(b.size == "field" && !b.expressive) && b.size == "md" && !b.expressive {
class.WriteString(` bx--btn--md`)
}
if b.size == "field" {
class.WriteString(` bx--btn--field`)
}
if b.size == "sm" {
class.WriteString(` bx--btn--sm`)
}
if b.size == "lg" {
class.WriteString(` bx--btn--lg`)
}
if b.size == "xl" {
class.WriteString(` bx--btn--xl`)
}
class.WriteString(` bx--btn--`)
class.WriteString(b.kind)
if b.disabled {
class.WriteString(` bx--btn--disabled`)
}
if hasIconOnly {
class.WriteString(` bx--btn--icon-only`)
class.WriteString(` bx--tooltip__trigger`)
class.WriteString(` bx--tooltip--a11y`)
}
if hasIconOnly && b.tooltipPosition != "" {
class.WriteString(` bx--btn--icon-only--`)
class.WriteString(b.tooltipPosition)
}
if hasIconOnly && b.tooltipAlignment != "" {
class.WriteString(` bx--tooltip--align-`)
class.WriteString(b.tooltipAlignment)
}
if hasIconOnly && b.isSelected && b.kind == "ghost" {
class.WriteString(` bx--btn--selected`)
}
attrs := append(b.attrs, Attr{"class", class.String()}, Attr{"aria-pressed", ternary(ariaPressed, "true", "false")})
if b.href != "" && b.disabled == false && hasIconOnly {
w.Write([]byte("<a"))
renderAttrs(w, attrs)
w.Write([]byte(" href=\""))
w.Write([]byte(b.href))
w.Write([]byte("\">"))
w.Write([]byte("<span class=\"bx--assistive-text\">"))
io.WriteString(w, b.iconDescription)
w.Write([]byte("</span>"))
b.icon.Attr("class", "bx--btn__icon").Attr("aria-hidden", "true").Attr("aria-label", b.iconDescription).Render(w)
w.Write([]byte("</a>"))
return
}
if b.href != "" && b.disabled == false {
w.Write([]byte("<a"))
renderAttrs(w, attrs)
w.Write([]byte(" href=\""))
w.Write([]byte(b.href))
w.Write([]byte("\">"))
renderAny(w, b.children)
renderIconInButton(w)
w.Write([]byte("</a>"))
return
}
if hasIconOnly {
w.Write([]byte("<button"))
renderAttrs(w, attrs)
w.Write([]byte(">"))
w.Write([]byte("<span class=\"bx--assistive-text\">"))
io.WriteString(w, b.iconDescription)
w.Write([]byte("</span>"))
b.icon.Attr("class", "bx--btn__icon").Attr("aria-hidden", "true").Attr("aria-label", b.iconDescription).Render(w)
w.Write([]byte("</button>"))
return
}
w.Write([]byte("<button"))
renderAttrs(w, attrs)
w.Write([]byte(">"))
renderAny(w, b.children)
renderIconInButton(w)
w.Write([]byte("</button>"))
}
func renderIconInButtonClosure(icon *iconComponent, description string) func(w io.Writer) {
if icon == nil {
return func(w io.Writer) {}
}
return func(w io.Writer) {
icon.Attr("class", "bx--btn__icon").Attr("aria-hidden", "true").Attr("aria-label", description).Render(w)
}
}