-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
block.go
153 lines (121 loc) · 2.56 KB
/
block.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
package ui
import (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// Block is the interface that describes a block of content.
type IBlock interface {
app.UI
// Sets the ID.
ID(v string) IBlock
// Sets the class. Multiple classes can be defined by successive calls.
Class(v string) IBlock
// Aligns content to the top.
Top() IBlock
// Aligns content to the middle.
Middle() IBlock
// Sets whether there is padding. Default is true.
Padding(v bool) IBlock
// The maximum content width. Default is 540px.
MaxContentWidth(px int) IBlock
// Sets the content.
Content(v ...app.UI) IBlock
}
// Block creates a block of content.
func Block() IBlock {
return &block{
Ialignment: stretch,
ImaxContentWidth: BlockContentWidth,
Ipadding: true,
padding: BlockPadding,
}
}
type block struct {
app.Compo
Iid string
Iclass string
Ialignment alignment
ImaxContentWidth int
Ipadding bool
Icontent []app.UI
padding int
width int
}
func (b *block) ID(v string) IBlock {
b.Iid = v
return b
}
func (b *block) Class(v string) IBlock {
b.Iclass = app.AppendClass(b.Iclass, v)
return b
}
func (b *block) Top() IBlock {
b.Ialignment = top
return b
}
func (b *block) Middle() IBlock {
b.Ialignment = middle
return b
}
func (b *block) MaxContentWidth(px int) IBlock {
b.ImaxContentWidth = px
return b
}
func (b *block) Padding(v bool) IBlock {
b.Ipadding = v
return b
}
func (b *block) Content(v ...app.UI) IBlock {
b.Icontent = app.FilterUIElems(v...)
return b
}
func (b *block) OnMount(ctx app.Context) {
b.resize(ctx)
}
func (b *block) OnResize(ctx app.Context) {
b.resize(ctx)
}
func (b *block) OnUpdate(ctx app.Context) {
b.resize(ctx)
}
func (b *block) Render() app.UI {
layout := Stack().
Style("width", "100%").
Style("height", "100%").
Center().
Content(
app.Div().
Style("padding", pxToString(b.padding)).
Style("width", fmt.Sprintf("calc(100%s - %vpx)", "%", b.padding*2)).
Style("max-width", pxToString(b.ImaxContentWidth)).
Body(b.Icontent...),
)
switch b.Ialignment {
case stretch:
layout.Stretch()
case top:
layout.Top()
case middle:
layout.Middle()
}
return app.Div().
DataSet("goapp-ui", "block").
ID(b.Iid).
Class(b.Iclass).
Body(layout)
}
func (b *block) resize(ctx app.Context) {
w, _ := ctx.Page().Size()
var padding int
if b.Ipadding {
if w <= 480 {
padding = BlockMobilePadding
} else {
padding = BlockPadding
}
}
if w != b.width || padding != b.padding {
b.width = w
b.padding = padding
}
}