-
Notifications
You must be signed in to change notification settings - Fork 7
/
link.go
37 lines (32 loc) · 858 Bytes
/
link.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
package router
import (
"github.com/hexops/vecty"
"github.com/hexops/vecty/elem"
"github.com/hexops/vecty/event"
"github.com/hexops/vecty/prop"
)
// EventCallback defines a vecty onClick handler
type EventCallback func(e *vecty.Event)
// LinkOptions - use to pass extra options to the link element
// like an ID, or class attribute.
type LinkOptions struct {
ID string
Class string
}
// Link implements a frontend history Anchor tag.
func Link(route, text string, opts LinkOptions) *vecty.HTML {
return elem.Anchor(
vecty.Markup(
prop.Href(route),
vecty.MarkupIf(opts.ID != "", prop.ID(opts.ID)),
vecty.MarkupIf(opts.Class != "", vecty.Class(opts.Class)),
event.Click(onClick(route)).PreventDefault(),
),
vecty.Text(text),
)
}
func onClick(route string) EventCallback {
return func(e *vecty.Event) {
Redirect(route)
}
}