-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawer.go
executable file
·112 lines (96 loc) · 2.78 KB
/
drawer.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
package drawer
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/mlctrez/goapp-mdc/pkg/base"
"github.com/mlctrez/goapp-mdc/pkg/list"
)
// https://github.com/material-components/material-components-web/tree/master/packages/mdc-drawer#types
// There are three types of navigation drawers: standard (1), modal (2), and bottom (3).
// MDC Web does not currently support bottom navigation drawers.
type Drawer struct {
app.Compo
base.JsUtil
Id string
List *list.List
Open bool
Type Type
api app.Value
}
type Type int
const (
Standard Type = iota
Modal
Dismissible
Bottom
)
func (d *Drawer) Render() app.UI {
content := app.Div().Class("mdc-drawer__content").Body(d.List)
aside := app.Aside().ID(d.Id).Class("mdc-drawer").Body(content)
if d.Open {
aside.Class("mdc-drawer--open")
}
switch d.Type {
case Modal:
aside.Class("mdc-drawer--modal")
aside.DataSet("mdc-auto-init", "MDCDrawer")
case Dismissible:
aside.Class("mdc-drawer--dismissible")
aside.DataSet("mdc-auto-init", "MDCDrawer")
case Bottom:
panic("MDC Web does not currently support bottom navigation drawers")
case Standard:
// no additional classes
}
return aside
}
type EventType string
const Open EventType = "MDCDrawer:open"
const Opened EventType = "MDCDrawer:opened"
const Close EventType = "MDCDrawer:close"
const Closed EventType = "MDCDrawer:closed"
const Toggle EventType = "MDCDrawer:toggle"
func (d *Drawer) OnMount(ctx app.Context) {
switch d.Type {
case Modal, Dismissible:
e := d.JSValue()
// TODO: can autoInit be called only on this element and document?
app.Window().Get("mdc").Call("autoInit")
d.api = e.Get("MDCDrawer")
e.Call("addEventListener", string(Opened), app.FuncOf(d.event(ctx, Opened)))
e.Call("addEventListener", string(Closed), app.FuncOf(d.event(ctx, Closed)))
ctx.Handle(string(Open), d.handle)
ctx.Handle(string(Close), d.handle)
ctx.Handle(string(Toggle), d.handle)
}
}
func (d *Drawer) event(ctx app.Context, event EventType) func(this app.Value, args []app.Value) interface{} {
return func(this app.Value, args []app.Value) interface{} {
ctx.NewActionWithValue(string(event), d)
return nil
}
}
func (d *Drawer) handle(ctx app.Context, action app.Action) {
if d == action.Value && d.api != nil {
switch EventType(action.Name) {
case Open:
d.api.Set("open", true)
case Close:
d.api.Set("open", false)
case Toggle:
current := d.api.Get("open").Bool()
d.api.Set("open", !current)
}
}
}
func (d *Drawer) ActionOpen(ctx app.Context) {
ctx.NewActionWithValue(string(Open), d)
}
func (d *Drawer) ActionClose(ctx app.Context) {
ctx.NewActionWithValue(string(Close), d)
}
func (d *Drawer) ActionToggle(ctx app.Context) {
ctx.NewActionWithValue(string(Toggle), d)
}
func Scrim() app.UI {
return app.Div().Class("mdc-drawer-scrim")
}