-
Notifications
You must be signed in to change notification settings - Fork 0
/
fab.go
100 lines (78 loc) · 1.5 KB
/
fab.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
package mdc
import (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// https://material.io/components/buttons-floating-action-button/web
const (
FABIconClass = "mdc-fab__icon"
)
type fab struct {
app.Compo
id string
mdcComponent app.Value
Iicon app.UI
Ilabel string
IonClick app.EventHandler
}
type IFAB interface {
app.UI
ID(string) IFAB
Label(string) IFAB
Icon(app.UI) IFAB
OnClick(app.EventHandler) IFAB
}
func (f *fab) ID(id string) IFAB {
f.id = id
return f
}
func (f *fab) Label(label string) IFAB {
f.Ilabel = label
return f
}
func (f *fab) Icon(icon app.UI) IFAB {
f.Iicon = icon
return f
}
func (f *fab) OnClick(handler app.EventHandler) IFAB {
f.IonClick = handler
return f
}
func (f *fab) OnMount(ctx app.Context) {
if f.id == "" {
f.id = fmt.Sprintf("fab-%d", allocID())
}
f.mdcComponent = app.Window().
Get("mdc").
Get("ripple").
Get("MDCRipple").
Call("attachTo", ctx.JSSrc())
app.Log("mounted", f.id, f.mdcComponent)
}
func (f *fab) OnDismount(ctx app.Context) {
if f.mdcComponent != nil {
f.mdcComponent.Call("destroy")
}
}
func (f *fab) Render() app.UI {
fab := app.Button().
Class("mdc-fab").
ID(f.id)
if f.IonClick != nil {
fab = fab.OnClick(f.IonClick)
}
if f.Ilabel != "" {
fab = fab.Class("mdc-fab--extended")
}
return fab.Body(
app.Span().Class("mdc-fab__ripple"),
app.If(f.Iicon != nil, f.Iicon),
app.If(
f.Ilabel != "",
app.Span().Class("mdc-fab__label").Text(f.Ilabel),
),
)
}
func FAB() IFAB {
return &fab{}
}