-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
scroll.go
152 lines (125 loc) · 3.23 KB
/
scroll.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
package ui
import (
"fmt"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// IScroll is the interface that describes a base with a scrollable content
// surrounded by a fixed header and footer.
type IScroll interface {
app.UI
// Sets the ID.
ID(v string) IScroll
// Sets the class. Multiple classes can be defined by successive calls.
Class(v string) IScroll
// Sets the header height in px. Default is 90px.
HeaderHeight(px int) IScroll
// Sets the header.
Header(v ...app.UI) IScroll
// Sets the content.
Content(v ...app.UI) IScroll
// Sets the footer height in px. Default is 0.
FooterHeight(px int) IScroll
// Sets the footer.
Footer(v ...app.UI) IScroll
}
// Scroll creates base with a scrollable content surrounded by a fixed header
// and footer.
func Scroll() IScroll {
return &scroll{
IheaderHeight: defaultHeaderHeight,
hpadding: BaseHPadding,
vpadding: BaseVPadding,
}
}
type scroll struct {
app.Compo
Iid string
Iclass string
IheaderHeight int
IfooterHeight int
Iheader []app.UI
Icontent []app.UI
Ifooter []app.UI
hpadding int
vpadding int
width int
}
func (s *scroll) ID(v string) IScroll {
s.Iid = v
return s
}
func (s *scroll) Class(v string) IScroll {
s.Iclass = app.AppendClass(s.Iclass, v)
return s
}
func (s *scroll) HeaderHeight(px int) IScroll {
s.IheaderHeight = px
return s
}
func (s *scroll) Header(v ...app.UI) IScroll {
s.Iheader = app.FilterUIElems(v...)
return s
}
func (s *scroll) Content(v ...app.UI) IScroll {
s.Icontent = app.FilterUIElems(v...)
return s
}
func (s *scroll) FooterHeight(px int) IScroll {
s.IfooterHeight = px
return s
}
func (s *scroll) Footer(v ...app.UI) IScroll {
s.Ifooter = app.FilterUIElems(v...)
return s
}
func (s *scroll) OnMount(ctx app.Context) {
s.resize(ctx)
}
func (s *scroll) OnResize(ctx app.Context) {
s.resize(ctx)
}
func (s *scroll) OnUpdate(ctx app.Context) {
s.resize(ctx)
}
func (s *scroll) Render() app.UI {
return app.Div().
DataSet("goapp-ui", "scroll").
ID(s.Iid).
Class(s.Iclass).
Body(
app.Div().
Style("width", "100%").
Style("height", fmt.Sprintf("calc(100%s - %vpx)", "%", s.vpadding*2)).
Style("padding", fmt.Sprintf("%vpx 0", s.vpadding)).
Body(
app.Div().
Style("width", fmt.Sprintf("calc(100%s - %vpx)", "%", s.hpadding*2)).
Style("padding", fmt.Sprintf("0 %vpx", s.hpadding)).
Style("height", pxToString(s.IheaderHeight)).
Body(s.Iheader...),
app.Div().
Style("width", fmt.Sprintf("calc(100%s - %vpx)", "%", s.hpadding*2)).
Style("height", fmt.Sprintf("calc(100%s - %vpx)", "%", s.IheaderHeight+s.IfooterHeight)).
Style("padding", fmt.Sprintf("0 %vpx", s.hpadding)).
Style("overflow-x", "hidden").
Style("overflow-y", "scroll").
Body(s.Icontent...),
app.Div().
Style("width", fmt.Sprintf("calc(100%s - %vpx)", "%", s.hpadding*2)).
Style("padding", fmt.Sprintf("0 %vpx", s.hpadding)).
Style("height", pxToString(s.IfooterHeight)).
Body(s.Ifooter...),
),
)
}
func (s *scroll) resize(ctx app.Context) {
w, _ := ctx.Page().Size()
if w <= 480 {
s.hpadding = BaseMobileHPadding
} else {
s.hpadding = BaseHPadding
}
if w != s.width {
s.width = w
}
}