6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-07-08 11:01:20 +00:00
prologic-saltyim/internal/pwa/components/link.go
2022-03-22 14:48:54 +10:00

113 lines
1.6 KiB
Go

package components
import (
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/maxence-charriere/go-app/v9/pkg/ui"
)
type link struct {
app.Compo
Iid string
Iclass string
Ilabel string
Ihelp string
Ihref string
Ifocus bool
IonClick func(app.Context)
Iicon app.UI
}
func newLink() *link {
return &link{}
}
func (l *link) ID(v string) *link {
l.Iid = v
return l
}
func (l *link) Class(v string) *link {
if v == "" {
return l
}
if l.Iclass != "" {
l.Iclass += " "
}
l.Iclass += v
return l
}
func (l *link) Label(v string) *link {
l.Ilabel = v
return l
}
func (l *link) Help(v string) *link {
l.Ihelp = v
return l
}
func (l *link) Href(v string) *link {
l.Ihref = v
return l
}
func (l *link) Focus(v bool) *link {
l.Ifocus = v
return l
}
func (l *link) OnClick(v func(app.Context)) *link {
l.IonClick = v
return l
}
func (l *link) Icon(v app.UI) *link {
l.Iicon = v
return l
}
func (l *link) Render() app.UI {
iconVisibility := ""
if l.Iicon == nil {
iconVisibility = "hide"
}
focus := ""
if l.Ifocus {
focus = "focus"
}
return app.A().
ID(l.Iid).
Class("link").
Class("heading").
Class("fit").
Class(l.Iclass).
Class(focus).
Title(l.Ihelp).
Href(l.Ihref).
OnClick(l.onClick).
Body(
ui.Stack().
Middle().
Content(
app.Div().
Class(iconVisibility).
Class("link-icon").
Body(l.Iicon),
app.Div().Text(l.Ilabel),
),
)
}
func (l *link) onClick(ctx app.Context, e app.Event) {
if l.IonClick == nil {
return
}
e.PreventDefault()
l.IonClick(ctx)
}