Click
Click a button and watch a counter update in place.
Click
Click a button and see a counter update.
Live Demo
Source
package examples
import (
"context"
l "github.com/SamHennessy/hlive"
. "github.com/SamHennessy/hlive/hhtml"
)
// ClickDemo is a live, interactive instance of the Click example, served for
// the "Live Demo" iframe.
func ClickDemo() *l.Page {
page := l.NewPage()
page.DOM().Title().Add("Click Example")
page.DOM().Head().Add(
Link(Rel("stylesheet"), Href("https://cdn.simplecss.org/simple.min.css")))
// A thread safe value container
count := l.Box(0)
page.DOM().Body().Add(
Header(
H1("Click"),
P("Click the button and see the count increase"),
),
Main(
P(
"Clicks: ",
Button(
l.On("click", func(_ context.Context, _ l.Event) {
// We need to read and write inside a single lock
count.Lock(func(v int) int { return v + 1 })
}),
count,
),
),
),
)
return page
}