-
Notifications
You must be signed in to change notification settings - Fork 0
/
element_style.go
66 lines (55 loc) · 1.26 KB
/
element_style.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
package html
import (
"fmt"
"syscall/js"
"github.com/VJftw/vigor"
)
type StyleElementPlugin struct {
propertyValues map[string]any
propertyOrder []string
}
func NewStyleElementPlugin() ElementPlugin {
return &StyleElementPlugin{
propertyValues: map[string]any{},
propertyOrder: []string{},
}
}
func (p *StyleElementPlugin) HandleChild(child any) (bool, error) {
if x, ok := child.(*elementStyle); ok {
if _, ok := p.propertyValues[x.property]; !ok {
p.propertyOrder = append(p.propertyOrder, x.property)
}
p.propertyValues[x.property] = x.value
return true, nil
}
return false, nil
}
func (p *StyleElementPlugin) Render(doc, obj js.Value) error {
if len(p.propertyValues) <= 0 {
return nil
}
subscriber := vigor.NewFnSubscriber()
subscriber.SetFn(func() {
styleAttr := ""
for _, k := range p.propertyOrder {
v := p.propertyValues[k]
if x, ok := v.(vigor.GetterFn); ok {
v = x(subscriber)
}
styleAttr += fmt.Sprintf("%s: %s;", k, v)
}
obj.Set("style", styleAttr)
}).Run()
return nil
}
// https://www.solidjs.com/tutorial/bindings_style
type elementStyle struct {
property string
value any
}
func Style(property string, value any) *elementStyle {
return &elementStyle{
property: property,
value: value,
}
}