-
Notifications
You must be signed in to change notification settings - Fork 0
/
element_class.go
65 lines (54 loc) · 1.06 KB
/
element_class.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
package html
import (
"fmt"
"syscall/js"
"github.com/VJftw/vigor"
)
type ClassElementPlugin struct {
classList []any
}
func NewClassElementPlugin() ElementPlugin {
return &ClassElementPlugin{
classList: []any{},
}
}
func (p *ClassElementPlugin) HandleChild(child any) (bool, error) {
if x, ok := child.(*elementClass); ok {
p.classList = append(p.classList, x.values()...)
return true, nil
}
return false, nil
}
func (p *ClassElementPlugin) Render(doc, obj js.Value) error {
subscriber := vigor.NewFnSubscriber()
subscriber.SetFn(func() {
className := ""
for i, class := range p.classList {
if i > 0 {
className += " "
}
if x, ok := class.(vigor.GetterFn); ok {
class = x(subscriber)
}
classStr := fmt.Sprintf("%s", class)
if len(classStr) > 0 {
className += classStr
}
}
if className != "" {
obj.Set("className", className)
}
}).Run()
return nil
}
type elementClass struct {
v []any
}
func (e *elementClass) values() []any {
return e.v
}
func Class(v ...any) *elementClass {
return &elementClass{
v: v,
}
}