-
Notifications
You must be signed in to change notification settings - Fork 0
/
classnames.go
45 lines (42 loc) · 1.04 KB
/
classnames.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
package smetana
import "strings"
// A map of conditional classes to be passed to ClassNames. The keys
// are class names and the values are booleans indicating whether or
// not to include that class name. For instance,
//
// {"foo": true, "bar" false}
//
// will evaluate to "foo".
type Classes map[ClassName]bool
// A utility function for concatenating multiple class names into a
// single string suitable for embedding in HTML. Arguments may be of
// several different types:
// - string
// - [ClassName]
// - [Classes]
//
// Arguments of other types are ignored.
func ClassNames(args ...any) ClassName {
classes := []string{}
for _, arg := range args {
switch item := arg.(type) {
case string:
if len(item) > 0 {
classes = append(classes, item)
}
case ClassName:
if len(item) > 0 {
classes = append(classes, string(item))
}
case Classes:
for key, value := range item {
if value {
classes = append(classes, string(key))
}
}
default:
break
}
}
return ClassName(strings.Join(classes, " "))
}