Skip to content

Commit a58a839

Browse files
committed
feat: Implement escape closer handlers for modals in go-app V10 compatible way
Signed-off-by: Felicitas Pojtinger <[email protected]>
1 parent 30f9038 commit a58a839

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

pkg/components/about_modal.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package components
22

3-
import "github.com/maxence-charriere/go-app/v10/pkg/app"
3+
import (
4+
"github.com/maxence-charriere/go-app/v10/pkg/app"
5+
)
46

57
type AboutModal struct {
68
app.Compo
@@ -92,9 +94,15 @@ func (c *AboutModal) Render() app.UI {
9294
}
9395

9496
func (c *AboutModal) OnMount(ctx app.Context) {
95-
// app.Window().AddEventListener("keyup", func(ctx app.Context, e app.Event) {
96-
// if e.Get("key").String() == "Escape" {
97-
// c.Close()
98-
// }
99-
// })
97+
app.Window().Call("addEventListener", "keyup", app.FuncOf(func(this app.Value, args []app.Value) any {
98+
ctx.Async(func() {
99+
if len(args) > 0 && args[0].Get("key").String() == "Escape" {
100+
c.Close()
101+
102+
ctx.Update()
103+
}
104+
})
105+
106+
return nil
107+
}))
100108
}

pkg/components/modal.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ func (c *Modal) Render() app.UI {
109109
}
110110

111111
func (c *Modal) OnMount(ctx app.Context) {
112-
// app.Window().AddEventListener("keyup", func(ctx app.Context, e app.Event) {
113-
// if e.Get("key").String() == "Escape" {
114-
// c.Close()
115-
// }
116-
// })
112+
app.Window().Call("addEventListener", "keyup", app.FuncOf(func(this app.Value, args []app.Value) any {
113+
ctx.Async(func() {
114+
if len(args) > 0 && args[0].Get("key").String() == "Escape" {
115+
c.Close()
116+
117+
ctx.Update()
118+
}
119+
})
120+
121+
return nil
122+
}))
117123
}

0 commit comments

Comments
 (0)