-
Notifications
You must be signed in to change notification settings - Fork 0
/
banner.go
166 lines (133 loc) · 2.48 KB
/
banner.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
package mdc
import (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// https://material.io/components/banners/web#banners
type banner struct {
app.Compo
id string
mdcComponent app.Value
Iclass []string
Iopen bool
Ifixed bool
Itext string
Igraphic app.UI
Iactions []app.UI
}
const (
BannerGraphicClass = "mdc-banner__icon"
)
type IBanner interface {
app.UI
ID(string) IBanner
Class(...string) IBanner
Fixed(bool) IBanner
Text(string) IBanner
Graphic(app.UI) IBanner
Actions(...app.UI) IBanner
Open() IBanner
Close() IBanner
}
func (b *banner) ID(id string) IBanner {
b.id = id
return b
}
func (b *banner) Class(class ...string) IBanner {
b.Iclass = class
return b
}
func (b *banner) Fixed(fixed bool) IBanner {
b.Ifixed = fixed
return b
}
func (b *banner) Text(text string) IBanner {
b.Itext = text
return b
}
func (b *banner) Graphic(graphic app.UI) IBanner {
b.Igraphic = graphic
return b
}
func (b *banner) Actions(actions ...app.UI) IBanner {
b.Iactions = actions
return b
}
func (b *banner) OnMount(ctx app.Context) {
if b.id == "" {
b.id = fmt.Sprintf("banner-%d", allocID())
}
b.mdcComponent = app.Window().
Get("mdc").
Get("banner").
Get("MDCBanner").
Call("attachTo", ctx.JSSrc())
if b.Iopen {
b.mdcComponent.Call("open")
}
app.Log("mounted", b.id, b.mdcComponent)
}
func (b *banner) Open() IBanner {
b.Iopen = true
if b.mdcComponent != nil {
b.mdcComponent.Call("open")
}
return b
}
func (b *banner) Close() IBanner {
b.Iopen = false
if b.mdcComponent != nil {
b.mdcComponent.Call("close")
}
return b
}
func (b *banner) OnDismount(ctx app.Context) {
if b.mdcComponent != nil {
b.mdcComponent.Call("destroy")
}
}
func (b *banner) Render() app.UI {
banner := app.Div().
Class("mdc-banner").
Class(b.Iclass...).
Role("banner")
var graphic app.UI
if b.Igraphic != nil {
graphic = app.Div().
Class("mdc-banner__graphic").
Role("img").
Body(b.Igraphic)
}
text := app.Div().
Class("mdc-banner__graphic-text-wrapper").
Body(
graphic,
app.Div().
Class("mdc-banner__text").
Text(b.Itext),
)
actions := app.Div().
Class("mdc-banner__actions").
Body(
b.Iactions...
)
content := app.Div().
Class("mdc-banner__content").
Role("alert-dialog").
Aria("live", "assistive").
Body(
text,
actions,
)
if b.Ifixed {
content = app.Div().
Class("mdc-banner__fixed").
Body(content)
}
return banner.Body(
content,
)
}
func Banner() IBanner {
return &banner{}
}